pthread_cond_var.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // This is a simple implementation of pthread condition variables. In essence,
  14. // the waiter creates its own semaphore to wait on and pushes it in the cond var
  15. // specific list. Upon notify and broadcast, all the waiters for the given cond
  16. // var are woken up.
  17. #include <errno.h>
  18. #include <pthread.h>
  19. #include <string.h>
  20. #include "esp_err.h"
  21. #include "esp_attr.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/task.h"
  24. #include "freertos/semphr.h"
  25. #include "freertos/list.h"
  26. #include "pthread_internal.h"
  27. #include <sys/queue.h>
  28. #include <sys/time.h>
  29. #include "esp_log.h"
  30. const static char *TAG = "esp_pthread";
  31. typedef struct esp_pthread_cond_waiter {
  32. SemaphoreHandle_t wait_sem; ///< task specific semaphore to wait on
  33. TAILQ_ENTRY(esp_pthread_cond_waiter) link; ///< stash on the list of semaphores to be notified
  34. } esp_pthread_cond_waiter_t;
  35. typedef struct esp_pthread_cond {
  36. _lock_t lock; ///< lock that protects the list of semaphores
  37. TAILQ_HEAD(, esp_pthread_cond_waiter) waiter_list; ///< head of the list of semaphores
  38. } esp_pthread_cond_t;
  39. static int s_check_and_init_if_static(pthread_cond_t *cv)
  40. {
  41. int res = 0;
  42. if (cv == NULL || *cv == (pthread_cond_t) 0) {
  43. return EINVAL;
  44. }
  45. if (*cv == PTHREAD_COND_INITIALIZER) {
  46. portENTER_CRITICAL(&pthread_lazy_init_lock);
  47. if (*cv == PTHREAD_COND_INITIALIZER) {
  48. res = pthread_cond_init(cv, NULL);
  49. }
  50. portEXIT_CRITICAL(&pthread_lazy_init_lock);
  51. }
  52. return res;
  53. }
  54. int pthread_cond_signal(pthread_cond_t *cv)
  55. {
  56. int res = s_check_and_init_if_static(cv);
  57. if (res) {
  58. return res;
  59. }
  60. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  61. _lock_acquire_recursive(&cond->lock);
  62. esp_pthread_cond_waiter_t *entry;
  63. entry = TAILQ_FIRST(&cond->waiter_list);
  64. if (entry) {
  65. xSemaphoreGive(entry->wait_sem);
  66. }
  67. _lock_release_recursive(&cond->lock);
  68. return 0;
  69. }
  70. int pthread_cond_broadcast(pthread_cond_t *cv)
  71. {
  72. int res = s_check_and_init_if_static(cv);
  73. if (res) {
  74. return res;
  75. }
  76. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  77. _lock_acquire_recursive(&cond->lock);
  78. esp_pthread_cond_waiter_t *entry;
  79. TAILQ_FOREACH(entry, &cond->waiter_list, link) {
  80. xSemaphoreGive(entry->wait_sem);
  81. }
  82. _lock_release_recursive(&cond->lock);
  83. return 0;
  84. }
  85. int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
  86. {
  87. return pthread_cond_timedwait(cv, mut, NULL);
  88. }
  89. int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *to)
  90. {
  91. int ret;
  92. TickType_t timeout_ticks;
  93. int res = s_check_and_init_if_static(cv);
  94. if (res) {
  95. return res;
  96. }
  97. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  98. if (to == NULL) {
  99. timeout_ticks = portMAX_DELAY;
  100. } else {
  101. struct timeval abs_time, cur_time, diff_time;
  102. long timeout_msec;
  103. gettimeofday(&cur_time, NULL);
  104. abs_time.tv_sec = to->tv_sec;
  105. abs_time.tv_usec = to->tv_nsec / 1000;
  106. if (timercmp(&abs_time, &cur_time, <)) {
  107. /* As per the pthread spec, if the time has already
  108. * passed, no sleep is required.
  109. */
  110. timeout_msec = 0;
  111. } else {
  112. timersub(&abs_time, &cur_time, &diff_time);
  113. timeout_msec = (diff_time.tv_sec * 1000) + (diff_time.tv_usec / 1000);
  114. }
  115. if (timeout_msec <= 0) {
  116. return ETIMEDOUT;
  117. }
  118. timeout_ticks = timeout_msec / portTICK_PERIOD_MS;
  119. }
  120. esp_pthread_cond_waiter_t w;
  121. w.wait_sem = xSemaphoreCreateCounting(1, 0); /* First get will block */
  122. _lock_acquire_recursive(&cond->lock);
  123. TAILQ_INSERT_TAIL(&cond->waiter_list, &w, link);
  124. _lock_release_recursive(&cond->lock);
  125. pthread_mutex_unlock(mut);
  126. if (xSemaphoreTake(w.wait_sem, timeout_ticks) == pdTRUE) {
  127. ret = 0;
  128. } else {
  129. ret = ETIMEDOUT;
  130. }
  131. _lock_acquire_recursive(&cond->lock);
  132. TAILQ_REMOVE(&cond->waiter_list, &w, link);
  133. _lock_release_recursive(&cond->lock);
  134. vSemaphoreDelete(w.wait_sem);
  135. pthread_mutex_lock(mut);
  136. return ret;
  137. }
  138. int pthread_condattr_init(pthread_condattr_t *attr)
  139. {
  140. ESP_LOGV(TAG, "%s not yet implemented (%p)", __FUNCTION__, attr);
  141. return ENOSYS;
  142. }
  143. int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *att)
  144. {
  145. (void) att; /* Unused argument as of now */
  146. if (cv == NULL) {
  147. return EINVAL;
  148. }
  149. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) calloc(1, sizeof(esp_pthread_cond_t));
  150. if (cond == NULL) {
  151. return ENOMEM;
  152. }
  153. _lock_init_recursive(&cond->lock);
  154. TAILQ_INIT(&cond->waiter_list);
  155. *cv = (pthread_cond_t) cond;
  156. return 0;
  157. }
  158. int pthread_cond_destroy(pthread_cond_t *cv)
  159. {
  160. int ret = 0;
  161. if (cv == NULL || *cv == (pthread_cond_t) 0) {
  162. return EINVAL;
  163. }
  164. if (*cv == PTHREAD_COND_INITIALIZER) {
  165. return 0; // never initialized
  166. }
  167. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  168. if (!cond) {
  169. return EINVAL;
  170. }
  171. _lock_acquire_recursive(&cond->lock);
  172. if (!TAILQ_EMPTY(&cond->waiter_list)) {
  173. ret = EBUSY;
  174. }
  175. _lock_release_recursive(&cond->lock);
  176. if (ret == 0) {
  177. *cv = (pthread_cond_t) 0;
  178. _lock_close_recursive(&cond->lock);
  179. free(cond);
  180. }
  181. return ret;
  182. }
  183. /* Hook function to force linking this file */
  184. void pthread_include_pthread_cond_var_impl(void)
  185. {
  186. }