[Gllug] Atomic increment/decrement

Alain Williams addw at phcomp.co.uk
Wed Aug 7 13:52:54 UTC 2002


On Wed, Aug 07, 2002 at 02:39:54PM +0100, t.clarke wrote:
> I have no ideas about mutexes - never fortunately had the need.
> 
> But I do remember many years ago having to do something vaguely similar on
> a TI990 computer.
> 
> As I recall, the fundamental issue was one of getting the CPU to do an atomic
> operation (of any sort) by first using a atomic 'test and set' cpu opcode.
> The routine was in assembler and did something along the lines of
> test and set a flag word
> if the set succeeded you have control so do the atomic operation
> and reset the flag word
> if no success spin for a bit and try again
> 
> The whole thing depended on an atomic cpu test-and-set instruction and was
> written in assembler.
> 
> I assume that unless the C language supports such a test-and-set cpu instruction
> directly, you will have do write something in assembler to gain access to the
> Intel X86 opcodes directly.

C doesn't support something directly -- it can't since the mechanisms by which you
do this depends very much on the CPU.

Note that schemes which would have been fine several years ago may not work today.
Years ago I remember using instructions that read & wrote memory in one instruction,
since I did not want to get time sliced or interrupted part way through. In those
days that was OK, if an interrupt was to happen it would be either before or
after the instruction.

Today: things are more complicated, in addition to ensuring that it is atomic on
an individual CPU you need to worry about the memory location not being updated
by the same/similar program running on another CPU - ie you are after memory
consistency. There are other techniques for doing this, again CPU specific; the
way that this works on intel is that you prefix the instruction by an op-code
that somehow grabs memory for the duration of the instruction - I forget the details.

For a little more illumination see:
	/usr/src/linux/include/asm/:
		atomic.h
		spinlock.h

-- 
Alain Williams

#include <std_disclaimer.h>

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list