thread_windows.odin 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //+build windows
  2. //+private
  3. package thread
  4. import "core:intrinsics"
  5. import "core:sync"
  6. import win32 "core:sys/windows"
  7. Thread_Os_Specific :: struct {
  8. win32_thread: win32.HANDLE,
  9. win32_thread_id: win32.DWORD,
  10. mutex: sync.Mutex,
  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) -> ^Thread {
  18. win32_thread_id: win32.DWORD
  19. __windows_thread_entry_proc :: proc "stdcall" (t_: rawptr) -> win32.DWORD {
  20. t := (^Thread)(t_)
  21. t.id = sync.current_thread_id()
  22. {
  23. init_context := t.init_context
  24. // NOTE(tetra, 2023-05-31): Must do this AFTER thread.start() is called, so that the user can set the init_context, etc!
  25. // Here on Windows, the thread is created in a suspended state, and so we can select the context anywhere before the call
  26. // to t.procedure().
  27. context = _select_context_for_thread(init_context)
  28. defer _maybe_destroy_default_temp_allocator(init_context)
  29. t.procedure(t)
  30. }
  31. intrinsics.atomic_store(&t.flags, t.flags + {.Done})
  32. if .Self_Cleanup in t.flags {
  33. win32.CloseHandle(t.win32_thread)
  34. t.win32_thread = win32.INVALID_HANDLE
  35. // NOTE(ftphikari): It doesn't matter which context 'free' received, right?
  36. context = {}
  37. free(t, t.creation_allocator)
  38. }
  39. return 0
  40. }
  41. thread := new(Thread)
  42. if thread == nil {
  43. return nil
  44. }
  45. thread.creation_allocator = context.allocator
  46. win32_thread := win32.CreateThread(nil, 0, __windows_thread_entry_proc, thread, win32.CREATE_SUSPENDED, &win32_thread_id)
  47. if win32_thread == nil {
  48. free(thread, thread.creation_allocator)
  49. return nil
  50. }
  51. thread.procedure = procedure
  52. thread.win32_thread = win32_thread
  53. thread.win32_thread_id = win32_thread_id
  54. ok := win32.SetThreadPriority(win32_thread, _thread_priority_map[priority])
  55. assert(ok == true)
  56. return thread
  57. }
  58. _start :: proc(t: ^Thread) {
  59. sync.guard(&t.mutex)
  60. t.flags += {.Started}
  61. win32.ResumeThread(t.win32_thread)
  62. }
  63. _is_done :: proc(t: ^Thread) -> bool {
  64. // NOTE(tetra, 2019-10-31): Apparently using wait_for_single_object and
  65. // checking if it didn't time out immediately, is not good enough,
  66. // so we do it this way instead.
  67. return .Done in sync.atomic_load(&t.flags)
  68. }
  69. _join :: proc(t: ^Thread) {
  70. sync.guard(&t.mutex)
  71. if .Joined in t.flags || t.win32_thread == win32.INVALID_HANDLE {
  72. return
  73. }
  74. win32.WaitForSingleObject(t.win32_thread, win32.INFINITE)
  75. win32.CloseHandle(t.win32_thread)
  76. t.win32_thread = win32.INVALID_HANDLE
  77. t.flags += {.Joined}
  78. }
  79. _join_multiple :: proc(threads: ..^Thread) {
  80. MAXIMUM_WAIT_OBJECTS :: 64
  81. handles: [MAXIMUM_WAIT_OBJECTS]win32.HANDLE
  82. for k := 0; k < len(threads); k += MAXIMUM_WAIT_OBJECTS {
  83. count := min(len(threads) - k, MAXIMUM_WAIT_OBJECTS)
  84. j := 0
  85. for i in 0..<count {
  86. handle := threads[i+k].win32_thread
  87. if handle != win32.INVALID_HANDLE {
  88. handles[j] = handle
  89. j += 1
  90. }
  91. }
  92. win32.WaitForMultipleObjects(u32(j), &handles[0], true, win32.INFINITE)
  93. }
  94. for t in threads {
  95. win32.CloseHandle(t.win32_thread)
  96. t.win32_thread = win32.INVALID_HANDLE
  97. }
  98. }
  99. _destroy :: proc(thread: ^Thread) {
  100. _join(thread)
  101. free(thread, thread.creation_allocator)
  102. }
  103. _terminate :: proc(thread: ^Thread, exit_code: int) {
  104. win32.TerminateThread(thread.win32_thread, u32(exit_code))
  105. }
  106. _yield :: proc() {
  107. win32.SwitchToThread()
  108. }