aboutsummaryrefslogtreecommitdiff
path: root/base/runtime
diff options
context:
space:
mode:
authorFeoramund <161657516+Feoramund@users.noreply.github.com>2024-08-26 05:56:22 -0400
committerFeoramund <161657516+Feoramund@users.noreply.github.com>2024-08-26 06:01:59 -0400
commitd338642dc4e47981ce353929a8b6ffa5c8464967 (patch)
treeaec1f10d652125b578ec2c011590e74a3a2dea16 /base/runtime
parent8de1e88c4f34f79ff8e2eac915507a30c185236e (diff)
Add API for freeing `thread_local` state
Diffstat (limited to 'base/runtime')
-rw-r--r--base/runtime/thread_management.odin34
1 files changed, 34 insertions, 0 deletions
diff --git a/base/runtime/thread_management.odin b/base/runtime/thread_management.odin
new file mode 100644
index 000000000..cabd4691c
--- /dev/null
+++ b/base/runtime/thread_management.odin
@@ -0,0 +1,34 @@
+package runtime
+
+Thread_Local_Cleaner :: #type proc "odin" ()
+
+@(private="file")
+thread_local_cleaners: [8]Thread_Local_Cleaner
+
+// Add a procedure that will be run at the end of a thread for the purpose of
+// deallocating state marked as `thread_local`.
+//
+// Intended to be called in an `init` procedure of a package with
+// dynamically-allocated memory that is stored in `thread_local` variables.
+add_thread_local_cleaner :: proc "contextless" (p: Thread_Local_Cleaner) {
+ for &v in thread_local_cleaners {
+ if v == nil {
+ v = p
+ return
+ }
+ }
+ panic_contextless("There are no more thread-local cleaner slots available.")
+}
+
+// Run all of the thread-local cleaner procedures.
+//
+// Intended to be called by the internals of a threading API at the end of a
+// thread's lifetime.
+run_thread_local_cleaners :: proc "odin" () {
+ for p in thread_local_cleaners {
+ if p == nil {
+ break
+ }
+ p()
+ }
+}