Zephyr Project API  3.1.0
A Scalable Open Source RTOS
pthread.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_POSIX_PTHREAD_H_
8#define ZEPHYR_INCLUDE_POSIX_PTHREAD_H_
9
10#include <zephyr/kernel.h>
11#include <zephyr/wait_q.h>
12#include <zephyr/posix/time.h>
13#include <zephyr/posix/unistd.h>
14#include "posix_types.h"
15#include "posix_sched.h"
17#include <stdlib.h>
18#include <string.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
25 /* The thread structure is unallocated and available for reuse. */
27 /* The thread is running and joinable. */
29 /* The thread is running and detached. */
31 /* A joinable thread exited and its return code is available. */
33};
34
37
38 /* List of keys that thread has called pthread_setspecific() on */
40
41 /* Exit status */
42 void *retval;
43
44 /* Pthread cancellation */
47 pthread_mutex_t cancel_lock;
48
49 /* Pthread State */
51 pthread_mutex_t state_lock;
52 pthread_cond_t state_cond;
53};
54
55/* Pthread detach/joinable */
56#define PTHREAD_CREATE_JOINABLE PTHREAD_JOINABLE
57#define PTHREAD_CREATE_DETACHED PTHREAD_DETACHED
58
59/* Pthread cancellation */
60#define _PTHREAD_CANCEL_POS 0
61#define PTHREAD_CANCEL_ENABLE (0U << _PTHREAD_CANCEL_POS)
62#define PTHREAD_CANCEL_DISABLE BIT(_PTHREAD_CANCEL_POS)
63
64/* Passed to pthread_once */
65#define PTHREAD_ONCE_INIT 1
66
67/* The minimum allowable stack size */
68#define PTHREAD_STACK_MIN Z_KERNEL_STACK_SIZE_ADJUST(0)
69
79#define PTHREAD_COND_DEFINE(name) \
80 struct pthread_cond name = { \
81 .wait_q = Z_WAIT_Q_INIT(&name.wait_q), \
82 }
83
89static inline int pthread_cond_init(pthread_cond_t *cv,
90 const pthread_condattr_t *att)
91{
92 ARG_UNUSED(att);
93 z_waitq_init(&cv->wait_q);
94 return 0;
95}
96
102static inline int pthread_cond_destroy(pthread_cond_t *cv)
103{
104 ARG_UNUSED(cv);
105 return 0;
106}
107
113int pthread_cond_signal(pthread_cond_t *cv);
114
120int pthread_cond_broadcast(pthread_cond_t *cv);
121
127int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut);
128
134int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut,
135 const struct timespec *abstime);
136
144static inline int pthread_condattr_init(pthread_condattr_t *att)
145{
146 ARG_UNUSED(att);
147 return 0;
148}
149
157static inline int pthread_condattr_destroy(pthread_condattr_t *att)
158{
159 ARG_UNUSED(att);
160 return 0;
161}
162
172#define PTHREAD_MUTEX_DEFINE(name) \
173 struct pthread_mutex name = \
174 { \
175 .lock_count = 0, \
176 .wait_q = Z_WAIT_Q_INIT(&name.wait_q), \
177 .owner = NULL, \
178 }
179
180/*
181 * Mutex attributes - type
182 *
183 * PTHREAD_MUTEX_NORMAL: Owner of mutex cannot relock it. Attempting
184 * to relock will cause deadlock.
185 * PTHREAD_MUTEX_RECURSIVE: Owner can relock the mutex.
186 * PTHREAD_MUTEX_ERRORCHECK: If owner attempts to relock the mutex, an
187 * error is returned.
188 *
189 */
190#define PTHREAD_MUTEX_NORMAL 0
191#define PTHREAD_MUTEX_RECURSIVE 1
192#define PTHREAD_MUTEX_ERRORCHECK 2
193#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
194
195/*
196 * Mutex attributes - protocol
197 *
198 * PTHREAD_PRIO_NONE: Ownership of mutex does not affect priority.
199 * PTHREAD_PRIO_INHERIT: Owner's priority is boosted to the priority of
200 * highest priority thread blocked on the mutex.
201 * PTHREAD_PRIO_PROTECT: Mutex has a priority ceiling. The owner's
202 * priority is boosted to the highest priority ceiling of all mutexes
203 * owned (regardless of whether or not other threads are blocked on
204 * any of these mutexes).
205 * FIXME: Only PRIO_NONE is supported. Implement other protocols.
206 */
207#define PTHREAD_PRIO_NONE 0
208
214int pthread_mutex_destroy(pthread_mutex_t *m);
215
221int pthread_mutex_lock(pthread_mutex_t *m);
222
228int pthread_mutex_unlock(pthread_mutex_t *m);
229
236int pthread_mutex_timedlock(pthread_mutex_t *m,
237 const struct timespec *abstime);
238
244int pthread_mutex_trylock(pthread_mutex_t *m);
245
251int pthread_mutex_init(pthread_mutex_t *m,
252 const pthread_mutexattr_t *att);
253
259int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
260
266int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
267
273int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr,
274 int *protocol);
275
281int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
282
290static inline int pthread_mutexattr_init(pthread_mutexattr_t *m)
291{
292 ARG_UNUSED(m);
293
294 return 0;
295}
296
304static inline int pthread_mutexattr_destroy(pthread_mutexattr_t *m)
305{
306 ARG_UNUSED(m);
307
308 return 0;
309}
310
311/* FIXME: these are going to be tricky to implement. Zephyr has (for
312 * good reason) deprecated its own "initializer" macros in favor of a
313 * static "declaration" macros instead. Using such a macro inside a
314 * gcc compound expression to declare and object then reference it
315 * would work, but gcc limits such expressions to function context
316 * (because they may need to generate code that runs at assignment
317 * time) and much real-world use of these initializers is for static
318 * variables. The best trick I can think of would be to declare it in
319 * a special section and then initialize that section at runtime
320 * startup, which sort of defeats the purpose of having these be
321 * static...
322 *
323 * Instead, see the nonstandard PTHREAD_*_DEFINE macros instead, which
324 * work similarly but conform to Zephyr's paradigms.
325 */
326/* #define PTHREAD_MUTEX_INITIALIZER */
327/* #define PTHREAD_COND_INITIALIZER */
328
340#define PTHREAD_BARRIER_DEFINE(name, count) \
341 struct pthread_barrier name = { \
342 .wait_q = Z_WAIT_Q_INIT(&name.wait_q), \
343 .max = count, \
344 }
345
346#define PTHREAD_BARRIER_SERIAL_THREAD 1
347
353int pthread_barrier_wait(pthread_barrier_t *b);
354
360static inline int pthread_barrier_init(pthread_barrier_t *b,
361 const pthread_barrierattr_t *attr,
362 unsigned int count)
363{
364 ARG_UNUSED(attr);
365
366 b->max = count;
367 b->count = 0;
368 z_waitq_init(&b->wait_q);
369
370 return 0;
371}
372
378static inline int pthread_barrier_destroy(pthread_barrier_t *b)
379{
380 ARG_UNUSED(b);
381
382 return 0;
383}
384
392static inline int pthread_barrierattr_init(pthread_barrierattr_t *b)
393{
394 ARG_UNUSED(b);
395
396 return 0;
397}
398
406static inline int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
407{
408 ARG_UNUSED(b);
409
410 return 0;
411}
412
413/* Predicates and setters for various pthread attribute values that we
414 * don't support (or always support: the "process shared" attribute
415 * can only be true given the way Zephyr implements these
416 * objects). Leave these undefined for simplicity instead of defining
417 * stubs to return an error that would have to be logged and
418 * interpreted just to figure out that we didn't support it in the
419 * first place. These APIs are very rarely used even in production
420 * Unix code. Leave the declarations here so they can be easily
421 * uncommented and implemented as needed.
422
423int pthread_condattr_getclock(const pthread_condattr_t * clockid_t *);
424int pthread_condattr_getpshared(const pthread_condattr_t * int *);
425int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
426int pthread_condattr_setpshared(pthread_condattr_t *, int);
427int pthread_mutex_consistent(pthread_mutex_t *);
428int pthread_mutex_getprioceiling(const pthread_mutex_t * int *);
429int pthread_mutex_setprioceiling(pthread_mutex_t *, int int *);
430int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *);
431int pthread_mutexattr_getpshared(const pthread_mutexattr_t * int *);
432int pthread_mutexattr_getrobust(const pthread_mutexattr_t * int *);
433int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
434int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
435int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
436int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *);
437int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
438*/
439
440/* Base Pthread related APIs */
441
450static inline pthread_t pthread_self(void)
451{
452 return (pthread_t)k_current_get();
453}
454
455
461static inline int pthread_equal(pthread_t pt1, pthread_t pt2)
462{
463 return (pt1 == pt2);
464}
465
471static inline int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
472{
473 ARG_UNUSED(attr);
474 return 0;
475}
476
482static inline int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
483{
484 ARG_UNUSED(attr);
485 return 0;
486}
487
488int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
489int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
490int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
491int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
492int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
493int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
494int pthread_attr_init(pthread_attr_t *attr);
495int pthread_attr_destroy(pthread_attr_t *attr);
496int pthread_attr_getschedparam(const pthread_attr_t *attr,
497 struct sched_param *schedparam);
498int pthread_getschedparam(pthread_t pthread, int *policy,
499 struct sched_param *param);
500int pthread_attr_getstack(const pthread_attr_t *attr,
501 void **stackaddr, size_t *stacksize);
502int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
503 size_t stacksize);
504int pthread_once(pthread_once_t *once, void (*initFunc)(void));
505void pthread_exit(void *retval);
506int pthread_join(pthread_t thread, void **status);
507int pthread_cancel(pthread_t pthread);
508int pthread_detach(pthread_t thread);
509int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
510 void *(*threadroutine)(void *), void *arg);
511int pthread_setcancelstate(int state, int *oldstate);
512int pthread_attr_setschedparam(pthread_attr_t *attr,
513 const struct sched_param *schedparam);
514int pthread_setschedparam(pthread_t pthread, int policy,
515 const struct sched_param *param);
516int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
517int pthread_rwlock_init(pthread_rwlock_t *rwlock,
518 const pthread_rwlockattr_t *attr);
519int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
520int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
521 const struct timespec *abstime);
522int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
523 const struct timespec *abstime);
524int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
525int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
526int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
527int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
528int pthread_key_create(pthread_key_t *key,
529 void (*destructor)(void *));
530int pthread_key_delete(pthread_key_t key);
531int pthread_setspecific(pthread_key_t key, const void *value);
532void *pthread_getspecific(pthread_key_t key);
533
534/* Glibc / Oracle Extension Functions */
535
550int pthread_setname_np(pthread_t thread, const char *name);
551
567int pthread_getname_np(pthread_t thread, char *name, size_t len);
568
569#ifdef __cplusplus
570}
571#endif
572
573#endif /* ZEPHYR_INCLUDE_POSIX_PTHREAD_H_ */
static struct k_thread thread[2]
Definition: atomic.c:26
ZTEST_BMEM int count
Definition: main.c:33
static __attribute_const__ k_tid_t k_current_get(void)
Get thread ID of the current thread.
Definition: kernel.h:522
state
Definition: http_parser_state.h:29
Public kernel APIs.
static int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
POSIX threading compatibility API.
Definition: pthread.h:406
int pthread_mutex_unlock(pthread_mutex_t *m)
POSIX threading compatibility API.
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
int pthread_attr_init(pthread_attr_t *attr)
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *schedparam)
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
POSIX threading compatibility API.
int pthread_setspecific(pthread_key_t key, const void *value)
void pthread_exit(void *retval)
static int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *att)
POSIX threading compatibility API.
Definition: pthread.h:89
int pthread_setcancelstate(int state, int *oldstate)
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
static int pthread_barrier_destroy(pthread_barrier_t *b)
POSIX threading compatibility API.
Definition: pthread.h:378
int pthread_attr_destroy(pthread_attr_t *attr)
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
static int pthread_mutexattr_init(pthread_mutexattr_t *m)
POSIX threading compatibility API.
Definition: pthread.h:290
static int pthread_condattr_destroy(pthread_condattr_t *att)
POSIX threading compatibility API.
Definition: pthread.h:157
int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
POSIX threading compatibility API.
static int pthread_mutexattr_destroy(pthread_mutexattr_t *m)
POSIX threading compatibility API.
Definition: pthread.h:304
static int pthread_barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *attr, unsigned int count)
POSIX threading compatibility API.
Definition: pthread.h:360
int pthread_cancel(pthread_t pthread)
int pthread_join(pthread_t thread, void **status)
static int pthread_cond_destroy(pthread_cond_t *cv)
POSIX threading compatibility API.
Definition: pthread.h:102
static int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
initialize the read-write lock attributes object.
Definition: pthread.h:482
int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *att)
POSIX threading compatibility API.
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
int pthread_detach(pthread_t thread)
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
POSIX threading compatibility API.
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
POSIX threading compatibility API.
static int pthread_equal(pthread_t pt1, pthread_t pt2)
Compare thread IDs.
Definition: pthread.h:461
int pthread_getname_np(pthread_t thread, char *name, size_t len)
Get name of POSIX thread and store in name buffer that is of size len.
int pthread_cond_broadcast(pthread_cond_t *cv)
POSIX threading compatibility API.
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
static int pthread_condattr_init(pthread_condattr_t *att)
POSIX threading compatibility API.
Definition: pthread.h:144
static int pthread_barrierattr_init(pthread_barrierattr_t *b)
POSIX threading compatibility API.
Definition: pthread.h:392
int pthread_setname_np(pthread_t thread, const char *name)
Set name of POSIX thread.
pthread_state
Definition: pthread.h:24
@ PTHREAD_DETACHED
Definition: pthread.h:30
@ PTHREAD_JOINABLE
Definition: pthread.h:28
@ PTHREAD_EXITED
Definition: pthread.h:32
@ PTHREAD_TERMINATED
Definition: pthread.h:26
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime)
POSIX threading compatibility API.
int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *schedparam)
static int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
Destroy the read-write lock attributes object.
Definition: pthread.h:471
int pthread_getschedparam(pthread_t pthread, int *policy, struct sched_param *param)
int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*threadroutine)(void *), void *arg)
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
int pthread_mutex_trylock(pthread_mutex_t *m)
POSIX threading compatibility API.
void * pthread_getspecific(pthread_key_t key)
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
int pthread_setschedparam(pthread_t pthread, int policy, const struct sched_param *param)
int pthread_cond_signal(pthread_cond_t *cv)
POSIX threading compatibility API.
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
POSIX threading compatibility API.
int pthread_once(pthread_once_t *once, void(*initFunc)(void))
int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize)
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime)
POSIX threading compatibility API.
int pthread_key_delete(pthread_key_t key)
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
int pthread_key_create(pthread_key_t *key, void(*destructor)(void *))
static pthread_t pthread_self(void)
Obtain ID of the calling thread.
Definition: pthread.h:450
int pthread_barrier_wait(pthread_barrier_t *b)
POSIX threading compatibility API.
int pthread_mutex_destroy(pthread_mutex_t *m)
POSIX threading compatibility API.
int pthread_mutex_lock(pthread_mutex_t *m)
POSIX threading compatibility API.
struct _slist sys_slist_t
Definition: slist.h:40
static k_spinlock_key_t key
Definition: spinlock_error_case.c:14
Definition: thread.h:231
Definition: pthread.h:35
int cancel_state
Definition: pthread.h:45
struct k_thread thread
Definition: pthread.h:36
int cancel_pending
Definition: pthread.h:46
void * retval
Definition: pthread.h:42
pthread_mutex_t state_lock
Definition: pthread.h:51
pthread_mutex_t cancel_lock
Definition: pthread.h:47
sys_slist_t key_list
Definition: pthread.h:39
pthread_cond_t state_cond
Definition: pthread.h:52
enum pthread_state state
Definition: pthread.h:50
Definition: posix_sched.h:23
Definition: _timespec.h:22