| | 365 | A guarded command is encoded in CIVL-C using a `$when` statement: |
| | 366 | {{{ |
| | 367 | $when (expr) stmt; |
| | 368 | }}} |
| | 369 | All statements have a guard, either implicit or explicit. For most statements, the guard is ''true''. The `$when` statement allows one to attach an explicit guard to a statement. |
| | 370 | |
| | 371 | When `expr` is ''true'', the statement is enabled, otherwise it is disabled. |
| | 372 | A disabled statement is blocked—it will not be scheduled for execution. |
| | 373 | When it is enabled, it may execute by moving control to the `stmt` and executing the first atomic action in the `stmt`. |
| | 374 | If `stmt` itself has a non-trivial guard, the guard of the `$when` statement is the conjunction of the `expr` and the guard of `stmt`. |
| | 375 | |
| | 376 | The evaluation of `expr` and the first atomic action of `stmt` occur as a single atomic action. |
| | 377 | There is no guarantee that execution of `stmt` will continue atomically if it contains more than one atomic action, i.e., other processes may be scheduled. |
| | 378 | |
| | 379 | Examples: |
| | 380 | |
| | 381 | {{{ |
| | 382 | $when (s>0) s--; |
| | 383 | }}} |
| | 384 | This will block until `s` is positive and then decrement `s`. |
| | 385 | The execution of `s--` is guaranteed to take place in an environment in which `s` is positive. |
| | 386 | |
| | 387 | {{{ |
| | 388 | $when (s>0) { s--; t++; } |
| | 389 | }}} |
| | 390 | The execution of `s--` must happen when `s>0`, but between `s--` and `t++`, other processes may execute. |
| | 391 | |
| | 392 | {{{ |
| | 393 | $when (s>0) $when (t>0) x=y*t; |
| | 394 | }}} |
| | 395 | This blocks until both `x` and `t` are positive then executes the assignment in that state. |
| | 396 | It is equivalent to |
| | 397 | {{{ |
| | 398 | $when (s>0 && t>0) x=y*t; |
| | 399 | }}} |
| | 400 | |
| | 401 | |
| | 402 | The following statements are all equivalent: |
| | 403 | {{{ |
| | 404 | $when (s>0) $atomic{ s--; t++; } |
| | 405 | $atomic{ $when(s>0); s--; t++; } |
| | 406 | $atomic{ $when(s>0) {s--; t++; } } |
| | 407 | }}} |
| | 408 | Each of these waits until `s` is positive, then from such a state, decrements `s` and increments `t` without the intervention of other processes. |