thread.odin 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package thread
  2. import "base:runtime"
  3. import "core:mem"
  4. import "base: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) * MAX_USER_ARGUMENTS {
  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(T2) <= size_of(rawptr) * MAX_USER_ARGUMENTS {
  93. create_and_start_with_poly_data2(arg1, arg2, fn, init_context, priority, true)
  94. }
  95. 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)
  96. where size_of(T1) + size_of(T2) + size_of(T3) <= size_of(rawptr) * MAX_USER_ARGUMENTS {
  97. create_and_start_with_poly_data3(arg1, arg2, arg3, fn, init_context, priority, true)
  98. }
  99. 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)
  100. where size_of(T1) + size_of(T2) + size_of(T3) + size_of(T4) <= size_of(rawptr) * MAX_USER_ARGUMENTS {
  101. create_and_start_with_poly_data4(arg1, arg2, arg3, arg4, fn, init_context, priority, true)
  102. }
  103. create_and_start :: proc(fn: proc(), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal, self_cleanup := false) -> ^Thread {
  104. thread_proc :: proc(t: ^Thread) {
  105. fn := cast(proc())t.data
  106. fn()
  107. }
  108. t := create(thread_proc, priority)
  109. t.data = rawptr(fn)
  110. if self_cleanup {
  111. t.flags += {.Self_Cleanup}
  112. }
  113. t.init_context = init_context
  114. start(t)
  115. return t
  116. }
  117. 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 {
  118. thread_proc :: proc(t: ^Thread) {
  119. fn := cast(proc(rawptr))t.data
  120. assert(t.user_index >= 1)
  121. data := t.user_args[0]
  122. fn(data)
  123. }
  124. t := create(thread_proc, priority)
  125. t.data = rawptr(fn)
  126. t.user_index = 1
  127. t.user_args[0] = data
  128. if self_cleanup {
  129. t.flags += {.Self_Cleanup}
  130. }
  131. t.init_context = init_context
  132. start(t)
  133. return t
  134. }
  135. 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
  136. where size_of(T) <= size_of(rawptr) * MAX_USER_ARGUMENTS {
  137. thread_proc :: proc(t: ^Thread) {
  138. fn := cast(proc(T))t.data
  139. assert(t.user_index >= 1)
  140. data := (^T)(&t.user_args[0])^
  141. fn(data)
  142. }
  143. t := create(thread_proc, priority)
  144. t.data = rawptr(fn)
  145. t.user_index = 1
  146. data := data
  147. mem.copy(&t.user_args[0], &data, size_of(T))
  148. if self_cleanup {
  149. t.flags += {.Self_Cleanup}
  150. }
  151. t.init_context = init_context
  152. start(t)
  153. return t
  154. }
  155. 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
  156. where size_of(T1) + size_of(T2) <= size_of(rawptr) * MAX_USER_ARGUMENTS {
  157. thread_proc :: proc(t: ^Thread) {
  158. fn := cast(proc(T1, T2))t.data
  159. assert(t.user_index >= 2)
  160. user_args := mem.slice_to_bytes(t.user_args[:])
  161. arg1 := (^T1)(raw_data(user_args))^
  162. arg2 := (^T2)(raw_data(user_args[size_of(T1):]))^
  163. fn(arg1, arg2)
  164. }
  165. t := create(thread_proc, priority)
  166. t.data = rawptr(fn)
  167. t.user_index = 2
  168. arg1, arg2 := arg1, arg2
  169. user_args := mem.slice_to_bytes(t.user_args[:])
  170. n := copy(user_args, mem.ptr_to_bytes(&arg1))
  171. _ = copy(user_args[n:], mem.ptr_to_bytes(&arg2))
  172. if self_cleanup {
  173. t.flags += {.Self_Cleanup}
  174. }
  175. t.init_context = init_context
  176. start(t)
  177. return t
  178. }
  179. 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
  180. where size_of(T1) + size_of(T2) + size_of(T3) <= size_of(rawptr) * MAX_USER_ARGUMENTS {
  181. thread_proc :: proc(t: ^Thread) {
  182. fn := cast(proc(T1, T2, T3))t.data
  183. assert(t.user_index >= 3)
  184. user_args := mem.slice_to_bytes(t.user_args[:])
  185. arg1 := (^T1)(raw_data(user_args))^
  186. arg2 := (^T2)(raw_data(user_args[size_of(T1):]))^
  187. arg3 := (^T3)(raw_data(user_args[size_of(T1) + size_of(T2):]))^
  188. fn(arg1, arg2, arg3)
  189. }
  190. t := create(thread_proc, priority)
  191. t.data = rawptr(fn)
  192. t.user_index = 3
  193. arg1, arg2, arg3 := arg1, arg2, arg3
  194. user_args := mem.slice_to_bytes(t.user_args[:])
  195. n := copy(user_args, mem.ptr_to_bytes(&arg1))
  196. n += copy(user_args[n:], mem.ptr_to_bytes(&arg2))
  197. _ = copy(user_args[n:], mem.ptr_to_bytes(&arg3))
  198. if self_cleanup {
  199. t.flags += {.Self_Cleanup}
  200. }
  201. t.init_context = init_context
  202. start(t)
  203. return t
  204. }
  205. 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
  206. where size_of(T1) + size_of(T2) + size_of(T3) + size_of(T4) <= size_of(rawptr) * MAX_USER_ARGUMENTS {
  207. thread_proc :: proc(t: ^Thread) {
  208. fn := cast(proc(T1, T2, T3, T4))t.data
  209. assert(t.user_index >= 4)
  210. user_args := mem.slice_to_bytes(t.user_args[:])
  211. arg1 := (^T1)(raw_data(user_args))^
  212. arg2 := (^T2)(raw_data(user_args[size_of(T1):]))^
  213. arg3 := (^T3)(raw_data(user_args[size_of(T1) + size_of(T2):]))^
  214. arg4 := (^T4)(raw_data(user_args[size_of(T1) + size_of(T2) + size_of(T3):]))^
  215. fn(arg1, arg2, arg3, arg4)
  216. }
  217. t := create(thread_proc, priority)
  218. t.data = rawptr(fn)
  219. t.user_index = 4
  220. arg1, arg2, arg3, arg4 := arg1, arg2, arg3, arg4
  221. user_args := mem.slice_to_bytes(t.user_args[:])
  222. n := copy(user_args, mem.ptr_to_bytes(&arg1))
  223. n += copy(user_args[n:], mem.ptr_to_bytes(&arg2))
  224. n += copy(user_args[n:], mem.ptr_to_bytes(&arg3))
  225. _ = copy(user_args[n:], mem.ptr_to_bytes(&arg4))
  226. if self_cleanup {
  227. t.flags += {.Self_Cleanup}
  228. }
  229. t.init_context = init_context
  230. start(t)
  231. return t
  232. }
  233. _select_context_for_thread :: proc(init_context: Maybe(runtime.Context)) -> runtime.Context {
  234. ctx, ok := init_context.?
  235. if !ok {
  236. return runtime.default_context()
  237. }
  238. /*
  239. NOTE(tetra, 2023-05-31):
  240. Ensure that the temp allocator is thread-safe when the user provides a specific initial context to use.
  241. Without this, the thread will use the same temp allocator state as the parent thread, and thus, bork it up.
  242. */
  243. if ctx.temp_allocator.procedure == runtime.default_temp_allocator_proc {
  244. ctx.temp_allocator.data = &runtime.global_default_temp_allocator_data
  245. }
  246. return ctx
  247. }
  248. _maybe_destroy_default_temp_allocator :: proc(init_context: Maybe(runtime.Context)) {
  249. if init_context != nil {
  250. // NOTE(tetra, 2023-05-31): If the user specifies a custom context for the thread,
  251. // then it's entirely up to them to handle whatever allocators they're using.
  252. return
  253. }
  254. if context.temp_allocator.procedure == runtime.default_temp_allocator_proc {
  255. runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
  256. }
  257. }