thread.odin 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package thread
  2. import "core:runtime"
  3. import "core:mem"
  4. import "core:intrinsics"
  5. _ :: intrinsics
  6. Thread_Proc :: #type proc(^Thread)
  7. MAX_USER_ARGUMENTS :: 8
  8. Thread_State :: enum u8 {
  9. Started,
  10. Joined,
  11. Done,
  12. Self_Cleanup,
  13. }
  14. Thread :: struct {
  15. using specific: Thread_Os_Specific,
  16. flags: bit_set[Thread_State; u8],
  17. id: int,
  18. procedure: Thread_Proc,
  19. /*
  20. These are values that the user can set as they wish, after the thread has been created.
  21. This data is easily available to the thread proc.
  22. These fields can be assigned to directly.
  23. Should be set after the thread is created, but before it is started.
  24. */
  25. data: rawptr,
  26. user_index: int,
  27. user_args: [MAX_USER_ARGUMENTS]rawptr,
  28. /*
  29. The context to be used as 'context' in the thread proc.
  30. This field can be assigned to directly, after the thread has been created, but __before__ the thread has been started.
  31. This field must not be changed after the thread has started.
  32. NOTE: If you __don't__ set this, the temp allocator will be managed for you;
  33. If you __do__ set this, then you're expected to handle whatever allocators you set, yourself.
  34. IMPORTANT:
  35. By default, the thread proc will get the same context as `main()` gets.
  36. In this situation, the thread will get a new temporary allocator which will be cleaned up when the thread dies.
  37. ***This does NOT happen when you set `init_context`.***
  38. This means that if you set `init_context`, but still have the `temp_allocator` field set to the default temp allocator,
  39. then you'll need to call `runtime.default_temp_allocator_destroy(auto_cast the_thread.init_context.temp_allocator.data)` manually,
  40. in order to prevent any memory leaks.
  41. This call ***must*** be done ***in the thread proc*** because the default temporary allocator uses thread local state!
  42. */
  43. init_context: Maybe(runtime.Context),
  44. creation_allocator: mem.Allocator,
  45. }
  46. #assert(size_of(Thread{}.user_index) == size_of(uintptr))
  47. Thread_Priority :: enum {
  48. Normal,
  49. Low,
  50. High,
  51. }
  52. /*
  53. Creates a thread in a suspended state with the given priority.
  54. To start the thread, call `thread.start()`.
  55. See `thread.create_and_start()`.
  56. */
  57. create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
  58. return _create(procedure, priority)
  59. }
  60. destroy :: proc(thread: ^Thread) {
  61. _destroy(thread)
  62. }
  63. start :: proc(thread: ^Thread) {
  64. _start(thread)
  65. }
  66. is_done :: proc(thread: ^Thread) -> bool {
  67. return _is_done(thread)
  68. }
  69. join :: proc(thread: ^Thread) {
  70. _join(thread)
  71. }
  72. join_multiple :: proc(threads: ..^Thread) {
  73. _join_multiple(..threads)
  74. }
  75. terminate :: proc(thread: ^Thread, exit_code: int) {
  76. _terminate(thread, exit_code)
  77. }
  78. yield :: proc() {
  79. _yield()
  80. }
  81. run :: proc(fn: proc(), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal) {
  82. create_and_start(fn, init_context, priority, true)
  83. }
  84. run_with_data :: proc(data: rawptr, fn: proc(data: rawptr), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal) {
  85. create_and_start_with_data(data, fn, init_context, priority, true)
  86. }
  87. run_with_poly_data :: proc(data: $T, fn: proc(data: T), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
  88. where size_of(T) <= size_of(rawptr) {
  89. create_and_start_with_poly_data(data, fn, init_context, priority, true)
  90. }
  91. run_with_poly_data2 :: proc(arg1: $T1, arg2: $T2, fn: proc(T1, T2), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
  92. where size_of(T1) <= size_of(rawptr),
  93. size_of(T2) <= size_of(rawptr) {
  94. create_and_start_with_poly_data2(arg1, arg2, fn, init_context, priority, true)
  95. }
  96. run_with_poly_data3 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, fn: proc(arg1: T1, arg2: T2, arg3: T3), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
  97. where size_of(T1) <= size_of(rawptr),
  98. size_of(T2) <= size_of(rawptr),
  99. size_of(T3) <= size_of(rawptr) {
  100. create_and_start_with_poly_data3(arg1, arg2, arg3, fn, init_context, priority, true)
  101. }
  102. run_with_poly_data4 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, arg4: $T4, fn: proc(arg1: T1, arg2: T2, arg3: T3, arg4: T4), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
  103. where size_of(T1) <= size_of(rawptr),
  104. size_of(T2) <= size_of(rawptr),
  105. size_of(T3) <= size_of(rawptr) {
  106. create_and_start_with_poly_data4(arg1, arg2, arg3, arg4, fn, init_context, priority, true)
  107. }
  108. create_and_start :: proc(fn: proc(), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal, self_cleanup := false) -> ^Thread {
  109. thread_proc :: proc(t: ^Thread) {
  110. fn := cast(proc())t.data
  111. fn()
  112. }
  113. t := create(thread_proc, priority)
  114. t.data = rawptr(fn)
  115. if self_cleanup do t.flags += {.Self_Cleanup}
  116. t.init_context = init_context
  117. start(t)
  118. return t
  119. }
  120. create_and_start_with_data :: proc(data: rawptr, fn: proc(data: rawptr), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal, self_cleanup := false) -> ^Thread {
  121. thread_proc :: proc(t: ^Thread) {
  122. fn := cast(proc(rawptr))t.data
  123. assert(t.user_index >= 1)
  124. data := t.user_args[0]
  125. fn(data)
  126. }
  127. t := create(thread_proc, priority)
  128. t.data = rawptr(fn)
  129. t.user_index = 1
  130. t.user_args = data
  131. if self_cleanup do t.flags += {.Self_Cleanup}
  132. t.init_context = init_context
  133. start(t)
  134. return t
  135. }
  136. create_and_start_with_poly_data :: proc(data: $T, fn: proc(data: T), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal, self_cleanup := false) -> ^Thread
  137. where size_of(T) <= size_of(rawptr) {
  138. thread_proc :: proc(t: ^Thread) {
  139. fn := cast(proc(T))t.data
  140. assert(t.user_index >= 1)
  141. data := (^T)(&t.user_args[0])^
  142. fn(data)
  143. }
  144. t := create(thread_proc, priority)
  145. t.data = rawptr(fn)
  146. t.user_index = 1
  147. data := data
  148. mem.copy(&t.user_args[0], &data, size_of(data))
  149. if self_cleanup do t.flags += {.Self_Cleanup}
  150. t.init_context = init_context
  151. start(t)
  152. return t
  153. }
  154. create_and_start_with_poly_data2 :: proc(arg1: $T1, arg2: $T2, fn: proc(T1, T2), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal, self_cleanup := false) -> ^Thread
  155. where size_of(T1) <= size_of(rawptr),
  156. size_of(T2) <= size_of(rawptr) {
  157. thread_proc :: proc(t: ^Thread) {
  158. fn := cast(proc(T1, T2))t.data
  159. assert(t.user_index >= 2)
  160. arg1 := (^T1)(&t.user_args[0])^
  161. arg2 := (^T2)(&t.user_args[1])^
  162. fn(arg1, arg2)
  163. }
  164. t := create(thread_proc, priority)
  165. t.data = rawptr(fn)
  166. t.user_index = 2
  167. arg1, arg2 := arg1, arg2
  168. mem.copy(&t.user_args[0], &arg1, size_of(arg1))
  169. mem.copy(&t.user_args[1], &arg2, size_of(arg2))
  170. if self_cleanup do t.flags += {.Self_Cleanup}
  171. t.init_context = init_context
  172. start(t)
  173. return t
  174. }
  175. create_and_start_with_poly_data3 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, fn: proc(arg1: T1, arg2: T2, arg3: T3), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal, self_cleanup := false) -> ^Thread
  176. where size_of(T1) <= size_of(rawptr),
  177. size_of(T2) <= size_of(rawptr),
  178. size_of(T3) <= size_of(rawptr) {
  179. thread_proc :: proc(t: ^Thread) {
  180. fn := cast(proc(T1, T2, T3))t.data
  181. assert(t.user_index >= 3)
  182. arg1 := (^T1)(&t.user_args[0])^
  183. arg2 := (^T2)(&t.user_args[1])^
  184. arg3 := (^T3)(&t.user_args[2])^
  185. fn(arg1, arg2, arg3)
  186. }
  187. t := create(thread_proc, priority)
  188. t.data = rawptr(fn)
  189. t.user_index = 3
  190. arg1, arg2, arg3 := arg1, arg2, arg3
  191. mem.copy(&t.user_args[0], &arg1, size_of(arg1))
  192. mem.copy(&t.user_args[1], &arg2, size_of(arg2))
  193. mem.copy(&t.user_args[2], &arg3, size_of(arg3))
  194. if self_cleanup do t.flags += {.Self_Cleanup}
  195. t.init_context = init_context
  196. start(t)
  197. return t
  198. }
  199. create_and_start_with_poly_data4 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, arg4: $T4, fn: proc(arg1: T1, arg2: T2, arg3: T3, arg4: T4), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal, self_cleanup := false) -> ^Thread
  200. where size_of(T1) <= size_of(rawptr),
  201. size_of(T2) <= size_of(rawptr),
  202. size_of(T3) <= size_of(rawptr) {
  203. thread_proc :: proc(t: ^Thread) {
  204. fn := cast(proc(T1, T2, T3, T4))t.data
  205. assert(t.user_index >= 4)
  206. arg1 := (^T1)(&t.user_args[0])^
  207. arg2 := (^T2)(&t.user_args[1])^
  208. arg3 := (^T3)(&t.user_args[2])^
  209. arg4 := (^T4)(&t.user_args[3])^
  210. fn(arg1, arg2, arg3, arg4)
  211. }
  212. t := create(thread_proc, priority)
  213. t.data = rawptr(fn)
  214. t.user_index = 4
  215. arg1, arg2, arg3, arg4 := arg1, arg2, arg3, arg4
  216. mem.copy(&t.user_args[0], &arg1, size_of(arg1))
  217. mem.copy(&t.user_args[1], &arg2, size_of(arg2))
  218. mem.copy(&t.user_args[2], &arg3, size_of(arg3))
  219. mem.copy(&t.user_args[3], &arg4, size_of(arg4))
  220. if self_cleanup do t.flags += {.Self_Cleanup}
  221. t.init_context = init_context
  222. start(t)
  223. return t
  224. }
  225. _select_context_for_thread :: proc(init_context: Maybe(runtime.Context)) -> runtime.Context {
  226. ctx, ok := init_context.?
  227. if !ok {
  228. return runtime.default_context()
  229. }
  230. /*
  231. NOTE(tetra, 2023-05-31):
  232. Ensure that the temp allocator is thread-safe when the user provides a specific initial context to use.
  233. Without this, the thread will use the same temp allocator state as the parent thread, and thus, bork it up.
  234. */
  235. if ctx.temp_allocator.procedure == runtime.default_temp_allocator_proc {
  236. ctx.temp_allocator.data = &runtime.global_default_temp_allocator_data
  237. }
  238. return ctx
  239. }
  240. _maybe_destroy_default_temp_allocator :: proc(init_context: Maybe(runtime.Context)) {
  241. if init_context != nil {
  242. // NOTE(tetra, 2023-05-31): If the user specifies a custom context for the thread,
  243. // then it's entirely up to them to handle whatever allocators they're using.
  244. return
  245. }
  246. if context.temp_allocator.procedure == runtime.default_temp_allocator_proc {
  247. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
  248. }
  249. }