10#ifndef STK_C_PTHREAD_H_
11#define STK_C_PTHREAD_H_
19#if defined(_SYS__PTHREADTYPES_H_) && !defined(STK_C_PTHREADTYPES_GUARD)
20 #error "The toolchain's pthread types are already defined: include the POSIX layer's headers (pthread.h, time.h, signal.h, ...) before any toolchain header, and put the layer's include directory ahead of the toolchain's"
22#ifndef _SYS__PTHREADTYPES_H_
23 #define _SYS__PTHREADTYPES_H_
24 #define STK_C_PTHREADTYPES_GUARD
40#include <stk_config.h>
45#ifndef STK_SYNC_DEBUG_NAMES
46 #if STK_SEGGER_SYSVIEW
47 #define STK_SYNC_DEBUG_NAMES (1)
49 #define STK_SYNC_DEBUG_NAMES (0)
251#if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
252 #define __stk_pthread_aligned __attribute__((aligned(sizeof(pthread_stk_word_t))))
254 #define __stk_pthread_aligned
260#define STK_C_PTHREAD_MUTEX_WORDS (10U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
261#define STK_C_PTHREAD_CV_WORDS (7U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
262#define STK_C_PTHREAD_RWMUTEX_WORDS (17U + (STK_SYNC_DEBUG_NAMES ? 3U : 0U))
263#define STK_C_PTHREAD_BARRIER_WORDS (STK_C_PTHREAD_MUTEX_WORDS + STK_C_PTHREAD_CV_WORDS + 4U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
273#define STK_C_PTHREAD_VERSION (0x20261004)
286#ifndef STK_C_PTHREAD_MAX_THREADS
287 #define STK_C_PTHREAD_MAX_THREADS (8U)
298#ifndef STK_C_PTHREAD_DEFAULT_STACK_WORDS
299 #define STK_C_PTHREAD_DEFAULT_STACK_WORDS (1024U)
306#ifndef STK_C_PTHREAD_REAPER_STACK_WORDS
307 #define STK_C_PTHREAD_REAPER_STACK_WORDS (256U)
314#ifndef STK_C_PTHREAD_KEYS_MAX
315 #define STK_C_PTHREAD_KEYS_MAX (8U)
351#ifndef STK_C_PTHREAD_NAME_MAX
352 #define STK_C_PTHREAD_NAME_MAX (16U)
361#ifndef STK_C_PTHREAD_RUNTIME_PRIORITY
362 #define STK_C_PTHREAD_RUNTIME_PRIORITY (1)
365#ifndef CLOCK_REALTIME
366 #define CLOCK_REALTIME (0)
368#ifndef CLOCK_MONOTONIC
369 #define CLOCK_MONOTONIC (1)
375#define PTHREAD_CANCEL_ENABLE (0)
376#define PTHREAD_CANCEL_DISABLE (1)
377#define PTHREAD_CANCEL_DEFERRED (0)
378#define PTHREAD_CANCEL_ASYNCHRONOUS (1)
383#define PTHREAD_CANCELED ((void *)(intptr_t)-1)
388#ifndef STK_C_PTHREAD_CLEANUP_MAX
389 #define STK_C_PTHREAD_CLEANUP_MAX (8U)
395#define PTHREAD_INHERIT_SCHED (0)
396#define PTHREAD_EXPLICIT_SCHED (1)
401#define PTHREAD_SCOPE_SYSTEM (0)
402#define PTHREAD_SCOPE_PROCESS (1)
407#define PTHREAD_PRIO_NONE (0)
408#define PTHREAD_PRIO_INHERIT (1)
409#define PTHREAD_PRIO_PROTECT (2)
428#define PTHREAD_CREATE_JOINABLE (0)
429#define PTHREAD_CREATE_DETACHED (1)
435#define PTHREAD_CREATE_USER_NP (0)
436#define PTHREAD_CREATE_PRIVILEGED_NP (1)
456#define PTHREAD_MUTEX_NORMAL (0)
457#define PTHREAD_MUTEX_DEFAULT (PTHREAD_MUTEX_NORMAL)
458#define PTHREAD_MUTEX_ERRORCHECK (1)
459#define PTHREAD_MUTEX_RECURSIVE (2)
467 #define STK_C_PTHREAD_ZERO_INIT {}
469 #define STK_C_PTHREAD_ZERO_INIT {0}
484#define PTHREAD_MUTEX_INITIALIZER STK_C_PTHREAD_ZERO_INIT
504#define PTHREAD_COND_INITIALIZER STK_C_PTHREAD_ZERO_INIT
530#define PTHREAD_RWLOCK_INITIALIZER STK_C_PTHREAD_ZERO_INIT
532#define PTHREAD_PROCESS_PRIVATE (0)
533#define PTHREAD_PROCESS_SHARED (1)
572#define PTHREAD_BARRIER_SERIAL_THREAD (-1)
587#define PTHREAD_ONCE_INIT STK_C_PTHREAD_ZERO_INIT
598#define PTHREAD_KEYS_MAX (STK_C_PTHREAD_KEYS_MAX)
606#define PTHREAD_DESTRUCTOR_ITERATIONS (4)
706 void *(*start_routine)(
void *),
void *arg);
927 const struct timespec *abstime);
1096 unsigned int count);
1350 const struct timespec *abstime);
1441int pthread_atfork(
void (*prepare)(
void),
void (*parent)(
void),
void (*child)(
void));
POSIX-named scheduling API (<sched.h>) for STK: sched_yield(), sched_get_priority_min/max() and struc...
POSIX time extensions for STK: clock_gettime(), clock_getres(), nanosleep(), clock_nanosleep(),...
#define STK_C_PTHREAD_RWMUTEX_WORDS
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
Try to acquire the write lock without blocking.
int pthread_condattr_destroy(pthread_condattr_t *attr)
Destroy a condition variable attributes object (no-op).
int pthread_attr_init(pthread_attr_t *attr)
Initialize a thread attributes object with default values (default stack size, no external stack,...
#define STK_C_PTHREAD_CV_WORDS
int pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)
Get the clock stored in attr.
int pthread_mutex_lock(pthread_mutex_t *mutex)
Lock a mutex, blocking until it becomes available.
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
Change the policy/priority of a running thread.
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
Initialize a read-write lock.
int pthread_tryjoin_np(pthread_t thread, void **retval)
Try to join a thread without blocking (GNU extension).
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
Acquire the write lock with an absolute deadline.
#define STK_C_PTHREAD_MUTEX_WORDS
Sizes (in pthread_stk_word_t units) of the opaque storage for the C++ sync objects.
int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
Call init_routine exactly once for a given once_control, no matter how many threads call pthread_once...
int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clock_id, const struct timespec *abstime)
pthread_cond_timedwait() with an explicit clock. See pthread_mutex_clocklock() for clock_id.
int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
#define STK_C_PTHREAD_NAME_MAX
Capacity of a thread name buffer including the terminating NUL (like Linux, 16).
int pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clock_id, const struct timespec *abstime)
pthread_rwlock_timedwrlock() with an explicit clock. See pthread_mutex_clocklock() for clock_id.
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
Get the mutex protocol stored in attr.
int pthread_cond_signal(pthread_cond_t *cond)
Wake one thread waiting on cond (no effect if none is waiting).
int pthread_setspecific(pthread_key_t key, const void *value)
Set the calling thread's value for key.
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
Destroy a mutex attributes object (no-op; no owned resources).
void pthread_exit(void *retval)
Terminate the calling thread.
int pthread_mutex_trylock(pthread_mutex_t *mutex)
Try to lock a mutex without blocking.
int pthread_join(pthread_t thread, void **retval)
Block until the given joinable thread finishes, then reclaim its resources.
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
Set the contention scope. Only PTHREAD_SCOPE_SYSTEM is supported.
int pthread_condattr_init(pthread_condattr_t *attr)
Initialize a condition variable attributes object (no settable properties).
int pthread_setcancelstate(int state, int *oldstate)
Enable or disable cancellation of the calling thread.
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
Get the current detach-state setting.
int pthread_spin_unlock(pthread_spinlock_t *lock)
Release a spinlock held by the calling thread.
int pthread_mutex_destroy(pthread_mutex_t *mutex)
Destroy a mutex, releasing the underlying STK mutex object.
int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clock_id, const struct timespec *abstime)
pthread_mutex_timedlock() with an explicit clock.
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared)
int pthread_attr_destroy(pthread_attr_t *attr)
Destroy a thread attributes object (no-op; no owned resources).
pthread_t pthread_self(void)
Return the calling thread's own handle.
int pthread_attr_setname_np(pthread_attr_t *attr, const char *name)
Set the name of threads created with attr (non-portable, GNU-style).
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
Set the guard size. Accepted and stored, but STK has no guard pages: it has no effect.
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
Set the scheduling priority (task weight) of threads created with attr.
int pthread_spin_lock(pthread_spinlock_t *lock)
Acquire the spinlock, spinning until available.
int pthread_kill(pthread_t thread, int sig)
Send a "signal" to a thread. STK has no signals, three values are supported:
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
Acquire the lock for exclusive writing. Blocks until available.
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
Lock with an absolute deadline.
void pthread_cleanup_push(void(*routine)(void *), void *arg)
Push a cleanup handler (run in LIFO order by pthread_exit() and by cancellation).
int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr)
Get the attributes of a running thread (GNU extension).
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
Set/get the process-shared attribute of mutex, condition variable, rwlock and barrier attributes.
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Initialize a mutex.
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
Destroy a read-write lock attributes object (no-op).
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
Destroy a read-write lock, releasing the underlying STK object.
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
Get the inherit-scheduling mode stored in attr.
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
Set the scheduling policy stored in attr (SCHED_OTHER, SCHED_FIFO or SCHED_RR).
int pthread_setschedprio(pthread_t thread, int prio)
Change the priority of a running thread (the policy is kept). See pthread_setschedparam().
int pthread_detach(pthread_t thread)
Mark a thread as detached.
unsigned int pthread_key_t
A thread-specific data key.
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
Release a read or write hold, whichever the calling thread holds.
int pthread_attr_setprivileged_np(pthread_attr_t *attr, int mode)
Non-portable: select the STK access mode of threads created with attr.
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
Get the mutex type stored in attr.
struct pthread_stk_ctrl_t * pthread_t
Opaque thread handle.
int pthread_atfork(void(*prepare)(void), void(*parent)(void), void(*child)(void))
Register fork handlers. STK has no fork(): the handlers are never run, the call only exists so that c...
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
Set the requested stack size in bytes.
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
Set the mutex type.
int pthread_barrier_wait(pthread_barrier_t *barrier)
Block until count threads have called this function, then release them all together; the barrier rese...
int pthread_attr_getprivileged_np(const pthread_attr_t *attr, int *mode)
Non-portable: get the access mode set in attr.
int pthread_spin_destroy(pthread_spinlock_t *lock)
Destroy a spinlock.
#define __stk_pthread_aligned
Alignment attribute of the opaque object storage (one machine word).
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
Initialize a barrier for count participating threads.
int pthread_getname_np(pthread_t thread, char *name, size_t len)
Get the name of a thread.
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Atomically unlock mutex and wait for a signal; re-locks mutex before returning.
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
Supply an external, caller-owned stack buffer for the thread.
int pthread_mutex_unlock(pthread_mutex_t *mutex)
Unlock a mutex held by the calling thread.
int pthread_equal(pthread_t t1, pthread_t t2)
Compare two thread handles for equality.
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
Initialize a read-write lock attributes object (no settable properties).
int pthread_cancel(pthread_t thread)
Request cancellation of a thread.
int pthread_barrierattr_init(pthread_barrierattr_t *attr)
Initialize a barrier attributes object (no settable properties).
int pthread_cond_destroy(pthread_cond_t *cond)
Destroy a condition variable, releasing the underlying STK object.
int pthread_timedjoin_np(pthread_t thread, void **retval, const struct timespec *abstime)
Join a thread, waiting until the absolute deadline abstime (GNU extension).
int pthread_setname_np(pthread_t thread, const char *name)
Set the name of a thread (shown by debug/trace tools), non-portable GNU-style.
int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
Initialize a spinlock.
int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
Get the stored guard size (0 by default).
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
int pthread_setcanceltype(int type, int *oldtype)
Set the cancellation type. Only PTHREAD_CANCEL_DEFERRED is supported.
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
Try to acquire the read lock without blocking.
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
Get the policy and priority (task weight) of a thread.
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
As pthread_cond_wait(), with an absolute deadline.
#define STK_C_PTHREAD_BARRIER_WORDS
uintptr_t pthread_stk_word_t
Native machine word used for stacks and opaque object storage (same as stk::Word).
int pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clock_id, const struct timespec *abstime)
pthread_rwlock_timedrdlock() with an explicit clock. See pthread_mutex_clocklock() for clock_id.
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
Destroy a barrier attributes object (no-op).
int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
Set the clock used by pthread_cond_timedwait() deadlines.
int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
Get the contention scope (always PTHREAD_SCOPE_SYSTEM).
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared)
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
Acquire the lock for shared reading. Blocks until available.
int pthread_spin_trylock(pthread_spinlock_t *lock)
Try to acquire the spinlock without blocking.
void * pthread_getspecific(pthread_key_t key)
Get the calling thread's value for key.
void stk_pthread_bind_kernel(void *kernel)
Bind the STK kernel instance that pthread_create() will add new threads to.
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
Select whether a new thread inherits the priority of the creating thread.
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Initialize a condition variable.
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
Acquire the read lock with an absolute deadline.
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
Get the stack size in bytes that threads created with attr will use.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Create and start a new thread.
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
Set PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED.
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
Set the mutex protocol.
int pthread_barrier_destroy(pthread_barrier_t *barrier)
Destroy a barrier, releasing the underlying STK barrier object.
int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize)
Get the previously-set external stack (stackaddr is NULL if none set).
int pthread_setconcurrency(int level)
Set the concurrency level hint. Accepted and ignored (STK always schedules kernel tasks).
int pthread_key_delete(pthread_key_t key)
Free a thread-specific data key.
int pthread_yield(void)
Voluntarily give up the CPU to another ready task (cooperative yield), then resume once rescheduled.
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
Get the scheduling policy stored in attr.
void pthread_testcancel(void)
Create a cancellation point: terminate the calling thread if a cancellation is pending and enabled.
int pthread_key_create(pthread_key_t *key, void(*destructor)(void *))
Allocate a new thread-specific data key.
int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared)
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
Initialize a mutex attributes object with default values (PTHREAD_MUTEX_DEFAULT).
int pthread_getconcurrency(void)
Get the concurrency level hint.
void pthread_cleanup_pop(int execute)
Pop the most recent cleanup handler, running it if execute is non-zero.
int pthread_cond_broadcast(pthread_cond_t *cond)
Wake all threads waiting on cond (no effect if none is waiting).
int pthread_attr_getname_np(const pthread_attr_t *attr, char *name, size_t len)
Get the name stored in attr.
int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
Get the scheduling priority stored in attr.
int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
pthread_stk_word_t data[(10U+((0) ? 1U :0U))]
pthread_stk_word_t data[(7U+((0) ? 1U :0U))]
pthread_stk_word_t data[(17U+((0) ? 3U :0U))]
pthread_stk_word_t data[((10U+((0) ? 1U :0U))+(7U+((0) ? 1U :0U))+4U+((0) ? 1U :0U))]
Thread creation attributes.
int __scope
Always PTHREAD_SCOPE_SYSTEM.
size_t __stack_bytes
0 = use STK_C_PTHREAD_DEFAULT_STACK_WORDS.
int __detachstate
PTHREAD_CREATE_JOINABLE / _DETACHED.
int __policy
SCHED_OTHER (default) / SCHED_FIFO / SCHED_RR (informational).
pthread_stk_word_t * __ext_stack
Caller-supplied stack, or NULL.
char __name[(16U)]
Thread name, "" = default ("pthread").
int __inheritsched
PTHREAD_EXPLICIT_SCHED (default) / PTHREAD_INHERIT_SCHED.
int __privileged
PTHREAD_CREATE_USER_NP (default) / _PRIVILEGED_NP.
size_t __guardsize
Stored for pthread_attr_getguardsize(), has no effect.
int __priority
Task weight (sched_priority), used if __inheritsched is PTHREAD_EXPLICIT_SCHED.
int __type
PTHREAD_MUTEX_NORMAL / DEFAULT / RECURSIVE / ERRORCHECK.
int __protocol
PTHREAD_PRIO_NONE (default) / PTHREAD_PRIO_INHERIT.
pthread_stk_mutex_mem_t __mem
In-place storage for the STK mutex object.
int __type
PTHREAD_MUTEX_* type; 0 (NORMAL) when statically initialized.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
Condition variable attributes (currently no settable properties).
int __clock
CLOCK_REALTIME (default) / CLOCK_MONOTONIC: both are the same STK tick timeline.
A pthread condition variable.
pthread_stk_cv_mem_t __mem
In-place storage for the STK condition variable object.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
Read-write lock attributes (currently no settable properties).
int __reserved
Unused (no settable properties); reserved for future use.
A pthread read-write lock.
volatile bool __wrlocked
True while held for writing; selects Unlock() vs ReadUnlock() on release.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
pthread_stk_rwmutex_mem_t __mem
In-place storage for the STK rwmutex object.
pthread_stk_word_t data[1]
Storage for the non-recursive fast hw::SpinLock.
struct pthread_spinlock_t::@001015320316047075072233215330060151327030003304 __mem
In-place storage for the STK spinlock object.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
Barrier attributes (currently no settable properties).
int __reserved
Unused (no settable properties); reserved for future use.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
pthread_stk_barrier_mem_t __mem
In-place storage for the STK barrier object.
A pthread_once() control object.
void * __handle
Guard mutex constructed in __mem, or NULL if not yet constructed.
pthread_stk_mutex_mem_t __mem
In-place storage for the guard mutex.
int __state
0 = not started, 1 = running, 2 = done.