thread_unix.odin 4.9 KB

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