test_pthread.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <errno.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "esp_pthread.h"
  5. #include <pthread.h>
  6. #include "unity.h"
  7. static void *compute_square(void *arg)
  8. {
  9. int *num = (int *) arg;
  10. *num = (*num) * (*num);
  11. pthread_exit((void *) num);
  12. return NULL;
  13. }
  14. TEST_CASE("pthread create join", "[pthread]")
  15. {
  16. int res = 0;
  17. volatile int num = 7;
  18. volatile bool attr_init = false;
  19. void *thread_rval = NULL;
  20. pthread_t new_thread = (pthread_t)NULL;
  21. pthread_attr_t attr;
  22. if (TEST_PROTECT()) {
  23. res = pthread_attr_init(&attr);
  24. TEST_ASSERT_EQUAL_INT(0, res);
  25. attr_init = true;
  26. res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  27. TEST_ASSERT_EQUAL_INT(0, res);
  28. res = pthread_create(&new_thread, &attr, compute_square, (void *) &num);
  29. TEST_ASSERT_EQUAL_INT(0, res);
  30. res = pthread_join(new_thread, &thread_rval);
  31. TEST_ASSERT_EQUAL_INT(EDEADLK, res);
  32. vTaskDelay(100 / portTICK_PERIOD_MS);
  33. TEST_ASSERT_EQUAL_INT(49, num);
  34. res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  35. TEST_ASSERT_EQUAL_INT(0, res);
  36. res = pthread_create(&new_thread, &attr, compute_square, (void *) &num);
  37. TEST_ASSERT_EQUAL_INT(0, res);
  38. res = pthread_join(new_thread, &thread_rval);
  39. TEST_ASSERT_EQUAL_INT(0, res);
  40. TEST_ASSERT_EQUAL_INT(2401, num);
  41. TEST_ASSERT_EQUAL_PTR(&num, thread_rval);
  42. }
  43. if (attr_init) {
  44. pthread_attr_destroy(&attr);
  45. }
  46. }
  47. static void *waiting_thread(void *arg)
  48. {
  49. TaskHandle_t *task_handle = (TaskHandle_t *)arg;
  50. TaskHandle_t parent_task = *task_handle;
  51. *task_handle = xTaskGetCurrentTaskHandle();
  52. xTaskNotify(parent_task, 0, eNoAction);
  53. xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
  54. return NULL;
  55. }
  56. TEST_CASE("pthread detach", "[pthread]")
  57. {
  58. int res = 0;
  59. pthread_t new_thread = (pthread_t)NULL;
  60. TaskHandle_t task_handle = NULL;
  61. const int task_count = uxTaskGetNumberOfTasks();
  62. bool detach_works = false;
  63. if (TEST_PROTECT()) {
  64. task_handle = xTaskGetCurrentTaskHandle();
  65. res = pthread_create(&new_thread, NULL, waiting_thread, (void *)&task_handle);
  66. TEST_ASSERT_EQUAL_INT(0, res);
  67. res = xTaskNotifyWait(0, 0, NULL, 100 / portTICK_PERIOD_MS);
  68. TEST_ASSERT_EQUAL_INT(pdTRUE, res);
  69. xTaskNotify(task_handle, 0, eNoAction);
  70. vTaskDelay(100 / portTICK_PERIOD_MS);
  71. res = pthread_detach(new_thread);
  72. TEST_ASSERT_EQUAL_INT(0, res);
  73. res = uxTaskGetNumberOfTasks();
  74. TEST_ASSERT_EQUAL_INT(task_count, res);
  75. detach_works = true;
  76. }
  77. if (!detach_works) {
  78. vTaskDelete(task_handle);
  79. } else {
  80. detach_works = false;
  81. }
  82. if (TEST_PROTECT()) {
  83. task_handle = xTaskGetCurrentTaskHandle();
  84. res = pthread_create(&new_thread, NULL, waiting_thread, (void *)&task_handle);
  85. TEST_ASSERT_EQUAL_INT(0, res);
  86. res = xTaskNotifyWait(0, 0, NULL, 100 / portTICK_PERIOD_MS);
  87. TEST_ASSERT_EQUAL_INT(pdTRUE, res);
  88. res = pthread_detach(new_thread);
  89. TEST_ASSERT_EQUAL_INT(0, res);
  90. xTaskNotify(task_handle, 0, eNoAction);
  91. vTaskDelay(100 / portTICK_PERIOD_MS);
  92. res = uxTaskGetNumberOfTasks();
  93. TEST_ASSERT_EQUAL_INT(task_count, res);
  94. detach_works = true;
  95. }
  96. if (!detach_works) {
  97. vTaskDelete(task_handle);
  98. }
  99. }
  100. TEST_CASE("pthread attr init destroy", "[pthread]")
  101. {
  102. int res = 0;
  103. size_t stack_size_1 = 0, stack_size_2 = 0;
  104. volatile bool attr_init = pdFALSE;
  105. pthread_attr_t attr;
  106. if (TEST_PROTECT()) {
  107. res = pthread_attr_init(&attr);
  108. TEST_ASSERT_EQUAL_INT(0, res);
  109. attr_init = true;
  110. res = pthread_attr_getstacksize(&attr, &stack_size_1);
  111. TEST_ASSERT_EQUAL_INT(0, res);
  112. res = pthread_attr_setstacksize(&attr, stack_size_1);
  113. TEST_ASSERT_EQUAL_INT(0, res);
  114. res = pthread_attr_getstacksize(&attr, &stack_size_2);
  115. TEST_ASSERT_EQUAL_INT(0, res);
  116. TEST_ASSERT_EQUAL_INT(stack_size_2, stack_size_1);
  117. stack_size_1 = PTHREAD_STACK_MIN - 1;
  118. res = pthread_attr_setstacksize(&attr, stack_size_1);
  119. TEST_ASSERT_EQUAL_INT(EINVAL, res);
  120. }
  121. if (attr_init) {
  122. TEST_ASSERT_EQUAL_INT(0, pthread_attr_destroy(&attr));
  123. }
  124. }
  125. static void *unlock_mutex(void *arg)
  126. {
  127. pthread_mutex_t *mutex = (pthread_mutex_t *) arg;
  128. intptr_t res = (intptr_t) pthread_mutex_unlock(mutex);
  129. pthread_exit((void *) res);
  130. return NULL;
  131. }
  132. static void test_mutex_lock_unlock(int mutex_type)
  133. {
  134. int res = 0;
  135. int set_type = -1;
  136. volatile bool attr_created = false;
  137. volatile bool mutex_created = false;
  138. volatile intptr_t thread_rval = 0;
  139. pthread_mutex_t mutex;
  140. pthread_mutexattr_t attr;
  141. pthread_t new_thread;
  142. if (TEST_PROTECT()) {
  143. res = pthread_mutexattr_init(&attr);
  144. TEST_ASSERT_EQUAL_INT(0, res);
  145. attr_created = true;
  146. res = pthread_mutexattr_settype(&attr, mutex_type);
  147. TEST_ASSERT_EQUAL_INT(0, res);
  148. res = pthread_mutexattr_gettype(&attr, &set_type);
  149. TEST_ASSERT_EQUAL_INT(0, res);
  150. TEST_ASSERT_EQUAL_INT(mutex_type, set_type);
  151. res = pthread_mutex_init(&mutex, &attr);
  152. TEST_ASSERT_EQUAL_INT(0, res);
  153. mutex_created = true;
  154. res = pthread_mutex_lock(&mutex);
  155. TEST_ASSERT_EQUAL_INT(0, res);
  156. res = pthread_mutex_lock(&mutex);
  157. if(mutex_type == PTHREAD_MUTEX_ERRORCHECK) {
  158. TEST_ASSERT_EQUAL_INT(EDEADLK, res);
  159. } else {
  160. TEST_ASSERT_EQUAL_INT(0, res);
  161. res = pthread_mutex_unlock(&mutex);
  162. TEST_ASSERT_EQUAL_INT(0, res);
  163. }
  164. pthread_create(&new_thread, NULL, unlock_mutex, &mutex);
  165. pthread_join(new_thread, (void **) &thread_rval);
  166. TEST_ASSERT_EQUAL_INT(EPERM, (int) thread_rval);
  167. res = pthread_mutex_unlock(&mutex);
  168. TEST_ASSERT_EQUAL_INT(0, res);
  169. }
  170. if (attr_created) {
  171. pthread_mutexattr_destroy(&attr);
  172. }
  173. if (mutex_created) {
  174. pthread_mutex_destroy(&mutex);
  175. }
  176. }
  177. TEST_CASE("pthread mutex lock unlock", "[pthread]")
  178. {
  179. int res = 0;
  180. /* Present behavior of mutex initializer is unlike what is
  181. * defined in Posix standard, ie. calling pthread_mutex_lock
  182. * on such a mutex would internally cause dynamic allocation.
  183. * Therefore pthread_mutex_destroy needs to be called in
  184. * order to avoid memory leak. */
  185. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  186. res = pthread_mutex_lock(&mutex);
  187. TEST_ASSERT_EQUAL_INT(0, res);
  188. res = pthread_mutex_unlock(&mutex);
  189. TEST_ASSERT_EQUAL_INT(0, res);
  190. /* This deviates from the Posix standard static mutex behavior.
  191. * This needs to be removed in the future when standard mutex
  192. * initializer is supported */
  193. pthread_mutex_destroy(&mutex);
  194. test_mutex_lock_unlock(PTHREAD_MUTEX_ERRORCHECK);
  195. test_mutex_lock_unlock(PTHREAD_MUTEX_RECURSIVE);
  196. }
  197. static void timespec_add_nano(struct timespec * out, struct timespec * in, long val)
  198. {
  199. out->tv_nsec = val + in->tv_nsec;
  200. if (out->tv_nsec < (in->tv_nsec)) {
  201. out->tv_sec += 1;
  202. }
  203. }
  204. TEST_CASE("pthread mutex trylock timedlock", "[pthread]")
  205. {
  206. int res = 0;
  207. volatile bool mutex_created = false;
  208. pthread_mutex_t mutex;
  209. struct timespec abs_timeout;
  210. if (TEST_PROTECT()) {
  211. res = pthread_mutex_init(&mutex, NULL);
  212. TEST_ASSERT_EQUAL_INT(0, res);
  213. mutex_created = true;
  214. res = pthread_mutex_trylock(&mutex);
  215. TEST_ASSERT_EQUAL_INT(0, res);
  216. res = pthread_mutex_trylock(&mutex);
  217. TEST_ASSERT_EQUAL_INT(EBUSY, res);
  218. clock_gettime(CLOCK_REALTIME, &abs_timeout);
  219. timespec_add_nano(&abs_timeout, &abs_timeout, 100000000LL);
  220. res = pthread_mutex_timedlock(&mutex, &abs_timeout);
  221. TEST_ASSERT_EQUAL_INT(ETIMEDOUT, res);
  222. res = pthread_mutex_unlock(&mutex);
  223. TEST_ASSERT_EQUAL_INT(0, res);
  224. }
  225. if (mutex_created) {
  226. pthread_mutex_destroy(&mutex);
  227. }
  228. }