1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
28
29
33
34
35
36
37
38
41
42
49
50
51
52
53
54
55
56
59
60
61
62
63
64
65
68
69
70
71
72
73
74
75
76
79
80
81
82
83
84
87
88
89
90
91
92
96
97
98
99
100
101
102
103
106
107
108
109
112
113
114
115
116
117
118
119
120
121
132
133
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
168
169
170
171
172
173
174
175
176
177
178
179
180
181
186
187
192
193
198
199
204
205
210
211
216
217
218
219
220
221
224
225
226
229
230
231
232
233
234
235
236
237
238
239
240
241
244
247
248
249
252
253
254
257
258
259
264
265
266
267
268
269
/* ... */
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include "esp_err.h"
#include "esp_attr.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/list.h"
#include "pthread_internal.h"
#include <sys/queue.h>
#include <sys/time.h>
#include "esp_log.h"13 includes
const static char *TAG = "esp_pthread";
typedef struct esp_pthread_cond_waiter {
SemaphoreHandle_t wait_sem;
TAILQ_ENTRY(esp_pthread_cond_waiter) link;
}{ ... } esp_pthread_cond_waiter_t;
typedef struct esp_pthread_cond {
_lock_t lock;
TAILQ_HEAD(, esp_pthread_cond_waiter) waiter_list;
}{ ... } esp_pthread_cond_t;
static int s_check_and_init_if_static(pthread_cond_t *cv)
{
int res = 0;
if (cv == NULL || *cv == (pthread_cond_t) 0) {
return EINVAL;
}{...}
if (*cv == PTHREAD_COND_INITIALIZER) {
portENTER_CRITICAL(&pthread_lazy_init_lock);
if (*cv == PTHREAD_COND_INITIALIZER) {
res = pthread_cond_init(cv, NULL);
}{...}
portEXIT_CRITICAL(&pthread_lazy_init_lock);
}{...}
return res;
}{ ... }
int pthread_cond_signal(pthread_cond_t *cv)
{
int res = s_check_and_init_if_static(cv);
if (res) {
return res;
}{...}
esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
_lock_acquire_recursive(&cond->lock);
esp_pthread_cond_waiter_t *entry;
entry = TAILQ_FIRST(&cond->waiter_list);
if (entry) {
xSemaphoreGive(entry->wait_sem);
}{...}
_lock_release_recursive(&cond->lock);
return 0;
}{ ... }
int pthread_cond_broadcast(pthread_cond_t *cv)
{
int res = s_check_and_init_if_static(cv);
if (res) {
return res;
}{...}
esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
_lock_acquire_recursive(&cond->lock);
esp_pthread_cond_waiter_t *entry;
TAILQ_FOREACH(entry, &cond->waiter_list, link) {
xSemaphoreGive(entry->wait_sem);
}{...}
_lock_release_recursive(&cond->lock);
return 0;
}{ ... }
int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
{
return pthread_cond_timedwait(cv, mut, NULL);
}{ ... }
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *to)
{
int ret;
TickType_t timeout_ticks;
int res = s_check_and_init_if_static(cv);
if (res) {
return res;
}{...}
esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
if (to == NULL) {
timeout_ticks = portMAX_DELAY;
}{...} else {
struct timeval abs_time, cur_time, diff_time;
long timeout_msec;
gettimeofday(&cur_time, NULL);
abs_time.tv_sec = to->tv_sec;
abs_time.tv_usec = (to->tv_nsec + 1000 - 1) / 1000;
if (timercmp(&abs_time, &cur_time, <)) {
/* ... */
timeout_msec = 0;
}{...} else {
timersub(&abs_time, &cur_time, &diff_time);
timeout_msec = (diff_time.tv_sec * 1000) +
((diff_time.tv_usec + 1000 - 1) / 1000);
}{...}
if (timeout_msec <= 0) {
return ETIMEDOUT;
}{...}
timeout_ticks = (timeout_msec + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
/* ... */
timeout_ticks += 1;
}{...}
esp_pthread_cond_waiter_t w;
StaticSemaphore_t sem_buffer;
w.wait_sem = xSemaphoreCreateCountingStatic(1, 0, &sem_buffer);
_lock_acquire_recursive(&cond->lock);
TAILQ_INSERT_TAIL(&cond->waiter_list, &w, link);
_lock_release_recursive(&cond->lock);
pthread_mutex_unlock(mut);
if (xSemaphoreTake(w.wait_sem, timeout_ticks) == pdTRUE) {
ret = 0;
}{...} else {
ret = ETIMEDOUT;
}{...}
_lock_acquire_recursive(&cond->lock);
TAILQ_REMOVE(&cond->waiter_list, &w, link);
_lock_release_recursive(&cond->lock);
vSemaphoreDelete(w.wait_sem);
pthread_mutex_lock(mut);
return ret;
}{ ... }
int pthread_condattr_init(pthread_condattr_t *attr)
{
ESP_LOGW(TAG, "%s not yet implemented (%p)", __FUNCTION__, attr);
return ENOSYS;
}{ ... }
int pthread_condattr_destroy(pthread_condattr_t *attr)
{
ESP_LOGW(TAG, "%s not yet implemented (%p)", __FUNCTION__, attr);
return ENOSYS;
}{ ... }
int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr, int *restrict pshared)
{
ESP_LOGW(TAG, "%s not yet implemented (%p)", __FUNCTION__, attr);
return ENOSYS;
}{ ... }
int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
{
ESP_LOGW(TAG, "%s not yet implemented (%p)", __FUNCTION__, attr);
return ENOSYS;
}{ ... }
int pthread_condattr_getclock(const pthread_condattr_t *restrict attr, clockid_t *restrict clock_id)
{
ESP_LOGW(TAG, "%s not yet implemented (%p)", __FUNCTION__, attr);
return ENOSYS;
}{ ... }
int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
{
ESP_LOGW(TAG, "%s: not yet supported!", __func__);
return 0;
}{ ... }
int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *att)
{
(void) att;
if (cv == NULL) {
return EINVAL;
}{...}
esp_pthread_cond_t *cond = (esp_pthread_cond_t *) calloc(1, sizeof(esp_pthread_cond_t));
if (cond == NULL) {
return ENOMEM;
}{...}
_lock_init_recursive(&cond->lock);
TAILQ_INIT(&cond->waiter_list);
*cv = (pthread_cond_t) cond;
return 0;
}{ ... }
int pthread_cond_destroy(pthread_cond_t *cv)
{
int ret = 0;
if (cv == NULL || *cv == (pthread_cond_t) 0) {
return EINVAL;
}{...}
if (*cv == PTHREAD_COND_INITIALIZER) {
return 0;
}{...}
esp_pthread_cond_t *cond = (esp_pthread_cond_t *) *cv;
if (!cond) {
return EINVAL;
}{...}
_lock_acquire_recursive(&cond->lock);
if (!TAILQ_EMPTY(&cond->waiter_list)) {
ret = EBUSY;
}{...}
_lock_release_recursive(&cond->lock);
if (ret == 0) {
*cv = (pthread_cond_t) 0;
_lock_close_recursive(&cond->lock);
free(cond);
}{...}
return ret;
}{ ... }
void pthread_include_pthread_cond_var_impl(void)
{
}{ ... }