thread_windows.odin 2.8 KB

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