Skip to content

Commit

Permalink
Mem lock win32 (#324)
Browse files Browse the repository at this point in the history
* mem: add win32 lock

* include windows.h
  • Loading branch information
alfredh authored Apr 19, 2022
1 parent 0928f75 commit bc9e69c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/mem/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <string.h>
#ifdef HAVE_PTHREAD
#include <pthread.h>
#elif defined (WIN32)
#include <windows.h>
#endif
#include <re_types.h>
#include <re_list.h>
Expand Down Expand Up @@ -64,6 +66,38 @@ static inline void mem_unlock(void)
pthread_mutex_unlock(&mem_mutex);
}

#elif defined (WIN32)

INIT_ONCE g_initMemLockOnce = INIT_ONCE_STATIC_INIT;
CRITICAL_SECTION g_memLock;


static BOOL CALLBACK InitHandleFunction(PINIT_ONCE initOnce,
PVOID parameter,
PVOID *lpContext)
{
(void)initOnce;
(void)parameter;

InitializeCriticalSection((LPCRITICAL_SECTION)lpContext);

return TRUE;
}


static inline void mem_lock(void)
{
InitOnceExecuteOnce(&g_initMemLockOnce, InitHandleFunction,
NULL, (PVOID*)&g_memLock);
EnterCriticalSection(&g_memLock);
}


static inline void mem_unlock(void)
{
LeaveCriticalSection(&g_memLock);
}

#else

#define mem_lock() /**< Stub */
Expand Down

0 comments on commit bc9e69c

Please sign in to comment.