diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2025-09-23 11:38:32 +0100 |
|---|---|---|
| committer | gingerBill <gingerBill@users.noreply.github.com> | 2025-09-23 11:38:32 +0100 |
| commit | e9d20a9b4a069815f76a23ce5f429862b155b2d6 (patch) | |
| tree | f006c610eaf5fbdd536c76cb4eb235b4d27b4ab4 /src/threading.cpp | |
| parent | 2f132b51ce133cb21986987b38f1d75fa560fa1b (diff) | |
Reimplement `RwMutex` on non-windows systems
Diffstat (limited to 'src/threading.cpp')
| -rw-r--r-- | src/threading.cpp | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/threading.cpp b/src/threading.cpp index a35176ce6..b1a0af2e4 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -423,28 +423,44 @@ gb_internal void semaphore_wait(Semaphore *s) { } struct RwMutex { - // TODO(bill): make this a proper RW mutex - BlockingMutex mutex; + BlockingMutex lock; + Condition cond; + int32_t readers; }; gb_internal void rw_mutex_lock(RwMutex *m) { - mutex_lock(&m->mutex); + mutex_lock(&m->lock); + while (m->readers != 0) { + condition_wait(&m->cond, &m->lock); + } } gb_internal bool rw_mutex_try_lock(RwMutex *m) { - return mutex_try_lock(&m->mutex); + // TODO(bill): rw_mutex_try_lock + rw_mutex_lock(m); + return true; } gb_internal void rw_mutex_unlock(RwMutex *m) { - mutex_unlock(&m->mutex); + condition_signal(&m->cond); + mutex_unlock(&m->lock); } gb_internal void rw_mutex_shared_lock(RwMutex *m) { - mutex_lock(&m->mutex); + mutex_lock(&m->lock); + m->readers += 1; + mutex_unlock(&m->lock); } gb_internal bool rw_mutex_try_shared_lock(RwMutex *m) { - return mutex_try_lock(&m->mutex); + // TODO(bill): rw_mutex_try_shared_lock + rw_mutex_shared_lock(m); + return true; } gb_internal void rw_mutex_shared_unlock(RwMutex *m) { - mutex_unlock(&m->mutex); + mutex_lock(&m->lock); + m->readers -= 1; + if (m->readers == 0) { + condition_signal(&m->cond); + } + mutex_unlock(&m->lock); } #endif |