Speed up llvm ir printing; Use CRITICAL_SECTION for Mutex on windows

This commit is contained in:
Ginger Bill
2017-07-30 19:47:37 +01:00
parent 629b248f53
commit a3c1ac2030
2 changed files with 417 additions and 374 deletions
+36 -5
View File
@@ -929,12 +929,18 @@ GB_DEF void gb_semaphore_wait (gbSemaphore *s);
// Mutex
// 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 {
gbSemaphore semaphore;
gbAtomic32 counter;
gbAtomic32 owner;
i32 recursion;
} gbMutex;
#endif
GB_DEF void gb_mutex_init (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
#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): 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->owner, gb_thread_current_id());
gb_semaphore_init(&m->semaphore);
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) {
#if defined(GB_SYSTEM_WINDOWS)
EnterCriticalSection(&m->win32_critical_section);
#else
i32 thread_id = cast(i32)gb_thread_current_id();
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_atomic32_store(&m->owner, thread_id);
m->recursion++;
#endif
}
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();
if (gb_atomic32_load(&m->owner) == thread_id) {
gb_atomic32_fetch_add(&m->counter, 1);
} else {
i32 expected = 0;
if (gb_atomic32_load(&m->counter) != 0)
if (gb_atomic32_load(&m->counter) != 0) {
return false;
if (!gb_atomic32_compare_exchange(&m->counter, expected, 1))
}
if (!gb_atomic32_compare_exchange(&m->counter, expected, 1)) {
return false;
}
gb_atomic32_store(&m->owner, thread_id);
}
m->recursion++;
return true;
#endif
}
gb_inline void gb_mutex_unlock(gbMutex *m) {
#if defined(GB_SYSTEM_WINDOWS)
LeaveCriticalSection(&m->win32_critical_section);
#else
i32 recursion;
i32 thread_id = cast(i32)gb_thread_current_id();
@@ -4648,6 +4678,7 @@ gb_inline void gb_mutex_unlock(gbMutex *m) {
if (recursion == 0)
gb_semaphore_release(&m->semaphore);
}
#endif
}
+381 -369
View File
File diff suppressed because it is too large Load Diff