thread_pool.odin 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. started_count: int
  84. for started_count < len(pool.threads) {
  85. started_count = 0
  86. for t in pool.threads {
  87. if .Started in t.flags {
  88. started_count += 1
  89. if .Joined not_in t.flags {
  90. join(t)
  91. }
  92. }
  93. }
  94. }
  95. }
  96. // Add a task to the thread pool.
  97. //
  98. // Tasks can be added from any thread, not just the thread that created
  99. // the thread pool. You can even add tasks from inside other tasks.
  100. //
  101. // Each task also needs an allocator which it either owns, or which is thread
  102. // safe.
  103. pool_add_task :: proc(pool: ^Pool, allocator: mem.Allocator, procedure: Task_Proc, data: rawptr, user_index: int = 0) {
  104. sync.guard(&pool.mutex)
  105. append(&pool.tasks, Task{
  106. procedure = procedure,
  107. data = data,
  108. user_index = user_index,
  109. allocator = allocator,
  110. })
  111. intrinsics.atomic_add(&pool.num_waiting, 1)
  112. intrinsics.atomic_add(&pool.num_outstanding, 1)
  113. sync.post(&pool.sem_available, 1)
  114. }
  115. // Number of tasks waiting to be processed. Only informational, mostly for
  116. // debugging. Don't rely on this value being consistent with other num_*
  117. // values.
  118. pool_num_waiting :: #force_inline proc(pool: ^Pool) -> int {
  119. return intrinsics.atomic_load(&pool.num_waiting)
  120. }
  121. // Number of tasks currently being processed. Only informational, mostly for
  122. // debugging. Don't rely on this value being consistent with other num_*
  123. // values.
  124. pool_num_in_processing :: #force_inline proc(pool: ^Pool) -> int {
  125. return intrinsics.atomic_load(&pool.num_in_processing)
  126. }
  127. // Outstanding tasks are all tasks that are not done, that is, tasks that are
  128. // waiting, as well as tasks that are currently being processed. Only
  129. // informational, mostly for debugging. Don't rely on this value being
  130. // consistent with other num_* values.
  131. pool_num_outstanding :: #force_inline proc(pool: ^Pool) -> int {
  132. return intrinsics.atomic_load(&pool.num_outstanding)
  133. }
  134. // Number of tasks which are done processing. Only informational, mostly for
  135. // debugging. Don't rely on this value being consistent with other num_*
  136. // values.
  137. pool_num_done :: #force_inline proc(pool: ^Pool) -> int {
  138. return intrinsics.atomic_load(&pool.num_done)
  139. }
  140. // If tasks are only being added from one thread, and this procedure is being
  141. // called from that same thread, it will reliably tell if the thread pool is
  142. // empty or not. Empty in this case means there are no tasks waiting, being
  143. // processed, or _done_.
  144. pool_is_empty :: #force_inline proc(pool: ^Pool) -> bool {
  145. return pool_num_outstanding(pool) == 0 && pool_num_done(pool) == 0
  146. }
  147. // Mostly for internal use.
  148. pool_pop_waiting :: proc(pool: ^Pool) -> (task: Task, got_task: bool) {
  149. sync.guard(&pool.mutex)
  150. if len(pool.tasks) != 0 {
  151. intrinsics.atomic_sub(&pool.num_waiting, 1)
  152. intrinsics.atomic_add(&pool.num_in_processing, 1)
  153. task = pop_front(&pool.tasks)
  154. got_task = true
  155. }
  156. return
  157. }
  158. // Use this to take out finished tasks.
  159. pool_pop_done :: proc(pool: ^Pool) -> (task: Task, got_task: bool) {
  160. sync.guard(&pool.mutex)
  161. if len(pool.tasks_done) != 0 {
  162. task = pop_front(&pool.tasks_done)
  163. got_task = true
  164. intrinsics.atomic_sub(&pool.num_done, 1)
  165. }
  166. return
  167. }
  168. // Mostly for internal use.
  169. pool_do_work :: proc(pool: ^Pool, task: Task) {
  170. {
  171. context.allocator = task.allocator
  172. task.procedure(task)
  173. }
  174. sync.guard(&pool.mutex)
  175. append(&pool.tasks_done, task)
  176. intrinsics.atomic_add(&pool.num_done, 1)
  177. intrinsics.atomic_sub(&pool.num_outstanding, 1)
  178. intrinsics.atomic_sub(&pool.num_in_processing, 1)
  179. }
  180. // Process the rest of the tasks, also use this thread for processing, then join
  181. // all the pool threads.
  182. pool_finish :: proc(pool: ^Pool) {
  183. for task in pool_pop_waiting(pool) {
  184. pool_do_work(pool, task)
  185. }
  186. pool_join(pool)
  187. }