furi_valuemutex_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <furi.h>
  4. #include "furi_hal_delay.h"
  5. #include "minunit.h"
  6. void test_furi_valuemutex() {
  7. const int init_value = 0xdeadbeef;
  8. const int changed_value = 0x12345678;
  9. int value = init_value;
  10. bool result;
  11. ValueMutex valuemutex;
  12. // init mutex case
  13. result = init_mutex(&valuemutex, &value, sizeof(value));
  14. mu_assert(result, "init mutex failed");
  15. // acquire mutex case
  16. int* value_pointer = acquire_mutex(&valuemutex, 100);
  17. mu_assert_pointers_eq(value_pointer, &value);
  18. // second acquire mutex case
  19. int* value_pointer_second = acquire_mutex(&valuemutex, 100);
  20. mu_assert_pointers_eq(value_pointer_second, NULL);
  21. // change value case
  22. *value_pointer = changed_value;
  23. mu_assert_int_eq(value, changed_value);
  24. // release mutex case
  25. result = release_mutex(&valuemutex, &value);
  26. mu_assert(result, "release mutex failed");
  27. // TODO
  28. //acquire mutex blocking case
  29. //write mutex blocking case
  30. //read mutex blocking case
  31. mu_check(delete_mutex(&valuemutex));
  32. }
  33. /*
  34. TEST: concurrent access
  35. 1. Create holding record
  36. 2. Open it twice
  37. 3. Change value simultaneously in two app and check integrity
  38. */
  39. // TODO this test broke because mutex in furi is not implemented
  40. typedef struct {
  41. // a and b must be equal
  42. uint8_t a;
  43. uint8_t b;
  44. } ConcurrentValue;
  45. void furi_concurent_app(void* p) {
  46. ValueMutex* mutex = (ValueMutex*)p;
  47. if(mutex == NULL) {
  48. printf("cannot open mutex\r\n");
  49. osThreadExit();
  50. }
  51. for(size_t i = 0; i < 10; i++) {
  52. ConcurrentValue* value = (ConcurrentValue*)acquire_mutex_block(mutex);
  53. if(value == NULL) {
  54. printf("cannot take record\r\n");
  55. release_mutex(mutex, value);
  56. osThreadExit();
  57. }
  58. // emulate read-modify-write broken by context switching
  59. uint8_t a = value->a;
  60. uint8_t b = value->b;
  61. a++;
  62. b++;
  63. furi_hal_delay_ms(2);
  64. value->a = a;
  65. value->b = b;
  66. release_mutex(mutex, value);
  67. }
  68. osThreadExit();
  69. }
  70. void test_furi_concurrent_access() {
  71. // TODO: reimplement or delete test
  72. return;
  73. /*
  74. // 1. Create holding record
  75. ConcurrentValue value = {.a = 0, .b = 0};
  76. ValueMutex mutex;
  77. mu_check(init_mutex(&mutex, &value, sizeof(value)));
  78. // 3. Create second app for interact with it
  79. FuriApp* second_app = furiac_start(furi_concurent_app, "furi concurent app", (void*)&mutex);
  80. // 4. multiply ConcurrentValue::a
  81. for(size_t i = 0; i < 4; i++) {
  82. ConcurrentValue* value = (ConcurrentValue*)acquire_mutex_block(&mutex);
  83. if(value == NULL) {
  84. release_mutex(&mutex, value);
  85. mu_fail("cannot take record\r\n");
  86. }
  87. // emulate read-modify-write broken by context switching
  88. uint8_t a = value->a;
  89. uint8_t b = value->b;
  90. a++;
  91. b++;
  92. value->a = a;
  93. furi_hal_delay_ms(10); // this is only for test, do not add delay between take/give in prod!
  94. value->b = b;
  95. release_mutex(&mutex, value);
  96. }
  97. furi_hal_delay_ms(50);
  98. mu_assert_pointers_eq(second_app->handler, NULL);
  99. mu_assert_int_eq(value.a, value.b);
  100. mu_check(delete_mutex(&mutex));
  101. */
  102. }