|
@@ -929,12 +929,18 @@ GB_DEF void gb_semaphore_wait (gbSemaphore *s);
|
|
|
|
|
|
// Mutex
|
|
// Mutex
|
|
// TODO(bill): Should this be replaced with a CRITICAL_SECTION on win32 or is the better?
|
|
// TODO(bill): Should this be replaced with a CRITICAL_SECTION on win32 or is the better?
|
|
|
|
+#if defined(GB_SYSTEM_WINDOWS)
|
|
|
|
+typedef struct gbMutex {
|
|
|
|
+ CRITICAL_SECTION win32_critical_section;
|
|
|
|
+} gbMutex;
|
|
|
|
+#else
|
|
typedef struct gbMutex {
|
|
typedef struct gbMutex {
|
|
gbSemaphore semaphore;
|
|
gbSemaphore semaphore;
|
|
gbAtomic32 counter;
|
|
gbAtomic32 counter;
|
|
gbAtomic32 owner;
|
|
gbAtomic32 owner;
|
|
i32 recursion;
|
|
i32 recursion;
|
|
} gbMutex;
|
|
} gbMutex;
|
|
|
|
+#endif
|
|
|
|
|
|
GB_DEF void gb_mutex_init (gbMutex *m);
|
|
GB_DEF void gb_mutex_init (gbMutex *m);
|
|
GB_DEF void gb_mutex_destroy (gbMutex *m);
|
|
GB_DEF void gb_mutex_destroy (gbMutex *m);
|
|
@@ -4595,46 +4601,70 @@ gb_inline void gb_semaphore_release(gbSemaphore *s) { gb_semaphore_post(s, 1); }
|
|
#error
|
|
#error
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
+gb_inline void gb_mutex_init(gbMutex *m) {
|
|
|
|
+#if defined(GB_SYSTEM_WINDOWS)
|
|
|
|
+ InitializeCriticalSection(&m->win32_critical_section);
|
|
|
|
+#else
|
|
// NOTE(bill): THIS IS FUCKING AWESOME THAT THIS "MUTEX" IS FAST AND RECURSIVE TOO!
|
|
// NOTE(bill): THIS IS FUCKING AWESOME THAT THIS "MUTEX" IS FAST AND RECURSIVE TOO!
|
|
// NOTE(bill): WHO THE FUCK NEEDS A NORMAL MUTEX NOW?!?!?!?!
|
|
// NOTE(bill): WHO THE FUCK NEEDS A NORMAL MUTEX NOW?!?!?!?!
|
|
-gb_inline void gb_mutex_init(gbMutex *m) {
|
|
|
|
gb_atomic32_store(&m->counter, 0);
|
|
gb_atomic32_store(&m->counter, 0);
|
|
gb_atomic32_store(&m->owner, gb_thread_current_id());
|
|
gb_atomic32_store(&m->owner, gb_thread_current_id());
|
|
gb_semaphore_init(&m->semaphore);
|
|
gb_semaphore_init(&m->semaphore);
|
|
m->recursion = 0;
|
|
m->recursion = 0;
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
-gb_inline void gb_mutex_destroy(gbMutex *m) { gb_semaphore_destroy(&m->semaphore); }
|
|
|
|
|
|
+gb_inline void gb_mutex_destroy(gbMutex *m) {
|
|
|
|
+#if defined(GB_SYSTEM_WINDOWS)
|
|
|
|
+ DeleteCriticalSection(&m->win32_critical_section);
|
|
|
|
+#else
|
|
|
|
+ gb_semaphore_destroy(&m->semaphore);
|
|
|
|
+#endif
|
|
|
|
+}
|
|
|
|
|
|
gb_inline void gb_mutex_lock(gbMutex *m) {
|
|
gb_inline void gb_mutex_lock(gbMutex *m) {
|
|
|
|
+#if defined(GB_SYSTEM_WINDOWS)
|
|
|
|
+ EnterCriticalSection(&m->win32_critical_section);
|
|
|
|
+#else
|
|
i32 thread_id = cast(i32)gb_thread_current_id();
|
|
i32 thread_id = cast(i32)gb_thread_current_id();
|
|
if (gb_atomic32_fetch_add(&m->counter, 1) > 0) {
|
|
if (gb_atomic32_fetch_add(&m->counter, 1) > 0) {
|
|
- if (thread_id != gb_atomic32_load(&m->owner))
|
|
|
|
|
|
+ if (thread_id != gb_atomic32_load(&m->owner)) {
|
|
gb_semaphore_wait(&m->semaphore);
|
|
gb_semaphore_wait(&m->semaphore);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
gb_atomic32_store(&m->owner, thread_id);
|
|
gb_atomic32_store(&m->owner, thread_id);
|
|
m->recursion++;
|
|
m->recursion++;
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
gb_inline b32 gb_mutex_try_lock(gbMutex *m) {
|
|
gb_inline b32 gb_mutex_try_lock(gbMutex *m) {
|
|
|
|
+#if defined(GB_SYSTEM_WINDOWS)
|
|
|
|
+ return TryEnterCriticalSection(&m->win32_critical_section) != 0;
|
|
|
|
+#else
|
|
i32 thread_id = cast(i32)gb_thread_current_id();
|
|
i32 thread_id = cast(i32)gb_thread_current_id();
|
|
if (gb_atomic32_load(&m->owner) == thread_id) {
|
|
if (gb_atomic32_load(&m->owner) == thread_id) {
|
|
gb_atomic32_fetch_add(&m->counter, 1);
|
|
gb_atomic32_fetch_add(&m->counter, 1);
|
|
} else {
|
|
} else {
|
|
i32 expected = 0;
|
|
i32 expected = 0;
|
|
- if (gb_atomic32_load(&m->counter) != 0)
|
|
|
|
|
|
+ if (gb_atomic32_load(&m->counter) != 0) {
|
|
return false;
|
|
return false;
|
|
- if (!gb_atomic32_compare_exchange(&m->counter, expected, 1))
|
|
|
|
|
|
+ }
|
|
|
|
+ if (!gb_atomic32_compare_exchange(&m->counter, expected, 1)) {
|
|
return false;
|
|
return false;
|
|
|
|
+ }
|
|
gb_atomic32_store(&m->owner, thread_id);
|
|
gb_atomic32_store(&m->owner, thread_id);
|
|
}
|
|
}
|
|
|
|
|
|
m->recursion++;
|
|
m->recursion++;
|
|
return true;
|
|
return true;
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
gb_inline void gb_mutex_unlock(gbMutex *m) {
|
|
gb_inline void gb_mutex_unlock(gbMutex *m) {
|
|
|
|
+#if defined(GB_SYSTEM_WINDOWS)
|
|
|
|
+ LeaveCriticalSection(&m->win32_critical_section);
|
|
|
|
+#else
|
|
i32 recursion;
|
|
i32 recursion;
|
|
i32 thread_id = cast(i32)gb_thread_current_id();
|
|
i32 thread_id = cast(i32)gb_thread_current_id();
|
|
|
|
|
|
@@ -4648,6 +4678,7 @@ gb_inline void gb_mutex_unlock(gbMutex *m) {
|
|
if (recursion == 0)
|
|
if (recursion == 0)
|
|
gb_semaphore_release(&m->semaphore);
|
|
gb_semaphore_release(&m->semaphore);
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|