pthread_cond_var.c 5.5 KB

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