Zephyr API Documentation  2.7.0-rc2
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 <kernel.h>
11#include <wait_q.h>
12#include <posix/time.h>
13#include <posix/unistd.h>
14#include "posix_types.h"
15#include "posix_sched.h"
16#include <posix/pthread_key.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
76#define PTHREAD_COND_DEFINE(name) \
77 struct pthread_cond name = { \
78 .wait_q = Z_WAIT_Q_INIT(&name.wait_q), \
79 }
80
86static inline int pthread_cond_init(pthread_cond_t *cv,
87 const pthread_condattr_t *att)
88{
89 ARG_UNUSED(att);
90 z_waitq_init(&cv->wait_q);
91 return 0;
92}
93
99static inline int pthread_cond_destroy(pthread_cond_t *cv)
100{
101 return 0;
102}
103
109int pthread_cond_signal(pthread_cond_t *cv);
110
116int pthread_cond_broadcast(pthread_cond_t *cv);
117
123int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut);
124
130int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut,
131 const struct timespec *abstime);
132
140static inline int pthread_condattr_init(pthread_condattr_t *att)
141{
142 return 0;
143}
144
152static inline int pthread_condattr_destroy(pthread_condattr_t *att)
153{
154 return 0;
155}
156
166#define PTHREAD_MUTEX_DEFINE(name) \
167 struct pthread_mutex name = \
168 { \
169 .lock_count = 0, \
170 .wait_q = Z_WAIT_Q_INIT(&name.wait_q), \
171 .owner = NULL, \
172 }
173
174/*
175 * Mutex attributes - type
176 *
177 * PTHREAD_MUTEX_NORMAL: Owner of mutex cannot relock it. Attempting
178 * to relock will cause deadlock.
179 * PTHREAD_MUTEX_RECURSIVE: Owner can relock the mutex.
180 * PTHREAD_MUTEX_ERRORCHECK: If owner attempts to relock the mutex, an
181 * error is returned.
182 *
183 */
184#define PTHREAD_MUTEX_NORMAL 0
185#define PTHREAD_MUTEX_RECURSIVE 1
186#define PTHREAD_MUTEX_ERRORCHECK 2
187#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
188
189/*
190 * Mutex attributes - protocol
191 *
192 * PTHREAD_PRIO_NONE: Ownership of mutex does not affect priority.
193 * PTHREAD_PRIO_INHERIT: Owner's priority is boosted to the priority of
194 * highest priority thread blocked on the mutex.
195 * PTHREAD_PRIO_PROTECT: Mutex has a priority ceiling. The owner's
196 * priority is boosted to the highest priority ceiling of all mutexes
197 * owned (regardless of whether or not other threads are blocked on
198 * any of these mutexes).
199 * FIXME: Only PRIO_NONE is supported. Implement other protocols.
200 */
201#define PTHREAD_PRIO_NONE 0
202
208int pthread_mutex_destroy(pthread_mutex_t *m);
209
215int pthread_mutex_lock(pthread_mutex_t *m);
216
222int pthread_mutex_unlock(pthread_mutex_t *m);
223
230int pthread_mutex_timedlock(pthread_mutex_t *m,
231 const struct timespec *abstime);
232
238int pthread_mutex_trylock(pthread_mutex_t *m);
239
245int pthread_mutex_init(pthread_mutex_t *m,
246 const pthread_mutexattr_t *att);
247
253int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
254
260int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
261
267int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr,
268 int *protocol);
269
275int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
276
284static inline int pthread_mutexattr_init(pthread_mutexattr_t *m)
285{
286 ARG_UNUSED(m);
287
288 return 0;
289}
290
298static inline int pthread_mutexattr_destroy(pthread_mutexattr_t *m)
299{
300 ARG_UNUSED(m);
301
302 return 0;
303}
304
305/* FIXME: these are going to be tricky to implement. Zephyr has (for
306 * good reason) deprecated its own "initializer" macros in favor of a
307 * static "declaration" macros instead. Using such a macro inside a
308 * gcc compound expression to declare and object then reference it
309 * would work, but gcc limits such expressions to function context
310 * (because they may need to generate code that runs at assignment
311 * time) and much real-world use of these initializers is for static
312 * variables. The best trick I can think of would be to declare it in
313 * a special section and then initialize that section at runtime
314 * startup, which sort of defeats the purpose of having these be
315 * static...
316 *
317 * Instead, see the nonstandard PTHREAD_*_DEFINE macros instead, which
318 * work similarly but conform to Zephyr's paradigms.
319 */
320/* #define PTHREAD_MUTEX_INITIALIZER */
321/* #define PTHREAD_COND_INITIALIZER */
322
334#define PTHREAD_BARRIER_DEFINE(name, count) \
335 struct pthread_barrier name = { \
336 .wait_q = Z_WAIT_Q_INIT(&name.wait_q), \
337 .max = count, \
338 }
339
340#define PTHREAD_BARRIER_SERIAL_THREAD 1
341
347int pthread_barrier_wait(pthread_barrier_t *b);
348
354static inline int pthread_barrier_init(pthread_barrier_t *b,
355 const pthread_barrierattr_t *attr,
356 unsigned int count)
357{
358 ARG_UNUSED(attr);
359
360 b->max = count;
361 b->count = 0;
362 z_waitq_init(&b->wait_q);
363
364 return 0;
365}
366
372static inline int pthread_barrier_destroy(pthread_barrier_t *b)
373{
374 ARG_UNUSED(b);
375
376 return 0;
377}
378
386static inline int pthread_barrierattr_init(pthread_barrierattr_t *b)
387{
388 ARG_UNUSED(b);
389
390 return 0;
391}
392
400static inline int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
401{
402 ARG_UNUSED(b);
403
404 return 0;
405}
406
407/* Predicates and setters for various pthread attribute values that we
408 * don't support (or always support: the "process shared" attribute
409 * can only be true given the way Zephyr implements these
410 * objects). Leave these undefined for simplicity instead of defining
411 * stubs to return an error that would have to be logged and
412 * interpreted just to figure out that we didn't support it in the
413 * first place. These APIs are very rarely used even in production
414 * Unix code. Leave the declarations here so they can be easily
415 * uncommented and implemented as needed.
416
417int pthread_condattr_getclock(const pthread_condattr_t * clockid_t *);
418int pthread_condattr_getpshared(const pthread_condattr_t * int *);
419int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
420int pthread_condattr_setpshared(pthread_condattr_t *, int);
421int pthread_mutex_consistent(pthread_mutex_t *);
422int pthread_mutex_getprioceiling(const pthread_mutex_t * int *);
423int pthread_mutex_setprioceiling(pthread_mutex_t *, int int *);
424int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *);
425int pthread_mutexattr_getpshared(const pthread_mutexattr_t * int *);
426int pthread_mutexattr_getrobust(const pthread_mutexattr_t * int *);
427int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
428int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
429int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
430int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *);
431int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
432*/
433
434/* Base Pthread related APIs */
435
444static inline pthread_t pthread_self(void)
445{
446 return (pthread_t)k_current_get();
447}
448
449
455static inline int pthread_equal(pthread_t pt1, pthread_t pt2)
456{
457 return (pt1 == pt2);
458}
459
465static inline int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
466{
467 return 0;
468}
469
475static inline int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
476{
477 return 0;
478}
479
480int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
481int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
482int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
483int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
484int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
485int pthread_attr_init(pthread_attr_t *attr);
486int pthread_attr_destroy(pthread_attr_t *attr);
487int pthread_attr_getschedparam(const pthread_attr_t *attr,
488 struct sched_param *schedparam);
489int pthread_getschedparam(pthread_t pthread, int *policy,
490 struct sched_param *param);
491int pthread_attr_getstack(const pthread_attr_t *attr,
492 void **stackaddr, size_t *stacksize);
493int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
494 size_t stacksize);
495int pthread_once(pthread_once_t *once, void (*initFunc)(void));
496void pthread_exit(void *retval);
497int pthread_join(pthread_t thread, void **status);
498int pthread_cancel(pthread_t pthread);
499int pthread_detach(pthread_t thread);
500int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
501 void *(*threadroutine)(void *), void *arg);
502int pthread_setcancelstate(int state, int *oldstate);
503int pthread_attr_setschedparam(pthread_attr_t *attr,
504 const struct sched_param *schedparam);
505int pthread_setschedparam(pthread_t pthread, int policy,
506 const struct sched_param *param);
507int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
508int pthread_rwlock_init(pthread_rwlock_t *rwlock,
509 const pthread_rwlockattr_t *attr);
510int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
511int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
512 const struct timespec *abstime);
513int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
514 const struct timespec *abstime);
515int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
516int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
517int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
518int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
519int pthread_key_create(pthread_key_t *key,
520 void (*destructor)(void *));
521int pthread_key_delete(pthread_key_t key);
522int pthread_setspecific(pthread_key_t key, const void *value);
523void *pthread_getspecific(pthread_key_t key);
524
525/* Glibc / Oracle Extension Functions */
526
541int pthread_setname_np(pthread_t thread, const char *name);
542
558int pthread_getname_np(pthread_t thread, char *name, size_t len);
559
560#ifdef __cplusplus
561}
562#endif
563
564#endif /* ZEPHYR_INCLUDE_POSIX_PTHREAD_H_ */
static struct k_thread thread[2]
Definition: atomic.c:22
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:521
state
Definition: http_parser_state.h:30
static int pthread_barrierattr_destroy(pthread_barrierattr_t *b)
POSIX threading compatibility API.
Definition: pthread.h:400
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:86
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:372
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:284
static int pthread_condattr_destroy(pthread_condattr_t *att)
POSIX threading compatibility API.
Definition: pthread.h:152
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:298
static int pthread_barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *attr, unsigned int count)
POSIX threading compatibility API.
Definition: pthread.h:354
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:99
static int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
initialize the read-write lock attributes object.
Definition: pthread.h:475
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_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:455
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:140
static int pthread_barrierattr_init(pthread_barrierattr_t *b)
POSIX threading compatibility API.
Definition: pthread.h:386
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:465
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:444
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:201
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