thread_unix.odin 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // +build linux, darwin, freebsd, openbsd, haiku
  2. // +private
  3. package thread
  4. import "base:intrinsics"
  5. import "core:sync"
  6. import "core:sys/unix"
  7. CAS :: intrinsics.atomic_compare_exchange_strong
  8. // NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
  9. // Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
  10. Thread_Os_Specific :: struct #align(16) {
  11. unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
  12. cond: sync.Cond,
  13. mutex: sync.Mutex,
  14. }
  15. //
  16. // Creates a thread which will run the given procedure.
  17. // It then waits for `start` to be called.
  18. //
  19. _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
  20. __linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  21. t := (^Thread)(t)
  22. when ODIN_OS != .Darwin {
  23. // We need to give the thread a moment to start up before we enable cancellation.
  24. can_set_thread_cancel_state := unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_DISABLE, nil) == 0
  25. }
  26. sync.lock(&t.mutex)
  27. t.id = sync.current_thread_id()
  28. for (.Started not_in t.flags) {
  29. sync.wait(&t.cond, &t.mutex)
  30. }
  31. when ODIN_OS != .Darwin {
  32. // Enable thread's cancelability.
  33. if can_set_thread_cancel_state {
  34. unix.pthread_setcanceltype (unix.PTHREAD_CANCEL_ASYNCHRONOUS, nil)
  35. unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_DISABLE, nil)
  36. }
  37. }
  38. {
  39. init_context := t.init_context
  40. // NOTE(tetra, 2023-05-31): Must do this AFTER thread.start() is called, so that the user can set the init_context, etc!
  41. // Here on Unix, we start the OS thread in a running state, and so we manually have it wait on a condition
  42. // variable above. We must perform that waiting BEFORE we select the context!
  43. context = _select_context_for_thread(init_context)
  44. defer _maybe_destroy_default_temp_allocator(init_context)
  45. t.procedure(t)
  46. }
  47. intrinsics.atomic_store(&t.flags, t.flags + { .Done })
  48. sync.unlock(&t.mutex)
  49. if .Self_Cleanup in t.flags {
  50. t.unix_thread = {}
  51. // NOTE(ftphikari): It doesn't matter which context 'free' received, right?
  52. context = {}
  53. free(t, t.creation_allocator)
  54. }
  55. return nil
  56. }
  57. attrs: unix.pthread_attr_t
  58. if unix.pthread_attr_init(&attrs) != 0 {
  59. return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
  60. }
  61. defer unix.pthread_attr_destroy(&attrs)
  62. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  63. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0)
  64. when ODIN_OS != .Haiku {
  65. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0)
  66. }
  67. thread := new(Thread)
  68. if thread == nil {
  69. return nil
  70. }
  71. thread.creation_allocator = context.allocator
  72. // Set thread priority.
  73. policy: i32
  74. res: i32
  75. when ODIN_OS != .Haiku {
  76. res = unix.pthread_attr_getschedpolicy(&attrs, &policy)
  77. assert(res == 0)
  78. }
  79. params: unix.sched_param
  80. res = unix.pthread_attr_getschedparam(&attrs, &params)
  81. assert(res == 0)
  82. low := unix.sched_get_priority_min(policy)
  83. high := unix.sched_get_priority_max(policy)
  84. switch priority {
  85. case .Normal: // Okay
  86. case .Low: params.sched_priority = low + 1
  87. case .High: params.sched_priority = high
  88. }
  89. res = unix.pthread_attr_setschedparam(&attrs, &params)
  90. assert(res == 0)
  91. thread.procedure = procedure
  92. if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
  93. free(thread, thread.creation_allocator)
  94. return nil
  95. }
  96. return thread
  97. }
  98. _start :: proc(t: ^Thread) {
  99. // sync.guard(&t.mutex)
  100. t.flags += { .Started }
  101. sync.signal(&t.cond)
  102. }
  103. _is_done :: proc(t: ^Thread) -> bool {
  104. return .Done in intrinsics.atomic_load(&t.flags)
  105. }
  106. _join :: proc(t: ^Thread) {
  107. // sync.guard(&t.mutex)
  108. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
  109. return
  110. }
  111. // Preserve other flags besides `.Joined`, like `.Started`.
  112. unjoined := intrinsics.atomic_load(&t.flags) - {.Joined}
  113. joined := unjoined + {.Joined}
  114. // Try to set `t.flags` from unjoined to joined. If it returns joined,
  115. // it means the previous value had that flag set and we can return.
  116. if res, ok := CAS(&t.flags, unjoined, joined); res == joined && !ok {
  117. return
  118. }
  119. unix.pthread_join(t.unix_thread, nil)
  120. }
  121. _join_multiple :: proc(threads: ..^Thread) {
  122. for t in threads {
  123. _join(t)
  124. }
  125. }
  126. _destroy :: proc(t: ^Thread) {
  127. _join(t)
  128. t.unix_thread = {}
  129. free(t, t.creation_allocator)
  130. }
  131. _terminate :: proc(t: ^Thread, exit_code: int) {
  132. // `pthread_cancel` is unreliable on Darwin for unknown reasons.
  133. when ODIN_OS != .Darwin {
  134. unix.pthread_cancel(t.unix_thread)
  135. }
  136. }
  137. _yield :: proc() {
  138. unix.sched_yield()
  139. }