I can't think of another way to do it, simply because you need to both swap and compare to detect if you're allowed to proceed. If you don't have a compare-and-swap command, you'll have to implement it with a looping swap and compare, something like:
; Emulate atomic add/sub with atomic swap.
; On entry:
; r0 contains address of variable
; r1 contains value to add or subtract.
mutex: defw 0 ; mutual exclusion semaphore (0 means free, 1 means busy).
chng: push r2 ; save variables.
ld r2,1 ; claiming value.
spin: swap r2,(mutex) ; atomic swap (sounds like a good name for a band).
bnz spin ; loop until you have it.
add (r0),r1 ; emulated atomic change.
swap r2,(mutex) ; free mutex for everyone else.
pop r2 ; restore registers.
ret
It's only really klunky if you're doing it in a lot of places in your code. I've often found that isolating 'klunky' code to a function (like above) makes it far less klunky since you then end up with lots of code segments looking like the much simpler:
myvar: defw 0
: : : : :
ld r0,myvar
ld r1,1 ; increment
call chng
or, if you want your code even simpler, provide separate incr
and decr
functions:
; Emulate atomic incr/decr with emulated atomic change.
; On entry:
; r0 contains address of variable
incr: push r1 ; save registers.
ld r1,1 ; increment.
call chng ; do it.
pop r1 ; restore registers.
ret
decr: push r1 ; save registers.
ld r1,-1 ; decrement.
call chng ; do it.
pop r1 ; restore registers.
ret
Then your code sequences become:
ld r0,myvar
call incr
or, if you can do macros, an even simpler:
atincr: defm ; do this once to define macro
ld r0,&1
call incr
endm
atincr myvar ; do this in your code, as much as you like.