thread_unix.odin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // +build linux, darwin, freebsd
  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. c := context;
  43. if ic, ok := t.init_context.?; ok {
  44. c = ic;
  45. }
  46. context = c;
  47. t.procedure(t);
  48. if t.init_context == nil {
  49. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  50. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
  51. }
  52. }
  53. intrinsics.atomic_store(&t.done, true);
  54. return nil;
  55. }
  56. attrs: unix.pthread_attr_t;
  57. if unix.pthread_attr_init(&attrs) != 0 {
  58. return nil; // NOTE(tetra, 2019-11-01): POSIX OOM.
  59. }
  60. defer unix.pthread_attr_destroy(&attrs);
  61. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  62. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0);
  63. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0);
  64. thread := new(Thread);
  65. if thread == nil {
  66. return nil;
  67. }
  68. thread.creation_allocator = context.allocator;
  69. // Set thread priority.
  70. policy: i32;
  71. res := unix.pthread_attr_getschedpolicy(&attrs, &policy);
  72. assert(res == 0);
  73. params: unix.sched_param;
  74. res = unix.pthread_attr_getschedparam(&attrs, &params);
  75. assert(res == 0);
  76. low := unix.sched_get_priority_min(policy);
  77. high := unix.sched_get_priority_max(policy);
  78. switch priority {
  79. case .Normal: // Okay
  80. case .Low: params.sched_priority = low + 1;
  81. case .High: params.sched_priority = high;
  82. }
  83. res = unix.pthread_attr_setschedparam(&attrs, &params);
  84. assert(res == 0);
  85. if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
  86. free(thread, thread.creation_allocator);
  87. return nil;
  88. }
  89. thread.procedure = procedure;
  90. sync.mutex_init(&thread.start_mutex);
  91. sync.condition_init(&thread.start_gate, &thread.start_mutex);
  92. return thread;
  93. }
  94. _start :: proc(t: ^Thread) {
  95. if intrinsics.atomic_xchg(&t.started, true) {
  96. return;
  97. }
  98. sync.condition_signal(&t.start_gate);
  99. }
  100. _is_done :: proc(t: ^Thread) -> bool {
  101. return intrinsics.atomic_load(&t.done);
  102. }
  103. _join :: proc(t: ^Thread) {
  104. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
  105. return;
  106. }
  107. // if unix.pthread_self().x == t.unix_thread.x do return;
  108. // NOTE(tetra): It's apparently UB for multiple threads to join the same thread
  109. // at the same time.
  110. // If someone else already did, spin until the thread dies.
  111. // See note on `already_joined` field.
  112. // TODO(tetra): I'm not sure if we should do this, or panic, since I'm not
  113. // sure it makes sense to need to join from multiple threads?
  114. if intrinsics.atomic_xchg(&t.already_joined, true) {
  115. for {
  116. if intrinsics.atomic_load(&t.done) {
  117. return;
  118. }
  119. intrinsics.cpu_relax();
  120. }
  121. }
  122. // NOTE(tetra): If we're already dead, don't bother calling to pthread_join as that
  123. // will just return 3 (ESRCH).
  124. // We do this instead because I don't know if there is a danger
  125. // that you may join a different thread from the one you called join on,
  126. // if the thread handle is reused.
  127. if intrinsics.atomic_load(&t.done) {
  128. return;
  129. }
  130. ret_val: rawptr;
  131. _ = unix.pthread_join(t.unix_thread, &ret_val);
  132. if !intrinsics.atomic_load(&t.done) {
  133. panic("thread not done after join");
  134. }
  135. }
  136. _join_multiple :: proc(threads: ..^Thread) {
  137. for t in threads {
  138. _join(t);
  139. }
  140. }
  141. _destroy :: proc(t: ^Thread) {
  142. _join(t);
  143. sync.condition_destroy(&t.start_gate);
  144. sync.mutex_destroy(&t.start_mutex);
  145. t.unix_thread = {};
  146. free(t, t.creation_allocator);
  147. }
  148. _terminate :: proc(t: ^Thread, exit_code: int) {
  149. // TODO(bill)
  150. }
  151. _yield :: proc() {
  152. unix.sched_yield();
  153. }