thread_unix.odin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // +build linux, darwin
  2. package thread;
  3. import "core:runtime"
  4. import "core:intrinsics"
  5. import "core:sync"
  6. import "core:sys/unix"
  7. // NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
  8. // Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
  9. Thread_Os_Specific :: struct #align 16 {
  10. unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
  11. // NOTE: pthread has a proc to query this, but it is marked
  12. // as non-portable ("np") so we do this instead.
  13. done: bool,
  14. // since libpthread doesn't seem to have a way to create a thread
  15. // in a suspended state, we have it wait on this gate, which we
  16. // signal to start it.
  17. // destroyed after thread is started.
  18. start_gate: sync.Condition,
  19. start_mutex: sync.Mutex,
  20. // if true, the thread has been started and the start_gate has been destroyed.
  21. started: bool,
  22. // NOTE: with pthreads, it is undefined behavior for multiple threads
  23. // to call join on the same thread at the same time.
  24. // this value is atomically updated to detect this.
  25. // See the comment in `join`.
  26. already_joined: bool,
  27. }
  28. Thread_Priority :: enum {
  29. Normal,
  30. Low,
  31. High,
  32. }
  33. //
  34. // Creates a thread which will run the given procedure.
  35. // It then waits for `start` to be called.
  36. //
  37. create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  38. __linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  39. context = runtime.default_context();
  40. t := (^Thread)(t);
  41. sync.condition_wait_for(&t.start_gate);
  42. sync.condition_destroy(&t.start_gate);
  43. sync.mutex_destroy(&t.start_mutex);
  44. t.start_gate = {};
  45. t.start_mutex = {};
  46. c := context;
  47. if ic, ok := t.init_context.?; ok {
  48. c = ic;
  49. }
  50. context = c;
  51. t.procedure(t);
  52. if t.init_context == nil {
  53. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  54. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
  55. }
  56. }
  57. sync.atomic_store(&t.done, true, .Sequentially_Consistent);
  58. return nil;
  59. }
  60. attrs: unix.pthread_attr_t;
  61. if unix.pthread_attr_init(&attrs) != 0 do return nil; // NOTE(tetra, 2019-11-01): POSIX OOM.
  62. defer unix.pthread_attr_destroy(&attrs);
  63. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  64. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0);
  65. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0);
  66. thread := new(Thread);
  67. if thread == nil do return nil;
  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. sync.mutex_init(&thread.start_mutex);
  85. sync.condition_init(&thread.start_gate, &thread.start_mutex);
  86. if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
  87. free(thread);
  88. return nil;
  89. }
  90. thread.procedure = procedure;
  91. return thread;
  92. }
  93. start :: proc(t: ^Thread) {
  94. if sync.atomic_swap(&t.started, true, .Sequentially_Consistent) do return;
  95. sync.condition_signal(&t.start_gate);
  96. }
  97. is_done :: proc(t: ^Thread) -> bool {
  98. return sync.atomic_load(&t.done, .Sequentially_Consistent);
  99. }
  100. join :: proc(t: ^Thread) {
  101. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
  102. return;
  103. }
  104. // if unix.pthread_self().x == t.unix_thread.x do return;
  105. // NOTE(tetra): It's apparently UB for multiple threads to join the same thread
  106. // at the same time.
  107. // If someone else already did, spin until the thread dies.
  108. // See note on `already_joined` field.
  109. // TODO(tetra): I'm not sure if we should do this, or panic, since I'm not
  110. // sure it makes sense to need to join from multiple threads?
  111. if sync.atomic_swap(&t.already_joined, true, .Sequentially_Consistent) {
  112. for {
  113. if sync.atomic_load(&t.done, .Sequentially_Consistent) {
  114. return;
  115. }
  116. intrinsics.cpu_relax();
  117. }
  118. }
  119. // NOTE(tetra): If we're already dead, don't bother calling to pthread_join as that
  120. // will just return 3 (ESRCH).
  121. // We do this instead because I don't know if there is a danger
  122. // that you may join a different thread from the one you called join on,
  123. // if the thread handle is reused.
  124. if sync.atomic_load(&t.done, .Sequentially_Consistent) {
  125. return;
  126. }
  127. ret_val: rawptr;
  128. _ = unix.pthread_join(t.unix_thread, &ret_val);
  129. if !sync.atomic_load(&t.done, .Sequentially_Consistent) {
  130. panic("thread not done after join");
  131. }
  132. }
  133. join_multiple :: proc(threads: ..^Thread) {
  134. for t in threads {
  135. join(t);
  136. }
  137. }
  138. destroy :: proc(t: ^Thread) {
  139. join(t);
  140. t.unix_thread = {};
  141. free(t);
  142. }
  143. yield :: proc() {
  144. unix.sched_yield();
  145. }