Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mem lock win32 #324

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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