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

Top Page

Reply to this message
Author: admin
Date:  
To: pcre-dev
Subject: [pcre-dev] [Bug 2377] allocator_grab_lock not thread safe on windows
https://bugs.exim.org/show_bug.cgi?id=2377

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:

https://docs.microsoft.com/it-it/windows/desktop/Sync/using-one-time-initialization

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,
NULL, NULL)) 
     /* 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:

https://docs.microsoft.com/en-us/windows/desktop/sync/interlocked-variable-access

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
      CloseHandle(mutex);
    }
  }


WaitForSingleObject(global_mutex, INFINITE);
}


Hope this helps.

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