It should also be noted that if you use spin-locks any code/data you touch while holding the spin-lock (functions you call into, etc…) must reside in non-pagable memory. I don’t think it is common to use spin locks, unless you are synchronizing with the code which runs at DISPATCH or above. Another synch primitive which should be mentioned is FAST_MUTEX which is just as fast as spin-lock in the uncontended case since it tries to lock the object using interlocked operations before waiting on the kernel dispatcher object (similar to the way CriticalSection works in user mode).
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Joseph M. Newcomer
Sent: Friday, July 31, 2009 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Spinlock vs Semaphore / Mutex
Spin locks are lightweight and fast, and can be used above PASSIVE_LEVEL. A
mutex is heavyweight. While a spin lock “just spins”, the question is *how
long* it spins. If the goal of the spin lock is, for example, to protect a
list link/unlink style operation, you will end up spinning for something on
the order of tens on nanoseconds, whereas a scheduling operation (even if
you were locking at PASSIVE_LEVEL) is going to on the order of microseconds.
Also, because the scheduler is involved, the “latency” from releasing the
lock until the next thread can be scheduled can be in the tens of
milliseconds.
Since semaphores do not provide mutual exclusion, their usage should not
enter the discussion. They are used for a different purpose (the case of a
semaphore whose range is 0…1 is a special case, but then you end up with
details such as recursive acquisition and other differences between a
“binary semaphore” and a mutex).
All synchronization mechanisms have to be multiprocessor-safe; it wouldn’t
make sense any other way.
You cannot cause a thread running at DISPATCH_LEVEL to wait on a mutex, so
the best you can do is poll, which means you have just reinvented a
massively expensive spin lock.
Also, spin locks have the queued spin lock variant, which reduces bus
contention and guarantees FIFO processing (no thread starvation). In
general, for any short interval, spin locks would be preferred. For
synchronizing PASSIVE_LEVEL threads with lengthy delays, mutexes would be
used. A PASSIVE_LEVEL thread working with a queue will use a semaphore.
Note that if you need lengthy delays, you *cannot* use spin locks (the
nominal spin lock budget is 10us), which means you may need to create a
complex architecture so your waiting thread is waiting at PASSIVE_LEVEL.
Generally, it is not usually a good idea to have threads blocked for lengthy
periods of time unless they are your own threads. Otherwise you can consume
threads from the thread pool and potentially interfere with the file system
performance.
joe
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Jack Huang
Sent: Friday, July 31, 2009 12:44 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Spinlock vs Semaphore / Mutex
I have a question about synchronization in kernel mode.
As I know, spinlock provided multi-processor safe manipulation for entering
critical section.
Semaphore and Mutext can also be used for synchronization purpose.
Are they multi-processor safe mechanism?
I was confused by them.
For example, if I have two dispatched function may access one shared
resource.
Could I use semaphore or mutex to synchronize the shared resource access?
What would be the case I should use spinlock if semaphore or mutex can be
used in this case for mutil-processor system?
Best Regards
Jack
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer
–
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer