thread_windows.odin 3.5 KB

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