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::pthread::PthreadTask Class Referencefinal
Inheritance diagram for stk::pthread::PthreadTask:
Collaboration diagram for stk::pthread::PthreadTask:

Public Member Functions

 PthreadTask ()
STK_VIRT_DTOR ~PthreadTask ()=default
 Destructor.
void Initialize (pthread_stk_ctrl_t *ctrl, stk_word_t *stack, size_t stack_size, EAccessMode mode)
EAccessMode GetAccessMode () const override
 Get pointer to the stack memory.
int32_t GetWeight () const override
 Get static base weight of the task.
const char * GetTraceName () const override
 Get task trace name set by application.
const Word * GetStack () const override
 Get pointer to the stack memory.
size_t GetStackSize () const override
 Get number of elements of the stack memory array.
virtual void OnDeadlineMissed (uint32_t duration)
 Called by the scheduler if deadline of the task is missed when Kernel is operating in Hard Real-Time mode (see stk::KERNEL_HRT).
virtual Timeout GetTimeSlice () const
 Get time-slice quantum of the task (ticks). KERNEL_TIMESLICE only.
virtual size_t GetStackSpace () const
 Get available stack space.

Private Member Functions

 STK_NONCOPYABLE_CLASS (PthreadTask)
void Run () override
 Entry point of the user task.
void OnExit () override
 Called by the kernel before removal from the scheduling (see stk::KERNEL_DYNAMIC).

Private Attributes

pthread_stk_ctrl_t * m_ctrl
stk_word_t * m_stack
size_t m_stack_size
EAccessMode m_mode

Detailed Description

Definition at line 70 of file stk_c_pthread.cpp.

Constructor & Destructor Documentation

◆ PthreadTask()

stk::pthread::PthreadTask::PthreadTask ( )
inlineexplicit

Definition at line 73 of file stk_c_pthread.cpp.

73 : m_ctrl(nullptr), m_stack(nullptr), m_stack_size(0U), m_mode(ACCESS_USER)
74 {}
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details)....
Definition stk_common.h:37
pthread_stk_ctrl_t * m_ctrl

References stk::ACCESS_USER, m_ctrl, m_mode, m_stack, and m_stack_size.

Referenced by STK_NONCOPYABLE_CLASS().

Here is the caller graph for this function:

◆ ~PthreadTask()

STK_VIRT_DTOR stk::pthread::PthreadTask::~PthreadTask ( )
default

Destructor.

Note
MISRA deviation: [STK-DEV-005] Rule 10-3-2.

References STK_VIRT_DTOR.

Member Function Documentation

◆ GetAccessMode()

EAccessMode stk::pthread::PthreadTask::GetAccessMode ( ) const
inlineoverridevirtual

Get pointer to the stack memory.

Note
Optional. ARM TrustZone only.
Returns
Pointer to the Secure stack memory.

Get up to (STK_MPU_TASK_REGIONS - 1) application-defined MPU regions for this task.

When STK_MPU_STACK_GUARD is enabled, each task owns STK_MPU_TASK_REGIONS (2, 4, 6, 8, 12 or 16, see STK_MPU_TASK_REGIONS) hardware MPU region slots. Slot 0 is always the automatic stack guard, computed by the driver from the task's own stack memory (see IStackMemory::GetStack/GetStackSize). This hook supplies the remaining STK_MPU_TASK_REGIONS-1 slots, letting an application additionally sandbox a task to e.g. a private data buffer, a specific peripheral block, or a shared IPC region - on top of its stack guard.

Returns
Pointer to a constant MpuRegionList containing up to (STK_MPU_TASK_REGIONS - 1) region descriptors, or nullptr if no extra task regions are configured. Array element i is applied at task-relative region index i +1 (i.e. the slots following the stack guard).
Note
Optional. Only consulted when STK_MPU_STACK_GUARD is enabled; a no-op on platforms/builds without MPU stack-guard support.
Read once, when the task is bound via Kernel::AddTask() (through IPlatform::InitStack()), not re-evaluated on every context switch. The underlying MpuRegionList instance referenced by the returned pointer must remain valid (e.g. a static const instance); a stack-local temporary is invalid once this function returns.
Warning
An element count greater than (STK_MPU_TASK_REGIONS - 1) is clamped by the driver (STK_ASSERT fires in debug builds); only the first STK_MPU_TASK_REGIONS-1 entries are ever applied.
class MyTask : public stk::Task<256, stk::ACCESS_USER>
{
static constexpr stk::MpuRegionConfig s_extra_regions[] =
{
{ .addr = MY_BUFFER_ADDR, .size = MY_BUFFER_SIZE,
.access_perm = stk::hw::mpu::ACCESS_FULL,
.mem_type = stk::hw::mpu::TYPE_NORMAL_CACHEABLE,
.exec = stk::hw::mpu::EXEC_NEVER },
};
const stk::MpuRegionList *GetMpuRegions() const override
{
static const stk::MpuRegionList mpu_regions(s_extra_regions, 1U);
return &mpu_regions;
}
void Run() override { ... }
};
virtual void Run()=0
Entry point of the user task.
Partial implementation of the user task.
Definition stk_helper.h:98
See also
TaskMpu::Configure, IPlatform::InitStack, MpuRegionList

Get hardware access mode of the user task.

Implements stk::ITask.

Definition at line 90 of file stk_c_pthread.cpp.

90{ return m_mode; }

References m_mode.

◆ GetStack()

const Word * stk::pthread::PthreadTask::GetStack ( ) const
inlineoverridevirtual

Get pointer to the stack memory.

Implements stk::IStackMemory.

Definition at line 95 of file stk_c_pthread.cpp.

95{ return m_stack; }

References m_stack.

◆ GetStackSize()

size_t stk::pthread::PthreadTask::GetStackSize ( ) const
inlineoverridevirtual

Get number of elements of the stack memory array.

Implements stk::IStackMemory.

Definition at line 96 of file stk_c_pthread.cpp.

96{ return m_stack_size; }

References m_stack_size.

◆ GetStackSpace()

virtual size_t stk::IStackMemory::GetStackSpace ( ) const
inlinevirtualinherited

Get available stack space.

Returns
Number of elements of the stack memory array remaining on the stack (computed via the watermark pattern). Returns 0 if the stack has been fully used or the watermark STK_STACK_MEMORY_FILLER was overwritten.
Warning
Stack type: Bottom to Top (index[0]).

Definition at line 465 of file stk_common.h.

466 {
467 const ArrayView<const Word> stack(GetStack(), GetStackSize());
468 const size_t total_size = stack.GetSize();
469 size_t space = 0U;
470
471 for (size_t i = 0U; i < total_size; ++i)
472 {
473 if (stack[i] == STK_STACK_MEMORY_FILLER)
474 {
475 space = i + 1U;
476 }
477 else
478 {
479 break; // terminate loop as soon as watermark ends
480 }
481 }
482
483 return space;
484 }
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:563
virtual size_t GetStackSize() const =0
Get number of elements of the stack memory array.
virtual const Word * GetStack() const =0
Get pointer to the stack memory.

References stk::ArrayView< T >::GetSize(), GetStack(), GetStackSize(), and STK_STACK_MEMORY_FILLER.

Referenced by FrtosTask::GetStackHighWaterMark(), and osThreadGetStackSpace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTimeSlice()

virtual Timeout stk::ITask::GetTimeSlice ( ) const
inlinevirtualinherited

Get time-slice quantum of the task (ticks). KERNEL_TIMESLICE only.

Returns
Number of ticks the task may run before being rotated behind its same-priority peers, or NO_TIME_SLICE (default) to run until it blocks, yields or is preempted.
Note
Read once when the task is bound by Kernel::AddTask(). Use IKernelService::SetTimeSlice() to change it at run-time.
See also
stk::KERNEL_TIMESLICE, IKernelService::SetTimeSlice

Reimplemented in anonymous_namespace{threadx_stk.cpp}::StkThread, and anonymous_namespace{threadx_stk.cpp}::StkTimerThread.

Definition at line 937 of file stk_common.h.

937{ return NO_TIME_SLICE; }
static constexpr Timeout NO_TIME_SLICE
Time-slice quantum value: no time-slicing for the task (it runs until it blocks, yields or is preempt...
Definition stk_common.h:250

References stk::NO_TIME_SLICE.

Referenced by stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::Bind().

Here is the caller graph for this function:

◆ GetTraceName()

const char * stk::pthread::PthreadTask::GetTraceName ( ) const
inlineoverridevirtual

Get task trace name set by application.

Returns
Null-terminated name string, or NULL if unused.
Note
Used for debugging and tracing only (e.g. SEGGER SystemView). Kernel does not interpret this value.

Reimplemented from stk::ITask.

Definition at line 92 of file stk_c_pthread.cpp.

92{ return "pthread"; }

◆ GetWeight()

int32_t stk::pthread::PthreadTask::GetWeight ( ) const
inlineoverridevirtual

Get static base weight of the task.

Returns
Static weight value of the task (must be non-zero, positive 24-bit number).
See also
SwitchStrategyFixedPriority, SwitchStrategySmoothWeightedRoundRobin, IKernelTask::GetWeight, IKernelService::InheritWeight, IKernelService::RestoreWeight

Reimplemented from stk::ITask.

Definition at line 91 of file stk_c_pthread.cpp.

91{ return DEFAULT_WEIGHT; }
static constexpr Weight DEFAULT_WEIGHT
Weight value: default weight of value (1) (see SwitchStrategySmoothWeightedRoundRobin).
Definition stk_common.h:244

References stk::DEFAULT_WEIGHT.

◆ Initialize()

void stk::pthread::PthreadTask::Initialize ( pthread_stk_ctrl_t * ctrl,
stk_word_t * stack,
size_t stack_size,
EAccessMode mode )
inline

Definition at line 81 of file stk_c_pthread.cpp.

82 {
83 m_ctrl = ctrl;
84 m_stack = stack;
85 m_stack_size = stack_size;
86 m_mode = mode;
87 }

References m_ctrl, m_mode, m_stack, and m_stack_size.

Referenced by pthread_create().

Here is the caller graph for this function:

◆ OnDeadlineMissed()

virtual void stk::ITask::OnDeadlineMissed ( uint32_t duration)
inlinevirtualinherited

Called by the scheduler if deadline of the task is missed when Kernel is operating in Hard Real-Time mode (see stk::KERNEL_HRT).

Parameters
[in]durationElapsed active time in ticks at the point the deadline was detected. Always greater than the task's configured deadline (ticks).
Note
Optional handler. Use it for fault logging.
After this call returns, IPlatform::ProcessHardFault() is invoked and the system enters a safe state. This function should not attempt to recover scheduling.

Reimplemented in anonymous_namespace{threadx_stk.cpp}::StkThread, anonymous_namespace{threadx_stk.cpp}::StkTimerThread, FrtosTask, stk::time::TimerHost::TimerWorkerTask, and StkThread.

Definition at line 911 of file stk_common.h.

911{ STK_UNUSED(duration); }
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715

References STK_UNUSED.

◆ OnExit()

void stk::pthread::PthreadTask::OnExit ( )
overrideprivatevirtual

Called by the kernel before removal from the scheduling (see stk::KERNEL_DYNAMIC).

Note
The task's stack is no longer in use but the ITask object itself is still valid.
The default no-op implementation is sufficient for detached tasks. Override to implement join semantics (signal a waiting joiner).
Called in kernel/tick context - keep it short. ISR-safe primitives only (e.g. stk::sync::Semaphore::Signal(), stk::sync::EventFlags::Set()).
KERNEL_DYNAMIC only. Never called in KERNEL_STATIC mode.

Reimplemented from stk::ITask.

Definition at line 570 of file stk_c_pthread.cpp.

571{
572 // Kernel context: no blocking calls. This is the last time the kernel touches
573 // this task object; after the flag is set the control block may be recycled.
574 const CsLock cs_;
575 m_ctrl->exited = true;
576}
stk::hw::CriticalSection::ScopedLock CsLock

References m_ctrl.

◆ Run()

void stk::pthread::PthreadTask::Run ( )
overrideprivatevirtual

Entry point of the user task.

Note
Called by the Kernel when the task is scheduled for execution. Implement this method with the task's main logic.
Warning
If Kernel is configured as KERNEL_STATIC, the body must contain an infinite loop.
void Run( override)
{
while (true)
{
// task logic here
}
}
void Run() override
Entry point of the user task.

Implements stk::ITask.

Definition at line 545 of file stk_c_pthread.cpp.

546{
547 ThreadCtrl *const ctrl = m_ctrl;
548
549 hw::SetTlsPtr(static_cast<void *>(ctrl));
550
551 void *const result = ctrl->start_routine(ctrl->arg);
552
553 RunKeyDestructors(ctrl);
554
555 {
556 const CsLock cs_;
557
558 ctrl->retval = result;
559 ctrl->finished = true;
560 }
561
562 (void)ctrl->done_event->Set();
563 MaybeReap(ctrl);
564
565 // Natural return: STK tears the task down and calls OnExit(). A joiner (or the
566 // reaper, for detached threads) reclaims *our* resources (stack, control block)
567 // only after OnExit() has set ctrl->exited.
568}
static void RunKeyDestructors(ThreadCtrl *ctrl)
static void MaybeReap(ThreadCtrl *ctrl)
pthread_stk_ctrl_t ThreadCtrl
bool Set()
Set event to signaled state.
void *(* start_routine)(void *)
stk::sync::Event * done_event

References pthread_stk_ctrl_t::arg, pthread_stk_ctrl_t::done_event, pthread_stk_ctrl_t::finished, m_ctrl, stk::pthread::MaybeReap(), pthread_stk_ctrl_t::retval, stk::pthread::RunKeyDestructors(), stk::sync::Event::Set(), and pthread_stk_ctrl_t::start_routine.

Here is the call graph for this function:

◆ STK_NONCOPYABLE_CLASS()

stk::pthread::PthreadTask::STK_NONCOPYABLE_CLASS ( PthreadTask )
private

References PthreadTask().

Here is the call graph for this function:

Member Data Documentation

◆ m_ctrl

pthread_stk_ctrl_t* stk::pthread::PthreadTask::m_ctrl
private

Definition at line 104 of file stk_c_pthread.cpp.

Referenced by Initialize(), OnExit(), PthreadTask(), and Run().

◆ m_mode

EAccessMode stk::pthread::PthreadTask::m_mode
private

Definition at line 107 of file stk_c_pthread.cpp.

Referenced by GetAccessMode(), Initialize(), and PthreadTask().

◆ m_stack

stk_word_t* stk::pthread::PthreadTask::m_stack
private

Definition at line 105 of file stk_c_pthread.cpp.

Referenced by GetStack(), Initialize(), and PthreadTask().

◆ m_stack_size

size_t stk::pthread::PthreadTask::m_stack_size
private

Definition at line 106 of file stk_c_pthread.cpp.

Referenced by GetStackSize(), Initialize(), and PthreadTask().


The documentation for this class was generated from the following file: