[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [f-cpu] another DATE report



On Mon, Mar 18, 2002 at 12:08:56AM +0100, Christophe wrote:
> CAS : Compare-And-Swap is a READ-MODIFY-WRITE operation which is atomic and
> allow us to update value in memory without unconsistancy.

Ah... *click*

> in C :
> 
> int CAS (int *pointer,int requested_value,int new_value) {
>     if (requested_value == *pointer) {
>         *pointer = new_value; return true;
>     }
>     return false;
> }

Except that this function is NOT atomic. There's no way to specify an
atomic operation in standard C (but it's ok, I know this is only for
demonstration).

We could easily do this kind of operation in hardware, btw.

[...]
> A CAS2 is in fact two atomic CAS :
> 
> int CAS (int *pointer1,int *pointer2,int requested_value1,int
> requested_value2,int new_value1,int new_value2) {
>     if ((requested_value1 == *pointer1) && (requested_value2 == *pointer2)) {
>         *pointer1 = new_value1; *pointer2 = new_value2; return true;
>     }
>     return false;
> }
> 
> Examples :
> 
> How to push a element in a stack :
> 
> void atomic_push (struct stack *stack,struct stack_node *element) {
>     struct stack_node *requested_top;
>     int requested_version;
>     do
>         {
>             requested_top = stack->top;
>             requested_version = stack->version;
>             element->link = requested_top;
>         }
>     while (CAS2
> (&stack->top,&stack->version,requested_top,requested_version,element,requested_
> version+1));
> }

I'm not sure whether the latency will be better than with a simple
spinlock around the update operation:

	void atomic_push (struct stack *stack,struct stack_node *element) {
		while (!CAS(&stack->spinlock, 0, 1));
		element->link = stack->top;
		stack->top = element;
		stack->version++;
		stack->spinlock = 0;
	}

-- 
 Michael "Tired" Riepe <Michael.Riepe@stud.uni-hannover.de>
 "All I wanna do is have a little fun before I die"
*************************************************************
To unsubscribe, send an e-mail to majordomo@seul.org with
unsubscribe f-cpu       in the body. http://f-cpu.seul.org/