7
7
8
8
#include "posix_internal.h"
9
9
10
+ #include <stdbool.h>
11
+
10
12
#include <zephyr/init.h>
11
13
#include <zephyr/kernel.h>
12
14
#include <zephyr/logging/log.h>
15
17
16
18
LOG_MODULE_REGISTER (pthread_cond , CONFIG_PTHREAD_COND_LOG_LEVEL );
17
19
18
- int64_t timespec_to_timeoutms (const struct timespec * abstime );
19
-
20
- __pinned_bss
21
- static struct k_condvar posix_cond_pool [CONFIG_MAX_PTHREAD_COND_COUNT ];
20
+ __pinned_bss static struct posix_cond posix_cond_pool [CONFIG_MAX_PTHREAD_COND_COUNT ];
22
21
23
22
SYS_BITARRAY_DEFINE_STATIC (posix_cond_bitarray , CONFIG_MAX_PTHREAD_COND_COUNT );
24
23
@@ -30,7 +29,7 @@ SYS_BITARRAY_DEFINE_STATIC(posix_cond_bitarray, CONFIG_MAX_PTHREAD_COND_COUNT);
30
29
BUILD_ASSERT (CONFIG_MAX_PTHREAD_COND_COUNT < PTHREAD_OBJ_MASK_INIT ,
31
30
"CONFIG_MAX_PTHREAD_COND_COUNT is too high" );
32
31
33
- static inline size_t posix_cond_to_offset (struct k_condvar * cv )
32
+ static inline size_t posix_cond_to_offset (struct posix_cond * cv )
34
33
{
35
34
return cv - posix_cond_pool ;
36
35
}
@@ -40,7 +39,7 @@ static inline size_t to_posix_cond_idx(pthread_cond_t cond)
40
39
return mark_pthread_obj_uninitialized (cond );
41
40
}
42
41
43
- static struct k_condvar * get_posix_cond (pthread_cond_t cond )
42
+ static struct posix_cond * get_posix_cond (pthread_cond_t cond )
44
43
{
45
44
int actually_initialized ;
46
45
size_t bit = to_posix_cond_idx (cond );
@@ -66,10 +65,30 @@ static struct k_condvar *get_posix_cond(pthread_cond_t cond)
66
65
return & posix_cond_pool [bit ];
67
66
}
68
67
69
- static struct k_condvar * to_posix_cond (pthread_cond_t * cvar )
68
+ static inline bool is_posix_condattr_valid (struct posix_condattr * attr )
69
+ {
70
+ if (attr == NULL ) {
71
+ return false;
72
+ }
73
+
74
+ if (!attr -> initialized ) {
75
+ LOG_DBG ("condattr is not initialized" );
76
+ return false;
77
+ }
78
+
79
+ /* revisit when POSIX_CLOCK_SELECTION is reworked */
80
+ if (!((attr -> clock == CLOCK_REALTIME ) || (attr -> clock == CLOCK_MONOTONIC ))) {
81
+ LOG_DBG ("condattr clock %u is invalid" , attr -> clock );
82
+ return false;
83
+ }
84
+
85
+ return true;
86
+ }
87
+
88
+ static struct posix_cond * to_posix_cond (pthread_cond_t * cvar )
70
89
{
71
90
size_t bit ;
72
- struct k_condvar * cv ;
91
+ struct posix_cond * cv ;
73
92
74
93
if (* cvar != PTHREAD_COND_INITIALIZER ) {
75
94
return get_posix_cond (* cvar );
@@ -85,6 +104,7 @@ static struct k_condvar *to_posix_cond(pthread_cond_t *cvar)
85
104
/* Record the associated posix_cond in mu and mark as initialized */
86
105
* cvar = mark_pthread_obj_initialized (bit );
87
106
cv = & posix_cond_pool [bit ];
107
+ (void )pthread_condattr_init ((pthread_condattr_t * )& cv -> attr );
88
108
89
109
return cv ;
90
110
}
@@ -93,7 +113,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
93
113
{
94
114
int ret ;
95
115
struct k_mutex * m ;
96
- struct k_condvar * cv ;
116
+ struct posix_cond * cv ;
97
117
98
118
m = to_posix_mutex (mu );
99
119
cv = to_posix_cond (cond );
@@ -102,7 +122,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
102
122
}
103
123
104
124
LOG_DBG ("Waiting on cond %p with timeout %llx" , cv , timeout .ticks );
105
- ret = k_condvar_wait (cv , m , timeout );
125
+ ret = k_condvar_wait (& cv -> condvar , m , timeout );
106
126
if (ret == - EAGAIN ) {
107
127
LOG_DBG ("Timeout waiting on cond %p" , cv );
108
128
ret = ETIMEDOUT ;
@@ -120,15 +140,15 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
120
140
int pthread_cond_signal (pthread_cond_t * cvar )
121
141
{
122
142
int ret ;
123
- struct k_condvar * cv ;
143
+ struct posix_cond * cv ;
124
144
125
145
cv = to_posix_cond (cvar );
126
146
if (cv == NULL ) {
127
147
return EINVAL ;
128
148
}
129
149
130
150
LOG_DBG ("Signaling cond %p" , cv );
131
- ret = k_condvar_signal (cv );
151
+ ret = k_condvar_signal (& cv -> condvar );
132
152
if (ret < 0 ) {
133
153
LOG_DBG ("k_condvar_signal() failed: %d" , ret );
134
154
return - ret ;
@@ -142,15 +162,15 @@ int pthread_cond_signal(pthread_cond_t *cvar)
142
162
int pthread_cond_broadcast (pthread_cond_t * cvar )
143
163
{
144
164
int ret ;
145
- struct k_condvar * cv ;
165
+ struct posix_cond * cv ;
146
166
147
167
cv = get_posix_cond (* cvar );
148
168
if (cv == NULL ) {
149
169
return EINVAL ;
150
170
}
151
171
152
172
LOG_DBG ("Broadcasting on cond %p" , cv );
153
- ret = k_condvar_broadcast (cv );
173
+ ret = k_condvar_broadcast (& cv -> condvar );
154
174
if (ret < 0 ) {
155
175
LOG_DBG ("k_condvar_broadcast() failed: %d" , ret );
156
176
return - ret ;
@@ -168,22 +188,30 @@ int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
168
188
169
189
int pthread_cond_timedwait (pthread_cond_t * cv , pthread_mutex_t * mut , const struct timespec * abstime )
170
190
{
171
- return cond_wait (cv , mut , K_MSEC ((int32_t )timespec_to_timeoutms (abstime )));
191
+ return cond_wait (cv , mut ,
192
+ K_MSEC ((int32_t )timespec_to_timeoutms (
193
+ ((struct posix_cond * )cv )-> attr .clock , abstime )));
172
194
}
173
195
174
196
int pthread_cond_init (pthread_cond_t * cvar , const pthread_condattr_t * att )
175
197
{
176
- struct k_condvar * cv ;
198
+ struct posix_cond * cv ;
177
199
178
- ARG_UNUSED (att );
179
200
* cvar = PTHREAD_COND_INITIALIZER ;
180
-
181
- /* calls k_condvar_init() */
182
201
cv = to_posix_cond (cvar );
183
202
if (cv == NULL ) {
184
203
return ENOMEM ;
185
204
}
186
205
206
+ if (att != NULL ) {
207
+ if (!is_posix_condattr_valid ((struct posix_condattr * )att )) {
208
+ LOG_ERR ("%s invalid" , "pthread_condattr_t" );
209
+ return EINVAL ;
210
+ }
211
+
212
+ cv -> attr = * ((const struct posix_condattr * )att );
213
+ }
214
+
187
215
LOG_DBG ("Initialized cond %p" , cv );
188
216
189
217
return 0 ;
@@ -193,7 +221,7 @@ int pthread_cond_destroy(pthread_cond_t *cvar)
193
221
{
194
222
int err ;
195
223
size_t bit ;
196
- struct k_condvar * cv ;
224
+ struct posix_cond * cv ;
197
225
198
226
cv = get_posix_cond (* cvar );
199
227
if (cv == NULL ) {
@@ -218,7 +246,7 @@ static int pthread_cond_pool_init(void)
218
246
size_t i ;
219
247
220
248
for (i = 0 ; i < CONFIG_MAX_PTHREAD_COND_COUNT ; ++ i ) {
221
- err = k_condvar_init (& posix_cond_pool [i ]);
249
+ err = k_condvar_init (& posix_cond_pool [i ]. condvar );
222
250
__ASSERT_NO_MSG (err == 0 );
223
251
}
224
252
@@ -227,35 +255,60 @@ static int pthread_cond_pool_init(void)
227
255
228
256
int pthread_condattr_init (pthread_condattr_t * att )
229
257
{
230
- __ASSERT_NO_MSG ( att != NULL ) ;
258
+ struct posix_condattr * const attr = ( struct posix_condattr * ) att ;
231
259
232
- att -> clock = CLOCK_MONOTONIC ;
260
+ if (attr == NULL ) {
261
+ return EINVAL ;
262
+ }
263
+
264
+ attr -> clock = CLOCK_MONOTONIC ;
265
+ attr -> initialized = true;
233
266
234
267
return 0 ;
235
268
}
236
269
237
270
int pthread_condattr_destroy (pthread_condattr_t * att )
238
271
{
239
- ARG_UNUSED (att );
272
+ struct posix_condattr * const attr = (struct posix_condattr * )att ;
273
+
274
+ if (attr == NULL ) {
275
+ return EINVAL ;
276
+ }
277
+
278
+ * attr = (struct posix_condattr ){0 };
240
279
241
280
return 0 ;
242
281
}
243
282
244
283
int pthread_condattr_getclock (const pthread_condattr_t * ZRESTRICT att ,
245
284
clockid_t * ZRESTRICT clock_id )
246
285
{
247
- * clock_id = att -> clock ;
286
+ struct posix_condattr * const attr = (struct posix_condattr * )att ;
287
+
288
+ if ((attr == NULL ) || !attr -> initialized ) {
289
+ LOG_DBG ("condattr is not initialized" );
290
+ return EINVAL ;
291
+ }
292
+
293
+ * clock_id = attr -> clock ;
248
294
249
295
return 0 ;
250
296
}
251
297
252
298
int pthread_condattr_setclock (pthread_condattr_t * att , clockid_t clock_id )
253
299
{
300
+ struct posix_condattr * const attr = (struct posix_condattr * )att ;
301
+
254
302
if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC ) {
255
303
return - EINVAL ;
256
304
}
257
305
258
- att -> clock = clock_id ;
306
+ if ((attr == NULL ) || !attr -> initialized ) {
307
+ LOG_DBG ("condattr is not initialized" );
308
+ return EINVAL ;
309
+ }
310
+
311
+ attr -> clock = clock_id ;
259
312
260
313
return 0 ;
261
314
}
0 commit comments