TestAndSet : BOOL

TestAndSet : BOOL 1:

The function TestAndSet can be used to check and set a flag, without the possibility to be interrupted by other tasks. 

VAR_IN_OUT

VAR_IN_OUT
    Flag : BOOL; (* Flag to check if TRUE or FALSE *)
END_VAR

Flag: Is a boolean flag, tha is being checked
- if it was FALSE, then the flag was available and is being set (to block other tasks), the function returns TRUE
- if it was TRUE, then the flag was blocked, the function returns FALSE

Sample

VAR_GLOBAL
   bGlobalTestFlag       : BOOL;
END_VAR
VAR
    iLocalBlockedCounter  : DINT;
END_VAR
IF TestAndSet(bGlobalTestFlag) THEN
    (* bGlobalTestFlag was FALSE, nobody was blocking, NOW bGlobalTestFlag is set to TRUE and blocking others *)
    (* ... *)
    ;
 
    (* remove blocking by resetting the flag*) 
    bGlobalTestFlag := FALSE;
ELSE
    (* bGlobalTestFlag was TRUE, somebody is blocking *)
    iLocalBlockedCounter := iLocalBlockedCounter + 1;
    (* ... *)
    ;
END_IF