Zephyr Project API  3.3.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 <zephyr/posix/sched.h>
16#include "pthread_key.h"
17#include <stdlib.h>
18#include <string.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/* Pthread detach/joinable */
25#define PTHREAD_CREATE_DETACHED 0
26#define PTHREAD_CREATE_JOINABLE 1
27
28/* Pthread cancellation */
29#define _PTHREAD_CANCEL_POS 0
30#define PTHREAD_CANCEL_ENABLE (0U << _PTHREAD_CANCEL_POS)
31#define PTHREAD_CANCEL_DISABLE BIT(_PTHREAD_CANCEL_POS)
32
33/* Passed to pthread_once */
34#define PTHREAD_ONCE_INIT \
35 { \
36 1, 0 \
37 }
38
39/* The minimum allowable stack size */
40#define PTHREAD_STACK_MIN Z_KERNEL_STACK_SIZE_ADJUST(0)
41
47#define PTHREAD_COND_INITIALIZER (-1)
48
59#define PTHREAD_COND_DEFINE(name) pthread_cond_t name = PTHREAD_COND_INITIALIZER
60
66int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *att);
67
73int pthread_cond_destroy(pthread_cond_t *cv);
74
80int pthread_cond_signal(pthread_cond_t *cv);
81
87int pthread_cond_broadcast(pthread_cond_t *cv);
88
94int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut);
95
101int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut,
102 const struct timespec *abstime);
103
111static inline int pthread_condattr_init(pthread_condattr_t *att)
112{
113 ARG_UNUSED(att);
114 return 0;
115}
116
124static inline int pthread_condattr_destroy(pthread_condattr_t *att)
125{
126 ARG_UNUSED(att);
127 return 0;
128}
129
135#define PTHREAD_MUTEX_INITIALIZER (-1)
136
147#define PTHREAD_MUTEX_DEFINE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
148
149/*
150 * Mutex attributes - type
151 *
152 * PTHREAD_MUTEX_NORMAL: Owner of mutex cannot relock it. Attempting
153 * to relock will cause deadlock.
154 * PTHREAD_MUTEX_RECURSIVE: Owner can relock the mutex.
155 * PTHREAD_MUTEX_ERRORCHECK: If owner attempts to relock the mutex, an
156 * error is returned.
157 *
158 */
159#define PTHREAD_MUTEX_NORMAL 0
160#define PTHREAD_MUTEX_RECURSIVE 1
161#define PTHREAD_MUTEX_ERRORCHECK 2
162#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
163
164/*
165 * Mutex attributes - protocol
166 *
167 * PTHREAD_PRIO_NONE: Ownership of mutex does not affect priority.
168 * PTHREAD_PRIO_INHERIT: Owner's priority is boosted to the priority of
169 * highest priority thread blocked on the mutex.
170 * PTHREAD_PRIO_PROTECT: Mutex has a priority ceiling. The owner's
171 * priority is boosted to the highest priority ceiling of all mutexes
172 * owned (regardless of whether or not other threads are blocked on
173 * any of these mutexes).
174 * FIXME: Only PRIO_NONE is supported. Implement other protocols.
175 */
176#define PTHREAD_PRIO_NONE 0
177
183int pthread_mutex_destroy(pthread_mutex_t *m);
184
190int pthread_mutex_lock(pthread_mutex_t *m);
191
197int pthread_mutex_unlock(pthread_mutex_t *m);
198
205int pthread_mutex_timedlock(pthread_mutex_t *m,
206 const struct timespec *abstime);
207
213int pthread_mutex_trylock(pthread_mutex_t *m);
214
220int pthread_mutex_init(pthread_mutex_t *m,
221 const pthread_mutexattr_t *att);
222
228int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
229
235int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
236
242int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr,
243 int *protocol);
244
250int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
251
259static inline int pthread_mutexattr_init(pthread_mutexattr_t *m)
260{
261 ARG_UNUSED(m);
262
263 return 0;
264}
265
273static inline int pthread_mutexattr_destroy(pthread_mutexattr_t *m)
274{
275 ARG_UNUSED(m);
276
277 return 0;
278}
279
280/* FIXME: these are going to be tricky to implement. Zephyr has (for
281 * good reason) deprecated its own "initializer" macros in favor of a
282 * static "declaration" macros instead. Using such a macro inside a
283 * gcc compound expression to declare and object then reference it
284 * would work, but gcc limits such expressions to function context
285 * (because they may need to generate code that runs at assignment
286 * time) and much real-world use of these initializers is for static
287 * variables. The best trick I can think of would be to declare it in
288 * a special section and then initialize that section at runtime
289 * startup, which sort of defeats the purpose of having these be
290 * static...
291 *
292 * Instead, see the nonstandard PTHREAD_*_DEFINE macros instead, which
293 * work similarly but conform to Zephyr's paradigms.
294 */
295/* #define PTHREAD_MUTEX_INITIALIZER */
296/* #define PTHREAD_COND_INITIALIZER */
297
309#define PTHREAD_BARRIER_DEFINE(name, count) \
310 struct pthread_barrier name = { \
311 .wait_q = Z_WAIT_Q_INIT(&name.wait_q), \
312 .max = count, \
313 }
314
315#define PTHREAD_BARRIER_SERIAL_THREAD 1
316
322int pthread_barrier_wait(pthread_barrier_t *b);
323
329static inline int pthread_barrier_init(pthread_barrier_t *b,
330 const pthread_barrierattr_t *attr,
331 unsigned int count)
332{
333 ARG_UNUSED(attr);
334
335 b->max = count;
336 b->count = 0;
337 z_waitq_init(&b->wait_q);
338
339 return 0;
340}
341
347static inline int pthread_barrier_destroy(pthread_barrier_t *b)
348{
349 ARG_UNUSED(b);
350
351 return 0;
352}
353
361static inline int pthread_barrierattr_init(pthread_barrierattr_t *b)
362{
363 ARG_UNUSED(b);
364
365 return 0;
366}
367
375static inline int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
376{
377 ARG_UNUSED(b);
378
379 return 0;
380}
381
382/* Predicates and setters for various pthread attribute values that we
383 * don't support (or always support: the "process shared" attribute
384 * can only be true given the way Zephyr implements these
385 * objects). Leave these undefined for simplicity instead of defining
386 * stubs to return an error that would have to be logged and
387 * interpreted just to figure out that we didn't support it in the
388 * first place. These APIs are very rarely used even in production
389 * Unix code. Leave the declarations here so they can be easily
390 * uncommented and implemented as needed.
391
392int pthread_condattr_getclock(const pthread_condattr_t * clockid_t *);
393int pthread_condattr_getpshared(const pthread_condattr_t * int *);
394int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
395int pthread_condattr_setpshared(pthread_condattr_t *, int);
396int pthread_mutex_consistent(pthread_mutex_t *);
397int pthread_mutex_getprioceiling(const pthread_mutex_t * int *);
398int pthread_mutex_setprioceiling(pthread_mutex_t *, int int *);
399int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *);
400int pthread_mutexattr_getpshared(const pthread_mutexattr_t * int *);
401int pthread_mutexattr_getrobust(const pthread_mutexattr_t * int *);
402int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
403int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
404int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
405int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *);
406int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
407*/
408
409/* Base Pthread related APIs */
410
419pthread_t pthread_self(void);
420
426static inline int pthread_equal(pthread_t pt1, pthread_t pt2)
427{
428 return (pt1 == pt2);
429}
430
436static inline int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
437{
438 ARG_UNUSED(attr);
439 return 0;
440}
441
447static inline int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
448{
449 ARG_UNUSED(attr);
450 return 0;
451}
452
453int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
454int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
455int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
456int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
457int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
458int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
459int pthread_attr_init(pthread_attr_t *attr);
460int pthread_attr_destroy(pthread_attr_t *attr);
461int pthread_attr_getschedparam(const pthread_attr_t *attr,
462 struct sched_param *schedparam);
463int pthread_getschedparam(pthread_t pthread, int *policy,
464 struct sched_param *param);
465int pthread_attr_getstack(const pthread_attr_t *attr,
466 void **stackaddr, size_t *stacksize);
467int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
468 size_t stacksize);
469int pthread_once(pthread_once_t *once, void (*initFunc)(void));
470void pthread_exit(void *retval);
471int pthread_join(pthread_t thread, void **status);
472int pthread_cancel(pthread_t pthread);
473int pthread_detach(pthread_t thread);
474int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
475 void *(*threadroutine)(void *), void *arg);
476int pthread_setcancelstate(int state, int *oldstate);
477int pthread_attr_setschedparam(pthread_attr_t *attr,
478 const struct sched_param *schedparam);
479int pthread_setschedparam(pthread_t pthread, int policy,
480 const struct sched_param *param);
481int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
482int pthread_rwlock_init(pthread_rwlock_t *rwlock,
483 const pthread_rwlockattr_t *attr);
484int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
485int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
486 const struct timespec *abstime);
487int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
488 const struct timespec *abstime);
489int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
490int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
491int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
492int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
493int pthread_key_create(pthread_key_t *key,
494 void (*destructor)(void *));
495int pthread_key_delete(pthread_key_t key);
496int pthread_setspecific(pthread_key_t key, const void *value);
497void *pthread_getspecific(pthread_key_t key);
498
499/* Glibc / Oracle Extension Functions */
500
515int pthread_setname_np(pthread_t thread, const char *name);
516
532int pthread_getname_np(pthread_t thread, char *name, size_t len);
533
534#ifdef __cplusplus
535}
536#endif
537
538#endif /* ZEPHYR_INCLUDE_POSIX_PTHREAD_H_ */
static struct k_thread thread[2]
Definition: atomic.c:26
ZTEST_BMEM int count
Definition: main.c:33
Public kernel APIs.
state
Definition: parser_state.h:29
static int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
POSIX threading compatibility API.
Definition: pthread.h:375
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)
int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *att)
POSIX threading compatibility API.
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:347
int pthread_attr_destroy(pthread_attr_t *attr)
pthread_t pthread_self(void)
Obtain ID of the calling thread.
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
static int pthread_mutexattr_init(pthread_mutexattr_t *m)
POSIX threading compatibility API.
Definition: pthread.h:259
static int pthread_condattr_destroy(pthread_condattr_t *att)
POSIX threading compatibility API.
Definition: pthread.h:124
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:273
static int pthread_barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *attr, unsigned int count)
POSIX threading compatibility API.
Definition: pthread.h:329
int pthread_cancel(pthread_t pthread)
int pthread_join(pthread_t thread, void **status)
static int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
initialize the read-write lock attributes object.
Definition: pthread.h:447
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:426
int pthread_cond_destroy(pthread_cond_t *cv)
POSIX threading compatibility API.
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:111
static int pthread_barrierattr_init(pthread_barrierattr_t *b)
POSIX threading compatibility API.
Definition: pthread.h:361
int pthread_setname_np(pthread_t thread, const char *name)
Set name of POSIX thread.
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:436
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 *))
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.
static k_spinlock_key_t key
Definition: spinlock_error_case.c:15
Definition: _timespec.h:22