>>-GUARD--+-ON--+------------------+--+--;--------------------->< | +-WHEN--expression-+ | +-OFF--+------------------+-+ +-WHEN--expression-+
GUARD controls a method's exclusive access to an object.
GUARD ON acquires for an active method exclusive use of its object variable pool. This prevents other methods that also require exclusive use of the same variable pool from running on the same object. If another method has already acquired exclusive access, the GUARD instruction causes the issuing method to wait until the variable pool is available.
GUARD OFF releases exclusive use of the object variable pool. Other methods that require exclusive use of the same variable pool can begin running.
If you specify WHEN, the method delays running until the expression evaluates to 1 (true). If the expression evaluates to 0 (false), GUARD waits until another method assigns or drops an object variable (that is, a variable named on an EXPOSE instruction) used in the WHEN expression. When an object variable changes, GUARD reevaluates the WHEN expression. If the expression evaluates to true, the method resumes running. If the expression evaluates to false, GUARD resumes waiting.
Example:
::method c expose y if y>0 then return 1 else return 0 ::method d expose z guard on when z>0 self~c /* Reevaluated when Z changes */ say "Method D"
If you specify WHEN and the method has exclusive access to the object's variable pool, then the exclusive access is released while GUARD is waiting for an object variable to change. Exclusive access is reacquired before the WHEN expression is evaluated. Once the WHEN expression evaluates to 1 (true), exclusive access is either retained (for GUARD ON WHEN) or released (for GUARD OFF WHEN), and the method resumes running.
Note: If the condition expression cannot be met, GUARD ON WHEN puts the program in a continuous wait condition. This can occur in particular when several activities run concurrently. See Guarded Methods for more information.