Building "instant-up" real-time operating systems

Michael Dorin

May 18, 2008

Michael Dorin

Cooperative schedulers
A cooperative scheduler works on a very simple concept. Given a tick or other delay mechanism, tasks are scheduled and executed at assigned times. In our example, the scheduler maintains a table of tasks, and, as each tick occurs, the expiration times for each task are decremented. When a task's timer expires, the task is scheduled for execution and the timer is reloaded with the reload value given during task creation.

The task control structure for the example cooperative scheduler is simple. It contains a function pointer to a task and a parameter to be passed to the function when called. It also contains data regarding when the task expiration timer and reload value expire.


typedef struct
{  int (*pTask) (void * data);
   void * param;  int ready;
   int valid;  int expires;
   int reload;
   int handle;
} cooperativeTask_t;

A tasksConstructor needs to be called first; it simply clears the data for all possible tasks and prepares for new tasks to be created.

cooperative_tasksConstructor ();

A task is created using the function createTask, which loads the function pointer of the task and the data pointer into the task control structure. An initial expire time and reload value are loaded as well. Note that expire and reload times are based on ticks.


int cooperative_createTask
    (int (*pFunction)
    (void * data),
void * data, int expires,
    int reload)

Some type of timer or interrupt service routine (ISR) is necessary to execute the processTick() function on a regular basis. This step gives developers flexibility to test and simulate their implementation on a desktop. Listing 1 shows how processTick() could be implemented. The processTasks() function simply checks which tasks are scheduled, have expired, executes the tasks' functions, and deletes any task with no programmed reload value. It's even possible to have a task execute one time and be deleted. Listing 2 is an example of how processTasks() might be implemented.

View the full-size image

View the full-size image

< Previous
Page 2 of 5
Next >

Loading comments...

Most Commented

Parts Search Datasheets.com

KNOWLEDGE CENTER