Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "pthread_key.h"
11
12#include <stdlib.h>
13#include <string.h>
14
15#include <zephyr/kernel.h>
16#include <zephyr/posix/time.h>
17#include <zephyr/posix/unistd.h>
18#include <zephyr/posix/sched.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 resource visibility */
29#define PTHREAD_PROCESS_PRIVATE 0
30#define PTHREAD_PROCESS_SHARED 1
31
32/* Pthread cancellation */
33#define _PTHREAD_CANCEL_POS 0
34#define PTHREAD_CANCEL_ENABLE (0U << _PTHREAD_CANCEL_POS)
35#define PTHREAD_CANCEL_DISABLE BIT(_PTHREAD_CANCEL_POS)
36
37/* Passed to pthread_once */
38#define PTHREAD_ONCE_INIT \
39 { \
40 1, 0 \
41 }
42
43/* The minimum allowable stack size */
44#define PTHREAD_STACK_MIN Z_KERNEL_STACK_SIZE_ADJUST(0)
45
51#define PTHREAD_COND_INITIALIZER (-1)
52
63#define PTHREAD_COND_DEFINE(name) pthread_cond_t name = PTHREAD_COND_INITIALIZER
64
70int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *att);
71
78
85
92
99
106 const struct timespec *abstime);
107
114int pthread_condattr_init(pthread_condattr_t *att);
115
122int pthread_condattr_destroy(pthread_condattr_t *att);
123
130int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
131 clockid_t *ZRESTRICT clock_id);
132
140int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id);
141
147#define PTHREAD_MUTEX_INITIALIZER (-1)
148
159#define PTHREAD_MUTEX_DEFINE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
160
161/*
162 * Mutex attributes - type
163 *
164 * PTHREAD_MUTEX_NORMAL: Owner of mutex cannot relock it. Attempting
165 * to relock will cause deadlock.
166 * PTHREAD_MUTEX_RECURSIVE: Owner can relock the mutex.
167 * PTHREAD_MUTEX_ERRORCHECK: If owner attempts to relock the mutex, an
168 * error is returned.
169 *
170 */
171#define PTHREAD_MUTEX_NORMAL 0
172#define PTHREAD_MUTEX_RECURSIVE 1
173#define PTHREAD_MUTEX_ERRORCHECK 2
174#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
175
176/*
177 * Mutex attributes - protocol
178 *
179 * PTHREAD_PRIO_NONE: Ownership of mutex does not affect priority.
180 * PTHREAD_PRIO_INHERIT: Owner's priority is boosted to the priority of
181 * highest priority thread blocked on the mutex.
182 * PTHREAD_PRIO_PROTECT: Mutex has a priority ceiling. The owner's
183 * priority is boosted to the highest priority ceiling of all mutexes
184 * owned (regardless of whether or not other threads are blocked on
185 * any of these mutexes).
186 * FIXME: Only PRIO_NONE is supported. Implement other protocols.
187 */
188#define PTHREAD_PRIO_NONE 0
189
196
203
210
218 const struct timespec *abstime);
219
226
233 const pthread_mutexattr_t *att);
234
240int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
241
247int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
248
254int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr,
255 int *protocol);
256
262int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
263
271static inline int pthread_mutexattr_init(pthread_mutexattr_t *m)
272{
273 ARG_UNUSED(m);
274
275 return 0;
276}
277
285static inline int pthread_mutexattr_destroy(pthread_mutexattr_t *m)
286{
287 ARG_UNUSED(m);
288
289 return 0;
290}
291
304#define PTHREAD_BARRIER_DEFINE(name, count) pthread_barrier_t name = -1 __DEPRECATED_MACRO
305
306#define PTHREAD_BARRIER_SERIAL_THREAD 1
307
308/*
309 * Barrier attributes - type
310 */
311#define PTHREAD_PROCESS_PRIVATE 0
312#define PTHREAD_PROCESS_PUBLIC 1
313
320
327 unsigned int count);
328
335
342
349
356
363 int *ZRESTRICT pshared);
364
365/* Predicates and setters for various pthread attribute values that we
366 * don't support (or always support: the "process shared" attribute
367 * can only be true given the way Zephyr implements these
368 * objects). Leave these undefined for simplicity instead of defining
369 * stubs to return an error that would have to be logged and
370 * interpreted just to figure out that we didn't support it in the
371 * first place. These APIs are very rarely used even in production
372 * Unix code. Leave the declarations here so they can be easily
373 * uncommented and implemented as needed.
374
375int pthread_condattr_getpshared(const pthread_condattr_t * int *);
376int pthread_condattr_setpshared(pthread_condattr_t *, int);
377int pthread_mutex_consistent(pthread_mutex_t *);
378int pthread_mutex_getprioceiling(const pthread_mutex_t * int *);
379int pthread_mutex_setprioceiling(pthread_mutex_t *, int int *);
380int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *);
381int pthread_mutexattr_getpshared(const pthread_mutexattr_t * int *);
382int pthread_mutexattr_getrobust(const pthread_mutexattr_t * int *);
383int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
384int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
385int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
386*/
387
388/* Base Pthread related APIs */
389
399
406
413{
414 ARG_UNUSED(attr);
415 return 0;
416}
417
424{
425 ARG_UNUSED(attr);
426 return 0;
427}
428
429int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
430int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
431int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
432int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
433int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
434int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
435int pthread_attr_init(pthread_attr_t *attr);
436int pthread_attr_destroy(pthread_attr_t *attr);
437int pthread_attr_getschedparam(const pthread_attr_t *attr,
438 struct sched_param *schedparam);
439int pthread_getschedparam(pthread_t pthread, int *policy,
440 struct sched_param *param);
441int pthread_attr_getstack(const pthread_attr_t *attr,
442 void **stackaddr, size_t *stacksize);
443int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
444 size_t stacksize);
445int pthread_once(pthread_once_t *once, void (*initFunc)(void));
446void pthread_exit(void *retval);
447int pthread_join(pthread_t thread, void **status);
450int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
451 void *(*threadroutine)(void *), void *arg);
452int pthread_setcancelstate(int state, int *oldstate);
453int pthread_attr_setschedparam(pthread_attr_t *attr,
454 const struct sched_param *schedparam);
455int pthread_setschedparam(pthread_t pthread, int policy,
456 const struct sched_param *param);
459 const pthread_rwlockattr_t *attr);
462 const struct timespec *abstime);
464 const struct timespec *abstime);
469int pthread_key_create(pthread_key_t *key,
470 void (*destructor)(void *));
471int pthread_key_delete(pthread_key_t key);
472int pthread_setspecific(pthread_key_t key, const void *value);
473void *pthread_getspecific(pthread_key_t key);
474
475/* Glibc / Oracle Extension Functions */
476
491int pthread_setname_np(pthread_t thread, const char *name);
492
508int pthread_getname_np(pthread_t thread, char *name, size_t len);
509
510#ifdef CONFIG_PTHREAD_IPC
511
517int pthread_spin_destroy(pthread_spinlock_t *lock);
518
524int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
525
531int pthread_spin_lock(pthread_spinlock_t *lock);
532
538int pthread_spin_trylock(pthread_spinlock_t *lock);
539
545int pthread_spin_unlock(pthread_spinlock_t *lock);
546
547#endif
548
549#ifdef __cplusplus
550}
551#endif
552
553#endif /* ZEPHYR_INCLUDE_POSIX_PTHREAD_H_ */
#define ZRESTRICT
Definition: common.h:36
Public kernel APIs.
state
Definition: parser_state.h:29
uint32_t pthread_cond_t
Definition: posix_types.h:75
uint32_t pthread_spinlock_t
Definition: posix_types.h:57
uint32_t pthread_t
Definition: posix_types.h:56
uint32_t pthread_mutex_t
Definition: posix_types.h:63
uint32_t pthread_rwlockattr_t
Definition: posix_types.h:94
uint32_t clockid_t
Definition: posix_types.h:31
uint32_t pthread_barrier_t
Definition: posix_types.h:88
int pthread_condattr_init(pthread_condattr_t *att)
POSIX threading compatibility API.
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_equal(pthread_t pt1, pthread_t pt2)
Compare thread IDs.
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)
int pthread_attr_destroy(pthread_attr_t *attr)
pthread_t pthread_self(void)
Obtain ID of the calling thread.
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *ZRESTRICT attr, int *ZRESTRICT pshared)
POSIX threading compatibility API.
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
POSIX threading compatibility API.
static int pthread_mutexattr_init(pthread_mutexattr_t *m)
POSIX threading compatibility API.
Definition: pthread.h:271
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:285
int pthread_cancel(pthread_t pthread)
int pthread_join(pthread_t thread, void **status)
int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att, clockid_t *ZRESTRICT clock_id)
POSIX threading comatibility API.
static int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
initialize the read-write lock attributes object.
Definition: pthread.h:423
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.
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)
int pthread_setname_np(pthread_t thread, const char *name)
Set name of POSIX thread.
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
POSIX threading compatibility API.
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
int pthread_barrier_destroy(pthread_barrier_t *b)
POSIX threading compatibility API.
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)
int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
POSIX threading compatibility API.
static int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
Destroy the read-write lock attributes object.
Definition: pthread.h:412
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_barrierattr_init(pthread_barrierattr_t *b)
POSIX threading compatibility API.
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_barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *attr, unsigned int count)
POSIX threading compatibility API.
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_condattr_destroy(pthread_condattr_t *att)
POSIX threading compatibility API.
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.
Definition: posix_types.h:90
Definition: posix_types.h:96
Definition: _timespec.h:22