thread_unix.odin 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // +build linux, darwin, freebsd, openbsd
  2. // +private
  3. package thread
  4. import "core:runtime"
  5. import "core:intrinsics"
  6. import "core:sync"
  7. import "core:sys/unix"
  8. Thread_State :: enum u8 {
  9. Started,
  10. Joined,
  11. Done,
  12. }
  13. // NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
  14. // Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
  15. Thread_Os_Specific :: struct #align 16 {
  16. unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
  17. cond: sync.Cond,
  18. mutex: sync.Mutex,
  19. flags: bit_set[Thread_State; u8],
  20. }
  21. //
  22. // Creates a thread which will run the given procedure.
  23. // It then waits for `start` to be called.
  24. //
  25. _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  26. __linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
  27. t := (^Thread)(t)
  28. context = runtime.default_context()
  29. sync.lock(&t.mutex)
  30. t.id = sync.current_thread_id()
  31. for (.Started not_in t.flags) {
  32. sync.wait(&t.cond, &t.mutex)
  33. }
  34. init_context := t.init_context
  35. context = init_context.? or_else runtime.default_context()
  36. t.procedure(t)
  37. intrinsics.atomic_store(&t.flags, t.flags + { .Done })
  38. sync.unlock(&t.mutex)
  39. if init_context == nil && context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
  40. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
  41. }
  42. return nil
  43. }
  44. attrs: unix.pthread_attr_t
  45. if unix.pthread_attr_init(&attrs) != 0 {
  46. return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
  47. }
  48. defer unix.pthread_attr_destroy(&attrs)
  49. // NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
  50. assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0)
  51. assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0)
  52. thread := new(Thread)
  53. if thread == nil {
  54. return nil
  55. }
  56. thread.creation_allocator = context.allocator
  57. // Set thread priority.
  58. policy: i32
  59. res := unix.pthread_attr_getschedpolicy(&attrs, &policy)
  60. assert(res == 0)
  61. params: unix.sched_param
  62. res = unix.pthread_attr_getschedparam(&attrs, &params)
  63. assert(res == 0)
  64. low := unix.sched_get_priority_min(policy)
  65. high := unix.sched_get_priority_max(policy)
  66. switch priority {
  67. case .Normal: // Okay
  68. case .Low: params.sched_priority = low + 1
  69. case .High: params.sched_priority = high
  70. }
  71. res = unix.pthread_attr_setschedparam(&attrs, &params)
  72. assert(res == 0)
  73. thread.procedure = procedure
  74. if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
  75. free(thread, thread.creation_allocator)
  76. return nil
  77. }
  78. return thread
  79. }
  80. _start :: proc(t: ^Thread) {
  81. sync.guard(&t.mutex)
  82. t.flags += { .Started }
  83. sync.signal(&t.cond)
  84. }
  85. _is_done :: proc(t: ^Thread) -> bool {
  86. return .Done in intrinsics.atomic_load(&t.flags)
  87. }
  88. _join :: proc(t: ^Thread) {
  89. sync.guard(&t.mutex)
  90. if .Joined in t.flags || unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
  91. return
  92. }
  93. unix.pthread_join(t.unix_thread, nil)
  94. t.flags += { .Joined }
  95. }
  96. _join_multiple :: proc(threads: ..^Thread) {
  97. for t in threads {
  98. _join(t)
  99. }
  100. }
  101. _destroy :: proc(t: ^Thread) {
  102. _join(t)
  103. t.unix_thread = {}
  104. free(t, t.creation_allocator)
  105. }
  106. _terminate :: proc(t: ^Thread, exit_code: int) {
  107. // TODO(bill)
  108. }
  109. _yield :: proc() {
  110. unix.sched_yield()
  111. }