Previous: Multitasker, Up: Multitasker

5.25.1 Ptheads

NewTask       stacksize – task         unknown       “NewTask”

creates a task, uses stacksize for stack, rstack, fpstack, locals

stacksize       – n         unknown       “stacksize”

stacksize for data stack

NewTask4       dsize rsize fsize lsize – task         unknown       “NewTask4”

creates a task, each stack individually sized

stacksize4       – dsize fsize rsize lsize         unknown       “stacksize4”

This gives you the system stack sizes

activate       task –         unknown       “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 –         unknown       “pass”

activates task, and passes n parameters from the data stack

pause              unknown       “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.

wake       task –         unknown       “wake”

Wake a task

sleep       task –         unknown       “sleep”

Stop a task

stop              unknown       “stop”

stops the current task, and waits for events (which may restart it)

stop-ns       timeout –         unknown       “stop-ns”

Stop with timeout (in nanoseconds), better replacement for ms

UValue       "name" –         unknown       “UValue”

Define a per-thread value

UDefer       "name" –         unknown       “UDefer”

Define a per-thread deferred word

user'       'user' – n         unknown       “user”'

USER' computes the task offset of a user variable

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. Semaphores

Semaphores can only be aquired by one thread, all other threads have to wait until the semapohre is released.

sema       "name" –         gforth       “sema”

create a named semaphore

lock       addr –         unknown       “lock”

lock the semaphore

unlock       addr –         unknown       “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.

c-section       xt addr –         unknown       “c-section”

implement a critical section that will unlock the semaphore even in case there's an exception within. Message Queues

Gforth implements executable message queues: you send instructions to other tasks, which they will execute as atomic sequence when they are enclosed in <event and event>. 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 instructions 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.

<event              unknown       “<event”

starts a sequence of events.

event>       task –         unknown       “event>”

ends a sequence and sends it to the mentioned task

event:       "name" –         unknown       “event:”

defines an event and the reaction to it as Forth code

?events              unknown       “?events”

checks for events and executes them

event-loop              unknown       “event-loop”

Tasks that are controlled by sending events to them should go into an event-loop

elit,       x –         unknown       “elit,”

sends a literal

e$,       addr u –         unknown       “e$,”

sends a string (actually only the address and the count, because it's shared memory

eflit,       x –         unknown       “eflit,”

sends a float Conditions

The pthreads library also provides conditional variables, which allow to wait for a condition. Using the message queue is generally preferred.

cond       "name" –         gforth       “cond”

create a named condition

pthread_cond_signal       unknown         unknown       “pthread_cond_signal”

pthread_cond_broadcast       unknown         unknown       “pthread_cond_broadcast”

pthread_cond_wait       unknown         unknown       “pthread_cond_wait”

pthread_cond_timedwait       unknown         unknown       “pthread_cond_timedwait”