blob: 2d5e47e1f8b6c029ff3d5cd2fc0cb0d9e52678fc [file] [log] [blame]
Andy Ross53c85992017-07-24 14:59:55 -07001/*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef __PTHREAD_H__
8#define __PTHREAD_H__
9
Youvedeep Singhc8aa6572017-12-28 14:31:57 +053010#include <kernel.h>
Andy Rossccf3bf72018-05-10 11:10:34 -070011#include <wait_q.h>
Ramakrishna Pallalaf603e602018-04-05 22:41:15 +053012#include <posix/time.h>
13#include <posix/unistd.h>
Youvedeep Singhabc94b82018-01-03 15:38:02 +053014#include "sys/types.h"
Youvedeep Singh7eabf102018-01-13 18:51:15 +053015#include "posix_sched.h"
Youvedeep Singhc8aa6572017-12-28 14:31:57 +053016
17enum pthread_state {
18 /* The thread is running and joinable. */
19 PTHREAD_JOINABLE = 0,
20 /* The thread is running and detached. */
21 PTHREAD_DETACHED,
22 /* A joinable thread exited and its return code is available. */
23 PTHREAD_EXITED,
24 /* The thread structure is unallocated and available for reuse. */
25 PTHREAD_TERMINATED
26};
27
28struct posix_thread {
29 struct k_thread thread;
30
31 /* Exit status */
32 void *retval;
33
34 /* Pthread cancellation */
35 int cancel_state;
36 int cancel_pending;
37 struct k_sem cancel_lock_sem;
38 pthread_mutex_t cancel_lock;
39
40 /* Pthread State */
41 enum pthread_state state;
42 pthread_mutex_t state_lock;
43 struct k_sem state_lock_sem;
44 pthread_cond_t state_cond;
45};
46
47/* Pthread detach/joinable */
48#define PTHREAD_CREATE_JOINABLE 0
49#define PTHREAD_CREATE_DETACHED 1
50
51/* Pthread cancellation */
52#define _PTHREAD_CANCEL_POS 0
53#define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS)
54#define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS)
Andy Ross53c85992017-07-24 14:59:55 -070055
Andy Ross53c85992017-07-24 14:59:55 -070056/**
57 * @brief Declare a pthread condition variable
58 *
59 * Declaration API for a pthread condition variable. This is not a
60 * POSIX API, it's provided to better conform with Zephyr's allocation
61 * strategies for kernel objects.
62 *
63 * @param name Symbol name of the condition variable
64 */
65#define PTHREAD_COND_DEFINE(name) \
66 struct pthread_cond name = { \
Andy Rossccf3bf72018-05-10 11:10:34 -070067 .wait_q = _WAIT_Q_INIT(&name.wait_q), \
Andy Ross53c85992017-07-24 14:59:55 -070068 }
69
70/**
71 * @brief POSIX threading compatibility API
72 *
73 * See IEEE 1003.1
74 */
75static inline int pthread_cond_init(pthread_cond_t *cv,
76 const pthread_condattr_t *att)
77{
78 ARG_UNUSED(att);
Andy Rossccf3bf72018-05-10 11:10:34 -070079 _waitq_init(&cv->wait_q);
Andy Ross53c85992017-07-24 14:59:55 -070080 return 0;
81}
82
83/**
84 * @brief POSIX threading compatibility API
85 *
86 * See IEEE 1003.1
87 */
88static inline int pthread_cond_destroy(pthread_cond_t *cv)
89{
90 return 0;
91}
92
93/**
94 * @brief POSIX threading compatibility API
95 *
96 * See IEEE 1003.1
97 */
98int pthread_cond_signal(pthread_cond_t *cv);
99
100/**
101 * @brief POSIX threading compatibility API
102 *
103 * See IEEE 1003.1
104 */
105int pthread_cond_broadcast(pthread_cond_t *cv);
106
107/**
108 * @brief POSIX threading compatibility API
109 *
110 * See IEEE 1003.1
111 */
112int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut);
113
114/**
115 * @brief POSIX threading compatibility API
116 *
117 * See IEEE 1003.1
118 */
119int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut,
120 const struct timespec *to);
121
122/**
123 * @brief POSIX threading compatibility API
124 *
125 * See IEEE 1003.1.
126 *
127 * Note that pthread attribute structs are currently noops in Zephyr.
128 */
129static inline int pthread_condattr_init(pthread_condattr_t *att)
130{
131 return 0;
132}
133
134/**
135 * @brief POSIX threading compatibility API
136 *
137 * See IEEE 1003.1
138 *
139 * Note that pthread attribute structs are currently noops in Zephyr.
140 */
141static inline int pthread_condattr_destroy(pthread_condattr_t *att)
142{
143 return 0;
144}
145
146/**
147 * @brief Declare a pthread mutex
148 *
149 * Declaration API for a pthread mutex. This is not a POSIX API, it's
150 * provided to better conform with Zephyr's allocation strategies for
151 * kernel objects.
152 *
153 * @param name Symbol name of the mutex
154 */
155#define PTHREAD_MUTEX_DEFINE(name) \
156 K_SEM_DEFINE(name##_psem, 1, 1); \
157 struct pthread_mutex name = { \
158 .sem = &name##_psem, \
159 }
160
Andy Ross53c85992017-07-24 14:59:55 -0700161/**
162 * @brief POSIX threading compatibility API
163 *
164 * See IEEE 1003.1
165 */
166static inline int pthread_mutex_init(pthread_mutex_t *m,
167 const pthread_mutexattr_t *att)
168{
169 ARG_UNUSED(att);
170
171 k_sem_init(m->sem, 1, 1);
172
173 return 0;
174}
175
176/**
177 * @brief POSIX threading compatibility API
178 *
179 * See IEEE 1003.1
180 */
181static inline int pthread_mutex_destroy(pthread_mutex_t *m)
182{
183 ARG_UNUSED(m);
184
185 return 0;
186}
187
188/**
189 * @brief POSIX threading compatibility API
190 *
191 * See IEEE 1003.1
192 */
193static inline int pthread_mutex_lock(pthread_mutex_t *m)
194{
195 return k_sem_take(m->sem, K_FOREVER);
196}
197
198/**
199 * @brief POSIX threading compatibility API
200 *
201 * See IEEE 1003.1
202 */
203static inline int pthread_mutex_timedlock(pthread_mutex_t *m,
204 const struct timespec *to)
205{
206 int ret = k_sem_take(m->sem, _ts_to_ms(to));
207
Youvedeep Singh325abfb2018-02-08 08:50:06 +0530208 return ret == 0 ? ret : ETIMEDOUT;
Andy Ross53c85992017-07-24 14:59:55 -0700209}
210
211/**
212 * @brief POSIX threading compatibility API
213 *
214 * See IEEE 1003.1
215 */
216int pthread_mutex_trylock(pthread_mutex_t *m);
217
218/**
219 * @brief POSIX threading compatibility API
220 *
221 * See IEEE 1003.1
222 */
223static inline int pthread_mutex_unlock(pthread_mutex_t *m)
224{
225 k_sem_give(m->sem);
226 return 0;
227}
228
229/**
230 * @brief POSIX threading compatibility API
231 *
232 * See IEEE 1003.1
233 *
234 * Note that pthread attribute structs are currently noops in Zephyr.
235 */
236static inline int pthread_mutexattr_init(pthread_mutexattr_t *m)
237{
238 ARG_UNUSED(m);
239
240 return 0;
241}
242
243/**
244 * @brief POSIX threading compatibility API
245 *
246 * See IEEE 1003.1
247 *
248 * Note that pthread attribute structs are currently noops in Zephyr.
249 */
250static inline int pthread_mutexattr_destroy(pthread_mutexattr_t *m)
251{
252 ARG_UNUSED(m);
253
254 return 0;
255}
256
257/* FIXME: these are going to be tricky to implement. Zephyr has (for
258 * good reason) deprecated its own "initializer" macros in favor of a
259 * static "declaration" macros instead. Using such a macro inside a
260 * gcc compound expression to declare and object then reference it
261 * would work, but gcc limits such expressions to function context
262 * (because they may need to generate code that runs at assignment
263 * time) and much real-world use of these initializers is for static
264 * variables. The best trick I can think of would be to declare it in
265 * a special section and then initialize that section at runtime
266 * startup, which sort of defeats the purpose of having these be
267 * static...
268 *
269 * Instead, see the nonstandard PTHREAD_*_DEFINE macros instead, which
270 * work similarly but conform to Zephyr's paradigms.
271 */
272/* #define PTHREAD_MUTEX_INITIALIZER */
273/* #define PTHREAD_COND_INITIALIZER */
274
Andy Ross53c85992017-07-24 14:59:55 -0700275/**
276 * @brief Declare a pthread barrier
277 *
278 * Declaration API for a pthread barrier. This is not a
279 * POSIX API, it's provided to better conform with Zephyr's allocation
280 * strategies for kernel objects.
281 *
282 * @param name Symbol name of the barrier
283 * @param count Thread count, same as the "count" argument to
284 * pthread_barrier_init()
285 */
286#define PTHREAD_BARRIER_DEFINE(name, count) \
287 struct pthread_barrier name = { \
Andy Rossccf3bf72018-05-10 11:10:34 -0700288 .wait_q = _WAIT_Q_INIT(&name.wait_q), \
Andy Ross53c85992017-07-24 14:59:55 -0700289 .max = count, \
290 }
291
292/**
293 * @brief POSIX threading compatibility API
294 *
295 * See IEEE 1003.1
296 */
297int pthread_barrier_wait(pthread_barrier_t *b);
298
299/**
300 * @brief POSIX threading compatibility API
301 *
302 * See IEEE 1003.1
303 */
304static inline int pthread_barrier_init(pthread_barrier_t *b,
305 const pthread_barrierattr_t *attr,
306 unsigned int count)
307{
308 ARG_UNUSED(attr);
309
310 b->max = count;
311 b->count = 0;
Andy Rossccf3bf72018-05-10 11:10:34 -0700312 _waitq_init(&b->wait_q);
Andy Ross53c85992017-07-24 14:59:55 -0700313
314 return 0;
315}
316
317/**
318 * @brief POSIX threading compatibility API
319 *
320 * See IEEE 1003.1
321 */
322static inline int pthread_barrier_destroy(pthread_barrier_t *b)
323{
324 ARG_UNUSED(b);
325
326 return 0;
327}
328
329/**
330 * @brief POSIX threading compatibility API
331 *
332 * See IEEE 1003.1
333 *
334 * Note that pthread attribute structs are currently noops in Zephyr.
335 */
336static inline int pthread_barrierattr_init(pthread_barrierattr_t *b)
337{
338 ARG_UNUSED(b);
339
340 return 0;
341}
342
343/**
344 * @brief POSIX threading compatibility API
345 *
346 * See IEEE 1003.1
347 *
348 * Note that pthread attribute structs are currently noops in Zephyr.
349 */
350static inline int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
351{
352 ARG_UNUSED(b);
353
354 return 0;
355}
356
357/* Predicates and setters for various pthread attribute values that we
358 * don't support (or always support: the "process shared" attribute
359 * can only be true given the way Zephyr implements these
360 * objects). Leave these undefined for simplicity instead of defining
361 * stubs to return an error that would have to be logged and
362 * interpreted just to figure out that we didn't support it in the
363 * first place. These APIs are very rarely used even in production
364 * Unix code. Leave the declarations here so they can be easily
365 * uncommented and implemented as needed.
366
367int pthread_condattr_getclock(const pthread_condattr_t * clockid_t *);
368int pthread_condattr_getpshared(const pthread_condattr_t * int *);
369int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
370int pthread_condattr_setpshared(pthread_condattr_t *, int);
371int pthread_mutex_consistent(pthread_mutex_t *);
372int pthread_mutex_getprioceiling(const pthread_mutex_t * int *);
373int pthread_mutex_setprioceiling(pthread_mutex_t *, int int *);
374int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *);
375int pthread_mutexattr_getprotocol(const pthread_mutexattr_t * int *);
376int pthread_mutexattr_getpshared(const pthread_mutexattr_t * int *);
377int pthread_mutexattr_getrobust(const pthread_mutexattr_t * int *);
378int pthread_mutexattr_gettype(const pthread_mutexattr_t * int *);
379int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
380int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
381int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
382int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
383int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
384int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *);
385int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
386*/
387
Youvedeep Singhc8aa6572017-12-28 14:31:57 +0530388/* Base Pthread related APIs */
389
390/**
391 * @brief Obtain ID of the calling thread.
392 *
393 * The results of calling this API from threads not created with
394 * pthread_create() are undefined.
395 *
396 * See IEEE 1003.1
397 */
398static inline pthread_t pthread_self(void)
399{
400 return (pthread_t)k_current_get();
401}
402
403
404/**
405 * @brief Compare thread IDs.
406 *
407 * See IEEE 1003.1
408 */
409static inline int pthread_equal(pthread_t pt1, pthread_t pt2)
410{
411 return (pt1 == pt2);
412}
413
Youvedeep Singh216883c2018-02-28 15:37:00 +0530414/**
415 * @brief Destroy the read-write lock attributes object.
416 *
417 * See IEEE 1003.1
418 */
419static inline int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
420{
421 return 0;
422}
423
424/**
425 * @brief initialize the read-write lock attributes object.
426 *
427 * See IEEE 1003.1
428 */
429static inline int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
430{
431 return 0;
432}
433
Youvedeep Singhc8aa6572017-12-28 14:31:57 +0530434int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
435int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
436int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
437int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
438int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
439int pthread_attr_init(pthread_attr_t *attr);
440int pthread_attr_destroy(pthread_attr_t *attr);
441int pthread_attr_getschedparam(const pthread_attr_t *attr,
442 struct sched_param *schedparam);
443int pthread_getschedparam(pthread_t pthread, int *policy,
444 struct sched_param *param);
445int pthread_attr_getstack(const pthread_attr_t *attr,
446 void **stackaddr, size_t *stacksize);
447int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
448 size_t stacksize);
449void pthread_exit(void *retval);
450int pthread_join(pthread_t thread, void **status);
451int pthread_cancel(pthread_t pthread);
452int pthread_detach(pthread_t thread);
453int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
454 void *(*threadroutine)(void *), void *arg);
455int pthread_setcancelstate(int state, int *oldstate);
456int pthread_attr_setschedparam(pthread_attr_t *attr,
457 const struct sched_param *schedparam);
458int pthread_setschedparam(pthread_t pthread, int policy,
459 const struct sched_param *param);
Youvedeep Singh216883c2018-02-28 15:37:00 +0530460int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
461int pthread_rwlock_init(pthread_rwlock_t *rwlock,
462 const pthread_rwlockattr_t *attr);
463int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
464int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
465 const struct timespec *abstime);
466int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
467 const struct timespec *abstime);
468int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
469int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
470int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
471int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
Youvedeep Singhc8aa6572017-12-28 14:31:57 +0530472
Andy Ross53c85992017-07-24 14:59:55 -0700473#endif /* __PTHREAD_H__ */