aboutsummaryrefslogtreecommitdiff
path: root/src/threading.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-08-19 17:51:19 +0100
committergingerBill <bill@gingerbill.org>2021-08-19 17:51:19 +0100
commitfa4f3aa7ad3f9530f78f22bd8d0e00d168ab2509 (patch)
treea5970ee21bd91b5a8a409e0e62f92ca94ec57a9c /src/threading.cpp
parenta90fe7211c226ab6dd7580ec2323758a98214bf7 (diff)
Correct atomic usage
Diffstat (limited to 'src/threading.cpp')
-rw-r--r--src/threading.cpp22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/threading.cpp b/src/threading.cpp
index 803cdb662..349f234f4 100644
--- a/src/threading.cpp
+++ b/src/threading.cpp
@@ -18,9 +18,9 @@ struct Thread {
isize user_index;
isize volatile return_value;
- Semaphore *semaphore;
- isize stack_size;
- b32 volatile is_running;
+ Semaphore * semaphore;
+ isize stack_size;
+ std::atomic<bool> is_running;
};
@@ -239,7 +239,7 @@ void thread_init(Thread *t) {
}
void thread_destroy(Thread *t) {
- if (t->is_running) thread_join(t);
+ thread_join(t);
semaphore_destroy(t->semaphore);
gb_free(heap_allocator(), t->semaphore);
}
@@ -254,14 +254,14 @@ void gb__thread_run(Thread *t) {
DWORD __stdcall internal_thread_proc(void *arg) {
Thread *t = cast(Thread *)arg;
gb__thread_run(t);
- t->is_running = false;
+ t->is_running.store(false);
return 0;
}
#else
void * internal_thread_proc(void *arg) {
Thread *t = cast(Thread *)arg;
gb__thread_run(t);
- t->is_running = false;
+ t->is_running.store(false);
return NULL;
}
#endif
@@ -269,12 +269,12 @@ void gb__thread_run(Thread *t) {
void thread_start(Thread *t, ThreadProc *proc, void *user_data) { thread_start_with_stack(t, proc, user_data, 0); }
void thread_start_with_stack(Thread *t, ThreadProc *proc, void *user_data, isize stack_size) {
- GB_ASSERT(!t->is_running);
+ GB_ASSERT(!t->is_running.load());
GB_ASSERT(proc != NULL);
t->proc = proc;
t->user_data = user_data;
t->stack_size = stack_size;
- t->is_running = true;
+ t->is_running.store(true);
#if defined(GB_SYSTEM_WINDOWS)
t->win32_handle = CreateThread(NULL, stack_size, internal_thread_proc, t, 0, NULL);
@@ -296,7 +296,7 @@ void thread_start_with_stack(Thread *t, ThreadProc *proc, void *user_data, isize
}
void thread_join(Thread *t) {
- if (!t->is_running) return;
+ if (!t->is_running.load()) return;
#if defined(GB_SYSTEM_WINDOWS)
WaitForSingleObject(t->win32_handle, INFINITE);
@@ -306,10 +306,10 @@ void thread_join(Thread *t) {
pthread_join(t->posix_handle, NULL);
t->posix_handle = 0;
#endif
- t->is_running = false;
+ t->is_running.store(false);
}
-bool thread_is_running(Thread const *t) { return t->is_running != 0; }
+bool thread_is_running(Thread const *t) { return t->is_running.load(); }
void thread_set_name(Thread *t, char const *name) {
#if defined(GB_COMPILER_MSVC)