Skip to content

Commit

Permalink
lib+win32: Swap out Mutex for CriticalSection
Browse files Browse the repository at this point in the history
Apparently, CriticalSection doesn't need to go to kernel space on every
acquisition, unlike with a Mutex.
Blind commit, awaiting runner to see if it builds.
  • Loading branch information
vkoskiv committed Jan 4, 2024
1 parent c0a6b2d commit 4dbf3b1
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/common/platform/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

struct cr_mutex {
#ifdef WINDOWS
HANDLE lock; // = INVALID_HANDLE_VALUE;
CRITICAL_SECTION lock;
#else
pthread_mutex_t lock; // = PTHREAD_MUTEX_INITIALIZER;
#endif
Expand All @@ -26,7 +26,7 @@ struct cr_mutex {
struct cr_mutex *mutex_create() {
struct cr_mutex *new = calloc(1, sizeof(*new));
#ifdef WINDOWS
new->lock = CreateMutex(NULL, FALSE, NULL);
InitializeCriticalSection(&new->lock);
#else
new->lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
#endif
Expand All @@ -35,15 +35,15 @@ struct cr_mutex *mutex_create() {

void mutex_lock(struct cr_mutex *m) {
#ifdef WINDOWS
WaitForSingleObject(m->lock, INFINITE);
EnterCriticalSection(&m->lock);
#else
pthread_mutex_lock(&m->lock);
#endif
}

void mutex_release(struct cr_mutex *m) {
#ifdef WINDOWS
ReleaseMutex(m->lock);
LeaveCriticalSection(&m->lock);
#else
pthread_mutex_unlock(&m->lock);
#endif
Expand Down

0 comments on commit 4dbf3b1

Please sign in to comment.