diff options
| author | gingerBill <bill@gingerbill.org> | 2022-01-10 14:50:28 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2022-01-10 14:50:28 +0000 |
| commit | 7cc265e14ce3ec08a5908d31441000bdcb4ac645 (patch) | |
| tree | 690cd607687a3879acaabe48d072dc5f358c59d4 /src/threading.cpp | |
| parent | 6f3e450c502a8d05653ffcd98c74e2e933a34d1d (diff) | |
Add mutex guards for signature scopes
Diffstat (limited to 'src/threading.cpp')
| -rw-r--r-- | src/threading.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/threading.cpp b/src/threading.cpp index b318e4ff1..e848bba00 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -68,6 +68,40 @@ void yield_thread(void); void yield_process(void); +struct MutexGuard { + MutexGuard() = delete; + MutexGuard(MutexGuard const &) = delete; + + MutexGuard(BlockingMutex *bm) : bm{bm} { + mutex_lock(this->bm); + } + MutexGuard(RecursiveMutex *rm) : rm{rm} { + mutex_lock(this->rm); + } + MutexGuard(BlockingMutex &bm) : bm{&bm} { + mutex_lock(this->bm); + } + MutexGuard(RecursiveMutex &rm) : rm{&rm} { + mutex_lock(this->rm); + } + ~MutexGuard() { + if (this->bm) { + mutex_unlock(this->bm); + } else if (this->rm) { + mutex_unlock(this->rm); + } + } + + operator bool() const { return true; } + + BlockingMutex *bm; + RecursiveMutex *rm; +}; + +#define MUTEX_GUARD_BLOCK(m) if (MutexGuard GB_DEFER_3(_mutex_guard_) = m) +#define MUTEX_GUARD(m) MutexGuard GB_DEFER_3(_mutex_guard_) = m + + #if defined(GB_SYSTEM_WINDOWS) struct BlockingMutex { SRWLOCK srwlock; |