[pcre-dev] [Bug 2377] allocator_grab_lock not thread safe on…

Top Page

Reply to this message
Author: admin
To: pcre-dev
Subject: [pcre-dev] [Bug 2377] allocator_grab_lock not thread safe on windows

Giuseppe D'Angelo <dangelog@???> changed:

           What    |Removed                     |Added
                 CC|                            |dangelog@???

--- Comment #2 from Giuseppe D'Angelo <dangelog@???> ---
What's the minimum Windows version that PCRE wants to support?

On Windows 7, it's fairly easy using the INIT_ONCE family of functions, for
instance as described here:


So possibly something like this (just written here, not tested...):

static HANDLE global_mutex = 0;
static INIT_ONCE global_mutex_once = INIT_ONCE_STATIC_INIT;

static BOOL CALLBACK sljit_grab_lock_initialize(PINIT_ONCE, PVOID, PVOID *) {
global_mutex = CreateMutex(NULL, TRUE, NULL);
return TRUE;

And change sljit_grab_lock to do

  if (!InitOnceExecuteOnce(&global_mutex_once, sljit_grab_lock_initialize,
     /* OS failure. abort()? */
  WaitForSingleObject(global_mutex, INFINITE);

If you also need to support earlier versions of Windows, then one has to use
the Interlocked API:


Basically using the fact that HANDLE is a PVOID. So (again, pseudocode):

static volatile HANDLE global_mutex = 0;

SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_grab_lock(void) {
  if (!global_mutex) { // this read is atomic on Windows
    HANDLE mutex = CreateMutex(NULL, TRUE, NULL);
    if (InterlockedCompareExchangePointer(&global_mutex, mutex, NULL) != NULL)
      // some other thread got there first

WaitForSingleObject(global_mutex, INFINITE);

Hope this helps.

You are receiving this mail because:
You are on the CC list for the bug.