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

Re: [f-cpu] another DATE report




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

Oh fine.

> [...]
> > 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:

well, there is a problem with the spinlock version : what happens if a task
switch occurs between the locking and the unlocking ? especially if other tasks
will try to acquire the same spinlock, they all shall be blocked until the
owner task may release the spinlock, which - not speaking about the invert
priority problem of tasks - is in fact a real degradation. Such a thing cannot
happen with CAS2 because the first to be serviced is the first to write, not
the first to read - the other tasks would just need a retry - which takes
basically less time to execute than waiting for owner task to unlock.

Indeed, to avoid this problem when task switching, the usual workaround is to
disable interrupts before acquiring and after releasing the spinlock. But such
a thing is acceptable if time is very short between locking and unlocking, that
is, not using a blocking function in-between.



*************************************************************
To unsubscribe, send an e-mail to majordomo@seul.org with
unsubscribe f-cpu       in the body. http://f-cpu.seul.org/