Danaher Motion
06/2005
BASIC Moves Development Studio
2. 6
S
EMAPHORES
Semaphores are the basis for synchronization and mutual exclusion. The
difference is that the mutual exclusion semaphore is created as “full” or “1”,
while the synchronization semaphore is empty “0”. If the semaphore is used
for protecting mutual resources, it is taken before accessing the resource
and releases at the end. A synchronization semaphore is given by the
producer and taken (consumed) by the consumer.
NOTE
A semaphore is created as “full.”
Global semaphore are defined with
COMMON
SHARED
in CONFIG.PRG or
from the command line. Since a semaphore's purpose is to protect data
among tasks, there is no meaning to local semaphores.
A semaphore is
given/released
by
SEMAPHOREGIVE
and
taken/consumed
by
SEMAPHORETAKE
. It is possible to specify a time out of up to 5000 ms.
SEMAPHORETAKE
acquires a semaphore and returns before timeout or
does not acquire a semaphore and returns after timeout.
NOTE
Mutual exclusion semaphores are taken and given by the same task.
Synchronization semaphores are given by one task and taken by
another task.
2.6.1.
Mutual Exclusion Semaphores
Mutual exclusion semaphores lock resources by taking a semaphore.
Another task(s) competing for the same resource is blocked until the
semaphore is released.
Example of a mutually exclusive semaphore:
‘ common shared comMutex as semaphore
‘ defined in config.prg
Program
Open COM2 BaudRate=9600 Parity=0 DataBits=8 StopBit=1 As #1
While 1
‘ take semaphore lock serial port
If SemTake(comMutex,5000) = 1 Then
Print #1,”Hello ”;
Print #1,”world”
SemGive(comMutex) ‘unlock serial port
End if
End While
End program
M-SS-005-03 Rev
E
45