thread_windows.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 = runtime.default_context();
  22. c := context;
  23. if ic, ok := t.init_context.?; ok {
  24. c = ic;
  25. }
  26. context = c;
  27. t.procedure(t);
  28. if t.init_context == nil {
  29. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  30. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
  31. }
  32. }
  33. sync.atomic_store(&t.done, true);
  34. return 0;
  35. }
  36. thread := new(Thread);
  37. if thread == nil {
  38. return nil;
  39. }
  40. thread.creation_allocator = context.allocator;
  41. win32_thread := win32.CreateThread(nil, 0, __windows_thread_entry_proc, thread, win32.CREATE_SUSPENDED, &win32_thread_id);
  42. if win32_thread == nil {
  43. free(thread, thread.creation_allocator);
  44. return nil;
  45. }
  46. thread.procedure = procedure;
  47. thread.win32_thread = win32_thread;
  48. thread.win32_thread_id = win32_thread_id;
  49. thread.init_context = context;
  50. ok := win32.SetThreadPriority(win32_thread, _thread_priority_map[priority]);
  51. assert(ok == true);
  52. return thread;
  53. }
  54. _start :: proc(thread: ^Thread) {
  55. win32.ResumeThread(thread.win32_thread);
  56. }
  57. _is_done :: proc(using thread: ^Thread) -> bool {
  58. // NOTE(tetra, 2019-10-31): Apparently using wait_for_single_object and
  59. // checking if it didn't time out immediately, is not good enough,
  60. // so we do it this way instead.
  61. return sync.atomic_load(&done);
  62. }
  63. _join :: proc(using thread: ^Thread) {
  64. if win32_thread != win32.INVALID_HANDLE {
  65. win32.WaitForSingleObject(win32_thread, win32.INFINITE);
  66. win32.CloseHandle(win32_thread);
  67. win32_thread = win32.INVALID_HANDLE;
  68. }
  69. }
  70. _join_multiple :: proc(threads: ..^Thread) {
  71. MAXIMUM_WAIT_OBJECTS :: 64;
  72. handles: [MAXIMUM_WAIT_OBJECTS]win32.HANDLE;
  73. for k := 0; k < len(threads); k += MAXIMUM_WAIT_OBJECTS {
  74. count := min(len(threads) - k, MAXIMUM_WAIT_OBJECTS);
  75. j := 0;
  76. for i in 0..<count {
  77. handle := threads[i+k].win32_thread;
  78. if handle != win32.INVALID_HANDLE {
  79. handles[j] = handle;
  80. j += 1;
  81. }
  82. }
  83. win32.WaitForMultipleObjects(u32(j), &handles[0], true, win32.INFINITE);
  84. }
  85. for t in threads {
  86. win32.CloseHandle(t.win32_thread);
  87. t.win32_thread = win32.INVALID_HANDLE;
  88. }
  89. }
  90. _destroy :: proc(thread: ^Thread) {
  91. _join(thread);
  92. free(thread, thread.creation_allocator);
  93. }
  94. _terminate :: proc(using thread : ^Thread, exit_code: int) {
  95. win32.TerminateThread(win32_thread, u32(exit_code));
  96. }
  97. _yield :: proc() {
  98. win32.SwitchToThread();
  99. }