thread_unix.odin 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // +build linux, darwin
  2. package thread;
  3. import "core:runtime"
  4. import "core:sync"
  5. import "core:sys/unix"
  6. // NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
  7. // Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
  8. Thread_Os_Specific :: struct #align 16 {
  9. unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
  10. // NOTE: pthread has a proc to query this, but it is marked
  11. // as non-portable ("np") so we do this instead.
  12. done: bool,
  13. // since libpthread doesn't seem to have a way to create a thread
  14. // in a suspended state, we have it wait on this gate, which we
  15. // signal to start it.
  16. // destroyed after thread is started.
  17. start_gate: sync.Condition,
  18. // if true, the thread has been started and the start_gate has been destroyed.
  19. started: bool,
  20. // NOTE: with pthreads, it is undefined behavior for multiple threads
  21. // to call join on the same thread at the same time.
  22. // this value is atomically updated to detect this.
  23. // See the comment in `join`.
  24. already_joined: bool,
  25. }
  26. Thread_Priority :: enum {
  27. Normal,
  28. Low,
  29. High,
  30. }
  31. //
  32. // Creates a thread which will run the given procedure.
  33. // It then waits for `start` to be called.
  34. //
  35. // You may provide a slice of bytes to use as the stack for the new thread,
  36. // but if you do, you are expected to set up the guard pages yourself.
  37. //
  38. // The stack must also be aligned appropriately for the platform.
  39. // We require it's at least 16 bytes aligned to help robustness; other
  40. // platforms may require page-size alignment.
  41. // Note also that pthreads requires the stack is at least 6 OS pages in size:
  42. // 4 are required by pthreads, and two extra for guards pages that will be applied.
  43. //
  44. create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  45. __linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  46. t := (^Thread)(t);
  47. sync.condition_wait_for(&t.start_gate);
  48. sync.condition_destroy(&t.start_gate);
  49. t.start_gate = {};
  50. c := context;
  51. if t.use_init_context {
  52. c = t.init_context;
  53. }
  54. context = c;
  55. t.procedure(t);
  56. if !t.use_init_context {
  57. if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  58. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
  59. }
  60. }
  61. sync.atomic_store(&t.done, true, .Sequentially_Consistent);
  62. return nil;
  63. }
  64. attrs: unix.pthread_attr_t;
  65. if unix.pthread_attr_init(&attrs) != 0 do return nil; // NOTE(tetra, 2019-11-01): POSIX OOM.
  66. defer unix.pthread_attr_destroy(&attrs);
  67. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  68. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0);
  69. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0);
  70. thread := new(Thread);
  71. if thread == nil do return nil;
  72. // Set thread priority.
  73. policy: i32;
  74. res := unix.pthread_attr_getschedpolicy(&attrs, &policy);
  75. assert(res == 0);
  76. params: unix.sched_param;
  77. res = unix.pthread_attr_getschedparam(&attrs, &params);
  78. assert(res == 0);
  79. low := unix.sched_get_priority_min(policy);
  80. high := unix.sched_get_priority_max(policy);
  81. switch priority {
  82. case .Normal: // Okay
  83. case .Low: params.sched_priority = low + 1;
  84. case .High: params.sched_priority = high;
  85. }
  86. res = unix.pthread_attr_setschedparam(&attrs, &params);
  87. assert(res == 0);
  88. sync.condition_init(&thread.start_gate);
  89. if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
  90. free(thread);
  91. return nil;
  92. }
  93. thread.procedure = procedure;
  94. return thread;
  95. }
  96. start :: proc(t: ^Thread) {
  97. if sync.atomic_swap(&t.started, true, .Sequentially_Consistent) do return;
  98. sync.condition_signal(&t.start_gate);
  99. }
  100. is_done :: proc(t: ^Thread) -> bool {
  101. return sync.atomic_load(&t.done, .Sequentially_Consistent);
  102. }
  103. join :: proc(t: ^Thread) {
  104. if unix.pthread_equal(unix.pthread_self(), t.unix_thread) do return;
  105. // if unix.pthread_self().x == t.unix_thread.x do return;
  106. // NOTE(tetra): It's apparently UB for multiple threads to join the same thread
  107. // at the same time.
  108. // If someone else already did, spin until the thread dies.
  109. // See note on `already_joined` field.
  110. // TODO(tetra): I'm not sure if we should do this, or panic, since I'm not
  111. // sure it makes sense to need to join from multiple threads?
  112. if sync.atomic_swap(&t.already_joined, true, .Sequentially_Consistent) {
  113. for {
  114. if sync.atomic_load(&t.done, .Sequentially_Consistent) do return;
  115. sync.yield_processor();
  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 sync.atomic_load(&t.done, .Sequentially_Consistent) do return;
  124. ret := unix.pthread_join(t.unix_thread, nil);
  125. assert(ret == 0, "cannot join thread");
  126. assert(sync.atomic_load(&t.done, .Sequentially_Consistent), "thread not done after join");
  127. }
  128. destroy :: proc(t: ^Thread) {
  129. join(t);
  130. t.unix_thread = {};
  131. free(t);
  132. }
  133. yield :: proc() {
  134. unix.sched_yield();
  135. }