thread_windows.odin 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //+build windows
  2. //+private
  3. package thread
  4. import "core:runtime"
  5. import "core:intrinsics"
  6. import "core:sync"
  7. import win32 "core:sys/windows"
  8. Thread_State :: enum u8 {
  9. Started,
  10. Joined,
  11. Done,
  12. }
  13. Thread_Os_Specific :: struct {
  14. win32_thread: win32.HANDLE,
  15. win32_thread_id: win32.DWORD,
  16. mutex: sync.Mutex,
  17. flags: bit_set[Thread_State; u8],
  18. }
  19. _thread_priority_map := [Thread_Priority]i32{
  20. .Normal = 0,
  21. .Low = -2,
  22. .High = +2,
  23. }
  24. _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  25. win32_thread_id: win32.DWORD
  26. __windows_thread_entry_proc :: proc "stdcall" (t_: rawptr) -> win32.DWORD {
  27. t := (^Thread)(t_)
  28. context = t.init_context.? or_else runtime.default_context()
  29. t.id = sync.current_thread_id()
  30. t.procedure(t)
  31. intrinsics.atomic_store(&t.flags, t.flags + {.Done})
  32. if t.init_context == nil {
  33. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  34. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
  35. }
  36. }
  37. return 0
  38. }
  39. thread := new(Thread)
  40. if thread == nil {
  41. return nil
  42. }
  43. thread.creation_allocator = context.allocator
  44. win32_thread := win32.CreateThread(nil, 0, __windows_thread_entry_proc, thread, win32.CREATE_SUSPENDED, &win32_thread_id)
  45. if win32_thread == nil {
  46. free(thread, thread.creation_allocator)
  47. return nil
  48. }
  49. thread.procedure = procedure
  50. thread.win32_thread = win32_thread
  51. thread.win32_thread_id = win32_thread_id
  52. thread.init_context = context
  53. ok := win32.SetThreadPriority(win32_thread, _thread_priority_map[priority])
  54. assert(ok == true)
  55. return thread
  56. }
  57. _start :: proc(t: ^Thread) {
  58. sync.guard(&t.mutex)
  59. t.flags += {.Started}
  60. win32.ResumeThread(t.win32_thread)
  61. }
  62. _is_done :: proc(t: ^Thread) -> bool {
  63. // NOTE(tetra, 2019-10-31): Apparently using wait_for_single_object and
  64. // checking if it didn't time out immediately, is not good enough,
  65. // so we do it this way instead.
  66. return .Done in sync.atomic_load(&t.flags)
  67. }
  68. _join :: proc(t: ^Thread) {
  69. sync.guard(&t.mutex)
  70. if .Joined in t.flags || t.win32_thread == win32.INVALID_HANDLE {
  71. return
  72. }
  73. win32.WaitForSingleObject(t.win32_thread, win32.INFINITE)
  74. win32.CloseHandle(t.win32_thread)
  75. t.win32_thread = win32.INVALID_HANDLE
  76. t.flags += {.Joined}
  77. }
  78. _join_multiple :: proc(threads: ..^Thread) {
  79. MAXIMUM_WAIT_OBJECTS :: 64
  80. handles: [MAXIMUM_WAIT_OBJECTS]win32.HANDLE
  81. for k := 0; k < len(threads); k += MAXIMUM_WAIT_OBJECTS {
  82. count := min(len(threads) - k, MAXIMUM_WAIT_OBJECTS)
  83. j := 0
  84. for i in 0..<count {
  85. handle := threads[i+k].win32_thread
  86. if handle != win32.INVALID_HANDLE {
  87. handles[j] = handle
  88. j += 1
  89. }
  90. }
  91. win32.WaitForMultipleObjects(u32(j), &handles[0], true, win32.INFINITE)
  92. }
  93. for t in threads {
  94. win32.CloseHandle(t.win32_thread)
  95. t.win32_thread = win32.INVALID_HANDLE
  96. }
  97. }
  98. _destroy :: proc(thread: ^Thread) {
  99. _join(thread)
  100. free(thread, thread.creation_allocator)
  101. }
  102. _terminate :: proc(using thread : ^Thread, exit_code: int) {
  103. win32.TerminateThread(win32_thread, u32(exit_code))
  104. }
  105. _yield :: proc() {
  106. win32.SwitchToThread()
  107. }