diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2019-12-01 11:33:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-01 11:33:23 +0000 |
| commit | 3fd5c3cd851d8f4dfd441141ca7e96889f069933 (patch) | |
| tree | 67f47e79f5c5bb80a3ed1b1e9d79a61c08c0a29d /core/thread/thread_windows.odin | |
| parent | 0c0c83ee295fe8787a4bdc8b826a5432abba2ca9 (diff) | |
| parent | 99121d6ff2b02f3d16b791eb103bb9f9e8b96475 (diff) | |
Merge pull request #458 from Tetralux/linux-threads
Implement core:thread and core:sync on Unix using pthreads
Diffstat (limited to 'core/thread/thread_windows.odin')
| -rw-r--r-- | core/thread/thread_windows.odin | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/core/thread/thread_windows.odin b/core/thread/thread_windows.odin index 743f0fec8..5b956940f 100644 --- a/core/thread/thread_windows.odin +++ b/core/thread/thread_windows.odin @@ -1,27 +1,29 @@ package thread -import "core:runtime" +import "core:sync" import "core:sys/win32" -Thread_Proc :: #type proc(^Thread) -> int; - Thread_Os_Specific :: struct { win32_thread: win32.Handle, win32_thread_id: u32, + done: bool, // see note in `is_done` } -Thread :: struct { - using specific: Thread_Os_Specific, - procedure: Thread_Proc, - data: rawptr, - user_index: int, - - init_context: runtime.Context, - use_init_context: bool, +THREAD_PRIORITY_IDLE :: -15; +THREAD_PRIORITY_LOWEST :: -2; +THREAD_PRIORITY_BELOW_NORMAL :: -1; +THREAD_PRIORITY_NORMAL :: 0; +THREAD_PRIORITY_ABOVE_NORMAL :: 1; +THREAD_PRIORITY_HIGHEST :: 2; +THREAD_PRIORITY_TIME_CRITICAL :: 15; + +Thread_Priority :: enum i32 { + Normal = THREAD_PRIORITY_NORMAL, + Low = THREAD_PRIORITY_LOWEST, + High = THREAD_PRIORITY_HIGHEST, } - -create :: proc(procedure: Thread_Proc) -> ^Thread { +create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread { win32_thread_id: u32; __windows_thread_entry_proc :: proc "c" (t: ^Thread) -> i32 { @@ -31,7 +33,9 @@ create :: proc(procedure: Thread_Proc) -> ^Thread { } context = c; - return i32(t.procedure(t)); + t.procedure(t); + sync.atomic_store(&t.done, true, .Sequentially_Consistent); + return 0; } @@ -47,6 +51,9 @@ create :: proc(procedure: Thread_Proc) -> ^Thread { thread.win32_thread = win32_thread; thread.win32_thread_id = win32_thread_id; + ok := win32.set_thread_priority(win32_thread, i32(priority)); + assert(ok == true); + return thread; } @@ -55,8 +62,10 @@ start :: proc(using thread: ^Thread) { } is_done :: proc(using thread: ^Thread) -> bool { - res := win32.wait_for_single_object(win32_thread, 0); - return res != win32.WAIT_TIMEOUT; + // NOTE(tetra, 2019-10-31): Apparently using wait_for_single_object and + // checking if it didn't time out immediately, is not good enough, + // so we do it this way instead. + return sync.atomic_load(&done, .Sequentially_Consistent); } join :: proc(using thread: ^Thread) { @@ -72,4 +81,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 |