thread_windows.odin 3.6 KB

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