Tasks
4-46
4.4.6 Task Yielding for Time-Slice Scheduling
Example 4-7 demonstrates an implementation of a time-slicing scheduling
model that can be managed by a user. This model is preemptive and does
not require any cooperation (which is, code) by the tasks. The tasks are
programmed as if they were the only thread running. Although DSP/BIOS
tasks of differing priorities can exist in any given application, the time-slicing
model only applies to tasks of equal priority.
In this example, the prd0 PRD object is configured to run a simple function
that calls the TSK_yield() function every one millisecond. The prd1 PRD
object is configured to run a simple function that calls the SEM_post(&sem)
function every 16 milliseconds.
Figure 4-12 shows the trace resulting from Example 4-7,.
Example 4-7. Time-Slice Scheduling
/*
* ======== slice.c ========
* This example utilizes time-slice scheduling among three
* tasks of equal priority. A fourth task of higher
* priority periodically preempts execution.
*
* A PRD object drives the time-slice scheduling. Every
* millisecond, the PRD object calls TSK_yield()
* which forces the current task to relinquish access to
* to the CPU. The time slicing could also be driven by
* a CLK object (as long as the time slice was the same interval
* as the clock interrupt), or by another hardware
* interrupt.
*
* The time-slice scheduling is best viewed in the Execution
* Graph with SWI logging and PRD logging turned off.
*
* Because a task is always ready to run, this program
* does not spend time in the idle loop. Calls to IDL_run()
* are added to force the update of the Real-Time Analysis
* tools. Calls to IDL_run() are within a TSK_disable(),
* TSK_enable() block because the call to IDL_run()
* is not reentrant.
*/
#include <std.h>
#include <clk.h>
#include <idl.h>
#include <log.h>
#include <sem.h>
#include <swi.h>
#include <tsk.h>
#include "slicecfg.h"
Void task(Arg id_arg);
Void hi_pri_task(Arg id_arg);
Uns counts_per_us; /* hardware timer counts per microsecond */