thread.odin 10 KB

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