thread_windows.odin 3.4 KB

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