thread_windows.odin 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //+build windows
  2. //+private
  3. package thread
  4. import "core:runtime"
  5. import sync "core:sync/sync2"
  6. import win32 "core:sys/windows"
  7. Thread_Os_Specific :: struct {
  8. win32_thread: win32.HANDLE,
  9. win32_thread_id: win32.DWORD,
  10. done: bool, // see note in `is_done`
  11. }
  12. _thread_priority_map := [Thread_Priority]i32{
  13. .Normal = 0,
  14. .Low = -2,
  15. .High = +2,
  16. };
  17. _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  18. win32_thread_id: win32.DWORD;
  19. __windows_thread_entry_proc :: proc "stdcall" (t_: rawptr) -> win32.DWORD {
  20. t := (^Thread)(t_);
  21. context = or_else(t.init_context.?, runtime.default_context());
  22. t.procedure(t);
  23. if t.init_context == nil {
  24. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  25. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
  26. }
  27. }
  28. sync.atomic_store(&t.done, true);
  29. return 0;
  30. }
  31. thread := new(Thread);
  32. if thread == nil {
  33. return nil;
  34. }
  35. thread.creation_allocator = context.allocator;
  36. win32_thread := win32.CreateThread(nil, 0, __windows_thread_entry_proc, thread, win32.CREATE_SUSPENDED, &win32_thread_id);
  37. if win32_thread == nil {
  38. free(thread, thread.creation_allocator);
  39. return nil;
  40. }
  41. thread.procedure = procedure;
  42. thread.win32_thread = win32_thread;
  43. thread.win32_thread_id = win32_thread_id;
  44. thread.init_context = context;
  45. ok := win32.SetThreadPriority(win32_thread, _thread_priority_map[priority]);
  46. assert(ok == true);
  47. return thread;
  48. }
  49. _start :: proc(thread: ^Thread) {
  50. win32.ResumeThread(thread.win32_thread);
  51. }
  52. _is_done :: proc(using thread: ^Thread) -> bool {
  53. // NOTE(tetra, 2019-10-31): Apparently using wait_for_single_object and
  54. // checking if it didn't time out immediately, is not good enough,
  55. // so we do it this way instead.
  56. return sync.atomic_load(&done);
  57. }
  58. _join :: proc(using thread: ^Thread) {
  59. if win32_thread != win32.INVALID_HANDLE {
  60. win32.WaitForSingleObject(win32_thread, win32.INFINITE);
  61. win32.CloseHandle(win32_thread);
  62. win32_thread = win32.INVALID_HANDLE;
  63. }
  64. }
  65. _join_multiple :: proc(threads: ..^Thread) {
  66. MAXIMUM_WAIT_OBJECTS :: 64;
  67. handles: [MAXIMUM_WAIT_OBJECTS]win32.HANDLE;
  68. for k := 0; k < len(threads); k += MAXIMUM_WAIT_OBJECTS {
  69. count := min(len(threads) - k, MAXIMUM_WAIT_OBJECTS);
  70. j := 0;
  71. for i in 0..<count {
  72. handle := threads[i+k].win32_thread;
  73. if handle != win32.INVALID_HANDLE {
  74. handles[j] = handle;
  75. j += 1;
  76. }
  77. }
  78. win32.WaitForMultipleObjects(u32(j), &handles[0], true, win32.INFINITE);
  79. }
  80. for t in threads {
  81. win32.CloseHandle(t.win32_thread);
  82. t.win32_thread = win32.INVALID_HANDLE;
  83. }
  84. }
  85. _destroy :: proc(thread: ^Thread) {
  86. _join(thread);
  87. free(thread, thread.creation_allocator);
  88. }
  89. _terminate :: proc(using thread : ^Thread, exit_code: int) {
  90. win32.TerminateThread(win32_thread, u32(exit_code));
  91. }
  92. _yield :: proc() {
  93. win32.SwitchToThread();
  94. }