Product Site

2.9. GUARD

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 .true. If the expression evaluates to .false, GUARD waits until another method assigns or drops an object variable 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.

The condition expression after a WHEN can also be a list of expressions which is evaluated left-to-right. Each expression must evaluate to either .false or .true. Evaluation will stop with the first .false result and .false will be returned as the condition result. If all of the expressions evaluate to .true, then the condition result is also .true.

The expression or list of expressions can reference both local and object variables, but must reference at least one object variable exposed by a USE LOCAL instruction or an EXPOSE instruction with a simple variable name (not a variable in parentheses representing a subsidiary list of variables).
Example 2.15. Instructions — GUARD
c = .Concurrent~new
c~work

::class Concurrent
::method init
  expose ready
  ready = .false
  reply
  call SysSleep 1 -- long-running initialization
  ready = .true

::method work unguarded
  expose ready
  guard on when ready -- wait until initialized
  say "starting work"

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 .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 Section 12.4.3, “Guarded Methods” for more information.

Additional information on multithreaded execution of programs can be found in Chapter 12, Concurrency. For debugging multithreaded programs see Section 15.4, “Debugging Multithreaded Programs”.