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

Re: spinlocks (was Re: [f-cpu] another DATE report)



g'night,

Michael Riepe wrote:
> On Tue, Mar 19, 2002 at 12:10:03AM +0100, Christophe wrote:
> [...]
> > > Note that this does not mean that I don't like CAS. Au contraire -- we
> > > definitely need an atomic read-modify-write operation, and this one
> > > looks pretty good to me, although it doesn't solve all problems.
> >
> > Of course, it doesn't solve all problems. But I prefer them to semaphores when
> > possible.
> 
> Semaphores are slightly different, and usually not present in hardware.
> When a task requests a semaphore that is in use, it is suspended (there
> will be a task switch). As soon as the semaphore is freed, the first
> process waiting for it is woken up, or maybe all of them, and probably
> another task switch is performed.
> 
> A simple semaphore can be written using CAS:
> 
>         down(struct sem *sem) {
>                 while (!CAS(&sem->count, 1, 0)) {
>                         wait_on_semaphore(sem); // does a task switch
>                         /* we come back here when the task is woken up again */
>                 }
>         }
> 
>         up(struct sem *sem) {
>                 sem->count = 1;
>         }
> 
> A real semaphore (with an initial count > 1) is a little harder...
> We'd need another instruction in order to implement that efficiently,
> like `atomic test-for-zero-and-decrement-otherwise'.

using load_lock and store_conditional, it's easy : just do a substraction
with saturation, instead of the xor in my latest example. that's all. voilą.

>  Michael "Tired" Riepe <Michael.Riepe@stud.uni-hannover.de>
WHYGEE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*************************************************************
To unsubscribe, send an e-mail to majordomo@seul.org with
unsubscribe f-cpu       in the body. http://f-cpu.seul.org/