Click to See Complete Forum and Search --> : User-mode semaphore possible?


jancellor
October 18th, 2008, 08:45 AM
Hello

In win32, I believe the following is true. EnterCriticalSection() and LeaveCriticalSection() can be used to synchronize threads without requiring expensive system calls that transition into kernel-mode when there is no contention on the lock object. They do this by using a specific test-and-set instruction on the CPU. When there is contention on the lock, blocked threads will wait on a kernel object and are not awakened until the lock is released.

I believe a semamphore is a more fundamental synchronization object and would like to know if it is possible to make one that does not require entering kernel-mode when there is no contention. That is, can it be made if you have access to TestAndSet() and any waitable kernel objects, such as events and semaphores? (I'm fairly sure Enter/LeaveCriticalSection() can be emulated from these.)

Eagerly awaiting responses...

Thanks

Arjay
October 18th, 2008, 11:48 AM
The critical section is the only user mode synchronization object available on Windows - all the rest are kernel mode objects (mutex, semaphore, etc.).

jancellor
October 21st, 2008, 02:38 PM
Thanks Arjay, I wasn't positive that the critical section was the only user-mode synchronization method available in Windows.

The main question, though, is it is possible to create a user-mode semaphore (ie object and related functions) with, for example, the Win32 functions

InterlockedCompareExchange(),
Create/ReleaseSemphore(),
Create/SetEventand(),
WaitForSingleObject(), etc?

Perhaps it is impossible and this is why such an synchronization object is not provided.

Arjay
October 21st, 2008, 03:15 PM
I believe the difference of synchronization objects between user mode and kernel mode is whether they can work across processes. The kernel mode mutex, event, and semaphore can all work across process whereas the user mode critical section cannot.

I would expect that you could make a user mode semaphore; however, like a critical section, it would only be for in-process use.

jancellor
October 23rd, 2008, 01:34 PM
Ahah, yes I see how to do it. See below for an implementation of something similar, which (I cannot resist adding) I though of myself just before.

http://msdn.microsoft.com/en-us/magazine/cc163642.aspx

I was struggling to see how to use the test-and-set instruction to do more general operations (eg Interlocked.Increment() in the example).

MikeAThon
October 23rd, 2008, 03:37 PM
As Arjay has pointed out, such an implementation will work only within the threads of one single process. It will not work across different processes.

tossy
October 25th, 2008, 01:43 AM
Well, hope following site will help you know more about User-mode semaphore. http://www.tech-archive.net/Archive/Development/microsoft.public.development.device.drivers/2008-02/msg00286.html

Arjay
October 25th, 2008, 12:33 PM
Well, hope following site will help you know more about User-mode semaphore. http://www.tech-archive.net/Archive/Development/microsoft.public.development.device.drivers/2008-02/msg00286.htmlNote there is a distinction about creating an event in user mode and a 'user mode' event that the link refers to. The event is still a kernel object, it just is getting created in user mode as opposed to getting created in kernel mode (like during creation of driver code).