aboutsummaryrefslogtreecommitdiff
path: root/src/threading.cpp
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-04-27 14:37:15 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2022-04-27 14:37:15 +0200
commitc4e0d1efa1ec655bae9134b95a0fcd060cc7bbea (patch)
treec29bd0b78138e8d67aebe34ac689d13e32d9d15f /src/threading.cpp
parent6e61abc7d06f22129f93110a9f652c3eec21f0c6 (diff)
parent9349dfba8fec53f52f77a0c8928e115ec93ff447 (diff)
Merge branch 'master' into xml
Diffstat (limited to 'src/threading.cpp')
-rw-r--r--src/threading.cpp40
1 files changed, 39 insertions, 1 deletions
diff --git a/src/threading.cpp b/src/threading.cpp
index e9412b411..63e3415b2 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;
@@ -296,6 +330,8 @@ u32 thread_current_id(void) {
__asm__("mov %%gs:0x08,%0" : "=r"(thread_id));
#elif defined(GB_ARCH_64_BIT) && defined(GB_CPU_X86)
__asm__("mov %%fs:0x10,%0" : "=r"(thread_id));
+#elif defined(GB_SYSTEM_LINUX)
+ thread_id = gettid();
#else
#error Unsupported architecture for thread_current_id()
#endif
@@ -315,6 +351,8 @@ gb_inline void yield_thread(void) {
#endif
#elif defined(GB_CPU_X86)
_mm_pause();
+#elif defined(GB_CPU_ARM)
+ __asm__ volatile ("yield" : : : "memory");
#else
#error Unknown architecture
#endif
@@ -448,7 +486,7 @@ void thread_set_name(Thread *t, char const *name) {
#elif defined(GB_SYSTEM_OSX)
// TODO(bill): Test if this works
pthread_setname_np(name);
-#elif defined(GB_SYSTEM_FREEBSD)
+#elif defined(GB_SYSTEM_FREEBSD) || defined(GB_SYSTEM_OPENBSD)
pthread_set_name_np(t->posix_handle, name);
#else
// TODO(bill): Test if this works