Skip to content

Commit fce0dee

Browse files
[gfm] add thread-safety support for Windows (#34)
1 parent 5bd1108 commit fce0dee

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ option(CMARK_TESTS "Build cmark-gfm tests and enable testing" ON)
1818
option(CMARK_STATIC "Build static libcmark-gfm library" ON)
1919
option(CMARK_SHARED "Build shared libcmark-gfm library" ON)
2020
option(CMARK_LIB_FUZZER "Build libFuzzer fuzzing harness" OFF)
21-
option(CMARK_THREADING "Add locks around static accesses via pthreads" OFF)
21+
option(CMARK_THREADING "Add locks around static accesses" OFF)
2222

2323
add_subdirectory(src)
2424
add_subdirectory(extensions)

src/include/mutex.h

+29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
#ifdef CMARK_THREADING
77

8+
#if __has_include(<unistd.h>)
9+
#include <unistd.h>
10+
#endif
11+
12+
#if defined (_POSIX_THREADS)
13+
814
#include <pthread.h>
915

1016
#define CMARK_DEFINE_ONCE(NAME) static pthread_once_t NAME##_once = PTHREAD_ONCE_INIT;
@@ -22,6 +28,29 @@ pthread_mutex_lock(&NAME##_lock);
2228

2329
#define CMARK_UNLOCK(NAME) pthread_mutex_unlock(&NAME##_lock);
2430

31+
#elif defined(_WIN32) // building for windows
32+
33+
#define _WIN32_WINNT 0x0600
34+
#include <synchapi.h>
35+
36+
#define CMARK_DEFINE_ONCE(NAME) static INIT_ONCE NAME##_once = INIT_ONCE_STATIC_INIT;
37+
38+
#define CMARK_RUN_ONCE(NAME, FUNC) do { \
39+
BOOL fStatus; BOOL fPending; \
40+
fStatus = InitOnceBeginInitialize(&NAME##_once, 0, &fPending, NULL); \
41+
if (!fStatus || !fPending) break; \
42+
FUNC(); \
43+
InitOnceComplete(&NAME##_once, 0, NULL); \
44+
} while (0);
45+
46+
#define CMARK_DEFINE_LOCK(NAME) static SRWLOCK NAME##_lock = SRWLOCK_INIT;
47+
48+
#define CMARK_INITIALIZE_AND_LOCK(NAME) AcquireSRWLockExclusive(&NAME##_lock);
49+
50+
#define CMARK_UNLOCK(NAME) ReleaseSRWLockExclusive(&NAME##_lock);
51+
52+
#endif
53+
2554
#else // no threading support
2655

2756
static CMARK_INLINE bool check_latch(int *latch) {

0 commit comments

Comments
 (0)