thread_unix.odin 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // if true, the thread has been started and the start_gate has been destroyed.
  20. started: bool,
  21. // NOTE: with pthreads, it is undefined behavior for multiple threads
  22. // to call join on the same thread at the same time.
  23. // this value is atomically updated to detect this.
  24. // See the comment in `join`.
  25. already_joined: bool,
  26. }
  27. Thread_Priority :: enum {
  28. Normal,
  29. Low,
  30. High,
  31. }
  32. //
  33. // Creates a thread which will run the given procedure.
  34. // It then waits for `start` to be called.
  35. //
  36. // You may provide a slice of bytes to use as the stack for the new thread,
  37. // but if you do, you are expected to set up the guard pages yourself.
  38. //
  39. // The stack must also be aligned appropriately for the platform.
  40. // We require it's at least 16 bytes aligned to help robustness; other
  41. // platforms may require page-size alignment.
  42. // Note also that pthreads requires the stack is at least 6 OS pages in size:
  43. // 4 are required by pthreads, and two extra for guards pages that will be applied.
  44. //
  45. create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  46. __linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  47. t := (^Thread)(t);
  48. sync.condition_wait_for(&t.start_gate);
  49. sync.condition_destroy(&t.start_gate);
  50. t.start_gate = {};
  51. c := context;
  52. if t.use_init_context {
  53. c = t.init_context;
  54. }
  55. context = c;
  56. t.procedure(t);
  57. if !t.use_init_context {
  58. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  59. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
  60. }
  61. }
  62. sync.atomic_store(&t.done, true, .Sequentially_Consistent);
  63. return nil;
  64. }
  65. attrs: unix.pthread_attr_t;
  66. if unix.pthread_attr_init(&attrs) != 0 do return nil; // NOTE(tetra, 2019-11-01): POSIX OOM.
  67. defer unix.pthread_attr_destroy(&attrs);
  68. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  69. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0);
  70. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0);
  71. thread := new(Thread);
  72. if thread == nil do return nil;
  73. // Set thread priority.
  74. policy: i32;
  75. res := unix.pthread_attr_getschedpolicy(&attrs, &policy);
  76. assert(res == 0);
  77. params: unix.sched_param;
  78. res = unix.pthread_attr_getschedparam(&attrs, &params);
  79. assert(res == 0);
  80. low := unix.sched_get_priority_min(policy);
  81. high := unix.sched_get_priority_max(policy);
  82. switch priority {
  83. case .Normal: // Okay
  84. case .Low: params.sched_priority = low + 1;
  85. case .High: params.sched_priority = high;
  86. }
  87. res = unix.pthread_attr_setschedparam(&attrs, &params);
  88. assert(res == 0);
  89. sync.condition_init(&thread.start_gate);
  90. if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
  91. free(thread);
  92. return nil;
  93. }
  94. thread.procedure = procedure;
  95. return thread;
  96. }
  97. start :: proc(t: ^Thread) {
  98. if sync.atomic_swap(&t.started, true, .Sequentially_Consistent) do return;
  99. sync.condition_signal(&t.start_gate);
  100. }
  101. is_done :: proc(t: ^Thread) -> bool {
  102. return sync.atomic_load(&t.done, .Sequentially_Consistent);
  103. }
  104. join :: proc(t: ^Thread) {
  105. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) do return;
  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 sync.atomic_swap(&t.already_joined, true, .Sequentially_Consistent) {
  114. for {
  115. if sync.atomic_load(&t.done, .Sequentially_Consistent) do return;
  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) do return;
  125. ret := unix.pthread_join(t.unix_thread, nil);
  126. assert(ret == 0, "cannot join thread");
  127. assert(sync.atomic_load(&t.done, .Sequentially_Consistent), "thread not done after join");
  128. }
  129. destroy :: proc(t: ^Thread) {
  130. join(t);
  131. t.unix_thread = {};
  132. free(t);
  133. }
  134. yield :: proc() {
  135. unix.sched_yield();
  136. }