thread_unix.odin 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/unix"
  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: unix.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. can_set_thread_cancel_state := unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_ENABLE, nil) == 0
  23. t.id = sync.current_thread_id()
  24. if .Started not_in sync.atomic_load(&t.flags) {
  25. sync.wait(&t.start_ok)
  26. }
  27. if .Joined in sync.atomic_load(&t.flags) {
  28. return nil
  29. }
  30. // Enable thread's cancelability.
  31. if can_set_thread_cancel_state {
  32. unix.pthread_setcanceltype (unix.PTHREAD_CANCEL_ASYNCHRONOUS, nil)
  33. unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_ENABLE, nil)
  34. }
  35. {
  36. init_context := t.init_context
  37. // NOTE(tetra, 2023-05-31): Must do this AFTER thread.start() is called, so that the user can set the init_context, etc!
  38. // Here on Unix, we start the OS thread in a running state, and so we manually have it wait on a condition
  39. // variable above. We must perform that waiting BEFORE we select the context!
  40. context = _select_context_for_thread(init_context)
  41. defer {
  42. _maybe_destroy_default_temp_allocator(init_context)
  43. runtime.run_thread_local_cleaners()
  44. }
  45. t.procedure(t)
  46. }
  47. sync.atomic_or(&t.flags, { .Done })
  48. if .Self_Cleanup in sync.atomic_load(&t.flags) {
  49. res := unix.pthread_detach(t.unix_thread)
  50. assert_contextless(res == 0)
  51. t.unix_thread = {}
  52. // NOTE(ftphikari): It doesn't matter which context 'free' received, right?
  53. context = {}
  54. free(t, t.creation_allocator)
  55. }
  56. return nil
  57. }
  58. attrs: unix.pthread_attr_t
  59. if unix.pthread_attr_init(&attrs) != 0 {
  60. return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
  61. }
  62. defer unix.pthread_attr_destroy(&attrs)
  63. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  64. res: i32
  65. res = unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE)
  66. assert(res == 0)
  67. when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
  68. res = unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED)
  69. assert(res == 0)
  70. }
  71. thread := new(Thread)
  72. if thread == nil {
  73. return nil
  74. }
  75. thread.creation_allocator = context.allocator
  76. // Set thread priority.
  77. policy: i32
  78. when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
  79. res = unix.pthread_attr_getschedpolicy(&attrs, &policy)
  80. assert(res == 0)
  81. }
  82. params: unix.sched_param
  83. res = unix.pthread_attr_getschedparam(&attrs, &params)
  84. assert(res == 0)
  85. low := unix.sched_get_priority_min(policy)
  86. high := unix.sched_get_priority_max(policy)
  87. switch priority {
  88. case .Normal: // Okay
  89. case .Low: params.sched_priority = low + 1
  90. case .High: params.sched_priority = high
  91. }
  92. res = unix.pthread_attr_setschedparam(&attrs, &params)
  93. assert(res == 0)
  94. thread.procedure = procedure
  95. if unix.pthread_create(&thread.unix_thread, &attrs, __unix_thread_entry_proc, thread) != 0 {
  96. free(thread, thread.creation_allocator)
  97. return nil
  98. }
  99. return thread
  100. }
  101. _start :: proc(t: ^Thread) {
  102. sync.atomic_or(&t.flags, { .Started })
  103. sync.post(&t.start_ok)
  104. }
  105. _is_done :: proc(t: ^Thread) -> bool {
  106. return .Done in sync.atomic_load(&t.flags)
  107. }
  108. _join :: proc(t: ^Thread) {
  109. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
  110. return
  111. }
  112. // If the previous value was already `Joined`, then we can return.
  113. if .Joined in sync.atomic_or(&t.flags, {.Joined}) {
  114. return
  115. }
  116. // Prevent non-started threads from blocking main thread with initial wait
  117. // condition.
  118. if .Started not_in sync.atomic_load(&t.flags) {
  119. _start(t)
  120. }
  121. unix.pthread_join(t.unix_thread, nil)
  122. }
  123. _join_multiple :: proc(threads: ..^Thread) {
  124. for t in threads {
  125. _join(t)
  126. }
  127. }
  128. _destroy :: proc(t: ^Thread) {
  129. _join(t)
  130. t.unix_thread = {}
  131. free(t, t.creation_allocator)
  132. }
  133. _terminate :: proc(t: ^Thread, exit_code: int) {
  134. // NOTE(Feoramund): For thread cancellation to succeed on BSDs and
  135. // possibly Darwin systems, the thread must call one of the pthread
  136. // cancelation points at some point after this.
  137. //
  138. // The most obvious one of these is `pthread_cancel`, but there is an
  139. // entire list of functions that act as cancelation points available in the
  140. // pthreads manual page.
  141. //
  142. // This is in contrast to behavior I have seen on Linux where the thread is
  143. // just terminated.
  144. unix.pthread_cancel(t.unix_thread)
  145. }
  146. _yield :: proc() {
  147. unix.sched_yield()
  148. }