thread_unix.odin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // +build linux, darwin, freebsd, openbsd
  2. // +private
  3. package thread
  4. import "core:runtime"
  5. import "core:intrinsics"
  6. import "core:sync"
  7. import "core:sys/unix"
  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. // NOTE: pthread has a proc to query this, but it is marked
  13. // as non-portable ("np") so we do this instead.
  14. done: bool,
  15. // since libpthread doesn't seem to have a way to create a thread
  16. // in a suspended state, we have it wait on this gate, which we
  17. // signal to start it.
  18. // destroyed after thread is started.
  19. start_gate: sync.Condition,
  20. start_mutex: sync.Mutex,
  21. // if true, the thread has been started and the start_gate has been destroyed.
  22. started: bool,
  23. // NOTE: with pthreads, it is undefined behavior for multiple threads
  24. // to call join on the same thread at the same time.
  25. // this value is atomically updated to detect this.
  26. // See the comment in `join`.
  27. already_joined: bool,
  28. }
  29. //
  30. // Creates a thread which will run the given procedure.
  31. // It then waits for `start` to be called.
  32. //
  33. _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  34. __linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  35. context = runtime.default_context()
  36. t := (^Thread)(t)
  37. sync.condition_wait_for(&t.start_gate)
  38. sync.condition_destroy(&t.start_gate)
  39. sync.mutex_destroy(&t.start_mutex)
  40. t.start_gate = {}
  41. t.start_mutex = {}
  42. context = t.init_context.? or_else runtime.default_context()
  43. t.id = sync.current_thread_id()
  44. t.procedure(t)
  45. if t.init_context == nil {
  46. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  47. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
  48. }
  49. }
  50. intrinsics.atomic_store(&t.done, true)
  51. return nil
  52. }
  53. attrs: unix.pthread_attr_t
  54. if unix.pthread_attr_init(&attrs) != 0 {
  55. return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
  56. }
  57. defer unix.pthread_attr_destroy(&attrs)
  58. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  59. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0)
  60. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0)
  61. thread := new(Thread)
  62. if thread == nil {
  63. return nil
  64. }
  65. thread.creation_allocator = context.allocator
  66. sync.mutex_init(&thread.start_mutex)
  67. sync.condition_init(&thread.start_gate, &thread.start_mutex)
  68. // Set thread priority.
  69. policy: i32
  70. res := unix.pthread_attr_getschedpolicy(&attrs, &policy)
  71. assert(res == 0)
  72. params: unix.sched_param
  73. res = unix.pthread_attr_getschedparam(&attrs, &params)
  74. assert(res == 0)
  75. low := unix.sched_get_priority_min(policy)
  76. high := unix.sched_get_priority_max(policy)
  77. switch priority {
  78. case .Normal: // Okay
  79. case .Low: params.sched_priority = low + 1
  80. case .High: params.sched_priority = high
  81. }
  82. res = unix.pthread_attr_setschedparam(&attrs, &params)
  83. assert(res == 0)
  84. if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
  85. free(thread, thread.creation_allocator)
  86. sync.condition_destroy(&thread.start_gate)
  87. sync.mutex_destroy(&thread.start_mutex)
  88. return nil
  89. }
  90. thread.procedure = procedure
  91. return thread
  92. }
  93. _start :: proc(t: ^Thread) {
  94. if intrinsics.atomic_xchg(&t.started, true) {
  95. return
  96. }
  97. sync.condition_signal(&t.start_gate)
  98. }
  99. _is_done :: proc(t: ^Thread) -> bool {
  100. return intrinsics.atomic_load(&t.done)
  101. }
  102. _join :: proc(t: ^Thread) {
  103. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
  104. return
  105. }
  106. // if unix.pthread_self().x == t.unix_thread.x do return;
  107. // NOTE(tetra): It's apparently UB for multiple threads to join the same thread
  108. // at the same time.
  109. // If someone else already did, spin until the thread dies.
  110. // See note on `already_joined` field.
  111. // TODO(tetra): I'm not sure if we should do this, or panic, since I'm not
  112. // sure it makes sense to need to join from multiple threads?
  113. if intrinsics.atomic_xchg(&t.already_joined, true) {
  114. for {
  115. if intrinsics.atomic_load(&t.done) {
  116. return
  117. }
  118. intrinsics.cpu_relax()
  119. }
  120. }
  121. // NOTE(tetra): If we're already dead, don't bother calling to pthread_join as that
  122. // will just return 3 (ESRCH).
  123. // We do this instead because I don't know if there is a danger
  124. // that you may join a different thread from the one you called join on,
  125. // if the thread handle is reused.
  126. if intrinsics.atomic_load(&t.done) {
  127. return
  128. }
  129. ret_val: rawptr
  130. _ = unix.pthread_join(t.unix_thread, &ret_val)
  131. if !intrinsics.atomic_load(&t.done) {
  132. panic("thread not done after join")
  133. }
  134. }
  135. _join_multiple :: proc(threads: ..^Thread) {
  136. for t in threads {
  137. _join(t)
  138. }
  139. }
  140. _destroy :: proc(t: ^Thread) {
  141. _join(t)
  142. t.unix_thread = {}
  143. free(t, t.creation_allocator)
  144. }
  145. _terminate :: proc(t: ^Thread, exit_code: int) {
  146. // TODO(bill)
  147. }
  148. _yield :: proc() {
  149. unix.sched_yield()
  150. }