blob: 4f187576e67f854c11d118ecf5bcc6f6a9fd3751 [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
Flavio Ceolin67ca1762018-09-14 10:43:44 -07007#ifndef ZEPHYR_INCLUDE_POSIX_PTHREAD_H_
8#define ZEPHYR_INCLUDE_POSIX_PTHREAD_H_
Andy Ross53c85992017-07-24 14:59:55 -07009
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"
Niranjhana N414c39f2018-04-11 16:00:09 +053016#include <posix/pthread_key.h>
Punit Varaeb8ba692018-05-03 15:24:08 +053017#include <stdlib.h>
18#include <string.h>
Youvedeep Singhc8aa6572017-12-28 14:31:57 +053019
20enum pthread_state {
21 /* The thread is running and joinable. */
22 PTHREAD_JOINABLE = 0,
23 /* The thread is running and detached. */
24 PTHREAD_DETACHED,
25 /* A joinable thread exited and its return code is available. */
26 PTHREAD_EXITED,
27 /* The thread structure is unallocated and available for reuse. */
28 PTHREAD_TERMINATED
29};
30
31struct posix_thread {
32 struct k_thread thread;
33
Niranjhana N414c39f2018-04-11 16:00:09 +053034 /* List of keys that thread has called pthread_setspecific() on */
35 sys_slist_t key_list;
36
Youvedeep Singhc8aa6572017-12-28 14:31:57 +053037 /* Exit status */
38 void *retval;
39
40 /* Pthread cancellation */
41 int cancel_state;
42 int cancel_pending;
Youvedeep Singhc8aa6572017-12-28 14:31:57 +053043 pthread_mutex_t cancel_lock;
44
45 /* Pthread State */
46 enum pthread_state state;
47 pthread_mutex_t state_lock;
Youvedeep Singhc8aa6572017-12-28 14:31:57 +053048 pthread_cond_t state_cond;
49};
50
51/* Pthread detach/joinable */
52#define PTHREAD_CREATE_JOINABLE 0
53#define PTHREAD_CREATE_DETACHED 1
54
55/* Pthread cancellation */
56#define _PTHREAD_CANCEL_POS 0
57#define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS)
58#define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS)
Andy Ross53c85992017-07-24 14:59:55 -070059
Niranjhana N414c39f2018-04-11 16:00:09 +053060/* Passed to pthread_once */
61#define PTHREAD_ONCE_INIT 1
62
Andy Ross53c85992017-07-24 14:59:55 -070063/**
64 * @brief Declare a pthread condition variable
65 *
66 * Declaration API for a pthread condition variable. This is not a
67 * POSIX API, it's provided to better conform with Zephyr's allocation
68 * strategies for kernel objects.
69 *
70 * @param name Symbol name of the condition variable
71 */
72#define PTHREAD_COND_DEFINE(name) \
73 struct pthread_cond name = { \
Andy Rossccf3bf72018-05-10 11:10:34 -070074 .wait_q = _WAIT_Q_INIT(&name.wait_q), \
Andy Ross53c85992017-07-24 14:59:55 -070075 }
76
77/**
78 * @brief POSIX threading compatibility API
79 *
80 * See IEEE 1003.1
81 */
82static inline int pthread_cond_init(pthread_cond_t *cv,
83 const pthread_condattr_t *att)
84{
85 ARG_UNUSED(att);
Andy Rossccf3bf72018-05-10 11:10:34 -070086 _waitq_init(&cv->wait_q);
Andy Ross53c85992017-07-24 14:59:55 -070087 return 0;
88}
89
90/**
91 * @brief POSIX threading compatibility API
92 *
93 * See IEEE 1003.1
94 */
95static inline int pthread_cond_destroy(pthread_cond_t *cv)
96{
97 return 0;
98}
99
100/**
101 * @brief POSIX threading compatibility API
102 *
103 * See IEEE 1003.1
104 */
105int pthread_cond_signal(pthread_cond_t *cv);
106
107/**
108 * @brief POSIX threading compatibility API
109 *
110 * See IEEE 1003.1
111 */
112int pthread_cond_broadcast(pthread_cond_t *cv);
113
114/**
115 * @brief POSIX threading compatibility API
116 *
117 * See IEEE 1003.1
118 */
119int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut);
120
121/**
122 * @brief POSIX threading compatibility API
123 *
124 * See IEEE 1003.1
125 */
126int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut,
127 const struct timespec *to);
128
129/**
130 * @brief POSIX threading compatibility API
131 *
132 * See IEEE 1003.1.
133 *
134 * Note that pthread attribute structs are currently noops in Zephyr.
135 */
136static inline int pthread_condattr_init(pthread_condattr_t *att)
137{
138 return 0;
139}
140
141/**
142 * @brief POSIX threading compatibility API
143 *
144 * See IEEE 1003.1
145 *
146 * Note that pthread attribute structs are currently noops in Zephyr.
147 */
148static inline int pthread_condattr_destroy(pthread_condattr_t *att)
149{
150 return 0;
151}
152
153/**
154 * @brief Declare a pthread mutex
155 *
156 * Declaration API for a pthread mutex. This is not a POSIX API, it's
157 * provided to better conform with Zephyr's allocation strategies for
158 * kernel objects.
159 *
160 * @param name Symbol name of the mutex
161 */
Punit Varaeb8ba692018-05-03 15:24:08 +0530162#define PTHREAD_MUTEX_DEFINE(name) \
163 struct pthread_mutex name \
164 __in_section(_k_mutex, static, name) = \
165 { \
166 .lock_count = 0, \
Andy Rossf4b6daf2018-05-18 17:33:16 -0700167 .wait_q = _WAIT_Q_INIT(&name.wait_q), \
Punit Varaeb8ba692018-05-03 15:24:08 +0530168 .owner = NULL, \
Andy Ross53c85992017-07-24 14:59:55 -0700169 }
170
Punit Varaeb8ba692018-05-03 15:24:08 +0530171/*
172 * Mutex attributes - type
Andy Ross53c85992017-07-24 14:59:55 -0700173 *
Punit Varaeb8ba692018-05-03 15:24:08 +0530174 * PTHREAD_MUTEX_NORMAL: Owner of mutex cannot relock it. Attempting
175 * to relock will cause deadlock.
176 * PTHREAD_MUTEX_RECURSIVE: Owner can relock the mutex.
177 * PTHREAD_MUTEX_ERRORCHECK: If owner attempts to relock the mutex, an
178 * error is returned.
179 *
Andy Ross53c85992017-07-24 14:59:55 -0700180 */
Punit Varaeb8ba692018-05-03 15:24:08 +0530181#define PTHREAD_MUTEX_NORMAL 0
182#define PTHREAD_MUTEX_RECURSIVE 1
183#define PTHREAD_MUTEX_ERRORCHECK 2
184#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
Andy Ross53c85992017-07-24 14:59:55 -0700185
Punit Varaeb8ba692018-05-03 15:24:08 +0530186/*
187 * Mutex attributes - protocol
188 *
189 * PTHREAD_PRIO_NONE: Ownership of mutex does not affect priority.
190 * PTHREAD_PRIO_INHERIT: Owner's priority is boosted to the priority of
191 * highest priority thread blocked on the mutex.
192 * PTHREAD_PRIO_PROTECT: Mutex has a priority ceiling. The owner's
193 * priority is boosted to the highest priority ceiling of all mutexes
194 * owned (regardless of whether or not other threads are blocked on
195 * any of these mutexes).
196 * FIXME: Only PRIO_NONE is supported. Implement other protocols.
197 */
198#define PTHREAD_PRIO_NONE 0
Andy Ross53c85992017-07-24 14:59:55 -0700199
200/**
201 * @brief POSIX threading compatibility API
202 *
203 * See IEEE 1003.1
204 */
Punit Varaeb8ba692018-05-03 15:24:08 +0530205int pthread_mutex_destroy(pthread_mutex_t *m);
Andy Ross53c85992017-07-24 14:59:55 -0700206
207/**
208 * @brief POSIX threading compatibility API
209 *
210 * See IEEE 1003.1
211 */
Punit Varaeb8ba692018-05-03 15:24:08 +0530212int pthread_mutex_lock(pthread_mutex_t *m);
Andy Ross53c85992017-07-24 14:59:55 -0700213
214/**
215 * @brief POSIX threading compatibility API
216 *
217 * See IEEE 1003.1
218 */
Punit Varaeb8ba692018-05-03 15:24:08 +0530219int pthread_mutex_unlock(pthread_mutex_t *m);
Andy Ross53c85992017-07-24 14:59:55 -0700220
Punit Varaeb8ba692018-05-03 15:24:08 +0530221/**
222 * @brief POSIX threading compatibility API
223 *
224 * See IEEE 1003.1
225 */
226
227int pthread_mutex_timedlock(pthread_mutex_t *m,
228 const struct timespec *to);
Andy Ross53c85992017-07-24 14:59:55 -0700229
230/**
231 * @brief POSIX threading compatibility API
232 *
233 * See IEEE 1003.1
234 */
235int pthread_mutex_trylock(pthread_mutex_t *m);
236
237/**
238 * @brief POSIX threading compatibility API
239 *
240 * See IEEE 1003.1
241 */
Punit Varaeb8ba692018-05-03 15:24:08 +0530242int pthread_mutex_init(pthread_mutex_t *m,
243 const pthread_mutexattr_t *att);
244
245/**
246 * @brief POSIX threading compatibility API
247 *
248 * See IEEE 1003.1
249 */
250int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
251
252/**
253 * @brief POSIX threading compatibility API
254 *
255 * See IEEE 1003.1
256 */
257int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
258
259/**
260 * @brief POSIX threading compatibility API
261 *
262 * See IEEE 1003.1
263 */
264int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr,
265 int *protocol);
266
267/**
268 * @brief POSIX threading compatibility API
269 *
270 * See IEEE 1003.1
271 */
272int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
Andy Ross53c85992017-07-24 14:59:55 -0700273
274/**
275 * @brief POSIX threading compatibility API
276 *
277 * See IEEE 1003.1
278 *
279 * Note that pthread attribute structs are currently noops in Zephyr.
280 */
281static inline int pthread_mutexattr_init(pthread_mutexattr_t *m)
282{
283 ARG_UNUSED(m);
284
285 return 0;
286}
287
288/**
289 * @brief POSIX threading compatibility API
290 *
291 * See IEEE 1003.1
292 *
293 * Note that pthread attribute structs are currently noops in Zephyr.
294 */
295static inline int pthread_mutexattr_destroy(pthread_mutexattr_t *m)
296{
297 ARG_UNUSED(m);
298
299 return 0;
300}
301
302/* FIXME: these are going to be tricky to implement. Zephyr has (for
303 * good reason) deprecated its own "initializer" macros in favor of a
304 * static "declaration" macros instead. Using such a macro inside a
305 * gcc compound expression to declare and object then reference it
306 * would work, but gcc limits such expressions to function context
307 * (because they may need to generate code that runs at assignment
308 * time) and much real-world use of these initializers is for static
309 * variables. The best trick I can think of would be to declare it in
310 * a special section and then initialize that section at runtime
311 * startup, which sort of defeats the purpose of having these be
312 * static...
313 *
314 * Instead, see the nonstandard PTHREAD_*_DEFINE macros instead, which
315 * work similarly but conform to Zephyr's paradigms.
316 */
317/* #define PTHREAD_MUTEX_INITIALIZER */
318/* #define PTHREAD_COND_INITIALIZER */
319
Andy Ross53c85992017-07-24 14:59:55 -0700320/**
321 * @brief Declare a pthread barrier
322 *
323 * Declaration API for a pthread barrier. This is not a
324 * POSIX API, it's provided to better conform with Zephyr's allocation
325 * strategies for kernel objects.
326 *
327 * @param name Symbol name of the barrier
328 * @param count Thread count, same as the "count" argument to
329 * pthread_barrier_init()
330 */
331#define PTHREAD_BARRIER_DEFINE(name, count) \
332 struct pthread_barrier name = { \
Andy Rossccf3bf72018-05-10 11:10:34 -0700333 .wait_q = _WAIT_Q_INIT(&name.wait_q), \
Andy Ross53c85992017-07-24 14:59:55 -0700334 .max = count, \
335 }
336
337/**
338 * @brief POSIX threading compatibility API
339 *
340 * See IEEE 1003.1
341 */
342int pthread_barrier_wait(pthread_barrier_t *b);
343
344/**
345 * @brief POSIX threading compatibility API
346 *
347 * See IEEE 1003.1
348 */
349static inline int pthread_barrier_init(pthread_barrier_t *b,
350 const pthread_barrierattr_t *attr,
351 unsigned int count)
352{
353 ARG_UNUSED(attr);
354
355 b->max = count;
356 b->count = 0;
Andy Rossccf3bf72018-05-10 11:10:34 -0700357 _waitq_init(&b->wait_q);
Andy Ross53c85992017-07-24 14:59:55 -0700358
359 return 0;
360}
361
362/**
363 * @brief POSIX threading compatibility API
364 *
365 * See IEEE 1003.1
366 */
367static inline int pthread_barrier_destroy(pthread_barrier_t *b)
368{
369 ARG_UNUSED(b);
370
371 return 0;
372}
373
374/**
375 * @brief POSIX threading compatibility API
376 *
377 * See IEEE 1003.1
378 *
379 * Note that pthread attribute structs are currently noops in Zephyr.
380 */
381static inline int pthread_barrierattr_init(pthread_barrierattr_t *b)
382{
383 ARG_UNUSED(b);
384
385 return 0;
386}
387
388/**
389 * @brief POSIX threading compatibility API
390 *
391 * See IEEE 1003.1
392 *
393 * Note that pthread attribute structs are currently noops in Zephyr.
394 */
395static inline int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
396{
397 ARG_UNUSED(b);
398
399 return 0;
400}
401
402/* Predicates and setters for various pthread attribute values that we
403 * don't support (or always support: the "process shared" attribute
404 * can only be true given the way Zephyr implements these
405 * objects). Leave these undefined for simplicity instead of defining
406 * stubs to return an error that would have to be logged and
407 * interpreted just to figure out that we didn't support it in the
408 * first place. These APIs are very rarely used even in production
409 * Unix code. Leave the declarations here so they can be easily
410 * uncommented and implemented as needed.
411
412int pthread_condattr_getclock(const pthread_condattr_t * clockid_t *);
413int pthread_condattr_getpshared(const pthread_condattr_t * int *);
414int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
415int pthread_condattr_setpshared(pthread_condattr_t *, int);
416int pthread_mutex_consistent(pthread_mutex_t *);
417int pthread_mutex_getprioceiling(const pthread_mutex_t * int *);
418int pthread_mutex_setprioceiling(pthread_mutex_t *, int int *);
419int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *);
Andy Ross53c85992017-07-24 14:59:55 -0700420int pthread_mutexattr_getpshared(const pthread_mutexattr_t * int *);
421int pthread_mutexattr_getrobust(const pthread_mutexattr_t * int *);
Andy Ross53c85992017-07-24 14:59:55 -0700422int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
Andy Ross53c85992017-07-24 14:59:55 -0700423int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
424int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
Andy Ross53c85992017-07-24 14:59:55 -0700425int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *);
426int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
427*/
428
Youvedeep Singhc8aa6572017-12-28 14:31:57 +0530429/* Base Pthread related APIs */
430
431/**
432 * @brief Obtain ID of the calling thread.
433 *
434 * The results of calling this API from threads not created with
435 * pthread_create() are undefined.
436 *
437 * See IEEE 1003.1
438 */
439static inline pthread_t pthread_self(void)
440{
441 return (pthread_t)k_current_get();
442}
443
444
445/**
446 * @brief Compare thread IDs.
447 *
448 * See IEEE 1003.1
449 */
450static inline int pthread_equal(pthread_t pt1, pthread_t pt2)
451{
452 return (pt1 == pt2);
453}
454
Youvedeep Singh216883c2018-02-28 15:37:00 +0530455/**
456 * @brief Destroy the read-write lock attributes object.
457 *
458 * See IEEE 1003.1
459 */
460static inline int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
461{
462 return 0;
463}
464
465/**
466 * @brief initialize the read-write lock attributes object.
467 *
468 * See IEEE 1003.1
469 */
470static inline int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
471{
472 return 0;
473}
474
Youvedeep Singhc8aa6572017-12-28 14:31:57 +0530475int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
476int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
477int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
478int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
479int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
480int pthread_attr_init(pthread_attr_t *attr);
481int pthread_attr_destroy(pthread_attr_t *attr);
482int pthread_attr_getschedparam(const pthread_attr_t *attr,
483 struct sched_param *schedparam);
484int pthread_getschedparam(pthread_t pthread, int *policy,
485 struct sched_param *param);
486int pthread_attr_getstack(const pthread_attr_t *attr,
487 void **stackaddr, size_t *stacksize);
488int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
489 size_t stacksize);
Niranjhana N414c39f2018-04-11 16:00:09 +0530490int pthread_once(pthread_once_t *once, void (*initFunc)(void));
Youvedeep Singhc8aa6572017-12-28 14:31:57 +0530491void pthread_exit(void *retval);
492int pthread_join(pthread_t thread, void **status);
493int pthread_cancel(pthread_t pthread);
494int pthread_detach(pthread_t thread);
495int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
496 void *(*threadroutine)(void *), void *arg);
497int pthread_setcancelstate(int state, int *oldstate);
498int pthread_attr_setschedparam(pthread_attr_t *attr,
499 const struct sched_param *schedparam);
500int pthread_setschedparam(pthread_t pthread, int policy,
501 const struct sched_param *param);
Youvedeep Singh216883c2018-02-28 15:37:00 +0530502int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
503int pthread_rwlock_init(pthread_rwlock_t *rwlock,
504 const pthread_rwlockattr_t *attr);
505int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
506int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
507 const struct timespec *abstime);
508int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
509 const struct timespec *abstime);
510int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
511int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
512int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
513int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
Niranjhana N414c39f2018-04-11 16:00:09 +0530514int pthread_key_create(pthread_key_t *key,
515 void (*destructor)(void *));
516int pthread_key_delete(pthread_key_t key);
517int pthread_setspecific(pthread_key_t key, const void *value);
518void *pthread_getspecific(pthread_key_t key);
Youvedeep Singhc8aa6572017-12-28 14:31:57 +0530519
Flavio Ceolin67ca1762018-09-14 10:43:44 -0700520#endif /* ZEPHYR_INCLUDE_POSIX_PTHREAD_H_ */