From: o0Zz on
Hello everybody,

I would like know what is the difference between spinlock and mutex.
I think know, there are a mutiprocessor story but I don't know exactly what
is it.

If I take this exemple :


KeAcquireSpinLock <- CPU2 is here and it have the hand.
.... <- CPU1 is here
KeReleaseSpinLock

What is the result ? CPU2 is locked until the CPU1 go on KeReleaseSpinLock ?
And for Mutex it's the same result ?

Thank you in advance,
Thierry.

From: Don Burn on

The operation from the point of view of the two threads is the same, but
there are differences:

1. When CPU2 waits the thread is blocked and another thread runs on CPU2
with a mutex, versus CPU2 consuming all its time spinning on the lock wth a
spin lock

2. Mutexs have a lot more overhead, spin locks are low overhead

3. Mutexs do not raise IRQL to DISPATCH_LEVEL, while spinlocks do.


--
Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr


"o0Zz" <oozz01(a)gmail.com> wrote in message
news:%233$VwpEdKHA.4724(a)TK2MSFTNGP05.phx.gbl...
> Hello everybody,
>
> I would like know what is the difference between spinlock and mutex.
> I think know, there are a mutiprocessor story but I don't know exactly
> what is it.
>
> If I take this exemple :
>
>
> KeAcquireSpinLock <- CPU2 is here and it have the hand.
> ... <- CPU1 is here
> KeReleaseSpinLock
>
> What is the result ? CPU2 is locked until the CPU1 go on KeReleaseSpinLock
> ?
> And for Mutex it's the same result ?
>
> Thank you in advance,
> Thierry.
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4658 (20091203) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4658 (20091203) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




From: m on
The most basic difference between a spinlock and a mutex is that a spinlock,
as the name implies, 'spins' in a tight loop waiting for the lock whereas a
mutex interacts with the OS scheduler and will yield to another thread while
waiting for the lock. Other than some specific optimizations for single
processor systems in KM (obsolete since multi-core desktops are the norm),
the number of processors in the system is irrelevant.

spinlock acquire psudocode: (ignoring recursive acquisition and race
conditions)

while(true)
{
if(TryToAcquireLock()) // this is a non-blocking test of a
variable using InterlockedXXX
{
break;
}
}

The loop will consume all 100% of a single CPU until the lock is acquired,
the thread is interrupted by a higher priority thread or the thread quantum
ends (and the OS schedules another thread).

Mutex acquire psudocode: (ignoring recursive acquisition and race
conditions)

SuspendThreadAndRegisterWaitForObject() // this is a blocking call

The scheduler will suspend this thread and schedule other threads on this
CPU until the mutex is signaled by another thread and this thread is marked
as ready to run. Once the thread is ready to run, the scheduler will
schedule it to run on an available CPU it it will continue normally.


"o0Zz" <oozz01(a)gmail.com> wrote in message
news:#3$VwpEdKHA.4724(a)TK2MSFTNGP05.phx.gbl...
> Hello everybody,
>
> I would like know what is the difference between spinlock and mutex.
> I think know, there are a mutiprocessor story but I don't know exactly
> what is it.
>
> If I take this exemple :
>
>
> KeAcquireSpinLock <- CPU2 is here and it have the hand.
> ... <- CPU1 is here
> KeReleaseSpinLock
>
> What is the result ? CPU2 is locked until the CPU1 go on KeReleaseSpinLock
> ?
> And for Mutex it's the same result ?
>
> Thank you in advance,
> Thierry.

From: Daniel Terhell on
"m" <m(a)b.c> wrote in message news:eP9kilIdKHA.6096(a)TK2MSFTNGP02.phx.gbl...
> The loop will consume all 100% of a single CPU until the lock is acquired,
> the thread is interrupted by a higher priority thread or the thread
> quantum ends (and the OS schedules another thread).
>

That information is incorrect, on Windows a spinlock raises to
DISPATCH_LEVEL (or higher) so it is not possible to get interrupted by any
thread. It's not even possible for the thread to finish its quantum while it
spins or for the dispatcher to schedule another thread on the same CPU. It
can get interrupted by an ISR but it will return (to the spinning state) as
soon as the ISR has executed and returns control.

//Daniel


From: o0Zz on
Thank you very much !

"Daniel Terhell" <daniel(a)resplendence.com> a �crit dans le message de groupe
de discussion : emRp7$LdKHA.2188(a)TK2MSFTNGP04.phx.gbl...
> "m" <m(a)b.c> wrote in message news:eP9kilIdKHA.6096(a)TK2MSFTNGP02.phx.gbl...
>> The loop will consume all 100% of a single CPU until the lock is
>> acquired, the thread is interrupted by a higher priority thread or the
>> thread quantum ends (and the OS schedules another thread).
>>
>
> That information is incorrect, on Windows a spinlock raises to
> DISPATCH_LEVEL (or higher) so it is not possible to get interrupted by any
> thread. It's not even possible for the thread to finish its quantum while
> it spins or for the dispatcher to schedule another thread on the same CPU.
> It can get interrupted by an ISR but it will return (to the spinning
> state) as soon as the ISR has executed and returns control.
>
> //Daniel
>
>