5
5
* SPDX-License-Identifier: Apache-2.0
6
6
*/
7
7
8
+ #include "posix_clock.h"
8
9
#include "posix_internal.h"
9
10
10
11
#include <zephyr/init.h>
15
16
16
17
LOG_MODULE_REGISTER (pthread_cond , CONFIG_PTHREAD_COND_LOG_LEVEL );
17
18
18
- __pinned_bss
19
- static struct k_condvar posix_cond_pool [CONFIG_MAX_PTHREAD_COND_COUNT ];
19
+ __pinned_bss static struct posix_cond posix_cond_pool [CONFIG_MAX_PTHREAD_COND_COUNT ];
20
20
21
21
SYS_BITARRAY_DEFINE_STATIC (posix_cond_bitarray , CONFIG_MAX_PTHREAD_COND_COUNT );
22
22
23
+ BUILD_ASSERT (sizeof (struct posix_condattr ) <= sizeof (pthread_condattr_t ),
24
+ "posix_condattr is too large" );
25
+
23
26
/*
24
27
* We reserve the MSB to mark a pthread_cond_t as initialized (from the
25
28
* perspective of the application). With a linear space, this means that
@@ -28,7 +31,7 @@ SYS_BITARRAY_DEFINE_STATIC(posix_cond_bitarray, CONFIG_MAX_PTHREAD_COND_COUNT);
28
31
BUILD_ASSERT (CONFIG_MAX_PTHREAD_COND_COUNT < PTHREAD_OBJ_MASK_INIT ,
29
32
"CONFIG_MAX_PTHREAD_COND_COUNT is too high" );
30
33
31
- static inline size_t posix_cond_to_offset (struct k_condvar * cv )
34
+ static inline size_t posix_cond_to_offset (struct posix_cond * cv )
32
35
{
33
36
return cv - posix_cond_pool ;
34
37
}
@@ -38,7 +41,7 @@ static inline size_t to_posix_cond_idx(pthread_cond_t cond)
38
41
return mark_pthread_obj_uninitialized (cond );
39
42
}
40
43
41
- static struct k_condvar * get_posix_cond (pthread_cond_t cond )
44
+ static struct posix_cond * get_posix_cond (pthread_cond_t cond )
42
45
{
43
46
int actually_initialized ;
44
47
size_t bit = to_posix_cond_idx (cond );
@@ -64,10 +67,10 @@ static struct k_condvar *get_posix_cond(pthread_cond_t cond)
64
67
return & posix_cond_pool [bit ];
65
68
}
66
69
67
- static struct k_condvar * to_posix_cond (pthread_cond_t * cvar )
70
+ static struct posix_cond * to_posix_cond (pthread_cond_t * cvar )
68
71
{
69
72
size_t bit ;
70
- struct k_condvar * cv ;
73
+ struct posix_cond * cv ;
71
74
72
75
if (* cvar != PTHREAD_COND_INITIALIZER ) {
73
76
return get_posix_cond (* cvar );
@@ -91,7 +94,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
91
94
{
92
95
int ret ;
93
96
struct k_mutex * m ;
94
- struct k_condvar * cv ;
97
+ struct posix_cond * cv ;
95
98
96
99
m = to_posix_mutex (mu );
97
100
cv = to_posix_cond (cond );
@@ -100,7 +103,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
100
103
}
101
104
102
105
LOG_DBG ("Waiting on cond %p with timeout %llx" , cv , timeout .ticks );
103
- ret = k_condvar_wait (cv , m , timeout );
106
+ ret = k_condvar_wait (& cv -> condvar , m , timeout );
104
107
if (ret == - EAGAIN ) {
105
108
LOG_DBG ("Timeout waiting on cond %p" , cv );
106
109
ret = ETIMEDOUT ;
@@ -118,15 +121,15 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
118
121
int pthread_cond_signal (pthread_cond_t * cvar )
119
122
{
120
123
int ret ;
121
- struct k_condvar * cv ;
124
+ struct posix_cond * cv ;
122
125
123
126
cv = to_posix_cond (cvar );
124
127
if (cv == NULL ) {
125
128
return EINVAL ;
126
129
}
127
130
128
131
LOG_DBG ("Signaling cond %p" , cv );
129
- ret = k_condvar_signal (cv );
132
+ ret = k_condvar_signal (& cv -> condvar );
130
133
if (ret < 0 ) {
131
134
LOG_DBG ("k_condvar_signal() failed: %d" , ret );
132
135
return - ret ;
@@ -140,15 +143,15 @@ int pthread_cond_signal(pthread_cond_t *cvar)
140
143
int pthread_cond_broadcast (pthread_cond_t * cvar )
141
144
{
142
145
int ret ;
143
- struct k_condvar * cv ;
146
+ struct posix_cond * cv ;
144
147
145
148
cv = get_posix_cond (* cvar );
146
149
if (cv == NULL ) {
147
150
return EINVAL ;
148
151
}
149
152
150
153
LOG_DBG ("Broadcasting on cond %p" , cv );
151
- ret = k_condvar_broadcast (cv );
154
+ ret = k_condvar_broadcast (& cv -> condvar );
152
155
if (ret < 0 ) {
153
156
LOG_DBG ("k_condvar_broadcast() failed: %d" , ret );
154
157
return - ret ;
@@ -171,12 +174,9 @@ int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struc
171
174
172
175
int pthread_cond_init (pthread_cond_t * cvar , const pthread_condattr_t * att )
173
176
{
174
- struct k_condvar * cv ;
177
+ struct posix_cond * cv ;
175
178
176
- ARG_UNUSED (att );
177
179
* cvar = PTHREAD_COND_INITIALIZER ;
178
-
179
- /* calls k_condvar_init() */
180
180
cv = to_posix_cond (cvar );
181
181
if (cv == NULL ) {
182
182
return ENOMEM ;
@@ -191,7 +191,7 @@ int pthread_cond_destroy(pthread_cond_t *cvar)
191
191
{
192
192
int err ;
193
193
size_t bit ;
194
- struct k_condvar * cv ;
194
+ struct posix_cond * cv ;
195
195
196
196
cv = get_posix_cond (* cvar );
197
197
if (cv == NULL ) {
@@ -216,7 +216,7 @@ static int pthread_cond_pool_init(void)
216
216
size_t i ;
217
217
218
218
for (i = 0 ; i < CONFIG_MAX_PTHREAD_COND_COUNT ; ++ i ) {
219
- err = k_condvar_init (& posix_cond_pool [i ]);
219
+ err = k_condvar_init (& posix_cond_pool [i ]. condvar );
220
220
__ASSERT_NO_MSG (err == 0 );
221
221
}
222
222
@@ -227,33 +227,45 @@ int pthread_condattr_init(pthread_condattr_t *att)
227
227
{
228
228
__ASSERT_NO_MSG (att != NULL );
229
229
230
- att -> clock = CLOCK_MONOTONIC ;
230
+ struct posix_condattr * const attr = (struct posix_condattr * )att ;
231
+
232
+ attr -> clock = CLOCK_MONOTONIC ;
231
233
232
234
return 0 ;
233
235
}
234
236
235
237
int pthread_condattr_destroy (pthread_condattr_t * att )
236
238
{
237
- ARG_UNUSED (att );
239
+ struct posix_condattr * const attr = (struct posix_condattr * )att ;
240
+
241
+ if (attr == NULL ) {
242
+ return EINVAL ;
243
+ }
244
+
245
+ * attr = (struct posix_condattr ){0 };
238
246
239
247
return 0 ;
240
248
}
241
249
242
250
int pthread_condattr_getclock (const pthread_condattr_t * ZRESTRICT att ,
243
251
clockid_t * ZRESTRICT clock_id )
244
252
{
245
- * clock_id = att -> clock ;
253
+ struct posix_condattr * const attr = (struct posix_condattr * )att ;
254
+
255
+ * clock_id = attr -> clock ;
246
256
247
257
return 0 ;
248
258
}
249
259
250
260
int pthread_condattr_setclock (pthread_condattr_t * att , clockid_t clock_id )
251
261
{
262
+ struct posix_condattr * const attr = (struct posix_condattr * )att ;
263
+
252
264
if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC ) {
253
265
return - EINVAL ;
254
266
}
255
267
256
- att -> clock = clock_id ;
268
+ attr -> clock = clock_id ;
257
269
258
270
return 0 ;
259
271
}
0 commit comments