Correct atomic usage

This commit is contained in:
gingerBill
2021-08-19 17:51:19 +01:00
parent a90fe7211c
commit fa4f3aa7ad
3 changed files with 20 additions and 12 deletions
+8
View File
@@ -4176,6 +4176,10 @@ void check_with_workers(Checker *c, ThreadProc *proc, isize total_count) {
semaphore_wait(&c->info.collect_semaphore); semaphore_wait(&c->info.collect_semaphore);
for (isize i = 0; i < worker_count; i++) {
thread_join(threads+i);
}
for (isize i = 0; i < worker_count; i++) { for (isize i = 0; i < worker_count; i++) {
thread_destroy(threads+i); thread_destroy(threads+i);
} }
@@ -4811,6 +4815,10 @@ void check_procedure_bodies(Checker *c) {
semaphore_wait(&c->procs_to_check_semaphore); semaphore_wait(&c->procs_to_check_semaphore);
for (isize i = 0; i < worker_count; i++) {
thread_join(threads+i);
}
for (isize i = 0; i < worker_count; i++) { for (isize i = 0; i < worker_count; i++) {
thread_destroy(threads+i); thread_destroy(threads+i);
} }
+1 -1
View File
@@ -250,7 +250,7 @@ bool entity_has_deferred_procedure(Entity *e) {
} }
gb_global std::atomic<u64> global_entity_id = 0; gb_global std::atomic<u64> global_entity_id;
Entity *alloc_entity(EntityKind kind, Scope *scope, Token token, Type *type) { Entity *alloc_entity(EntityKind kind, Scope *scope, Token token, Type *type) {
gbAllocator a = permanent_allocator(); gbAllocator a = permanent_allocator();
+9 -9
View File
@@ -20,7 +20,7 @@ struct Thread {
Semaphore * semaphore; Semaphore * semaphore;
isize stack_size; isize stack_size;
b32 volatile is_running; std::atomic<bool> is_running;
}; };
@@ -239,7 +239,7 @@ void thread_init(Thread *t) {
} }
void thread_destroy(Thread *t) { void thread_destroy(Thread *t) {
if (t->is_running) thread_join(t); thread_join(t);
semaphore_destroy(t->semaphore); semaphore_destroy(t->semaphore);
gb_free(heap_allocator(), 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) { DWORD __stdcall internal_thread_proc(void *arg) {
Thread *t = cast(Thread *)arg; Thread *t = cast(Thread *)arg;
gb__thread_run(t); gb__thread_run(t);
t->is_running = false; t->is_running.store(false);
return 0; return 0;
} }
#else #else
void * internal_thread_proc(void *arg) { void * internal_thread_proc(void *arg) {
Thread *t = cast(Thread *)arg; Thread *t = cast(Thread *)arg;
gb__thread_run(t); gb__thread_run(t);
t->is_running = false; t->is_running.store(false);
return NULL; return NULL;
} }
#endif #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(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) { 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); GB_ASSERT(proc != NULL);
t->proc = proc; t->proc = proc;
t->user_data = user_data; t->user_data = user_data;
t->stack_size = stack_size; t->stack_size = stack_size;
t->is_running = true; t->is_running.store(true);
#if defined(GB_SYSTEM_WINDOWS) #if defined(GB_SYSTEM_WINDOWS)
t->win32_handle = CreateThread(NULL, stack_size, internal_thread_proc, t, 0, NULL); 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) { void thread_join(Thread *t) {
if (!t->is_running) return; if (!t->is_running.load()) return;
#if defined(GB_SYSTEM_WINDOWS) #if defined(GB_SYSTEM_WINDOWS)
WaitForSingleObject(t->win32_handle, INFINITE); WaitForSingleObject(t->win32_handle, INFINITE);
@@ -306,10 +306,10 @@ void thread_join(Thread *t) {
pthread_join(t->posix_handle, NULL); pthread_join(t->posix_handle, NULL);
t->posix_handle = 0; t->posix_handle = 0;
#endif #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) { void thread_set_name(Thread *t, char const *name) {
#if defined(GB_COMPILER_MSVC) #if defined(GB_COMPILER_MSVC)