SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_c_pthread.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_C_PTHREAD_H_
11#define STK_C_PTHREAD_H_
12
13// Undefine system headers:
14#undef _POSIX_THREADS
15#define _SYS__PTHREADTYPES_H_
16
17#include "stk_c.h"
18
19#ifdef __cplusplus
20 #include <ctime>
21#else
22 #include <time.h>
23#endif
24
194
195#ifdef __cplusplus
196extern "C" {
197#endif
198
202#define STK_C_PTHREAD_VERSION (0x20261004)
203
204// =============================================================================
205// Configuration macros (can be overridden before including this file)
206// =============================================================================
207
215#ifndef STK_C_PTHREAD_MAX_THREADS
216 #define STK_C_PTHREAD_MAX_THREADS (8U)
217#endif
218
227#ifndef STK_C_PTHREAD_DEFAULT_STACK_WORDS
228 #define STK_C_PTHREAD_DEFAULT_STACK_WORDS (1024U)
229#endif
230
235#ifndef STK_C_PTHREAD_REAPER_STACK_WORDS
236 #define STK_C_PTHREAD_REAPER_STACK_WORDS (256U)
237#endif
238
243#ifndef STK_C_PTHREAD_KEYS_MAX
244 #define STK_C_PTHREAD_KEYS_MAX (8U)
245#endif
246
247// =============================================================================
248// Bootstrap (STK-specific, not part of POSIX)
249// =============================================================================
250
266
267// =============================================================================
268// Types
269// =============================================================================
270
274
285
286#define PTHREAD_CREATE_JOINABLE (0)
287#define PTHREAD_CREATE_DETACHED (1)
288
293#define PTHREAD_CREATE_USER_NP (0)
294#define PTHREAD_CREATE_PRIVILEGED_NP (1)
295
312
313#define PTHREAD_MUTEX_NORMAL (0)
314#define PTHREAD_MUTEX_DEFAULT (PTHREAD_MUTEX_NORMAL)
315#define PTHREAD_MUTEX_ERRORCHECK (1)
316#define PTHREAD_MUTEX_RECURSIVE (2)
317
329
330#define PTHREAD_MUTEX_INITIALIZER {0}
331
338
349
350#define PTHREAD_COND_INITIALIZER {0}
351
361
375
376#define PTHREAD_RWLOCK_INITIALIZER {0}
377
378#define PTHREAD_PROCESS_PRIVATE (0)
379#define PTHREAD_PROCESS_SHARED (1)
380
394
401
412
418#define PTHREAD_BARRIER_SERIAL_THREAD (-1)
419
432
433#define PTHREAD_ONCE_INIT {0}
434
439typedef unsigned int pthread_key_t;
440
444#define PTHREAD_KEYS_MAX (STK_C_PTHREAD_KEYS_MAX)
445
452#define PTHREAD_DESTRUCTOR_ITERATIONS (4)
453
454// =============================================================================
455// Thread attributes
456// =============================================================================
457
463
468
476int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
477
485int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
486
494int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
495
503int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
504
509int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
510
516int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
517
527
532int pthread_attr_getprivileged_np(const pthread_attr_t *attr, int *mode);
533
534// =============================================================================
535// Thread lifecycle
536// =============================================================================
537
550int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
551 void *(*start_routine)(void *), void *arg);
552
561int pthread_join(pthread_t thread, void **retval);
562
570int pthread_detach(pthread_t thread);
571
578void pthread_exit(void *retval);
579
586
592
600int pthread_yield(void);
601
602// =============================================================================
603// Mutex
604// =============================================================================
605
612
618
626
632int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
633
642
653
667
677
688
701int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
702
703// =============================================================================
704// Condition variable
705// =============================================================================
706
713
719
727
736
755
772 const struct timespec *abstime);
773
781
787
788// =============================================================================
789// Read-write lock
790// =============================================================================
791
798
804
811
819
826
832
841int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime);
842
849
855
864int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime);
865
872
873// =============================================================================
874// Spin lock
875// =============================================================================
876
883int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
884
892
902
909
915
916// =============================================================================
917// Barrier
918// =============================================================================
919
925
931
941 unsigned int count);
942
951
960
961// =============================================================================
962// Once
963// =============================================================================
964
974int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
975
976// =============================================================================
977// Thread-specific data (keys)
978// =============================================================================
979
987int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
988
998
1007int pthread_setspecific(pthread_key_t key, const void *value);
1008
1015
1016#ifdef __cplusplus
1017}
1018#endif
1019
1021
1022#endif /* STK_C_PTHREAD_H_ */
C language binding/interface for SuperTinyKernel RTOS.
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
Definition stk_c.h:131
uintptr_t stk_word_t
CPU register type.
Definition stk_c.h:99
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,...
int pthread_mutex_lock(pthread_mutex_t *mutex)
Lock a mutex, blocking until it becomes available.
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
Initialize a read-write lock.
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
Acquire the write lock with an absolute deadline.
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_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_condattr_init(pthread_condattr_t *attr)
Initialize a condition variable attributes object (no settable properties).
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_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_spin_lock(pthread_spinlock_t *lock)
Acquire the spinlock, spinning until available.
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.
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_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_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.
void stk_pthread_bind_kernel(stk_kernel_t *kernel)
Bind the STK kernel instance that pthread_create() will add new threads to.
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_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_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_spin_init(pthread_spinlock_t *lock, int pshared)
Initialize a spinlock.
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
Try to acquire the read lock without blocking.
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
As pthread_cond_wait(), with an absolute deadline.
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
Destroy a barrier attributes object (no-op).
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.
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_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
Set PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED.
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_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_key_create(pthread_key_t *key, void(*destructor)(void *))
Allocate a new thread-specific data key.
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
Initialize a mutex attributes object with default values (PTHREAD_MUTEX_DEFAULT).
int pthread_cond_broadcast(pthread_cond_t *cond)
Wake all threads waiting on cond (no effect if none is waiting).
Opaque memory container for a Mutex instance.
Definition stk_c.h:951
Opaque memory container for a ConditionVariable instance.
Definition stk_c.h:1078
Opaque memory container for an RWMutex instance.
Definition stk_c.h:2011
Opaque memory container for a Barrier instance.
Definition stk_c.h:2113
Thread creation attributes.
size_t __stack_bytes
0 = use STK_C_PTHREAD_DEFAULT_STACK_WORDS.
int __detachstate
PTHREAD_CREATE_JOINABLE / _DETACHED.
stk_word_t * __ext_stack
Caller-supplied stack, or NULL.
int __privileged
PTHREAD_CREATE_USER_NP (default) / _PRIVILEGED_NP.
Mutex attributes.
int __type
PTHREAD_MUTEX_NORMAL / DEFAULT / RECURSIVE / ERRORCHECK.
A pthread mutex.
int __type
PTHREAD_MUTEX_* type; 0 (NORMAL) when statically initialized.
stk_mutex_mem_t __mem
In-place storage for the STK mutex object.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
Condition variable attributes (currently no settable properties).
int __reserved
Unused (no settable properties); reserved for future use.
A pthread condition variable.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
stk_cv_mem_t __mem
In-place storage for the STK condition variable object.
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.
stk_rwmutex_mem_t __mem
In-place storage for the STK rwmutex object.
A pthread spinlock.
stk_word_t data[1] __stk_c_aligned
Storage for the non-recursive fast hw::SpinLock.
struct pthread_spinlock_t::@072117035232205037233337242056154122155161307376 __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.
A pthread barrier.
stk_barrier_mem_t __mem
In-place storage for the STK barrier object.
void * __handle
Object constructed in __mem, or NULL if not yet constructed.
A pthread_once() control object.
stk_mutex_mem_t __mem
In-place storage for the guard mutex.
void * __handle
Guard mutex constructed in __mem, or NULL if not yet constructed.
int __state
0 = not started, 1 = running, 2 = done.