thread_unix.odin 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // +build linux, darwin, freebsd, openbsd, netbsd, haiku
  2. // +private
  3. package thread
  4. import "core:sync"
  5. import "core:sys/unix"
  6. import "core:time"
  7. _IS_SUPPORTED :: true
  8. CAS :: sync.atomic_compare_exchange_strong
  9. // NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
  10. // Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
  11. Thread_Os_Specific :: struct #align(16) {
  12. unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
  13. cond: sync.Cond,
  14. mutex: sync.Mutex,
  15. }
  16. //
  17. // Creates a thread which will run the given procedure.
  18. // It then waits for `start` to be called.
  19. //
  20. _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
  21. __unix_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  22. t := (^Thread)(t)
  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_ENABLE, nil) == 0
  25. sync.lock(&t.mutex)
  26. t.id = sync.current_thread_id()
  27. for (.Started not_in sync.atomic_load(&t.flags)) {
  28. // HACK: use a timeout so in the event that the condition is signalled at THIS comment's exact point
  29. // (after checking flags, before starting the wait) it gets itself out of that deadlock after a ms.
  30. sync.wait_with_timeout(&t.cond, &t.mutex, time.Millisecond)
  31. }
  32. if .Joined in sync.atomic_load(&t.flags) {
  33. return nil
  34. }
  35. // Enable thread's cancelability.
  36. if can_set_thread_cancel_state {
  37. unix.pthread_setcanceltype (unix.PTHREAD_CANCEL_ASYNCHRONOUS, nil)
  38. unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_ENABLE, nil)
  39. }
  40. {
  41. init_context := t.init_context
  42. // NOTE(tetra, 2023-05-31): Must do this AFTER thread.start() is called, so that the user can set the init_context, etc!
  43. // Here on Unix, we start the OS thread in a running state, and so we manually have it wait on a condition
  44. // variable above. We must perform that waiting BEFORE we select the context!
  45. context = _select_context_for_thread(init_context)
  46. defer _maybe_destroy_default_temp_allocator(init_context)
  47. t.procedure(t)
  48. }
  49. sync.atomic_or(&t.flags, { .Done })
  50. sync.unlock(&t.mutex)
  51. if .Self_Cleanup in sync.atomic_load(&t.flags) {
  52. t.unix_thread = {}
  53. // NOTE(ftphikari): It doesn't matter which context 'free' received, right?
  54. context = {}
  55. free(t, t.creation_allocator)
  56. }
  57. return nil
  58. }
  59. attrs: unix.pthread_attr_t
  60. if unix.pthread_attr_init(&attrs) != 0 {
  61. return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
  62. }
  63. defer unix.pthread_attr_destroy(&attrs)
  64. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  65. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0)
  66. when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
  67. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0)
  68. }
  69. thread := new(Thread)
  70. if thread == nil {
  71. return nil
  72. }
  73. thread.creation_allocator = context.allocator
  74. // Set thread priority.
  75. policy: i32
  76. res: i32
  77. when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
  78. res = unix.pthread_attr_getschedpolicy(&attrs, &policy)
  79. assert(res == 0)
  80. }
  81. params: unix.sched_param
  82. res = unix.pthread_attr_getschedparam(&attrs, &params)
  83. assert(res == 0)
  84. low := unix.sched_get_priority_min(policy)
  85. high := unix.sched_get_priority_max(policy)
  86. switch priority {
  87. case .Normal: // Okay
  88. case .Low: params.sched_priority = low + 1
  89. case .High: params.sched_priority = high
  90. }
  91. res = unix.pthread_attr_setschedparam(&attrs, &params)
  92. assert(res == 0)
  93. thread.procedure = procedure
  94. if unix.pthread_create(&thread.unix_thread, &attrs, __unix_thread_entry_proc, thread) != 0 {
  95. free(thread, thread.creation_allocator)
  96. return nil
  97. }
  98. return thread
  99. }
  100. _start :: proc(t: ^Thread) {
  101. sync.atomic_or(&t.flags, { .Started })
  102. sync.signal(&t.cond)
  103. }
  104. _is_done :: proc(t: ^Thread) -> bool {
  105. return .Done in sync.atomic_load(&t.flags)
  106. }
  107. _join :: proc(t: ^Thread) {
  108. // sync.guard(&t.mutex)
  109. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
  110. return
  111. }
  112. // Preserve other flags besides `.Joined`, like `.Started`.
  113. unjoined := sync.atomic_load(&t.flags) - {.Joined}
  114. joined := unjoined + {.Joined}
  115. // Try to set `t.flags` from unjoined to joined. If it returns joined,
  116. // it means the previous value had that flag set and we can return.
  117. if res, ok := CAS(&t.flags, unjoined, joined); res == joined && !ok {
  118. return
  119. }
  120. // Prevent non-started threads from blocking main thread with initial wait
  121. // condition.
  122. if .Started not_in unjoined {
  123. _start(t)
  124. }
  125. unix.pthread_join(t.unix_thread, nil)
  126. }
  127. _join_multiple :: proc(threads: ..^Thread) {
  128. for t in threads {
  129. _join(t)
  130. }
  131. }
  132. _destroy :: proc(t: ^Thread) {
  133. _join(t)
  134. t.unix_thread = {}
  135. free(t, t.creation_allocator)
  136. }
  137. _terminate :: proc(t: ^Thread, exit_code: int) {
  138. // NOTE(Feoramund): For thread cancellation to succeed on BSDs and
  139. // possibly Darwin systems, the thread must call one of the pthread
  140. // cancelation points at some point after this.
  141. //
  142. // The most obvious one of these is `pthread_cancel`, but there is an
  143. // entire list of functions that act as cancelation points available in the
  144. // pthreads manual page.
  145. //
  146. // This is in contrast to behavior I have seen on Linux where the thread is
  147. // just terminated.
  148. unix.pthread_cancel(t.unix_thread)
  149. }
  150. _yield :: proc() {
  151. unix.sched_yield()
  152. }