Information on SPARC assembly (atomic Test and Set)

David Goodenough dg at lakart.UUCP
Fri Jun 16 03:29:49 AEST 1989


rp at osc.COM (Rich Patterson) sez:
> Hi,
> 	I need some help in finding information on coding atmoic bit
> operations on a Sun-4 (SPARC).  I wasn't able to find a reference to a
> "Test and Set" operation in the Assembly guide that comes with our Sun-4.
> 	Are there any other references on SPARC assembly, Sun published or
> otherwise ?  Any help or code would be appreciated.  Please e-mail to the
> address below.

I have never understood the need for a test and set instruction, when
you can make do with adc (add with carry). Allow me to explain:

The point behind TAS is to allow a process to test if a flag is set or
clear, and set it no matter what the result. But why does the test have
to be in the same instruction? In fact all that is needed is the ability
to capture the state of a bit, setting it as you do the capture, and
test it later. If you think about the following:

	Bit clear (i.e. resource available)

	Task 1 grabs a copy of the bit and sets it, but does not test it.

	Bit is now set

	Task 1 gets swapped out, and Task 2 runs

	Task 2 grabs a copy of the bit, and sets it again.

	Task 2 tests thre copy it captured - finds it was set, and assumes
		the resource is not available.

	Task 2 gets swapped out, and Task 1 comes back

	Task 1 tests it's copy of the bit, finds it was clear, and proceeds
		to use the resource.

Note that Task 1 got interrupted between sampling the bit, and testing it,
_BUT_IT_DIDN'T_MAKE_ANY_DIFFERENCE_ - the system still worked.

So the bottom line is all you need is the ability to capture the state
of a bit, and set it no matter what, all in one atomic instruction.
Add with carry works just nicely to do this:

put the flag in memory, it is a whole byte, initialize it to 0x7f (i.e.
all bits set, except the MS bit is clear) - the flag is now clear (resource
is available). To get and set the bit do the following:

	set carry
	add with carry		flag, flag
	jump on carry clear	resource available

Now if you break this sequence anywhere, it is still secure. Note that it
assumes you can adc memory,memory - if you can't look for a rotate left
instruction, which does about the same thing.

To release the resource, simply move 0x7f to the flag byte after you've
finished with the resource - that is trivial.
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.unix.wizards mailing list