TAS real-valued function Example The following example shows the key aspects of using the TAS real-valued function to ensure exclusive access by an application program to data that is also used by another program task. (The same instruction sequence must be used in any other application program that wants to access the data.) The real-variable data.locked has the value FALSE when the data is not interlocked, and the value TRUE when the data is interlocked. This variable is set to TRUE with the TAS function, so that we can detect if the other program task has already set it to TRUE.
Since TAS tests and sets the value indivisibly, there is no chance of both programs setting data.locked to TRUE simultaneously without the conflict being detected. Use of the "semaphore" variable data.locked involves the three steps shown below. ; Step1: Look for the lock variable to have the "unlocked" setting (FALSE), ; and simultaneously apply the "lock" setting (TRUE). This loop will cycle ; continuously until another task sets the lock variable to the "unlocked" ; setting (FALSE), at which time this task asserts the lock for itself. WHILE TAS(data.locked,TRUE) DO ;Wait for the lock to be released WAIT ;Sleep this task until the next cycle END ; Step 2:
Perform desired operations accessing the shared data ... ... ; Step 3: Release the lock on the shared data structure. data.locked = FALSE The WHILE loop causes program execution to be blocked until the variable data.locked is found to have the value FALSE. Thus, the program is blocked if the other program has locked the semaphore variable in order to access the shared data. Note that the TAS function will set the variable data.locked to TRUE each time the function is executed, but that will have no effect if the variable already has that value. Once the program gains exclusive access to the shared data, it can safely access the data.
The last instruction releases the data for access by the application executing as the other program task. Related Keyword CAS real-valued function eV+Language Reference Guide, v2.x, 18319-000 Rev A Page 523