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