📄 tep106.txt
字号:
============================
Schedulers and Tasks
============================
:TEP: 106
:Group: Core Working Group
:Type: Documentary
:Status: Final
:TinyOS-Version: 2.x
:Author: Philip Levis and Cory Sharp
.. Note::
This memo documents a part of TinyOS for the TinyOS Community, and
requests discussion and suggestions for improvements. Distribution
of this memo is unlimited. This memo is in full compliance with
TEP 1.
Abstract
====================================================================
This memo documents the structure and implementation of tasks
and task schedulers in TinyOS 2.x.
1. Introduction
====================================================================
TinyOS has two basic computational abstractions: asynchronous events
and tasks. Early versions of TinyOS provided a single type of task --
parameter free -- and only a FIFO scheduling policy. While changing
the latter was possible, the incorporation of tasks into the nesC
language made it very difficult. Presenting task schedulers as a
TinyOS component enables much easier customization, and allowing tasks
to be presented as an interface enables extending the classes of tasks
available. TinyOS 2.0 takes both approaches, and this memo documents
the structure of how it does so as well as a simple mechanism that
greatly increases system dependability.
2. Tasks and the Scheduler in TinyOS 1.x
====================================================================
Tasks in TinyOS are a form of deferred procedure call (DPC) [1]_, which
enable a program to defer a computation or operation until a later
time. TinyOS tasks run to completion and do not pre-empt one
another. These two constraints mean that code called from tasks
runs synchonously with respect to other tasks. Put another way, tasks
are atomic with respect to other tasks [2]_.
In TinyOS 1.x, the nesC language supports tasks through two
mechanisms, ``task`` declarations and ``post`` expressions::
task void computeTask() {
// Code here
}
and::
result_t rval = post computeTask();
TinyOS 1.x provides a single kind of task, a parameter-free function,
and a single scheduling policy, FIFO. ``post`` expressions can return
FAIL, to denote that TinyOS was unable to post the task. Tasks can be
posted multiple times. For example, if a task is posted twice in quick
succession and the first succeeds while the second fails, then the
task will be run once in the future; for this reason, even if a ``post``
fails, the task may run.
The TinyOS 1.x scheduler is implemented as a set of C functions in the
file ``sched.c``. Modifying the scheduler requires replacing or
changing this file. Additionally, as tasks are supported solely through
nesC ``task`` declarations and ``post`` expressions, which assume
a parameter-free function, modifying the syntax or capabilities of
tasks is not possible.
The task queue in TinyOS 1.x is implemented as a fixed size circular
buffer of function pointers. Posting a task puts the task's function
pointer in the next free element of the buffer; if there are no free
elements, the ``post`` returns fail. This model has several issues:
1) Some components do not have a reasonable response to a failed post
2) As a given task can be posted multiple times, it can consume more than one element in the buffer
3) All tasks from all components share a single resource: one misbehaving component can cause other's posts to fail
Fundamentally, in order for a component A to repost a task after post
failure, another component B must call a function on it (either a
command or event). E.g., component A must schedule a timer, or expect
a retry from its client. However, as many of these systems might
depend on tasks as well (e.g., timers), it is possible that an
overflowing task queue can cause the entire system to fail.
The combination of the above three issues mean that one misbehaving
component can cause TinyOS to hang. Consider, for example, this
scenario (a real and encountered problem on the Telos platform):
* A packet-based hardware radio, which issues an interrupt only when it finishes sending a packet
* A networking component that handles the interrupt to post a task to signal ``SendMsg.sendDone``.
* A sensing component that posts a task when it handles an ``ADC.dataReady`` event
* An application component that sends a packet and then sets its ADC sampling rate too high
In this scenario, the sensing component will start handling events at
a faster rate than it can process them. It will start posting tasks to
handle the data it receives, until it fills the task queue. At some
point later, the radio finishes sending a packet and signals its
interrupt. The networking component, however, is unable to post its
task that signals ``SendMsg.sendDone()``, losing the event. The
application component does not try to send another packet until it
knows the one it is sending completes (so it can re-use the
buffer). As the ``sendDone()`` event was lost, this does not occur,
and the application stops sending network traffic.
The solution to this particular problem in TinyOS 1.x is to signal
sendDone() in the radio send complete interrupt if the post fails:
this violates the sync/async boundary, but the justification is that
a *possible* rare race condition is better than *certain* failure.
Another solution would be to use an interrupt source to periodically
retry posting the task; while this does not break the sync/async
boundary, until the post succeeds the system cannot send packets.
The TinyOS 1.x model prevents it from doing any better.
3. Tasks in TinyOS 2.x
====================================================================
The semantics of tasks in TinyOS 2.x are different than those in 1.x.
This change is based on experiences with the limitations and run time
errors that the 1.x model introduces. **In TinyOS 2.x, a basic post will
only fail if and only if the task has already been posted and has not
started execution.** A task can always run, but can only have one
outstanding post at any time.
2.x achieves these semantics by allocating one
byte of state per task (the assumption is that there will be fewer than 255
tasks in the system). While a very large number of tasks could make
this overhead noticable, it is not significant in practice.
If a component needs to post a task several times, then the end of
the task logic can repost itself as need be.
For example, one can do this::
post processTask();
...
task void processTask() {
// do work
if (moreToProcess) {
post processTask();
}
}
These semantics prevent several problems, such as the inability to
signal completion of split-phase events because the task queue is
full, task queue overflow at initialization, and unfair task
allocation by components that post a task many times.
TinyOS 2.x takes the position that the basic use case of tasks should
remain simple and easy to use, but that it should be possible to
introduce new kinds of tasks beyond the basic use case. TinyOS
achieves this by keeping ``post`` and ``task`` for the basic case,
and introducing task interfaces for additional ones.
Task interfaces allow users to extend the syntax and semantics of
tasks. Generally, a task interface has an ``async`` command, post ,
and an event, ``run``. The exact signature of these functions are
up to the interface. For example, a task interface that allows a task
to take an integer parameter could look like this::
interface TaskParameter {
async error_t command postTask(uint16_t param);
event void runTask(uint16_t param);
}
Using this task interface, a component could post a task with a
``uint16_t`` parameter. When the scheduler runs the task, it will
signal the ``runTask`` event with the passed parameter, which contains
the task's logic. Note, however, that this does not save any RAM:
the scheduler must have RAM allocated for the parameter. Furthermore, as
there can only be one copy of a task outstanding at any time, it
is just as simple to store the variable in the component. E.g.,
rather than::
call TaskParameter.postTask(34);
...
event void TaskParameter.runTask(uint16_t param) {
...
}
one can::
uint16_t param;
...
param = 34;
post parameterTask();
...
task void parameterTask() {
// use param
}
The principal difference between the simplest code for these
two models is that if the component posts the task twice, it
will use the older parameter in the TaskParameter example,
while it will use the newer parameter in the basic task example.
If a component wants to use the oldest parameter, then it can do
this::
if (post myTask() == SUCCESS) {
param = 34;
}
4. The Scheduler in TinyOS 2.x
====================================================================
In TinyOS 2.x, the scheduler is a TinyOS component. Every scheduler
MUST support nesC tasks. It MAY also support any number of
additional task interfaces. The scheduler component is resonsible for
the policy of reconciling different task types (e.g., earliest
deadline first tasks vs. priority tasks).
The basic task in TinyOS 2.x is parameterless and FIFO. Tasks continue
to follow the nesC semantics of task and post, which are linguistic
shortcuts for declaring an interface and wiring it to the
scheduler component. Appendix A describes how these shortcuts can be
configured. A scheduler provides a task interface as a parameterized
interface. Every task that wires to the interface uses the unique()
function to obtain a unique identifier, which the scheduler uses to
dispatch tasks.
For example, the standard TinyOS scheduler has this signature::
module SchedulerBasicP {
provides interface Scheduler;
provides interface TaskBasic[uint8_t taskID];
uses interface McuSleep;
}
A scheduler MUST provide a parameterized TaskBasic interface.
If a call to TaskBasic.postTask() returns SUCCESS, the scheduler MUST run it
eventually, so that starvation is not a concern. The scheduler MUST
return SUCCESS to a TaskBasic.postTask()
operation unless it is not the first call to TaskBasic.postTask() since
that task's TaskBasic.runTask() event has been signaled. The
McuSleep interface is used for microcontroller power management;
its workings are explained in TEP 112 [3]_.
A scheduler MUST provide the Scheduler interface.
The Scheduler interface has commands for initialization and running
tasks, and is used by TinyOS to execute tasks::
interface Scheduler {
command void init();
command bool runNextTask(bool sleep);
command void taskLoop();
}
The init() command initializes the task queue and scheduler data
structures. runNextTask() MUST run to completion whatever task the
scheduler's policy decides is the next one: the return value indicates
whether it ran a task. The bool parameter sleep indicates what the
scheduler should do if there are no tasks to execute. If sleep is
FALSE, then the command will return immediately with FALSE as a return
value. If sleep is TRUE, then the command MUST NOT return until a task
is executed, and SHOULD put the CPU to sleep until a new task arrives.
Calls of runNextTask(FALSE) may return TRUE or FALSE; calls of
runNextTask(TRUE) always return TRUE. The taskLoop() command tells
the scheduler to enter an infinite task-running loop, putting the MCU
into a low power state when the processor is idle: it never returns.
The scheduler is repsonsible for putting the processor to sleep
predominantly for efficiency reasons. Including the sleep call
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -