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.cpp File Reference
#include <cstddef>
#include <cstdint>
#include <new>
#include <cerrno>
#include "stk.h"
#include "sync/stk_sync.h"
#include "stk_c.h"
#include "stk_c_memory.h"
#include "stk_c_pthread.h"
Include dependency graph for stk_c_pthread.cpp:

Go to the source code of this file.

Classes

class  stk::pthread::PthreadTask
struct  pthread_stk_ctrl_t
struct  stk::pthread::KeyEntry

Namespaces

namespace  stk
 Namespace of STK package.
namespace  stk::pthread

Typedefs

typedef stk::hw::CriticalSection::ScopedLock CsLock
using stk::pthread::ThreadCtrl = pthread_stk_ctrl_t

Functions

void * malloc (std::size_t size)
void free (void *ptr)
static IKernel * stk::pthread::ToKernel (stk_kernel_t *k)
template<typename T>
static T stk::pthread::LoadAcquire (T *p)
template<typename T>
static void stk::pthread::StoreRelease (T *p, T v)
template<typename T, typename Mem>
static T * stk::pthread::EnsureObject (void **slot, Mem &mem)
static sync::Mutex * stk::pthread::HandleOf (pthread_mutex_t *o)
static sync::Mutex * stk::pthread::HandleOf (pthread_once_t *o)
static sync::ConditionVariable * stk::pthread::HandleOf (pthread_cond_t *o)
static sync::RWMutex * stk::pthread::HandleOf (pthread_rwlock_t *o)
static hw::SpinLock * stk::pthread::HandleOf (pthread_spinlock_t *o)
static sync::Barrier * stk::pthread::HandleOf (pthread_barrier_t *o)
static void stk::pthread::RunKeyDestructors (ThreadCtrl *ctrl)
 stk::pthread::STK_BLOCKPOOL_STORAGE_DECL (s_PthreadStackStorage, STK_C_PTHREAD_MAX_THREADS, kDefaultStackBytes)
static stk_blockpool_t * stk::pthread::EnsureStackPool ()
static void stk::pthread::ReleaseThreadResources (ThreadCtrl *ctrl)
static void stk::pthread::WaitForTaskExit (ThreadCtrl *ctrl)
static void stk::pthread::ReaperEntry (void *arg)
static void stk::pthread::EnsureReaper ()
static void stk::pthread::EnqueueReap (ThreadCtrl *ctrl)
static ThreadCtrl * stk::pthread::DequeueReap ()
template<typename TObj>
static void stk::pthread::WaitSignaled (TObj &obj)
static void stk::pthread::MaybeReap (ThreadCtrl *ctrl)
static ThreadCtrl * stk::pthread::AcquireThreadSlot ()
static bool stk::pthread::AllocateStack (ThreadCtrl *ctrl, const pthread_attr_t *attr, std::size_t stack_bytes)
static Timeout stk::pthread::TimespecToRelativeTimeout (const struct timespec *abstime)
static int stk::pthread::TimedResultToErrno (EWaitResult wr)
static sync::Mutex * stk::pthread::EnsureMutex (pthread_mutex_t *m)
static bool stk::pthread::IsMutexOwner (const sync::Mutex *m)
static bool stk::pthread::IsValidMutexType (int type)
static int stk::pthread::PreLockCheck (const pthread_mutex_t *pm, const sync::Mutex *m, int errorcheck_code)
static sync::ConditionVariable * stk::pthread::EnsureCond (pthread_cond_t *c)
static sync::RWMutex * stk::pthread::EnsureRWLock (pthread_rwlock_t *rw)
static sync::Mutex * stk::pthread::EnsureOnceMutex (pthread_once_t *o)
static ThreadCtrl * stk::pthread::CurrentCtrl ()
static ThreadCtrl * stk::pthread::CurrentCtrlOrAdopt ()
static bool stk::pthread::IsKeyUsed (pthread_key_t key)
void stk_pthread_bind_kernel (stk_kernel_t *kernel)
 Bind the STK kernel instance that pthread_create() will add new threads to.
int pthread_attr_init (pthread_attr_t *attr)
 Initialize a thread attributes object with default values (default stack size, no external stack, joinable).
int pthread_attr_destroy (pthread_attr_t *attr)
 Destroy a thread attributes object (no-op; no owned resources).
int pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize)
 Set the requested stack size in bytes.
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_attr_setstack (pthread_attr_t *attr, void *stackaddr, size_t stacksize)
 Supply an external, caller-owned stack buffer for the thread.
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_attr_setdetachstate (pthread_attr_t *attr, int detachstate)
 Set PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED.
int pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstate)
 Get the current detach-state setting.
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_attr_getprivileged_np (const pthread_attr_t *attr, int *mode)
 Non-portable: get the access mode set in attr.
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_join (pthread_t thread, void **retval)
 Block until the given joinable thread finishes, then reclaim its resources.
int pthread_detach (pthread_t thread)
 Mark a thread as detached.
void pthread_exit (void *retval)
 Terminate the calling thread.
pthread_t pthread_self (void)
 Return the calling thread's own handle.
int pthread_equal (pthread_t t1, pthread_t t2)
 Compare two thread handles for equality.
int pthread_yield (void)
 Voluntarily give up the CPU to another ready task (cooperative yield), then resume once rescheduled.
int pthread_mutexattr_init (pthread_mutexattr_t *attr)
 Initialize a mutex attributes object with default values (PTHREAD_MUTEX_DEFAULT).
int pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
 Destroy a mutex attributes object (no-op; no owned resources).
int pthread_mutexattr_settype (pthread_mutexattr_t *attr, int type)
 Set the mutex type.
int pthread_mutexattr_gettype (const pthread_mutexattr_t *attr, int *type)
 Get the mutex type stored in attr.
int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
 Initialize a mutex.
int pthread_mutex_destroy (pthread_mutex_t *mutex)
 Destroy a mutex, releasing the underlying STK mutex object.
int pthread_mutex_lock (pthread_mutex_t *mutex)
 Lock a mutex, blocking until it becomes available.
int pthread_mutex_trylock (pthread_mutex_t *mutex)
 Try to lock a mutex without blocking.
int pthread_mutex_unlock (pthread_mutex_t *mutex)
 Unlock a mutex held by the calling thread.
int pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime)
 Lock with an absolute deadline.
int pthread_condattr_init (pthread_condattr_t *attr)
 Initialize a condition variable attributes object (no settable properties).
int pthread_condattr_destroy (pthread_condattr_t *attr)
 Destroy a condition variable attributes object (no-op).
int pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *)
 Initialize a condition variable.
int pthread_cond_destroy (pthread_cond_t *cond)
 Destroy a condition variable, releasing the underlying STK object.
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_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
 As pthread_cond_wait(), with an absolute deadline.
int pthread_cond_signal (pthread_cond_t *cond)
 Wake one thread waiting on cond (no effect if none is waiting).
int pthread_cond_broadcast (pthread_cond_t *cond)
 Wake all threads waiting on cond (no effect if none is waiting).
int pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
 Initialize a read-write lock attributes object (no settable properties).
int pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)
 Destroy a read-write lock attributes object (no-op).
int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *)
 Initialize a read-write lock.
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
 Destroy a read-write lock, releasing the underlying STK object.
int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
 Acquire the lock for shared reading. Blocks until available.
int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
 Try to acquire the read lock without blocking.
int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime)
 Acquire the read lock with an absolute deadline.
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
 Acquire the lock for exclusive writing. Blocks until available.
int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
 Try to acquire the write lock without blocking.
int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime)
 Acquire the write lock with an absolute deadline.
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
 Release a read or write hold, whichever the calling thread holds.
int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
 Initialize a spinlock.
int pthread_spin_destroy (pthread_spinlock_t *lock)
 Destroy a spinlock.
int pthread_spin_lock (pthread_spinlock_t *lock)
 Acquire the spinlock, spinning until available.
int pthread_spin_trylock (pthread_spinlock_t *lock)
 Try to acquire the spinlock without blocking.
int pthread_spin_unlock (pthread_spinlock_t *lock)
 Release a spinlock held by the calling thread.
int pthread_barrierattr_init (pthread_barrierattr_t *attr)
 Initialize a barrier attributes object (no settable properties).
int pthread_barrierattr_destroy (pthread_barrierattr_t *attr)
 Destroy a barrier attributes object (no-op).
int pthread_barrier_init (pthread_barrier_t *barrier, const pthread_barrierattr_t *, unsigned int count)
 Initialize a barrier for count participating threads.
int pthread_barrier_destroy (pthread_barrier_t *barrier)
 Destroy a barrier, releasing the underlying STK barrier object.
int pthread_barrier_wait (pthread_barrier_t *barrier)
 Block until count threads have called this function, then release them all together; the barrier resets for reuse.
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() on it concurrently.
int pthread_key_create (pthread_key_t *key, void(*destructor)(void *))
 Allocate a new thread-specific data key.
int pthread_key_delete (pthread_key_t key)
 Free a thread-specific data key.
int pthread_setspecific (pthread_key_t key, const void *value)
 Set the calling thread's value for key.
void * pthread_getspecific (pthread_key_t key)
 Get the calling thread's value for key.

Variables

constexpr std::size_t stk::pthread::kDefaultStackBytes
constexpr int stk::pthread::kOnceRunning = 1
constexpr int stk::pthread::kOnceDone = 2
static ThreadCtrl stk::pthread::s_Threads [STK_C_PTHREAD_MAX_THREADS]
static stk_kernel_t * stk::pthread::s_BoundKernel = nullptr
static stk_blockpool_t * stk::pthread::s_StackPool = nullptr
static ThreadCtrl * stk::pthread::s_ReapQueue [STK_C_PTHREAD_MAX_THREADS]
static std::size_t stk::pthread::s_ReapHead = 0U
static std::size_t stk::pthread::s_ReapCount = 0U
static sync::Semaphore * stk::pthread::s_ReapSem = nullptr
struct { 
   uint8_t   stk::pthread::data [sizeof(sync::Semaphore)] 
} stk::pthread::s_ReapSemBuf
static stk_task_t * stk::pthread::s_ReaperTask = nullptr
static stk_word_t stk::pthread::s_ReaperStack [STK_C_PTHREAD_REAPER_STACK_WORDS]
static KeyEntry stk::pthread::s_Keys [STK_C_PTHREAD_KEYS_MAX]

Typedef Documentation

◆ CsLock

Definition at line 43 of file stk_c_pthread.cpp.

Function Documentation

◆ free()

void free ( void * ptr)

◆ malloc()

void * malloc ( std::size_t size)