74static const size_t LOCK_BLOCK_SIZE = LockPool::AlignBlockSize(
sizeof(
__lock));
77 "STK_NEWLIB_MAX_LOCKS is out of the pool capacity range");
79 "pool block size must preserve the alignment of the lock object");
88 alignas(LockPool) uint8_t data[
sizeof(LockPool)];
90LockPoolBuffer s_LockPoolBuffer;
91LockPool *s_LockPool =
nullptr;
96LockPool *GetLockPool()
100 if (s_LockPool ==
nullptr)
102 s_LockPool =
new (
static_cast<void *
>(s_LockPoolBuffer.data))
116 bool running =
false;
138template <
typename T,
typename Mem,
typename... Args>
139static inline T *
ConstructIn(Mem &mem, Args &&... args)
142 "storage is too small for the C++ object");
144 "storage is not sufficiently aligned for the C++ object");
146 return new (
static_cast<void *
>(mem.storage)) T(
static_cast<Args &&
>(args)...);
180 Construct(lock, kind);
196 _LOCK_T new_lock = GetLockPool()->TryAllocT<
__lock>();
198 if (new_lock ==
nullptr)
206 new_lock->dynamic =
true;
207 new_lock->mutex =
nullptr;
209 Construct(new_lock, kind);
217void Close(_LOCK_T lock)
224 lock->mutex =
nullptr;
229 lock->mutex =
nullptr;
241 if (!GetLockPool()->Free(lock))
256 GetMutex(lock, kind)->Lock();
269 if (!GetMutex(lock, kind)->TryLock())
283 if (IsTaskContext() && (lock !=
nullptr) && (lock->kind == kind) && (lock->mutex !=
nullptr))
285 lock->mutex->Unlock();
297#if STK_NEWLIB_STATIC_LOCKS
334 stk::newlib::Close(lock);
339 stk::newlib::Close(lock);
Implementation of fixed-size block memory pool: stk::memory::BlockMemoryPool.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_STATIC_ASSERT_DESC(X, DESC)
Compile-time assertion with a custom error description. Produces a compilation error if X is false.
Implementation of synchronization primitive: stk::sync::FastMutex.
Implementation of synchronization primitive: stk::sync::Mutex.
struct __lock __lock___at_quick_exit_mutex
void __retarget_lock_acquire(_LOCK_T lock)
Acquire lock immediately after the lock object is available.
struct __lock __lock___tz_mutex
struct __lock __lock___arc4random_mutex
int __retarget_lock_try_acquire_recursive(_LOCK_T lock)
Acquire recursive lock if the lock object is available.
struct __lock __lock___dd_hash_mutex
struct __lock __lock___atexit_recursive_mutex
struct __lock __lock___sfp_recursive_mutex
struct __lock __lock___env_recursive_mutex
void __retarget_lock_release_recursive(_LOCK_T lock)
Relinquish the recursive lock ownership.
void __retarget_lock_acquire_recursive(_LOCK_T lock)
Acquire recursive lock immediately after the lock object is available.
struct __lock __lock___malloc_recursive_mutex
void __retarget_lock_init_recursive(_LOCK_T *lock)
Allocate recursive lock related resources.
void __retarget_lock_close(_LOCK_T lock)
Free lock related resources.
void __retarget_lock_init(_LOCK_T *lock)
Allocate lock related resources.
void __retarget_lock_release(_LOCK_T lock)
Relinquish the lock ownership.
struct __lock __lock___sinit_recursive_mutex
int __retarget_lock_try_acquire(_LOCK_T lock)
Acquire lock if the lock object is available.
void __retarget_lock_close_recursive(_LOCK_T lock)
Free recursive lock related resources.
GCC Newlib OS interface: retargetable locking (__retarget_lock_*) on top of STK.
#define STK_NEWLIB_MAX_LOCKS
Capacity of the static pool for dynamic lock objects: the maximum number of locks created by __retarg...
Namespace of STK package.
static T * ConstructIn(Mem &mem, Args &&... args)
Construct object T in-place inside a C memory container.
void STK_KERNEL_PANIC(stk::EKernelPanicId id)
Called when the kernel detects an unrecoverable internal fault.
@ KERNEL_PANIC_BAD_MEMORY_REGION
Bad memory region (ARM TrustZone: provided memory region overlaps with another one; memory pools: a p...
@ KERNEL_PANIC_NO_MEM
Out of memory: a mandatory allocation (heap or a fixed-size static pool) failed in a context that can...
static constexpr TId TID_NONE
Reserved task/thread id representing zero/none thread id.
Fixed-size block allocator with O(1) alloc/free and proper task-blocking semantics.
RAII instance that enters the critical section on construction and exits it on destruction.
Interface for mutex synchronization primitive.
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
virtual TId GetTid() const =0
Get thread Id of the currently running task.
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
Non-recursive (binary) mutex primitive with ownership tracking.
Recursive mutex primitive that allows the same thread to acquire the lock multiple times.
Lock object behind Newlib's opaque _LOCK_T (struct __lock *).
@ KIND_NONE
not constructed (zero-initialized static lock)
stk::IMutex * mutex
constructed FastMutex (KIND_PLAIN) or Mutex (KIND_RECURSIVE), otherwise nullptr
uint8_t storage[STORAGE_SIZE]
bool dynamic
true if taken from the lock pool by __retarget_lock_init* (and therefore returned by close)
EKind kind
kind of the constructed mutex
static const size_t STORAGE_SIZE
storage large enough for either mutex type