Whiley / RFCs

Request for Comment (RFC) proposals for substantial changes to the Whiley language.
3 stars 2 forks source link

Compare and Swap with Multiple Assignment? #92

Open DavePearce opened 3 years ago

DavePearce commented 3 years ago

An interesting question is whether or not we can already implement a compare and swap primitive with the existing syntax of Whiley. My angle of attack on this is to target the multiple assignment statement. Specifically, in a form like this:

A[i], A[j] = A[i], X

The key here is that, if i == j, then the above assigns X to A[i] otherwise A[i] is untouched. This is somehow a compare and set. We could have some definitions like this:

uint lock[NTHREADS]
uint HOLDER = 0
native uint THIS where THIS != HOLDER && THIS < NTHREADS

Don't quite know how to exploit this observation yet

DavePearce commented 3 years ago

Perhaps a minimal primitive would be an extended assignment statement:

x = e when c

Basically, the assignment either spinlocks until the condition is true (not very flexible) or just fails if the condition is not true.