aboutsummaryrefslogtreecommitdiff
path: root/src/common.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-08-19 15:38:21 +0100
committergingerBill <bill@gingerbill.org>2021-08-19 15:38:21 +0100
commitdf372dbd5be1fd219322f0b0b8f141e036fb5203 (patch)
treebe6649d50105d2c9b4d8ef66509e99a33b67efee /src/common.cpp
parent5c4d95d539944dcb6617ce9c70bcedd12552225a (diff)
Migrate and remove more from gb.h
Diffstat (limited to 'src/common.cpp')
-rw-r--r--src/common.cpp130
1 files changed, 2 insertions, 128 deletions
diff --git a/src/common.cpp b/src/common.cpp
index fd6268a82..ab6dbabb1 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -29,135 +29,9 @@
#include <string.h>
#include <atomic> // Because I wanted the C++11 memory order semantics, of which gb.h does not offer (because it was a C89 library)
+gbAllocator heap_allocator(void);
-#if defined(GB_SYSTEM_WINDOWS)
- struct BlockingMutex {
- SRWLOCK srwlock;
- };
- void mutex_init(BlockingMutex *m) {
- }
- void mutex_destroy(BlockingMutex *m) {
- }
- void mutex_lock(BlockingMutex *m) {
- AcquireSRWLockExclusive(&m->srwlock);
- }
- bool mutex_try_lock(BlockingMutex *m) {
- return !!TryAcquireSRWLockExclusive(&m->srwlock);
- }
- void mutex_unlock(BlockingMutex *m) {
- ReleaseSRWLockExclusive(&m->srwlock);
- }
-
- struct RecursiveMutex {
- CRITICAL_SECTION win32_critical_section;
- };
- void mutex_init(RecursiveMutex *m) {
- InitializeCriticalSection(&m->win32_critical_section);
- }
- void mutex_destroy(RecursiveMutex *m) {
- DeleteCriticalSection(&m->win32_critical_section);
- }
- void mutex_lock(RecursiveMutex *m) {
- EnterCriticalSection(&m->win32_critical_section);
- }
- bool mutex_try_lock(RecursiveMutex *m) {
- return TryEnterCriticalSection(&m->win32_critical_section) != 0;
- }
- void mutex_unlock(RecursiveMutex *m) {
- LeaveCriticalSection(&m->win32_critical_section);
- }
-
- struct Semaphore {
- void *win32_handle;
- };
-
- gb_inline void semaphore_init(Semaphore *s) {
- s->win32_handle = CreateSemaphoreA(NULL, 0, I32_MAX, NULL);
- }
- gb_inline void semaphore_destroy(Semaphore *s) {
- CloseHandle(s->win32_handle);
- }
- gb_inline void semaphore_post(Semaphore *s, i32 count) {
- ReleaseSemaphore(s->win32_handle, count, NULL);
- }
- gb_inline void semaphore_wait(Semaphore *s) {
- WaitForSingleObjectEx(s->win32_handle, INFINITE, FALSE);
- }
-
- gb_inline void semaphore_release(Semaphore *s) {
- semaphore_post(s, 1);
- }
-
-#else
- struct BlockingMutex {
- pthread_mutex_t pthread_mutex;
- };
- void mutex_init(BlockingMutex *m) {
- pthread_mutex_init(&m->pthread_mutex, nullptr);
- }
- void mutex_destroy(BlockingMutex *m) {
- pthread_mutex_destroy(&m->pthread_mutex);
- }
- void mutex_lock(BlockingMutex *m) {
- pthread_mutex_lock(&m->pthread_mutex);
- }
- bool mutex_try_lock(BlockingMutex *m) {
- return pthread_mutex_trylock(&m->pthread_mutex) == 0;
- }
- void mutex_unlock(BlockingMutex *m) {
- pthread_mutex_unlock(&m->pthread_mutex);
- }
-
- struct RecursiveMutex {
- pthread_mutex_t pthread_mutex;
- pthread_mutexattr_t pthread_mutexattr;
- };
- void mutex_init(RecursiveMutex *m) {
- pthread_mutexattr_init(&m->pthread_mutexattr);
- pthread_mutexattr_settype(&m->pthread_mutexattr, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&m->pthread_mutex, &m->pthread_mutexattr);
- }
- void mutex_destroy(RecursiveMutex *m) {
- pthread_mutex_destroy(&m->pthread_mutex);
- }
- void mutex_lock(RecursiveMutex *m) {
- pthread_mutex_lock(&m->pthread_mutex);
- }
- bool mutex_try_lock(RecursiveMutex *m) {
- return pthread_mutex_trylock(&m->pthread_mutex) == 0;
- }
- void mutex_unlock(RecursiveMutex *m) {
- pthread_mutex_unlock(&m->pthread_mutex);
- }
-
- #if defined(GB_SYSTEM_OSX)
- struct Semaphore {
- semaphore_t osx_handle;
- };
-
- gb_inline void semaphore_init (Semaphore *s) { semaphore_create(mach_task_self(), &s->osx_handle, SYNC_POLICY_FIFO, 0); }
- gb_inline void semaphore_destroy(Semaphore *s) { semaphore_destroy(mach_task_self(), s->osx_handle); }
- gb_inline void semaphore_post (Semaphore *s, i32 count) { while (count --> 0) semaphore_signal(s->osx_handle); }
- gb_inline void semaphore_wait (Semaphore *s) { semaphore_wait(s->osx_handle); }
- #elif defined(GB_SYSTEM_UNIX)
- struct Semaphore {
- sem_t unix_handle;
- };
-
- gb_inline void semaphore_init (Semaphore *s) { sem_init(&s->unix_handle, 0, 0); }
- gb_inline void semaphore_destroy(Semaphore *s) { sem_destroy(&s->unix_handle); }
- gb_inline void semaphore_post (Semaphore *s, i32 count) { while (count --> 0) sem_post(&s->unix_handle); }
- gb_inline void semaphore_wait (Semaphore *s) { int i; do { i = sem_wait(&s->unix_handle); } while (i == -1 && errno == EINTR); }
- #else
- #error
- #endif
-
- gb_inline void semaphore_release(Semaphore *s) {
- semaphore_post(s, 1);
- }
-#endif
-
-
+#include "threading.cpp"
gb_inline void zero_size(void *ptr, isize len) {
memset(ptr, 0, len);