aboutsummaryrefslogtreecommitdiff
path: root/core/thread
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2019-12-29 18:08:48 +0000
committergingerBill <bill@gingerbill.org>2019-12-29 18:08:48 +0000
commita8a4dc1eb1d70bf54dff983ec70ec5c63fd001a2 (patch)
tree2366eefd8ca19c463e7b2b9cef553440dd502e0b /core/thread
parent9e9e905431a3c2841fc809f9addbff32b2288d4f (diff)
Make default `context.temp_allocator` thread safe when using `package thread`
Diffstat (limited to 'core/thread')
-rw-r--r--core/thread/thread_unix.odin10
-rw-r--r--core/thread/thread_windows.odin18
2 files changed, 23 insertions, 5 deletions
diff --git a/core/thread/thread_unix.odin b/core/thread/thread_unix.odin
index 52b8a81f4..741e52566 100644
--- a/core/thread/thread_unix.odin
+++ b/core/thread/thread_unix.odin
@@ -1,8 +1,9 @@
// +build linux, darwin
package thread;
-import "core:sys/unix"
+import "core:runtime"
import "core:sync"
+import "core:sys/unix"
// NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
// Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
@@ -62,6 +63,13 @@ create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^T
context = c;
t.procedure(t);
+
+ if !t.use_init_context {
+ if context.temp_allocator.data == &runtime.global_scratch_allocator_data {
+ runtime.global_scratch_allocator_destroy(auto_cast context.temp_allocator.data);
+ }
+ }
+
sync.atomic_store(&t.done, true, .Sequentially_Consistent);
return nil;
}
diff --git a/core/thread/thread_windows.odin b/core/thread/thread_windows.odin
index 5b956940f..79ebe48d6 100644
--- a/core/thread/thread_windows.odin
+++ b/core/thread/thread_windows.odin
@@ -1,5 +1,6 @@
package thread
+import "core:runtime"
import "core:sync"
import "core:sys/win32"
@@ -34,6 +35,13 @@ create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^T
context = c;
t.procedure(t);
+
+ if !t.use_init_context {
+ if context.temp_allocator.data == &runtime.global_scratch_allocator_data {
+ runtime.global_scratch_allocator_destroy(auto_cast context.temp_allocator.data);
+ }
+ }
+
sync.atomic_store(&t.done, true, .Sequentially_Consistent);
return 0;
}
@@ -69,9 +77,11 @@ is_done :: proc(using thread: ^Thread) -> bool {
}
join :: proc(using thread: ^Thread) {
- win32.wait_for_single_object(win32_thread, win32.INFINITE);
- win32.close_handle(win32_thread);
- win32_thread = win32.INVALID_HANDLE;
+ if win32_thread != win32.INVALID_HANDLE {
+ win32.wait_for_single_object(win32_thread, win32.INFINITE);
+ win32.close_handle(win32_thread);
+ win32_thread = win32.INVALID_HANDLE;
+ }
}
destroy :: proc(thread: ^Thread) {
@@ -81,4 +91,4 @@ destroy :: proc(thread: ^Thread) {
terminate :: proc(using thread : ^Thread, exit_code : u32) {
win32.terminate_thread(win32_thread, exit_code);
-} \ No newline at end of file
+}