furi_valuemutex_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <furi.h>
  4. #include "minunit.h"
  5. void test_furi_valuemutex() {
  6. const int init_value = 0xdeadbeef;
  7. const int changed_value = 0x12345678;
  8. int value = init_value;
  9. bool result;
  10. ValueMutex valuemutex;
  11. // init mutex case
  12. result = init_mutex(&valuemutex, &value, sizeof(value));
  13. mu_assert(result, "init mutex failed");
  14. // acquire mutex case
  15. int* value_pointer = acquire_mutex(&valuemutex, 100);
  16. mu_assert_pointers_eq(value_pointer, &value);
  17. // second acquire mutex case
  18. int* value_pointer_second = acquire_mutex(&valuemutex, 100);
  19. mu_assert_pointers_eq(value_pointer_second, NULL);
  20. // change value case
  21. *value_pointer = changed_value;
  22. mu_assert_int_eq(value, changed_value);
  23. // release mutex case
  24. result = release_mutex(&valuemutex, &value);
  25. mu_assert(result, "release mutex failed");
  26. // TODO
  27. //acquire mutex blocking case
  28. //write mutex blocking case
  29. //read mutex blocking case
  30. mu_check(delete_mutex(&valuemutex));
  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\r\n");
  48. osThreadExit();
  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\r\n");
  54. release_mutex(mutex, value);
  55. osThreadExit();
  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. osThreadExit();
  68. }
  69. void test_furi_concurrent_access() {
  70. mu_assert(false, "please reimplement or delete test");
  71. /*
  72. // 1. Create holding record
  73. ConcurrentValue value = {.a = 0, .b = 0};
  74. ValueMutex mutex;
  75. mu_check(init_mutex(&mutex, &value, sizeof(value)));
  76. // 3. Create second app for interact with it
  77. FuriApp* second_app = furiac_start(furi_concurent_app, "furi concurent app", (void*)&mutex);
  78. // 4. multiply ConcurrentValue::a
  79. for(size_t i = 0; i < 4; i++) {
  80. ConcurrentValue* value = (ConcurrentValue*)acquire_mutex_block(&mutex);
  81. if(value == NULL) {
  82. release_mutex(&mutex, value);
  83. mu_fail("cannot take record\r\n");
  84. }
  85. // emulate read-modify-write broken by context switching
  86. uint8_t a = value->a;
  87. uint8_t b = value->b;
  88. a++;
  89. b++;
  90. value->a = a;
  91. delay(10); // this is only for test, do not add delay between take/give in prod!
  92. value->b = b;
  93. release_mutex(&mutex, value);
  94. }
  95. delay(50);
  96. mu_assert_pointers_eq(second_app->handler, NULL);
  97. mu_assert_int_eq(value.a, value.b);
  98. mu_check(delete_mutex(&mutex));
  99. */
  100. }