Task/Thread Management

Multitasking is going to be crucial to building a robust robot. Make sure you know how to use these commands with at least some ease.

It is often said that one of the best ways to learn code is to read through examples and to play with them. See the sample code section for examples on how to use tasks and events. Download some samples and experiment with them, making sure you understand how everything works.


pid_t execi ( int (*code_start) (int, char **), int argc, char **argv, priority_t priority, size_t stack_size );
Starts a new thread of control. The new thread begins execution by calling the function pointed to by code_start

argc and argv are used for passing parameters to the function code_start, but you most likely will not need them, so just leave them both as 0 for now.
Parameters: code_start - a function pointer for where the thread should begin execution
priority the priority of the new thread. Should be in between PRIO_LOWEST and PRIO_HIGHEST. Typically, set this value to PRIO_NORMAL plus or minus one or two.
stack_size unless you are an elite h4X0r, leave this as DEFAULT_STACK_SIZE

returns: the pid of the new thread

void exit ( int code );
causes the current task to halt execution and terminate, returning code. This will also free any memory allocated by the task.

void kill ( pid_t pid );
Kill task associated with pid as assigned when it was started by execi()

void killall ( priority_t prio );
Kills all tasks with priority less than prio

wakeup_t wait_event ( wakeup_t (*wakeup) (wakeup_t), wakeup_t data );
Suspends the current task until a call to the wakeup function wakeup returns a non-null value. This is confusing if you are not familiar with function pointers, so see the example on how to use it.

void yield ( );
Yields the rest of the current task's timeslice.

void systime_set_timeslice ( unsigned char slice );
#include <sys/time.h>
Sets the multitasking timeslice in ms. Minimum value of 5 ms. The default value is 20 ms.