]> git.draconx.ca Git - cdecl99.git/blobdiff - src/thread-stdc.h
libcdecl: Punt gnulib tls module.
[cdecl99.git] / src / thread-stdc.h
diff --git a/src/thread-stdc.h b/src/thread-stdc.h
new file mode 100644 (file)
index 0000000..46fe361
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2024 Nick Bowler
+ *
+ * Helpers for hosts using standard C threading API.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <threads.h>
+
+static tss_t tls_key;
+static bool tls_key_valid;
+
+static void tls_key_init(void)
+{
+       tls_key_valid = (tss_create(&tls_key, free) == thrd_success);
+}
+
+static void *tls_get(void)
+{
+       return tss_get(tls_key);
+}
+
+static int tls_set(void *p)
+{
+       return tss_set(tls_key, p) == thrd_success;
+}