User`s manual

62 digi.com Multitasking with Dynamic C
5.4.5 Firsttime Functions
In a function definition, the keyword firsttime causes the function to have an implicit first parameter:
a pointer to the CoData structure of the costatement that calls it. User-defined
firsttime functions are
allowed.
The following firsttime functions are defined in COSTATE.LIB.
DelayMs(), DelaySec(), DelayTicks()
IntervalMs(), IntervalSec(), IntervalTick()
For more information see the Dynamic C Function Reference Manual. These functions should be called
inside a waitfor statement because they do not yield while waiting for the desired time to elapse, but
instead return 0 to indicate that the desired time has not yet elapsed.
5.4.6 Shared Global Variables
The variables SEC_TIMER, MS_TIMER and TICK_TIMER are shared, making them atomic when being
updated. They are defined and initialized in VDRIVER.LIB. They are updated by the periodic interrupt
and are used by firsttime functions. They should not be modified by an application program. Costate-
ments and cofunctions depend on these timer variables being valid for use in waitfor statements that
call functions that read them. For example, the following statement will access SEC_TIMER.
waitfor(DelaySec(3));
5.5 Cofunctions
Cofunctions, like costatements, are used to implement cooperative multitasking. But, unlike costatements,
they have a form similar to functions in that arguments can be passed to them and a value can be returned
(but not a structure).
The default storage class for a cofunction’s variables is Instance. An instance variable behaves like
a static variable, i.e., its value persists between function calls. Each instance of an Indexed Cofunction
has its own set of instance variables
. The compiler directive #class does not change the default storage
class for a cofunction’s variables.
All cofunctions in the program are initialized when the function chain _GLOBAL_INIT is called. This
call is made by premain.
5.5.1 Cofunction Syntax
A cofunction definition is similar to the definition of a C function.
cofunc|scofunc type [name][[dim]]([type arg1, ..., type argN])
{[ statement | yield; | abort; | waitfor(expression);]... }
cofunc, scofunc
The keywords cofunc or scofunc (a single-user cofunction) identify the statements enclosed in curly
braces that follow as a cofunction.