[TI ASM] atomics
Posted: Sat 30 Apr, 2011 3:37 pm
I've been working on threading on the z80 a bit, and I don't really like hacking around with di/ei all the time. It's especially annoying when you don't know whether interrupts are disabled already or not and you want to nice and leave them disabled.
So I needed some atomic stuff to implement locks and the like (which are also hugely useful by themselves in a multithreaded context)
I have this one:
I suppose I use "ld (hl),$C9" instead but whatever.
It's nice for locks, but locks have "the lock problem" and otherwise TaS is a pretty weak routine with a consensus number of just 2.
Are more powerful atomic constructs possible on the z80?
In the mean time, here are some more "Test and Set"-like things.
"useful" because the result is in both the Z and C flags. Bad because it kills b.
Potentially useful because the push hl/ret automatically sends it into a loop when it fails. Bad because it kills c.
Potentially useful because it's the shortest TaS-like instruction sequence I've found (if called with A = 10010110, the result is in the Z flag. call with A >= 10010110 for result in C flag) - kills A though, obviously (and sometimes h, but that's less of an issue)
So I needed some atomic stuff to implement locks and the like (which are also hugely useful by themselves in a multithreaded context)
I have this one:
Code: Select all
testandset:
ld hl,tas_thingy
scf
jp (hl)
tas_thingy:
res 1,(hl) ; changes itself to ret (res = 11001011)
or a
ret
It's nice for locks, but locks have "the lock problem" and otherwise TaS is a pretty weak routine with a consensus number of just 2.
Are more powerful atomic constructs possible on the z80?
In the mean time, here are some more "Test and Set"-like things.
Code: Select all
rlc (hl)
ret
ret
; changes itself into
sub a
ld b,$C9
ret
Code: Select all
rrc (hl)
ret
ret
; changes itself into
push hl
ld c,$C9
ret
Code: Select all
sla (hl)
ret
; changes itself into
sub (hl)
ld h,(hl)
ret