test_locks.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/lock.h>
  6. #include "unity.h"
  7. #include "test_utils.h"
  8. #include "sdkconfig.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/semphr.h"
  11. #if defined(_RETARGETABLE_LOCKING)
  12. static void locking_task(void* arg)
  13. {
  14. _LOCK_T lock = (_LOCK_T) arg;
  15. __lock_acquire(lock);
  16. __lock_release(lock);
  17. vTaskSuspend(NULL);
  18. }
  19. static void recursive_locking_task(void* arg)
  20. {
  21. _LOCK_T lock = (_LOCK_T) arg;
  22. __lock_acquire_recursive(lock);
  23. __lock_release_recursive(lock);
  24. vTaskSuspend(NULL);
  25. }
  26. static void test_inner_normal(_LOCK_T lock)
  27. {
  28. /* Acquire the lock */
  29. __lock_acquire(lock);
  30. /* Create another task to try acquire same lock */
  31. TaskHandle_t task_hdl;
  32. TEST_ASSERT(xTaskCreate(&locking_task, "locking_task", 2048, lock, UNITY_FREERTOS_PRIORITY, &task_hdl));
  33. vTaskDelay(2);
  34. /* It should get blocked */
  35. TEST_ASSERT_EQUAL(eBlocked, eTaskGetState(task_hdl));
  36. /* Once we release the lock, the task should succeed and suspend itself */
  37. __lock_release(lock);
  38. vTaskDelay(2);
  39. TEST_ASSERT_EQUAL(eSuspended, eTaskGetState(task_hdl));
  40. vTaskDelete(task_hdl);
  41. /* Can not recursively acquire the lock from same task */
  42. TEST_ASSERT_EQUAL(0, __lock_try_acquire(lock));
  43. TEST_ASSERT_EQUAL(-1, __lock_try_acquire(lock));
  44. __lock_release(lock);
  45. }
  46. static void test_inner_recursive(_LOCK_T lock)
  47. {
  48. /* Acquire the lock */
  49. __lock_acquire_recursive(lock);
  50. /* Create another task to try acquire same lock */
  51. TaskHandle_t task_hdl;
  52. TEST_ASSERT(xTaskCreate(&recursive_locking_task, "locking_task", 2048, lock, UNITY_FREERTOS_PRIORITY, &task_hdl));
  53. vTaskDelay(2);
  54. /* It should get blocked */
  55. TEST_ASSERT_EQUAL(eBlocked, eTaskGetState(task_hdl));
  56. /* Once we release the lock, the task should succeed and suspend itself */
  57. __lock_release_recursive(lock);
  58. vTaskDelay(2);
  59. TEST_ASSERT_EQUAL(eSuspended, eTaskGetState(task_hdl));
  60. vTaskDelete(task_hdl);
  61. /* Try recursively acquiring the lock */
  62. TEST_ASSERT_EQUAL(0, __lock_try_acquire_recursive(lock));
  63. TEST_ASSERT_EQUAL(0, __lock_try_acquire_recursive(lock));
  64. __lock_release_recursive(lock);
  65. __lock_release_recursive(lock);
  66. }
  67. TEST_CASE("Retargetable static locks", "[newlib_locks]")
  68. {
  69. StaticSemaphore_t semaphore;
  70. _LOCK_T lock = (_LOCK_T) xSemaphoreCreateMutexStatic(&semaphore);
  71. test_inner_normal(lock);
  72. }
  73. TEST_CASE("Retargetable static recursive locks", "[newlib_locks]")
  74. {
  75. StaticSemaphore_t semaphore;
  76. _LOCK_T lock = (_LOCK_T) xSemaphoreCreateRecursiveMutexStatic(&semaphore);
  77. test_inner_recursive(lock);
  78. }
  79. TEST_CASE("Retargetable dynamic locks", "[newlib_locks]")
  80. {
  81. _LOCK_T lock;
  82. __lock_init(lock);
  83. test_inner_normal(lock);
  84. __lock_close(lock);
  85. }
  86. TEST_CASE("Retargetable dynamic recursive locks", "[newlib_locks]")
  87. {
  88. _LOCK_T lock;
  89. __lock_init_recursive(lock);
  90. test_inner_recursive(lock);
  91. __lock_close_recursive(lock);
  92. }
  93. #endif // _RETARGETABLE_LOCKING