thread_pool.odin 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package thread
  2. /*
  3. thread.Pool
  4. Copyright 2022 eisbehr
  5. Made available under Odin's BSD-3 license.
  6. */
  7. import "core:intrinsics"
  8. import "core:sync"
  9. import "core:mem"
  10. Task_Proc :: #type proc(task: Task)
  11. Task :: struct {
  12. procedure: Task_Proc,
  13. data: rawptr,
  14. user_index: int,
  15. allocator: mem.Allocator,
  16. }
  17. // Do not access the pool's members directly while the pool threads are running,
  18. // since they use different kinds of locking and mutual exclusion devices.
  19. // Careless access can and will lead to nasty bugs. Once initialized, the
  20. // pool's memory address is not allowed to change until it is destroyed.
  21. Pool :: struct {
  22. allocator: mem.Allocator,
  23. mutex: sync.Mutex,
  24. sem_available: sync.Sema,
  25. // the following values are atomic
  26. num_waiting: int,
  27. num_in_processing: int,
  28. num_outstanding: int, // num_waiting + num_in_processing
  29. num_done: int,
  30. // end of atomics
  31. is_running: bool,
  32. threads: []^Thread,
  33. tasks: [dynamic]Task,
  34. tasks_done: [dynamic]Task,
  35. }
  36. // Once initialized, the pool's memory address is not allowed to change until
  37. // it is destroyed.
  38. //
  39. // The thread pool requires an allocator which it either owns, or which is thread safe.
  40. pool_init :: proc(pool: ^Pool, allocator: mem.Allocator, thread_count: int) {
  41. context.allocator = allocator
  42. pool.allocator = allocator
  43. pool.tasks = make([dynamic]Task)
  44. pool.tasks_done = make([dynamic]Task)
  45. pool.threads = make([]^Thread, max(thread_count, 1))
  46. pool.is_running = true
  47. for _, i in pool.threads {
  48. t := create(proc(t: ^Thread) {
  49. pool := (^Pool)(t.data)
  50. for intrinsics.atomic_load(&pool.is_running) {
  51. sync.wait(&pool.sem_available)
  52. if task, ok := pool_pop_waiting(pool); ok {
  53. pool_do_work(pool, task)
  54. }
  55. }
  56. sync.post(&pool.sem_available, 1)
  57. })
  58. t.user_index = i
  59. t.data = pool
  60. pool.threads[i] = t
  61. }
  62. }
  63. pool_destroy :: proc(pool: ^Pool) {
  64. delete(pool.tasks)
  65. delete(pool.tasks_done)
  66. for t in &pool.threads {
  67. destroy(t)
  68. }
  69. delete(pool.threads, pool.allocator)
  70. }
  71. pool_start :: proc(pool: ^Pool) {
  72. for t in pool.threads {
  73. start(t)
  74. }
  75. }
  76. // Finish tasks that have already started processing, then shut down all pool
  77. // threads. Might leave over waiting tasks, any memory allocated for the
  78. // user data of those tasks will not be freed.
  79. pool_join :: proc(pool: ^Pool) {
  80. intrinsics.atomic_store(&pool.is_running, false)
  81. sync.post(&pool.sem_available, len(pool.threads))
  82. yield()
  83. for t in pool.threads {
  84. join(t)
  85. }
  86. }
  87. // Add a task to the thread pool.
  88. //
  89. // Tasks can be added from any thread, not just the thread that created
  90. // the thread pool. You can even add tasks from inside other tasks.
  91. //
  92. // Each task also needs an allocator which it either owns, or which is thread
  93. // safe.
  94. pool_add_task :: proc(pool: ^Pool, allocator: mem.Allocator, procedure: Task_Proc, data: rawptr, user_index: int = 0) {
  95. sync.guard(&pool.mutex)
  96. append(&pool.tasks, Task{
  97. procedure = procedure,
  98. data = data,
  99. user_index = user_index,
  100. allocator = allocator,
  101. })
  102. intrinsics.atomic_add(&pool.num_waiting, 1)
  103. intrinsics.atomic_add(&pool.num_outstanding, 1)
  104. sync.post(&pool.sem_available, 1)
  105. }
  106. // Number of tasks waiting to be processed. Only informational, mostly for
  107. // debugging. Don't rely on this value being consistent with other num_*
  108. // values.
  109. pool_num_waiting :: #force_inline proc(pool: ^Pool) -> int {
  110. return intrinsics.atomic_load(&pool.num_waiting)
  111. }
  112. // Number of tasks currently being processed. Only informational, mostly for
  113. // debugging. Don't rely on this value being consistent with other num_*
  114. // values.
  115. pool_num_in_processing :: #force_inline proc(pool: ^Pool) -> int {
  116. return intrinsics.atomic_load(&pool.num_in_processing)
  117. }
  118. // Outstanding tasks are all tasks that are not done, that is, tasks that are
  119. // waiting, as well as tasks that are currently being processed. Only
  120. // informational, mostly for debugging. Don't rely on this value being
  121. // consistent with other num_* values.
  122. pool_num_outstanding :: #force_inline proc(pool: ^Pool) -> int {
  123. return intrinsics.atomic_load(&pool.num_outstanding)
  124. }
  125. // Number of tasks which are done processing. Only informational, mostly for
  126. // debugging. Don't rely on this value being consistent with other num_*
  127. // values.
  128. pool_num_done :: #force_inline proc(pool: ^Pool) -> int {
  129. return intrinsics.atomic_load(&pool.num_done)
  130. }
  131. // If tasks are only being added from one thread, and this procedure is being
  132. // called from that same thread, it will reliably tell if the thread pool is
  133. // empty or not. Empty in this case means there are no tasks waiting, being
  134. // processed, or _done_.
  135. pool_is_empty :: #force_inline proc(pool: ^Pool) -> bool {
  136. return pool_num_outstanding(pool) == 0 && pool_num_done(pool) == 0
  137. }
  138. // Mostly for internal use.
  139. pool_pop_waiting :: proc(pool: ^Pool) -> (task: Task, got_task: bool) {
  140. sync.guard(&pool.mutex)
  141. if len(pool.tasks) != 0 {
  142. intrinsics.atomic_sub(&pool.num_waiting, 1)
  143. intrinsics.atomic_add(&pool.num_in_processing, 1)
  144. task = pop_front(&pool.tasks)
  145. got_task = true
  146. }
  147. return
  148. }
  149. // Use this to take out finished tasks.
  150. pool_pop_done :: proc(pool: ^Pool) -> (task: Task, got_task: bool) {
  151. sync.guard(&pool.mutex)
  152. if len(pool.tasks_done) != 0 {
  153. task = pop_front(&pool.tasks_done)
  154. got_task = true
  155. intrinsics.atomic_sub(&pool.num_done, 1)
  156. }
  157. return
  158. }
  159. // Mostly for internal use.
  160. pool_do_work :: proc(pool: ^Pool, task: Task) {
  161. {
  162. context.allocator = task.allocator
  163. task.procedure(task)
  164. }
  165. sync.guard(&pool.mutex)
  166. append(&pool.tasks_done, task)
  167. intrinsics.atomic_add(&pool.num_done, 1)
  168. intrinsics.atomic_sub(&pool.num_outstanding, 1)
  169. intrinsics.atomic_sub(&pool.num_in_processing, 1)
  170. }
  171. // Process the rest of the tasks, also use this thread for processing, then join
  172. // all the pool threads.
  173. pool_finish :: proc(pool: ^Pool) {
  174. for task in pool_pop_waiting(pool) {
  175. pool_do_work(pool, task)
  176. }
  177. pool_join(pool)
  178. }