furi_valuemutex_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "flipper_v2.h"
  4. #include "log.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. }
  32. /*
  33. TEST: concurrent access
  34. 1. Create holding record
  35. 2. Open it twice
  36. 3. Change value simultaneously in two app and check integrity
  37. */
  38. // TODO this test broke because mutex in furi is not implemented
  39. typedef struct {
  40. // a and b must be equal
  41. uint8_t a;
  42. uint8_t b;
  43. } ConcurrentValue;
  44. void furi_concurent_app(void* p) {
  45. ValueMutex* mutex = (ValueMutex*)p;
  46. if(mutex == NULL) {
  47. printf("cannot open mutex\n");
  48. furiac_exit(NULL);
  49. }
  50. for(size_t i = 0; i < 10; i++) {
  51. ConcurrentValue* value = (ConcurrentValue*)acquire_mutex_block(mutex);
  52. if(value == NULL) {
  53. printf("cannot take record\n");
  54. release_mutex(mutex, value);
  55. furiac_exit(NULL);
  56. }
  57. // emulate read-modify-write broken by context switching
  58. uint8_t a = value->a;
  59. uint8_t b = value->b;
  60. a++;
  61. b++;
  62. delay(2);
  63. value->a = a;
  64. value->b = b;
  65. release_mutex(mutex, value);
  66. }
  67. furiac_exit(NULL);
  68. }
  69. void test_furi_concurrent_access() {
  70. // 1. Create holding record
  71. ConcurrentValue value = {.a = 0, .b = 0};
  72. ValueMutex mutex;
  73. mu_check(init_mutex(&mutex, &value, sizeof(value)));
  74. // 3. Create second app for interact with it
  75. FuriApp* second_app = furiac_start(furi_concurent_app, "furi concurent app", (void*)&mutex);
  76. // 4. multiply ConcurrentValue::a
  77. for(size_t i = 0; i < 4; i++) {
  78. ConcurrentValue* value = (ConcurrentValue*)acquire_mutex_block(&mutex);
  79. if(value == NULL) {
  80. release_mutex(&mutex, value);
  81. mu_fail("cannot take record\n");
  82. }
  83. // emulate read-modify-write broken by context switching
  84. uint8_t a = value->a;
  85. uint8_t b = value->b;
  86. a++;
  87. b++;
  88. value->a = a;
  89. delay(10); // this is only for test, do not add delay between take/give in prod!
  90. value->b = b;
  91. release_mutex(&mutex, value);
  92. }
  93. delay(50);
  94. mu_assert_pointers_eq(second_app->handler, NULL);
  95. mu_assert_int_eq(value.a, value.b);
  96. }