Skip to content

C++ semaphores cause a deadlock if a GC happens while they're locked #1305

@davidgiven

Description

@davidgiven

...or at least, I think.

I was using a semaphore to wait for a thread pool to empty, and would find that my program would get some work done and then deadlock. Looking at the stack traces, I could see my master thread waiting on the semaphore, my worker threads waiting inside hx::ExitGCFreeZone, and the garbage collector waiting for all the threads to halt.

Finding the mutex and semaphore implementations, I found this in hxMutex:

        void Acquire()
        {
                hx::EnterGCFreeZone();
                mMutex.Lock();
                hx::ExitGCFreeZone();
        }

...and this in hxSemaphore:

  void Acquire() {
#if HX_WINDOWS
        WaitForSingleObject(sem, INFINITE);
#elif defined(POSIX_SEMAPHORE)
    sem_wait(&sem);
#elif defined(APPLE_SEMAPHORE)
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
#endif
  }

That is, no calls to EnterGCFreeZone() and ExitGCFreeZone(). I tried adding them without really knowing what I was doing and now my program runs fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions