furi_valuemutex_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <furi.h>
  4. #include "api-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. delay(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. mu_assert(false, "please reimplement or delete test");
  72. /*
  73. // 1. Create holding record
  74. ConcurrentValue value = {.a = 0, .b = 0};
  75. ValueMutex mutex;
  76. mu_check(init_mutex(&mutex, &value, sizeof(value)));
  77. // 3. Create second app for interact with it
  78. FuriApp* second_app = furiac_start(furi_concurent_app, "furi concurent app", (void*)&mutex);
  79. // 4. multiply ConcurrentValue::a
  80. for(size_t i = 0; i < 4; i++) {
  81. ConcurrentValue* value = (ConcurrentValue*)acquire_mutex_block(&mutex);
  82. if(value == NULL) {
  83. release_mutex(&mutex, value);
  84. mu_fail("cannot take record\r\n");
  85. }
  86. // emulate read-modify-write broken by context switching
  87. uint8_t a = value->a;
  88. uint8_t b = value->b;
  89. a++;
  90. b++;
  91. value->a = a;
  92. delay(10); // this is only for test, do not add delay between take/give in prod!
  93. value->b = b;
  94. release_mutex(&mutex, value);
  95. }
  96. delay(50);
  97. mu_assert_pointers_eq(second_app->handler, NULL);
  98. mu_assert_int_eq(value.a, value.b);
  99. mu_check(delete_mutex(&mutex));
  100. */
  101. }