Monday, 29 September 2008

Semaphores in Redcode

I'm looking for suggestions on how to code busy-waiting semaphores in Redcode.  The lack of an atomic test-and-set operation complicates matters.
The code below works for up to three processes, but under certain circumstances can fail with four processes.  The entry points are wait and signal.

semaphore: dat 1
fail: add #1, semaphore
wait: djn fail, semaphore
signal: add #1, semaphore

If you have any suggestions, please let me know in the comments below.