Skip to content

Commit

Permalink
[gfm] add thread-safety support for Windows (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietMisdreavus authored Mar 8, 2022
1 parent 5bd1108 commit fce0dee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ option(CMARK_TESTS "Build cmark-gfm tests and enable testing" ON)
option(CMARK_STATIC "Build static libcmark-gfm library" ON)
option(CMARK_SHARED "Build shared libcmark-gfm library" ON)
option(CMARK_LIB_FUZZER "Build libFuzzer fuzzing harness" OFF)
option(CMARK_THREADING "Add locks around static accesses via pthreads" OFF)
option(CMARK_THREADING "Add locks around static accesses" OFF)

add_subdirectory(src)
add_subdirectory(extensions)
Expand Down
29 changes: 29 additions & 0 deletions src/include/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

#ifdef CMARK_THREADING

#if __has_include(<unistd.h>)
#include <unistd.h>
#endif

#if defined (_POSIX_THREADS)

#include <pthread.h>

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

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

#elif defined(_WIN32) // building for windows

#define _WIN32_WINNT 0x0600
#include <synchapi.h>

#define CMARK_DEFINE_ONCE(NAME) static INIT_ONCE NAME##_once = INIT_ONCE_STATIC_INIT;

#define CMARK_RUN_ONCE(NAME, FUNC) do { \
BOOL fStatus; BOOL fPending; \
fStatus = InitOnceBeginInitialize(&NAME##_once, 0, &fPending, NULL); \
if (!fStatus || !fPending) break; \
FUNC(); \
InitOnceComplete(&NAME##_once, 0, NULL); \
} while (0);

#define CMARK_DEFINE_LOCK(NAME) static SRWLOCK NAME##_lock = SRWLOCK_INIT;

#define CMARK_INITIALIZE_AND_LOCK(NAME) AcquireSRWLockExclusive(&NAME##_lock);

#define CMARK_UNLOCK(NAME) ReleaseSRWLockExclusive(&NAME##_lock);

#endif

#else // no threading support

static CMARK_INLINE bool check_latch(int *latch) {
Expand Down

0 comments on commit fce0dee

Please sign in to comment.