thread_unix.odin 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #+build linux, darwin, freebsd, openbsd, netbsd, haiku
  2. #+private
  3. package thread
  4. import "base:runtime"
  5. import "core:sync"
  6. import "core:sys/posix"
  7. _IS_SUPPORTED :: true
  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: posix.pthread_t, // NOTE: very large on Darwin, small on Linux.
  12. start_ok: sync.Sema,
  13. }
  14. //
  15. // Creates a thread which will run the given procedure.
  16. // It then waits for `start` to be called.
  17. //
  18. _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
  19. __unix_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  20. t := (^Thread)(t)
  21. // We need to give the thread a moment to start up before we enable cancellation.
  22. // NOTE(laytan): setting to .DISABLE on darwin, with .ENABLE pthread_cancel would deadlock
  23. // most of the time, don't ask me why.
  24. can_set_thread_cancel_state := posix.pthread_setcancelstate(.DISABLE when ODIN_OS == .Darwin else .ENABLE, nil) == nil
  25. t.id = sync.current_thread_id()
  26. for (.Started not_in sync.atomic_load(&t.flags)) {
  27. sync.wait(&t.start_ok)
  28. }
  29. // Enable thread's cancelability.
  30. // NOTE(laytan): Darwin does not correctly/fully support all of this, not doing this does
  31. // actually make pthread_cancel work in the capacity of my tests, while executing this would
  32. // basically always make it deadlock.
  33. if ODIN_OS != .Darwin && can_set_thread_cancel_state {
  34. err := posix.pthread_setcancelstate(.ENABLE, nil)
  35. assert_contextless(err == nil)
  36. err = posix.pthread_setcanceltype(.ASYNCHRONOUS, nil)
  37. assert_contextless(err == nil)
  38. }
  39. {
  40. init_context := t.init_context
  41. // NOTE(tetra, 2023-05-31): Must do this AFTER thread.start() is called, so that the user can set the init_context, etc!
  42. // Here on Unix, we start the OS thread in a running state, and so we manually have it wait on a condition
  43. // variable above. We must perform that waiting BEFORE we select the context!
  44. context = _select_context_for_thread(init_context)
  45. defer {
  46. _maybe_destroy_default_temp_allocator(init_context)
  47. runtime.run_thread_local_cleaners()
  48. }
  49. t.procedure(t)
  50. }
  51. sync.atomic_or(&t.flags, { .Done })
  52. if .Self_Cleanup in sync.atomic_load(&t.flags) {
  53. res := posix.pthread_detach(t.unix_thread)
  54. assert_contextless(res == nil)
  55. t.unix_thread = {}
  56. // NOTE(ftphikari): It doesn't matter which context 'free' received, right?
  57. context = {}
  58. free(t, t.creation_allocator)
  59. }
  60. return nil
  61. }
  62. attrs: posix.pthread_attr_t
  63. if posix.pthread_attr_init(&attrs) != nil {
  64. return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
  65. }
  66. defer posix.pthread_attr_destroy(&attrs)
  67. stacksize: posix.rlimit
  68. if res := posix.getrlimit(.STACK, &stacksize); res == .OK && stacksize.rlim_cur > 0 {
  69. _ = posix.pthread_attr_setstacksize(&attrs, uint(stacksize.rlim_cur))
  70. }
  71. res: posix.Errno
  72. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  73. res = posix.pthread_attr_setdetachstate(&attrs, .CREATE_JOINABLE)
  74. assert(res == nil)
  75. when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
  76. res = posix.pthread_attr_setinheritsched(&attrs, .EXPLICIT_SCHED)
  77. assert(res == nil)
  78. }
  79. thread := new(Thread)
  80. if thread == nil {
  81. return nil
  82. }
  83. thread.creation_allocator = context.allocator
  84. // Set thread priority.
  85. policy: posix.Sched_Policy
  86. when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
  87. res = posix.pthread_attr_getschedpolicy(&attrs, &policy)
  88. assert(res == nil)
  89. }
  90. params: posix.sched_param
  91. res = posix.pthread_attr_getschedparam(&attrs, &params)
  92. assert(res == nil)
  93. low := posix.sched_get_priority_min(policy)
  94. high := posix.sched_get_priority_max(policy)
  95. switch priority {
  96. case .Normal: // Okay
  97. case .Low: params.sched_priority = low + 1
  98. case .High: params.sched_priority = high
  99. }
  100. res = posix.pthread_attr_setschedparam(&attrs, &params)
  101. assert(res == nil)
  102. thread.procedure = procedure
  103. if posix.pthread_create(&thread.unix_thread, &attrs, __unix_thread_entry_proc, thread) != nil {
  104. free(thread, thread.creation_allocator)
  105. return nil
  106. }
  107. return thread
  108. }
  109. _start :: proc(t: ^Thread) {
  110. sync.atomic_or(&t.flags, { .Started })
  111. sync.post(&t.start_ok)
  112. }
  113. _is_done :: proc(t: ^Thread) -> bool {
  114. return .Done in sync.atomic_load(&t.flags)
  115. }
  116. _join :: proc(t: ^Thread) {
  117. if posix.pthread_equal(posix.pthread_self(), t.unix_thread) {
  118. return
  119. }
  120. // If the previous value was already `Joined`, then we can return.
  121. if .Joined in sync.atomic_or(&t.flags, {.Joined}) {
  122. return
  123. }
  124. // Prevent non-started threads from blocking main thread with initial wait
  125. // condition.
  126. for (.Started not_in sync.atomic_load(&t.flags)) {
  127. _start(t)
  128. }
  129. posix.pthread_join(t.unix_thread, nil)
  130. t.flags += {.Joined}
  131. }
  132. _join_multiple :: proc(threads: ..^Thread) {
  133. for t in threads {
  134. _join(t)
  135. }
  136. }
  137. _destroy :: proc(t: ^Thread) {
  138. _join(t)
  139. t.unix_thread = {}
  140. free(t, t.creation_allocator)
  141. }
  142. _terminate :: proc(t: ^Thread, exit_code: int) {
  143. // NOTE(Feoramund): For thread cancellation to succeed on BSDs and
  144. // possibly Darwin systems, the thread must call one of the pthread
  145. // cancelation points at some point after this.
  146. //
  147. // The most obvious one of these is `pthread_cancel`, but there is an
  148. // entire list of functions that act as cancelation points available in the
  149. // pthreads manual page.
  150. //
  151. // This is in contrast to behavior I have seen on Linux where the thread is
  152. // just terminated.
  153. posix.pthread_cancel(t.unix_thread)
  154. }
  155. _yield :: proc() {
  156. posix.sched_yield()
  157. }