ZestCode
 
Loading...
Searching...
No Matches
RTOS Facilities C API

Files

file  rtos.h
 

Macros

#define CURRENT_TASK   ((task_t)NULL)
 

Functions

uint32_t millis (void)
 
uint64_t micros (void)
 
task_t task_create (task_fn_t function, void *const parameters, uint32_t prio, const uint16_t stack_depth, const char *const name)
 
void task_delete (task_t task)
 
void task_delay (const uint32_t milliseconds)
 
void delay (const uint32_t milliseconds)
 
void task_delay_until (uint32_t *const prev_time, const uint32_t delta)
 
uint32_t task_get_priority (task_t task)
 
void task_set_priority (task_t task, uint32_t prio)
 
task_state_e_t task_get_state (task_t task)
 
void task_suspend (task_t task)
 
void task_resume (task_t task)
 
uint32_t task_get_count (void)
 
char * task_get_name (task_t task)
 
task_t task_get_by_name (const char *name)
 
task_t task_get_current ()
 
uint32_t task_notify (task_t task)
 
void task_join (task_t task)
 
uint32_t task_notify_ext (task_t task, uint32_t value, notify_action_e_t action, uint32_t *prev_value)
 
uint32_t task_notify_take (bool clear_on_exit, uint32_t timeout)
 
bool task_notify_clear (task_t task)
 
mutex_t mutex_create (void)
 
bool mutex_take (mutex_t mutex, uint32_t timeout)
 
bool mutex_give (mutex_t mutex)
 
mutex_t mutex_recursive_create (void)
 
bool mutex_recursive_take (mutex_t mutex, uint32_t timeout)
 
bool mutex_recursive_give (mutex_t mutex)
 
void mutex_delete (mutex_t mutex)
 

Enumerations

enum  task_state_e_t {
  E_TASK_STATE_RUNNING = 0 , E_TASK_STATE_READY , E_TASK_STATE_BLOCKED , E_TASK_STATE_SUSPENDED ,
  E_TASK_STATE_DELETED , E_TASK_STATE_INVALID
}
 
enum  notify_action_e_t {
  E_NOTIFY_ACTION_NONE , E_NOTIFY_ACTION_BITS , E_NOTIFY_ACTION_INCR , E_NOTIFY_ACTION_OWRITE ,
  E_NOTIFY_ACTION_NO_OWRITE
}
 

Typedefs

typedef void * task_t
 
typedef void(* task_fn_t) (void *)
 
typedef void * mutex_t
 

Macros

#define TASK_PRIORITY_MAX   16
 
#define TASK_PRIORITY_MIN   1
 
#define TASK_PRIORITY_DEFAULT   8
 
#define TASK_STACK_DEPTH_DEFAULT   0x2000
 
#define TASK_STACK_DEPTH_MIN   0x200
 
#define TASK_NAME_MAX_LEN   32
 
#define TIMEOUT_MAX   ((uint32_t)0xffffffffUL)
 

Detailed Description

Note
Additional example code for this module can be found in its Tutorial.

Macro Definition Documentation

◆ CURRENT_TASK

#define CURRENT_TASK   ((task_t)NULL)

The task handle of the currently running task.

◆ TASK_NAME_MAX_LEN

#define TASK_NAME_MAX_LEN   32

The maximum number of characters allowed in a task's name.

◆ TASK_PRIORITY_DEFAULT

#define TASK_PRIORITY_DEFAULT   8

The default task priority, which should be used for most tasks unless you have a specific need for a higher or lower priority task.

The default tasks, such as autonomous(), are run with this priority

◆ TASK_PRIORITY_MAX

#define TASK_PRIORITY_MAX   16

The highest priority that can be assigned to a task.

A task with this priority will always run if it is available to. Beware of deadlocks when using this priority.

◆ TASK_PRIORITY_MIN

#define TASK_PRIORITY_MIN   1

The lowest priority that can be assigned to a task.

This can cause severe performance problems and is generally not recommended that users use this priority.

◆ TASK_STACK_DEPTH_DEFAULT

#define TASK_STACK_DEPTH_DEFAULT   0x2000

The recommended stack size for a new task.

This stack size is used for the default tasks such as autonomous(). This size is 8,192 words, or 32,768 bytes. This should be enough for the majority of tasks

◆ TASK_STACK_DEPTH_MIN

#define TASK_STACK_DEPTH_MIN   0x200

The minimal stack size for a task.

This equates to 512 words, or 2,048 bytes.

◆ TIMEOUT_MAX

#define TIMEOUT_MAX   ((uint32_t)0xffffffffUL)

The maximum timeout value that can be given to, for instance, a mutex grab.

Typedef Documentation

◆ mutex_t

typedef void* mutex_t

A mutex.

A mutex is a synchronization object that can be used to protect a shared resource from being accessed by multiple tasks at the same time. A mutex can be claimed by a task, which will prevent other tasks from claiming it until that task releases it.

◆ task_fn_t

typedef void(* task_fn_t) (void *)

A pointer to a task's function.

Such a function is called when a task starts, and exiting said function will terminate the task.

◆ task_t

typedef void* task_t

An opaque type that pontis to a task handle. This is used for referencing a task.

Enumeration Type Documentation

◆ notify_action_e_t

brief The action to take when a task is notified.

Enumerator
E_NOTIFY_ACTION_NONE 

The task’s notification value will not be touched.

E_NOTIFY_ACTION_BITS 

The task’s notification value will be bitwise ORed with the new value.

E_NOTIFY_ACTION_INCR 

The task’s notification value will be incremented by one, effectively using it as a notification counter.

E_NOTIFY_ACTION_OWRITE 

The task’s notification value will be unconditionally set to the new value.

E_NOTIFY_ACTION_NO_OWRITE 

The task’s notification value will be set to the new value if the task does not already have a pending notification.

◆ task_state_e_t

The state of a task.

Enumerator
E_TASK_STATE_RUNNING 

The task is actively executing.

E_TASK_STATE_READY 

The task exists and is available to run, but is not currently running.

E_TASK_STATE_BLOCKED 

The task is delayed or blocked by a mutex, semaphore, or I/O operation.

E_TASK_STATE_SUSPENDED 

The task is supended using task_suspend.

E_TASK_STATE_DELETED 

The task has been deleted using task_delete.

E_TASK_STATE_INVALID 

The task handle does not point to a current or past task.

Function Documentation

◆ delay()

void delay ( const uint32_t milliseconds)

Delays the current task for a given number of milliseconds.

This is not the best method to have a task execute code at predefined intervals, as the delay time is measured from when the delay is requested. To delay cyclically, use task_delay_until().

Parameters
millisecondsThe number of milliseconds to wait (1000 milliseconds per second)

Example

◆ micros()

uint64_t micros ( void )

Gets the number of microseconds since PROS initialized,

Returns
The number of microseconds since PROS initialized

Example

◆ millis()

uint32_t millis ( void )

Gets the number of milliseconds since PROS initialized.

Returns
The number of milliseconds since PROS initialized

Example

◆ mutex_create()

mutex_t mutex_create ( void )

Creates a mutex.

See https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes for details.

Returns
A handle to a newly created mutex. If an error occurred, NULL will be returned and errno can be checked for hints as to why mutex_create failed.

Example

◆ mutex_delete()

void mutex_delete ( mutex_t mutex)

Deletes a mutex or recursive mutex

Parameters
mutexMutex to unlock.

Example

◆ mutex_give()

bool mutex_give ( mutex_t mutex)

Unlocks a mutex.

See https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes for details.

Parameters
mutexMutex to unlock.
Returns
True if the mutex was successfully returned, false otherwise. If false is returned, then errno is set with a hint about why the mutex couldn't be returned.

Example

◆ mutex_recursive_create()

mutex_t mutex_recursive_create ( void )

Creates a recursive mutex which can be locked recursively by the owner.

Returns
A newly created recursive mutex.

Example:

◆ mutex_recursive_give()

bool mutex_recursive_give ( mutex_t mutex)

Gives a recursive mutex.

Parameters
mutexA mutex handle created by mutex_recursive_create
Returns
1 if the mutex was obtained, 0 otherwise

Example:

◆ mutex_recursive_take()

bool mutex_recursive_take ( mutex_t mutex,
uint32_t timeout )

Takes a recursive mutex.

Parameters
mutexA mutex handle created by mutex_recursive_create
wait_timeAmount of time to wait before timing out
Returns
1 if the mutex was obtained, 0 otherwise

Example:

◆ mutex_take()

bool mutex_take ( mutex_t mutex,
uint32_t timeout )

Takes and locks a mutex, waiting for up to a certain number of milliseconds before timing out.

See https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes for details.

Parameters
mutexMutex to attempt to lock.
timeoutTime to wait before the mutex becomes available. A timeout of 0 can be used to poll the mutex. TIMEOUT_MAX can be used to block indefinitely.
Returns
True if the mutex was successfully taken, false otherwise. If false is returned, then errno is set with a hint about why the the mutex couldn't be taken.

Example

◆ task_create()

task_t task_create ( task_fn_t function,
void *const parameters,
uint32_t prio,
const uint16_t stack_depth,
const char *const name )

Creates a new task and add it to the list of tasks that are ready to run.

This function uses the following values of errno when an error state is reached: ENOMEM - The stack cannot be used as the TCB was not created.

Parameters
functionPointer to the task entry function
parametersPointer to memory that will be used as a parameter for the task being created. This memory should not typically come from stack, but rather from dynamically (i.e., malloc'd) or statically allocated memory.
prioThe priority at which the task should run. TASK_PRIO_DEFAULT plus/minus 1 or 2 is typically used.
stack_depthThe number of words (i.e. 4 * stack_depth) available on the task's stack. TASK_STACK_DEPTH_DEFAULT is typically sufficienct.
nameA descriptive name for the task. This is mainly used to facilitate debugging. The name may be up to 32 characters long.
Returns
A handle by which the newly created task can be referenced. If an error occurred, NULL will be returned and errno can be checked for hints as to why task_create failed.

Example

◆ task_delay()

void task_delay ( const uint32_t milliseconds)

Delays the current task for a given number of milliseconds.

This is not the best method to have a task execute code at predefined intervals, as the delay time is measured from when the delay is requested. To delay cyclically, use task_delay_until().

Parameters
millisecondsThe number of milliseconds to wait (1000 milliseconds per second)

Example

◆ task_delay_until()

void task_delay_until ( uint32_t *const prev_time,
const uint32_t delta )

Delays the current task until a specified time. This function can be used by periodic tasks to ensure a constant execution frequency.

The task will be woken up at the time *prev_time + delta, and *prev_time will be updated to reflect the time at which the task will unblock.

Parameters
prev_timeA pointer to the location storing the setpoint time. This should typically be initialized to the return value of millis().
deltaThe number of milliseconds to wait (1000 milliseconds per second)

Example

◆ task_delete()

void task_delete ( task_t task)

Removes a task from the RTOS real time kernel's management. The task being deleted will be removed from all ready, blocked, suspended and event lists.

Memory dynamically allocated by the task is not automatically freed, and should be freed before the task is deleted.

Parameters
taskThe handle of the task to be deleted. Passing NULL will cause the calling task to be deleted.

Example

◆ task_get_by_name()

task_t task_get_by_name ( const char * name)

Gets a task handle from the specified name

The operation takes a relatively long time and should be used sparingly.

Parameters
nameThe name to query
Returns
A task handle with a matching name, or NULL if none were found.

Example

◆ task_get_count()

uint32_t task_get_count ( void )

Gets the number of tasks the kernel is currently managing, including all ready, blocked, or suspended tasks. A task that has been deleted, but not yet reaped by the idle task will also be included in the count. Tasks recently created may take one context switch to be counted.

Returns
The number of tasks that are currently being managed by the kernel.

Example

◆ task_get_current()

task_t task_get_current ( )

Get the currently running task handle. This could be useful if a task wants to tell another task about itself.

Returns
The currently running task handle.

Example

◆ task_get_name()

char * task_get_name ( task_t task)

Gets the name of the specified task.

Parameters
taskThe task to check
Returns
A pointer to the name of the task

Example

◆ task_get_priority()

uint32_t task_get_priority ( task_t task)

Gets the priority of the specified task.

Parameters
taskThe task to check
Returns
The priority of the task

Example

◆ task_get_state()

task_state_e_t task_get_state ( task_t task)

Gets the state of the specified task.

Parameters
taskThe task to check
Returns
The state of the task

Example

◆ task_join()

void task_join ( task_t task)

Utilizes task notifications to wait until specified task is complete and deleted, then continues to execute the program. Analogous to std::thread::join in C++.

Parameters
taskThe handle of the task to wait on.
Returns
void

Example

◆ task_notify()

uint32_t task_notify ( task_t task)

Sends a simple notification to task and increments the notification counter.

Parameters
taskThe task to notify
Returns
Always returns true.

Example

◆ task_notify_clear()

bool task_notify_clear ( task_t task)

Clears the notification for a task.

See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for details.

Parameters
taskThe task to clear
Returns
False if there was not a notification waiting, true if there was

Example

◆ task_notify_ext()

uint32_t task_notify_ext ( task_t task,
uint32_t value,
notify_action_e_t action,
uint32_t * prev_value )

Sends a notification to a task, optionally performing some action. Will also retrieve the value of the notification in the target task before modifying the notification value.

Parameters
taskThe task to notify
valueThe value used in performing the action
actionAn action to optionally perform on the receiving task's notification value
prev_valueA pointer to store the previous value of the target task's notification, may be NULL
Returns
Dependent on the notification action. For NOTIFY_ACTION_NO_WRITE: return 0 if the value could be written without needing to overwrite, 1 otherwise. For all other NOTIFY_ACTION values: always return 0

Example

◆ task_notify_take()

uint32_t task_notify_take ( bool clear_on_exit,
uint32_t timeout )

Waits for a notification to be nonzero.

See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for details.

Parameters
clear_on_exitIf true (1), then the notification value is cleared. If false (0), then the notification value is decremented.
timeoutSpecifies the amount of time to be spent waiting for a notification to occur.
Returns
The value of the task's notification value before it is decremented or cleared

Example

◆ task_resume()

void task_resume ( task_t task)

Resumes the specified task, making it eligible to be scheduled.

Parameters
taskThe task to resume

Example

◆ task_set_priority()

void task_set_priority ( task_t task,
uint32_t prio )

Sets the priority of the specified task.

If the specified task's state is available to be scheduled (e.g. not blocked) and new priority is higher than the currently running task, a context switch may occur.

Parameters
taskThe task to set
prioThe new priority of the task

Example

◆ task_suspend()

void task_suspend ( task_t task)

Suspends the specified task, making it ineligible to be scheduled.

Parameters
taskThe task to suspend

Example