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