A number of synchronization primitives are provided. These primitives include:
Barriers (test, set, wait)
Locks ( test, set, clear)
Events (test, set, wait, clear)
Barriers provides an explicit mechanism for a task to indicate its arrival at a program point and to wait there until all other tasks arrive. A task may test and optionally wait at an explicit barrier point. In the following example, a barrier is used to make sure that block3 is not entered by any task until all tasks have completed execution of block1.
The following example performs a similar function as above. However, while waiting for all tasks to arrive at the barrier, the early tasks perform work within a loop.
DO WHILE (.NOT. TEST_BARRIER())
Locks are used to prevent the simultaneous access of data by multiple tasks.
The SET_LOCK(lock) intrinsic sets the mapped integer variable lock atomically. If the lock is already set, the task that called SET_LOCK is suspended until the lock is cleared by another task and then sets it. Individual locks may be tested or cleared using result = TEST_LOCK(lock) and CLEAR_LOCK(lock) respectively.
A critical section protects access to a section of code rather than to a data object. The CRITICAL directive marks the beginning of a code region in which only one task can enter at a time. The END CRITICAL directive marks the end of the critical section. Transfer of control into or out of a critical section is prohibited.
GLOBAL_SUM = GLOBAL_SUM + LOCAL_SUM
!HPF$ END CRITICAL
Events are typically used to record the state of a program's execution and to communicate that state to another task. Because they do not set locks, as do the lock routines described earlier, they cannot easily be used to enforce serial access of data. They are suited to work such as signalling other tasks when a certain value has been located in a search procedure. There are four routines needed to perform the event functions, and each requires a mapped argument.
The SET_EVENT(event) routine sets or posts an event; it declares that an action has been accomplished or a certain point in the program has been reached. A task can post an event at any time, whether the state of the event is cleared or already posted. The CLEAR_EVENT(event) routine clears an event, the WAIT_EVENT(event) routine waits until a particualr event is posted, and the result = TEST_EVENT(event) function returns a logical value indicating whether a particular event has been posted.