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#include "stk_c.h"
14#include "stk_c_memory.h"
15
16#ifdef __cplusplus
17 #include <ctime>
18 #include <cerrno>
19#else
20 #include <time.h>
21 #include <errno.h>
22#endif
23
145
146#ifdef __cplusplus
147extern "C" {
148#endif
149
150// =============================================================================
151// Configuration macros (can be overridden before including this file)
152// =============================================================================
153
160#ifndef STK_C_PTHREAD_MAX_THREADS
161 #define STK_C_PTHREAD_MAX_THREADS (8U)
162#endif
163
172#ifndef STK_C_PTHREAD_DEFAULT_STACK_WORDS
173 #define STK_C_PTHREAD_DEFAULT_STACK_WORDS (1024U)
174#endif
175
180#ifndef STK_C_PTHREAD_REAPER_STACK_WORDS
181 #define STK_C_PTHREAD_REAPER_STACK_WORDS (256U)
182#endif
183
188#ifndef STK_C_PTHREAD_KEYS_MAX
189 #define STK_C_PTHREAD_KEYS_MAX (8U)
190#endif
191
192// =============================================================================
193// Bootstrap (STK-specific, not part of POSIX)
194// =============================================================================
195
207
208// =============================================================================
209// Types
210// =============================================================================
211
215
225
226#define PTHREAD_CREATE_JOINABLE (0)
227#define PTHREAD_CREATE_DETACHED (1)
228
236
237#define PTHREAD_MUTEX_NORMAL (0)
238#define PTHREAD_MUTEX_DEFAULT (PTHREAD_MUTEX_NORMAL)
239#define PTHREAD_MUTEX_ERRORCHECK (1)
240#define PTHREAD_MUTEX_RECURSIVE (2)
241
252
253#define PTHREAD_MUTEX_INITIALIZER {0}
254
261
272
273#define PTHREAD_COND_INITIALIZER {0}
274
284
298
299#define PTHREAD_RWLOCK_INITIALIZER {0}
300
301#define PTHREAD_PROCESS_PRIVATE (0)
302#define PTHREAD_PROCESS_SHARED (1)
303
315
322
333
339#define PTHREAD_BARRIER_SERIAL_THREAD (-1)
340
353
354#define PTHREAD_ONCE_INIT {0}
355
360typedef unsigned int pthread_key_t;
361
365#define PTHREAD_KEYS_MAX (STK_C_PTHREAD_KEYS_MAX)
366
373#define PTHREAD_DESTRUCTOR_ITERATIONS (4)
374
375// =============================================================================
376// Thread attributes
377// =============================================================================
378
383
387
393int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
394
397int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
398
405int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
406
409int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
410
413int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
414
417int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
418
419// =============================================================================
420// Thread lifecycle
421// =============================================================================
422
434int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
435 void *(*start_routine)(void *), void *arg);
436
445int pthread_join(pthread_t thread, void **retval);
446
453int pthread_detach(pthread_t thread);
454
460void pthread_exit(void *retval);
461
468
472
480int pthread_yield(void);
481
482// =============================================================================
483// Mutex
484// =============================================================================
485
488
494int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
495
506
512int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
513
514// =============================================================================
515// Condition variable
516// =============================================================================
517
520
523
528
535 const struct timespec *abstime);
536
539
540// =============================================================================
541// Read-write lock
542// =============================================================================
543
546
552
558
563
569int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime);
570
576
581
587int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime);
588
593
594// =============================================================================
595// Spin lock
596// =============================================================================
597
603int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
605
610
616
617// =============================================================================
618// Barrier
619// =============================================================================
620
623
632 unsigned int count);
634
642
643// =============================================================================
644// Once
645// =============================================================================
646
652int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
653
654// =============================================================================
655// Thread-specific data (keys)
656// =============================================================================
657
665int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
666
675
682int pthread_setspecific(pthread_key_t key, const void *value);
683
689
690#ifdef __cplusplus
691}
692#endif
693
695
696#endif /* STK_C_PTHREAD_H_ */
C language binding/interface for SuperTinyKernel RTOS.
C language binding for stk::memory::BlockMemoryPool.
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
Definition stk_c.h:126
uintptr_t stk_word_t
CPU register type.
Definition stk_c.h:94
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
Try to acquire the write lock without blocking.
int pthread_condattr_destroy(pthread_condattr_t *attr)
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)
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)
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)
void pthread_exit(void *retval)
Terminate the calling thread.
int pthread_mutex_trylock(pthread_mutex_t *mutex)
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)
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)
int pthread_mutex_destroy(pthread_mutex_t *mutex)
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)
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
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_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
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_spin_destroy(pthread_spinlock_t *lock)
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)
int pthread_equal(pthread_t t1, pthread_t t2)
Compare two thread handles for equality.
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
int pthread_barrierattr_init(pthread_barrierattr_t *attr)
int pthread_cond_destroy(pthread_cond_t *cond)
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)
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)
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 currently requested stack size in bytes (0 = default).
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)
int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize)
Get the previously-set external stack (NULL/0 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)
int pthread_cond_broadcast(pthread_cond_t *cond)
Opaque memory container for a Mutex instance.
Definition stk_c.h:916
Opaque memory container for SpinLock object.
Definition stk_c.h:982
Opaque memory container for a ConditionVariable instance.
Definition stk_c.h:1033
Opaque memory container for an RWMutex instance.
Definition stk_c.h:1957
Opaque memory container for a Barrier instance.
Definition stk_c.h:2060
Thread creation attributes.
stk_word_t * __ext_stack
Mutex attributes.
A pthread mutex.
stk_mutex_mem_t __mem
stk_mutex_t * __handle
Condition variable attributes (currently no settable properties).
A pthread condition variable.
stk_cv_t * __handle
stk_cv_mem_t __mem
Read-write lock attributes (currently no settable properties).
A pthread read-write lock.
stk_rwmutex_t * __handle
stk_rwmutex_mem_t __mem
A pthread spinlock.
stk_spinlock_mem_t __mem
stk_spinlock_t * __handle
Barrier attributes (currently no settable properties).
A pthread barrier.
stk_barrier_mem_t __mem
stk_barrier_t * __handle
A pthread_once() control object.
stk_mutex_mem_t __mem
stk_mutex_t * __handle