pthread_cond_var.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <rom/queue.h>
  27. #include <sys/time.h>
  28. #define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
  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. int pthread_cond_signal(pthread_cond_t *cv)
  40. {
  41. ESP_LOGV(TAG, "%s %p", __FUNCTION__, cv);
  42. if (cv == NULL || *cv == (pthread_cond_t) 0) {
  43. return EINVAL;
  44. }
  45. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  46. _lock_acquire_recursive(&cond->lock);
  47. esp_pthread_cond_waiter_t *entry;
  48. entry = TAILQ_FIRST(&cond->waiter_list);
  49. if (entry) {
  50. xSemaphoreGive(entry->wait_sem);
  51. }
  52. _lock_release_recursive(&cond->lock);
  53. return 0;
  54. }
  55. int pthread_cond_broadcast(pthread_cond_t *cv)
  56. {
  57. ESP_LOGV(TAG, "%s %p", __FUNCTION__, cv);
  58. if (cv == NULL || *cv == (pthread_cond_t) 0) {
  59. return EINVAL;
  60. }
  61. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  62. _lock_acquire_recursive(&cond->lock);
  63. esp_pthread_cond_waiter_t *entry;
  64. TAILQ_FOREACH(entry, &cond->waiter_list, link) {
  65. xSemaphoreGive(entry->wait_sem);
  66. }
  67. _lock_release_recursive(&cond->lock);
  68. return 0;
  69. }
  70. int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
  71. {
  72. ESP_LOGV(TAG, "%s %p %p", __FUNCTION__, cv, mut);
  73. return pthread_cond_timedwait(cv, mut, NULL);
  74. }
  75. int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *to)
  76. {
  77. int ret;
  78. TickType_t timeout_ticks;
  79. ESP_LOGV(TAG, "%s %p %p %p", __FUNCTION__, cv, mut, to);
  80. if (cv == NULL || *cv == (pthread_cond_t) 0) {
  81. return EINVAL;
  82. }
  83. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  84. if (to == NULL) {
  85. timeout_ticks = portMAX_DELAY;
  86. } else {
  87. struct timeval abs_time, cur_time, diff_time;
  88. long timeout_msec;
  89. gettimeofday(&cur_time, NULL);
  90. abs_time.tv_sec = to->tv_sec;
  91. abs_time.tv_usec = to->tv_nsec / 1000;
  92. if (timercmp(&abs_time, &cur_time, <)) {
  93. /* As per the pthread spec, if the time has already
  94. * passed, no sleep is required.
  95. */
  96. timeout_msec = 0;
  97. } else {
  98. timersub(&abs_time, &cur_time, &diff_time);
  99. timeout_msec = (diff_time.tv_sec * 1000) + (diff_time.tv_usec / 1000);
  100. }
  101. if (timeout_msec <= 0) {
  102. return ETIMEDOUT;
  103. }
  104. timeout_ticks = timeout_msec / portTICK_PERIOD_MS;
  105. }
  106. esp_pthread_cond_waiter_t w;
  107. w.wait_sem = xSemaphoreCreateCounting(1, 0); /* First get will block */
  108. _lock_acquire_recursive(&cond->lock);
  109. TAILQ_INSERT_TAIL(&cond->waiter_list, &w, link);
  110. _lock_release_recursive(&cond->lock);
  111. pthread_mutex_unlock(mut);
  112. if (xSemaphoreTake(w.wait_sem, timeout_ticks) == pdTRUE) {
  113. ret = 0;
  114. } else {
  115. ret = ETIMEDOUT;
  116. }
  117. _lock_acquire_recursive(&cond->lock);
  118. TAILQ_REMOVE(&cond->waiter_list, &w, link);
  119. _lock_release_recursive(&cond->lock);
  120. vSemaphoreDelete(w.wait_sem);
  121. pthread_mutex_lock(mut);
  122. return ret;
  123. }
  124. int pthread_condattr_init(pthread_condattr_t *attr)
  125. {
  126. ESP_LOGV(TAG, "%s not yet implemented (%p)", __FUNCTION__, attr);
  127. return ENOSYS;
  128. }
  129. int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *att)
  130. {
  131. (void) att; /* Unused argument as of now */
  132. ESP_LOGV(TAG, "%s %p %p", __FUNCTION__, cv, att);
  133. if (cv == NULL) {
  134. return EINVAL;
  135. }
  136. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) calloc(1, sizeof(esp_pthread_cond_t));
  137. if (cond == NULL) {
  138. return ENOMEM;
  139. }
  140. _lock_init_recursive(&cond->lock);
  141. TAILQ_INIT(&cond->waiter_list);
  142. *cv = (pthread_cond_t) cond;
  143. return 0;
  144. }
  145. int pthread_cond_destroy(pthread_cond_t *cv)
  146. {
  147. int ret = 0;
  148. ESP_LOGV(TAG, "%s %p", __FUNCTION__, cv);
  149. if (cv == NULL || *cv == (pthread_cond_t) 0) {
  150. return EINVAL;
  151. }
  152. esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
  153. _lock_acquire_recursive(&cond->lock);
  154. if (!TAILQ_EMPTY(&cond->waiter_list)) {
  155. ret = EBUSY;
  156. }
  157. _lock_release_recursive(&cond->lock);
  158. if (ret == 0) {
  159. *cv = (pthread_cond_t) 0;
  160. _lock_close_recursive(&cond->lock);
  161. free(cond);
  162. }
  163. return ret;
  164. }