Next Previous Up Top Contents Index

4.11 Conditional update

conditional-update!

Statement macro

Summary

Performs an atomic test-and-set operation.

Macro call

conditional-update!(local-name = place) 
body
[success success-expr]
[failure failure-expr]
end

Arguments

local-name
A Dylan variable-namebnf.

place
A Dylan variable-namebnf,

If the implementation provides the extended form of conditional-update!, place can also be a function call.

body
A Dylan bodybnf.

Values

See Description.

Library

threads

Module

threads

Description

Performs an atomic test-and-set operation. Where appropriate, it should be implemented using dedicated processor instructions, and is expected to be extremely efficient on most platforms.

The value of the place is evaluated once to determine the initial value, which is then bound to the local-name as a lexical variable. The body is then evaluated to determine the new value for the place. The place is then conditionally updated -- which means that the following steps are performed atomically:

1. The place is evaluated again, and a test is made to see if it has been updated since the initial evaluation. This may involve a comparison with the old value using ==, though implementations might use a more direct test for there having been an assignment to the place. It is undefined whether the test will succeed or fail in the case where the place was updated with a value that is identical to the old value when compared using \==.
2. If the value was found not to have been updated since the initial evaluation, the new value is stored by assignment. Otherwise the conditional update fails.

If the update was successful, then conditional-update! returns the result of the success expression, or returns the new value of the place if no success clause was supplied.

If the update failed, then conditional-update! signals a condition, unless a failure clause was given, in which case the value is returned.

If the place is a name, it must be the name of a locked variable in the current module scope. See Section 4.10 on page 114.

Exceptions

conditional-update! may signal a condition of the following class (which is a subclass of <error>), unless a failure clause is supplied.

<conditional-update-error>

Example

The following example does an atomic increment of *number-detected*.

until (conditional-update! 
        (current-val = *number-detected*) 
          current-val + 1
        failure #f
       end conditional-update!) 
end until
atomic-increment!
atomic-decrement!

Common Dylan and Functional Extensions - 31 Mar 00

Next Previous Up Top Contents Index