VSF Documented
vsf_thread.c File Reference
#include "../vsf_kernel_cfg.h"
#include "../vsf_kernel_common.h"
#include "./vsf_thread.h"
#include "../vsf_os.h"

Macros

#define __VSF_THREAD_CLASS_IMPLEMENT
 
#define __VSF_EDA_CLASS_INHERIT__
 

Functions

vsf_evtq_t__vsf_os_evtq_get (vsf_prio_t priority)
 
vsf_thread_t * vsf_thread_get_cur (void)
 Get the currently running thread.
 
VSF_CAL_NO_RETURN void vsf_thread_exit (void)
 Terminate the current thread.
 
void __vsf_eda_return_to_thread (vsf_eda_t *eda)
 
vsf_evt_t vsf_thread_wait (void)
 Wait for any event inside a thread.
 
void vsf_thread_wait_for_evt (vsf_evt_t evt)
 Wait for a specific event inside a thread.
 
uintptr_t vsf_thread_wait_for_evt_msg (vsf_evt_t evt)
 Wait for an event carrying a message inside a thread.
 
uintptr_t vsf_thread_wait_for_msg (void)
 Wait for a message inside a thread.
 
void vsf_thread_sendevt (vsf_thread_t *thread_obj, vsf_evt_t evt)
 Post an event to a thread.
 
void __vsf_thread_host_thread (void *arg)
 
void vsf_thread_stack_check (void)
 Check whether the current stack pointer is inside the stack range of the current thread.
 
vsf_err_t vsf_thread_start (vsf_thread_t *pthis, vsf_thread_cb_t *thread_cb, vsf_prio_t priority)
 Start a thread.
 
vsf_err_t vk_eda_call_thread_prepare (vsf_thread_cb_t *pthis, vsf_thread_prepare_cfg_t *cfg)
 Prepare a thread control block before an eda calls a thread.
 
vsf_err_t vk_eda_call_thread (vsf_thread_cb_t *thread_cb)
 Call a prepared thread from the current eda and wait for its completion.
 
vsf_err_t vk_thread_call_eda (uintptr_t eda_handler, uintptr_t param, size_t local_size, size_t local_buff_size, uintptr_t local_buff)
 Call an eda (event-driven task) from the current thread and wait for its completion.
 
vsf_err_t vk_thread_call_thread (vsf_thread_cb_t *pthis, vsf_thread_prepare_cfg_t *cfg)
 Call a thread from the current thread and wait for its completion.
 
fsm_rt_t vk_thread_call_task (vsf_task_entry_t task_handler, uintptr_t param, size_t local_size)
 Call a task from the current thread and wait for its completion.
 
void vsf_thread_delay (vsf_systimer_tick_t tick)
 Delay (sleep) the current thread for the given ticks.
 
void vsf_thread_yield (void)
 Yield the CPU voluntarily to other tasks of the same priority.
 
vsf_prio_t vsf_thread_set_priority (vsf_prio_t priority)
 Set the priority of the current thread dynamically.
 
void vsf_thread_signal (vsf_thread_t *thread, int sig)
 Send a POSIX-like signal to a thread.
 
vsf_sync_reason_t __vsf_thread_wait_for_sync (vsf_sync_t *sync, vsf_timeout_tick_t time_out)
 Wait for a sync object (semaphore, trigger or mutex) inside a thread.
 
vsf_err_t vsf_thread_mutex_leave (vsf_mutex_t *mtx)
 Leave (release) a mutex inside a thread.
 
vsf_sync_reason_t vsf_thread_queue_send (vsf_eda_queue_t *queue, void *node, vsf_timeout_tick_t timeout)
 Send a node to a queue inside a thread.
 
vsf_sync_reason_t vsf_thread_queue_recv (vsf_eda_queue_t *queue, void **node, vsf_timeout_tick_t timeout)
 Receive a node from a queue inside a thread.
 
vsf_sync_reason_t vsf_thread_bmpevt_pend (vsf_bmpevt_t *bmpevt, vsf_bmpevt_pender_t *pender, vsf_timeout_tick_t timeout)
 Pend on a bitmap event inside a thread.
 

Macro Definition Documentation

◆ __VSF_THREAD_CLASS_IMPLEMENT

#define __VSF_THREAD_CLASS_IMPLEMENT

◆ __VSF_EDA_CLASS_INHERIT__

#define __VSF_EDA_CLASS_INHERIT__

Function Documentation

◆ __vsf_os_evtq_get()

vsf_evtq_t * __vsf_os_evtq_get ( vsf_prio_t priority)
extern

◆ vsf_thread_get_cur()

vsf_thread_t * vsf_thread_get_cur ( void )

Get the currently running thread.

Returns
vsf_thread_t *: a pointer to the current thread vsf_thread_t
Note
Must be called in thread context.

◆ vsf_thread_exit()

VSF_CAL_NO_RETURN void vsf_thread_exit ( void )

Terminate the current thread.

Returns
none
Note
This function does not return. If the current thread was called as a sub-call, the caller is resumed.
Must be called in thread context.

◆ __vsf_eda_return_to_thread()

void __vsf_eda_return_to_thread ( vsf_eda_t * eda)

◆ vsf_thread_wait()

vsf_evt_t vsf_thread_wait ( void )

Wait for any event inside a thread.

Returns
vsf_evt_t: the event received
Note
Must be called in thread context; the current thread is blocked until the operation completes.

◆ vsf_thread_wait_for_evt()

void vsf_thread_wait_for_evt ( vsf_evt_t evt)

Wait for a specific event inside a thread.

Parameters
[in]evtthe event to wait for, of type vsf_evt_t
Returns
none
Note
The current thread is blocked until the next event arrives; in debug builds an assertion fires if that event is not evt.
Must be called in thread context; the current thread is blocked until the operation completes.
Note
make sure there is no message ignored

◆ vsf_thread_wait_for_evt_msg()

uintptr_t vsf_thread_wait_for_evt_msg ( vsf_evt_t evt)

Wait for an event carrying a message inside a thread.

Parameters
[in]evtthe event to wait for, of type vsf_evt_t
Returns
uintptr_t: the message carried by the event
Note
Only available when VSF_KERNEL_CFG_SUPPORT_EVT_MESSAGE is ENABLED.
Must be called in thread context; the current thread is blocked until the operation completes.

◆ vsf_thread_wait_for_msg()

uintptr_t vsf_thread_wait_for_msg ( void )

Wait for a message inside a thread.

Returns
uintptr_t: the message received
Note
The current thread is blocked until a message arrives.
Must be called in thread context; the current thread is blocked until the operation completes.

◆ vsf_thread_sendevt()

void vsf_thread_sendevt ( vsf_thread_t * thread,
vsf_evt_t evt )

Post an event to a thread.

Parameters
[in]threada pointer to structure vsf_thread_t
[in]evtthe event to post, of type vsf_evt_t
Returns
none
Note
Can be called in task or interrupt context (it is a plain wrapper of vsf_eda_post_evt()).

◆ __vsf_thread_host_thread()

void __vsf_thread_host_thread ( void * arg)

◆ vsf_thread_stack_check()

void vsf_thread_stack_check ( void )

Check whether the current stack pointer is inside the stack range of the current thread.

Returns
none
Note
Only available when VSF_KERNEL_CFG_THREAD_STACK_CHECK is ENABLED; assertion fails on stack overflow. It is a no-op in host-thread mode (VSF_KERNEL_THREAD_USE_HOST).
Must be called in task context, NOT in interrupt context (in interrupt context use the corresponding _isr API if available).

◆ vsf_thread_start()

vsf_err_t vsf_thread_start ( vsf_thread_t * thread,
vsf_thread_cb_t * thread_cb,
vsf_prio_t priority )

Start a thread.

Parameters
[in]threada pointer to structure vsf_thread_t
[in]thread_cba pointer to the thread control block vsf_thread_cb_t, whose entry, stack and stack_size members must be initialized
[in]prioritypriority of the thread
Returns
vsf_err_t: VSF_ERR_NONE if the thread is started successfully, VSF_ERR_PROVIDED_RESOURCE_NOT_SUFFICIENT if the stack is too small, VSF_ERR_PROVIDED_RESOURCE_NOT_ALIGNED if the stack size is not page-aligned, VSF_ERR_NOT_ENOUGH_RESOURCES if no frame/event node is available to start the task
Note
Must be called in task context, NOT in interrupt context (in interrupt context use the corresponding _isr API if available).

◆ vk_eda_call_thread_prepare()

vsf_err_t vk_eda_call_thread_prepare ( vsf_thread_cb_t * thread_cb,
vsf_thread_prepare_cfg_t * cfg )

Prepare a thread control block before an eda calls a thread.

Parameters
[in]thread_cba pointer to the thread control block vsf_thread_cb_t
[in]cfga pointer to the prepare configuration vsf_thread_prepare_cfg_t, including entry, stack and stack_size
Returns
vsf_err_t: VSF_ERR_NONE if the preparation is successful
Note
Call vk_eda_call_thread after the preparation to run the thread and wait for its completion.
Must be called in eda task context (e.g. from an eda, pt or task event handler); the caller waits for the called thread to complete.

◆ vk_eda_call_thread()

vsf_err_t vk_eda_call_thread ( vsf_thread_cb_t * thread_cb)

Call a prepared thread from the current eda and wait for its completion.

Parameters
[in]thread_cba pointer to the thread control block vsf_thread_cb_t, prepared by vk_eda_call_thread_prepare
Returns
vsf_err_t: VSF_ERR_NONE if the call is started successfully; VSF_ERR_NOT_ENOUGH_RESOURCES if no frame is available
Note
Must be called in eda task context (e.g. from an eda, pt or task event handler); the caller waits for the called thread to complete.

◆ vk_thread_call_eda()

vsf_err_t vk_thread_call_eda ( uintptr_t eda_handler,
uintptr_t param,
size_t local_size,
size_t local_buff_size,
uintptr_t local_buff )

Call an eda (event-driven task) from the current thread and wait for its completion.

Parameters
[in]eda_handlerentry handler of the target eda
[in]paramparameter passed to the target eda
[in]local_sizesize of the local variables of the target eda
[in]local_buff_sizesize of the buffer used to initialize the local variables
[in]local_buffbuffer used to initialize the local variables, NULL if not needed
Returns
vsf_err_t: always VSF_ERR_NONE (frame exhaustion triggers an assertion in debug builds)
Note
Must be called in thread context; the current thread is blocked until the called task completes.

◆ vk_thread_call_thread()

vsf_err_t vk_thread_call_thread ( vsf_thread_cb_t * thread_cb,
vsf_thread_prepare_cfg_t * cfg )

Call a thread from the current thread and wait for its completion.

Parameters
[in]thread_cba pointer to the thread control block vsf_thread_cb_t of the target thread
[in]cfga pointer to the prepare configuration vsf_thread_prepare_cfg_t, including entry, stack and stack_size
Returns
vsf_err_t: always VSF_ERR_NONE (frame exhaustion triggers an assertion in debug builds)
Note
Must be called in thread context; the current thread is blocked until the called thread completes.

◆ vk_thread_call_task()

fsm_rt_t vk_thread_call_task ( vsf_task_entry_t task_handler,
uintptr_t param,
size_t local_size )

Call a task from the current thread and wait for its completion.

Parameters
[in]task_handlerentry handler of the target task, of type vsf_task_entry_t
[in]paramparameter passed to the target task
[in]local_sizesize of the local variables of the target task
Returns
fsm_rt_t: the final state of the called task
Note
Only available when VSF_KERNEL_CFG_EDA_SUPPORT_TASK and VSF_KERNEL_CFG_EDA_SUBCALL_HAS_RETURN_VALUE are ENABLED.
Must be called in thread context; the current thread is blocked until the called task completes.

◆ vsf_thread_delay()

void vsf_thread_delay ( vsf_systimer_tick_t tick)

Delay (sleep) the current thread for the given ticks.

Parameters
[in]tickdelay time in system ticks
Returns
none
Note
Only available when VSF_KERNEL_CFG_EDA_SUPPORT_TIMER is ENABLED.
Must be called in thread context; the current thread is blocked until the operation completes.

◆ vsf_thread_yield()

void vsf_thread_yield ( void )

Yield the CPU voluntarily to other tasks of the same priority.

Returns
none
Note
The current thread is blocked until it is scheduled again.
Must be called in thread context.

◆ vsf_thread_set_priority()

vsf_prio_t vsf_thread_set_priority ( vsf_prio_t priority)

Set the priority of the current thread dynamically.

Parameters
[in]prioritynew priority of the current thread
Returns
vsf_prio_t: the original priority before the change
Note
Only available when VSF_KERNEL_CFG_SUPPORT_DYNAMIC_PRIOTIRY is ENABLED.
If the new priority differs from the original one, the current thread yields after the change.
Must be called in thread context.

◆ vsf_thread_signal()

void vsf_thread_signal ( vsf_thread_t * thread,
int sig )

Send a POSIX-like signal to a thread.

Parameters
[in]threada pointer to structure vsf_thread_t
[in]sigsignal number
Returns
none
Note
Only available when VSF_KERNEL_CFG_THREAD_SIGNAL is ENABLED.
The sighandler member of the target thread is called when the thread processes the signal; if the thread is currently running, has no pending events (blocked in a wait), or is in a sub-call, the signal is recorded and handled later.
Must be called in task context, NOT in interrupt context (in interrupt context use the corresponding _isr API if available).

◆ __vsf_thread_wait_for_sync()

vsf_sync_reason_t __vsf_thread_wait_for_sync ( vsf_sync_t * sync,
vsf_timeout_tick_t time_out )

Wait for a sync object (semaphore, trigger or mutex) inside a thread.

Parameters
[in]synca pointer to structure vsf_sync_t
[in]time_outtimeout in ticks, negative to wait forever, 0 to try once
Returns
vsf_sync_reason_t: VSF_SYNC_GET if the sync object is obtained, VSF_SYNC_TIMEOUT if timed out, VSF_SYNC_CANCEL if cancelled, VSF_SYNC_FAIL on error
Note
Internal use only. Use vsf_thread_sem_pend, vsf_thread_trig_pend or vsf_thread_mutex_enter instead.
Must be called in thread context; the current thread is blocked until the operation completes.
Note
there is a VSF_ASSERT() in __vsf_eda_sync_get_reason, which ! validated the evt value. Hence, there is no need to assert ! the evt value here.

◆ vsf_thread_mutex_leave()

vsf_err_t vsf_thread_mutex_leave ( vsf_mutex_t * mtx)

Leave (release) a mutex inside a thread.

Parameters
[in]mtxa pointer to structure vsf_mutex_t
Returns
vsf_err_t: VSF_ERR_NONE if successful; VSF_ERR_OVERRUN if the mutex is not owned
Note
Must be called in task context, NOT in interrupt context (in interrupt context use the corresponding _isr API if available).

◆ vsf_thread_queue_send()

vsf_sync_reason_t vsf_thread_queue_send ( vsf_eda_queue_t * queue,
void * node,
vsf_timeout_tick_t timeout )

Send a node to a queue inside a thread.

Parameters
[in]queuea pointer to structure vsf_eda_queue_t
[in]nodea pointer to the node to send
[in]timeouttimeout in ticks; negative to wait forever, 0 to try once, a positive value to wait up to the given ticks
Returns
vsf_sync_reason_t: VSF_SYNC_GET if the node is sent, VSF_SYNC_TIMEOUT if timed out, VSF_SYNC_CANCEL if cancelled
Note
The current thread is blocked while waiting.
Must be called in thread context; the current thread is blocked until the operation completes.

◆ vsf_thread_queue_recv()

vsf_sync_reason_t vsf_thread_queue_recv ( vsf_eda_queue_t * queue,
void ** node,
vsf_timeout_tick_t timeout )

Receive a node from a queue inside a thread.

Parameters
[in]queuea pointer to structure vsf_eda_queue_t
[out]nodea pointer to receive the dequeued node
[in]timeouttimeout in ticks; negative to wait forever, 0 to try once, a positive value to wait up to the given ticks
Returns
vsf_sync_reason_t: VSF_SYNC_GET if a node is received, VSF_SYNC_TIMEOUT if timed out, VSF_SYNC_CANCEL if cancelled
Note
The current thread is blocked while waiting.
Must be called in thread context; the current thread is blocked until the operation completes.

◆ vsf_thread_bmpevt_pend()

vsf_sync_reason_t vsf_thread_bmpevt_pend ( vsf_bmpevt_t * bmpevt,
vsf_bmpevt_pender_t * pender,
vsf_timeout_tick_t timeout )

Pend on a bitmap event inside a thread.

Parameters
[in]bmpevta pointer to structure vsf_bmpevt_t
[in]pendera pointer to structure vsf_bmpevt_pender_t describing the bits to wait for
[in]timeouttimeout in ticks; negative to wait forever, 0 to try once, a positive value to wait up to the given ticks
Returns
vsf_sync_reason_t: VSF_SYNC_GET if the expected bits are set, VSF_SYNC_TIMEOUT if timed out, VSF_SYNC_CANCEL if cancelled, VSF_SYNC_FAIL on error
Note
The current thread is blocked while waiting. Only available when VSF_KERNEL_CFG_SUPPORT_BITMAP_EVENT is ENABLED.
Must be called in thread context; the current thread is blocked until the operation completes.
Note
there is a VSF_ASSERT() in vsf_eda_bmpevt_poll, which ! validated the evt value. Hence, there is no need to assert ! the evt value here.
Generated from commit: vsfteam/vsf@a5104db