Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Vista Drivers > Spinlock vs Semaphore \ Mutex

Reply
Thread Tools Display Modes

Spinlock vs Semaphore \ Mutex

 
 
Jack Huang
Guest
Posts: n/a

 
      07-31-2009
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


 
Reply With Quote
 
 
 
 
Don Burn
Guest
Posts: n/a

 
      07-31-2009
See my response to this on NTDEV.


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



"Jack Huang" <> wrote in message
news:...
>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
>
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4294 (20090731) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4294 (20090731) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




 
Reply With Quote
 
Maxim S. Shatskih
Guest
Posts: n/a

 
      07-31-2009
> Semaphore and Mutext can also be used for synchronization purpose.
> Are they multi-processor safe mechanism?


Yes.

> Could I use semaphore or mutex to synchronize the shared resource access?


Spinlock: can be used at DISPATCH_LEVEL, but busy-stalls the CPU trying to acquire it.

All other objects: cannot be used at DISPATCH_LEVEL, but allow the CPU to pick another thread for execution instead of the waiting one.

I never ever used a KSEMAPHORE or KMUTEX in my code :-) only FAST_MUTEX.

--
Maxim S. Shatskih
Windows DDK MVP

http://www.storagecraft.com

 
Reply With Quote
 
Jack Huang
Guest
Posts: n/a

 
      08-01-2009
Thanks for all replies.
You did help me so much.

Best Regards
Jack

"Maxim S. Shatskih" <> ..
> Semaphore and Mutext can also be used for synchronization purpose.
> Are they multi-processor safe mechanism?


Yes.

> Could I use semaphore or mutex to synchronize the shared resource access?


Spinlock: can be used at DISPATCH_LEVEL, but busy-stalls the CPU trying to
acquire it.

All other objects: cannot be used at DISPATCH_LEVEL, but allow the CPU to
pick another thread for execution instead of the waiting one.

I never ever used a KSEMAPHORE or KMUTEX in my code :-) only FAST_MUTEX.

--
Maxim S. Shatskih
Windows DDK MVP

http://www.storagecraft.com


 
Reply With Quote
 
Jack Huang
Guest
Posts: n/a

 
      08-01-2009
Does this news group exchange posts with NTDVE?
I should only post my question in either one group if the answer is yes.

Jack Huang

"Don Burn" <> .
> See my response to this on NTDEV.
>
>
> --
> Don Burn (MVP, Windows DKD)
> Windows Filesystem and Driver Consulting
> Website: http://www.windrvr.com
> Blog: http://msmvps.com/blogs/WinDrvr
> Remove StopSpam to reply
>
>
>
> "Jack Huang" <> wrote in message
> news:...
>>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
>>
>>
>>
>> __________ Information from ESET NOD32 Antivirus, version of virus
>> signature database 4294 (20090731) __________
>>
>> The message was checked by ESET NOD32 Antivirus.
>>
>> http://www.eset.com
>>
>>
>>

>
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4294 (20090731) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>
>



 
Reply With Quote
 
Don Burn
Guest
Posts: n/a

 
      08-01-2009
Most of the experienced driver dev's who hang out on the newsgroups are on
both. So you essentially asked the question twice. I for one do not like
cross posting messages so it is much easier if you only post in one place,
then wait a reasonable while to see if the question is answered.


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



"Jack Huang" <> wrote in message
news:u%...
> Does this news group exchange posts with NTDVE?
> I should only post my question in either one group if the answer is yes.
>
> Jack Huang
>
> "Don Burn" <> .
>> See my response to this on NTDEV.
>>
>>
>> --
>> Don Burn (MVP, Windows DKD)
>> Windows Filesystem and Driver Consulting
>> Website: http://www.windrvr.com
>> Blog: http://msmvps.com/blogs/WinDrvr
>> Remove StopSpam to reply
>>
>>
>>
>> "Jack Huang" <> wrote in message
>> news:...
>>>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
>>>
>>>
>>>
>>> __________ Information from ESET NOD32 Antivirus, version of virus
>>> signature database 4294 (20090731) __________
>>>
>>> The message was checked by ESET NOD32 Antivirus.
>>>
>>> http://www.eset.com
>>>
>>>
>>>

>>
>>
>>
>> __________ Information from ESET NOD32 Antivirus, version of virus
>> signature database 4294 (20090731) __________
>>
>> The message was checked by ESET NOD32 Antivirus.
>>
>> http://www.eset.com
>>
>>
>>
>>

>
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4296 (20090801) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4296 (20090801) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




 
Reply With Quote
 
Maxim S. Shatskih
Guest
Posts: n/a

 
      08-01-2009
> Does this news group exchange posts with NTDVE?

No, it does not.

--
Maxim S. Shatskih
Windows DDK MVP

http://www.storagecraft.com

 
Reply With Quote
 
Maxim S. Shatskih
Guest
Posts: n/a

 
      08-04-2009
>Someone correct me if I'm wrong, but I get the impression that in
>order for a synchronization construct to be MP safe it must eventually
>reduce to some kind of busy-wait test and set hardware level loop.


All high-level sync objects in Windows reduce to KiDispatcherLock spinlock :-)

In Win7, this was changed to several lock-free atomic operations (Win7 has lock-free dispatcher).

>this is true, I don't see much difference between a Mutex and a
>Spinlock, except that the Mutex gives the system a chance not to spin


Correct. When the thread stops on mutex, KiDispatcherLock is taken and, under this lock, the thread is marked as "waiting" and another thread is chosen for execution.

When the thread stops on spinlock, the whole CPU is stalled.

>Talking about fast mutexes, I have had plenty of trouble trying to
>issue driver log messages to my log file from inside fast mutexes -
>caveat emptor.


Correct, ZwXxx do not work at APC_LEVEL, but I would use a producer/consumer logic and logging work item for logging, not direct ZwWriteFile.

>One idea is to synchronize through a homegrown macro or a C++ class -


Yes. Beware of C++ exceptions in the kernel though and note that you must wrap _any_ function call which can fail to the exception. Without this, the C++ "locker" object is useless.

--
Maxim S. Shatskih
Windows DDK MVP

http://www.storagecraft.com

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Semaphore Timeout Period Has Expired William McIlroy Windows Server 2 08-19-2007 02:04 AM
Re: "The semaphore timeout has expired" when joining pc to domain Gazza Windows Server 1 09-28-2004 05:13 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59