Next: Cilk, Previous: Multitasker, Up: Multitasker [Contents][Index]
Tasks can be created with newtask
or newtask4
with a
given amount of stack space (either all the same or each stack’s size
specified); these tasks neet to be activate
d or send an
xt through initiate
. Tasks can stop
themselves
when they are done or wait for new instructions.
newtask
( stacksize – task ) gforth-experimental “newtask”
creates a task, uses stacksize for stack, rstack, fpstack, locals
task
( stacksize "name" – ) gforth-experimental “task”
create a named task with stacksize stacksize
execute-task
( xt – task ) gforth-experimental “execute-task”
create a new task task and initiate it with xt
stacksize
( – n ) gforth-experimental “stacksize”
stacksize for data stack
newtask4
( dsize rsize fsize lsize – task ) gforth-experimental “newtask4”
creates a task, each stack individually sized
stacksize4
( – dsize fsize rsize lsize ) gforth-experimental “stacksize4”
This gives you the system stack sizes
activate
( task – ) gforth-experimental “activate”
activates a task. The remaining part of the word calling
activate
will be executed in the context of the task.
pass
( x1 .. xn n task – ) gforth-experimental “pass”
activates task, and passes n parameters from the data stack
initiate
( xt task – ) gforth-experimental “initiate”
pass an xt to a task (VFX compatible)
pause
( – ) gforth-experimental “pause”
voluntarily switch to the next waiting task (pause
is
the traditional cooperative task switcher; in the pthread
multitasker, you don’t need pause
for cooperation, but
you still can use it e.g. when you have to resort to polling
for some reason). This also checks for events in the queue.
restart
( task – ) gforth-experimental “restart”
Wake a task
halt
( task – ) gforth-experimental “halt”
Stop a task
stop
( – ) gforth-experimental “stop”
stops the current task, and waits for events (which may restart it)
stop-ns
( timeout – ) gforth-experimental “stop-ns”
Stop with timeout (in nanoseconds), better replacement for ms
A cooperative multitasker can ensure that there is no other task
interacting between two invocations of pause
. Pthreads however
are really concurrent tasks (at least on a multi-core CPU), and
therefore, several techniques to avoid conflicts when accessing the same
resources.
Aside from the user variables that are already defined in the kernel, tasks may want to have user values and user defers, optain the offset of a user variable, or the address of those related to another task to initialize that task’s user area.
UValue
( "name" – ) gforth-1.0 “UValue”
UDefer
( "name" – ) gforth-experimental “UDefer”
Define a per-thread deferred word
user'
( ’user’ – n ) gforth-experimental “user”’
USER’ computes the task offset of a user variable
's
( user task – user’ ) gforth-experimental “’s”
get the tasks’s address of our user variable
Semaphores can only be aquired by one thread, all other threads have to wait until the semapohre is released.
semaphore
( "name" – ) gforth-experimental “semaphore”
create a named semaphore "name" \\ "name"-execution: ( – semaphore )
lock
( semaphore – ) gforth-experimental “lock”
lock the semaphore
unlock
( semaphore – ) gforth-experimental “unlock”
unlock the semaphore
The other approach to prevent concurrent access is the critical section. Here, we implement a critical section with a semaphore, so you have to specify the semaphore which is used for the critical section. Only those critical sections which use the same semaphore are mutually exclusive.
critical-section
( xt semaphore – ) gforth-experimental “critical-section”
implement a critical section that will unlock the semaphore even in case there’s an exception within.
Atomic operations can be used to synchronize tasks without using slow OS primitives.
!@
( u1 a-addr – u2 ) gforth-experimental “store-fetch”
load u2 from a_addr, and store u1 there, as atomic operation
+!@
( u1 a-addr – u2 ) gforth-experimental “add-store-fetch”
load u2 from a_addr, and increment this location by u1, as atomic operation
?!@
( unew uold a-addr – uprev ) gforth-experimental “question-store-fetch”
load uprev from a_addr, compare it to uold, and if equal, store unew there, as atomic operation
barrier
( – ) gforth-experimental “barrier”
Insert a full memory barrier
Gforth implements executable message queues for event driven programs:
you send instructions to other tasks, enclosed in <event
and
event>
; the entire event sequence is executed atomically. You
can pass integers, floats, and strings (only the addresses, so treat
the string as read-only after you have send it to another task). The
messages you send are defined with event:
name, which,
when invoked, will add the code for its execution to the message
queue, and when recieved, will execute the code following. The
message queue is queried when you stop
a task, or when you
check for events with ?events
. You can define a maximum of 256
different events.
<event
( – ) gforth-experimental “<event”
starts a sequence of events.
event>
( task – ) gforth-experimental “event>”
ends a sequence and sends it to the mentioned task
event:
( "name" – ) gforth-experimental “event:”
defines an event and the reaction to it as Forth code.
If name
is invoked, the event gets assembled to the event buffer.
If the event name
is received, the Forth definition
that follows the event declaration is executed.
?events
( – ) gforth-experimental “?events”
checks for events and executes them
event-loop
( – ) gforth-experimental “event-loop”
Tasks that are controlled by sending events to them should go into an event-loop
elit,
( x – ) gforth-experimental “elit,”
sends a literal
e$,
( addr u – ) gforth-experimental “e$,”
sends a string (actually only the address and the count, because it’s shared memory
eflit,
( x – ) gforth-experimental “eflit,”
sends a float
The naming conventions for events is :>
name.
The pthreads library also provides conditional variables, which allow to wait for a condition. Using the message queue is generally preferred.
cond
( "name" – ) gforth-experimental “cond”
create a named condition
pthread_cond_signal
( cond – r ) gforth-experimental “pthread_cond_signal”
pthread_cond_broadcast
( cond – r ) gforth-experimental “pthread_cond_broadcast”
pthread_cond_wait
( cond mutex – r ) gforth-experimental “pthread_cond_wait”
pthread_cond_timedwait
( cond mutex abstime – r ) gforth-experimental “pthread_cond_timedwait”
Next: Cilk, Previous: Multitasker, Up: Multitasker [Contents][Index]