CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    5

    User-mode semaphore possible?

    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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: User-mode semaphore possible?

    The critical section is the only user mode synchronization object available on Windows - all the rest are kernel mode objects (mutex, semaphore, etc.).

  3. #3
    Join Date
    Apr 2008
    Posts
    5

    Re: User-mode semaphore possible?

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: User-mode semaphore possible?

    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.

  5. #5
    Join Date
    Apr 2008
    Posts
    5

    Re: User-mode semaphore possible?

    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).

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: User-mode semaphore possible?

    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.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: User-mode semaphore possible?

    Quote Originally Posted by Arjay View Post
    The critical section is the only user mode synchronization object available on Windows - all the rest are kernel mode objects (mutex, semaphore, etc.).
    This isn't entirely true anymore since Windows Server 2003.

    When a thread is waiting for a critical section and it ends up either reaching it's spin count limit or reaching the end of it's timeslice, the thread is for all intents and purposes suspended.

    The thread then won't wake up again and retest the critical section by itself anymore. It's the Kernel that will manage the suspended threads in a wait queue, and when the CS becomes available, the kernel will awaken a thread from the wait queue (not necessarily in FIFO order)and transfer control to it.

    This was done to increase overall throughput by prevending lots of threads all continuously polling the same CS. Internally the kernel uses a kernel-semaphore for each CS to achieve this.


    So while it's true that the CS is the most lightweight basic synchronisation object, it does not guarantee it won't ever leave user mode. Under the normal assumptions a CS should be used for (very short lived locks) this ends up being an overall win. Fewer kernel transitions, at a slight overhead on user mode.
    Abusing a CS under the 'lightweight' assumption for something they aren't really designed for, could end up giving you poorer overal performance.

    If for some reason you MUST guarantee absolutely no kernel transition, you will need to make your own lock primitive.

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: User-mode semaphore possible?

    Quote Originally Posted by OReubens View Post
    If for some reason you MUST guarantee absolutely no kernel transition, [...]
    just for my personal curiosity, in which scenario would this be required ?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: User-mode semaphore possible?

    Just wanted to mention that unnamed kernel objects can work cross-process as well using DuplicateHandle.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: User-mode semaphore possible?

    true. I keep forgetting about that.

    It just never seems like a "better" approach to named objects for the stuff I typically work on.

    It also means you have to have some formal communication between the 2 processes so one process can access the handle of the other to duplicate it, or one process services a "give me a (duplicate) handle to X" request of the other process.

    Named objects seem more elegant.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: User-mode semaphore possible?

    With the duplicate handle approach, there is the chicken and the egg problem (of getting the handle over to the second process so it can be duplicated). I generally prefer using a named object as well.

    In fact, that's what I do in my memory mapped file CG article.

    C++ Programming: Memory Mapped Files using RAII

    ...because I know you guys have nothing better to do than read a 3 year old article of mine.

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: User-mode semaphore possible?

    Anything where timing is critical, i.e. anything "real time" (in so far as windows can even satisfy that).
    Multimedia recording/playback, process control, drivers (although for windows you'd usually want a kernel driver for this case), ...

    There's better dedicated OSes for such type of things, but sometimes, people insist on doing such with Windows.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured