commit 0eb701a9f2b15ddc9eab7866bc07e65ac5ae1fcf
parent 9a6e997bcaf97936193900c2aa46d4818d4462e0
Author: Alexandre Bique <bique.alexandre@gmail.com>
Date: Thu, 30 Nov 2023 13:36:20 +0100
Add some preprocessor checks before using threads.h
Diffstat:
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/src/plugin-template.c b/src/plugin-template.c
@@ -6,9 +6,13 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
-#include <threads.h>
#include <assert.h>
+#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
+# define CLAP_HAS_THREAD
+# include <threads.h>
+#endif
+
#include <clap/clap.h>
static const clap_plugin_descriptor_t s_my_plug_desc = {
@@ -367,20 +371,28 @@ static void entry_deinit(void) {
// perform the plugin de-initialization
}
+#ifdef CLAP_HAS_THREAD
static mtx_t g_entry_lock;
static once_flag g_entry_once = ONCE_FLAG_INIT;
+#endif
+
static int g_entry_init_counter = 0;
+#ifdef CLAP_HAS_THREAD
// Initializes the necessary mutex for the entry guard
static void entry_init_guard_init(void) {
mtx_init(&g_entry_lock, mtx_plain);
}
+#endif
// Thread safe init counter
static bool entry_init_guard(const char *plugin_path) {
+#ifdef CLAP_HAS_THREAD
call_once(&g_entry_once, entry_init_guard_init);
mtx_lock(&g_entry_lock);
+#endif
+
const int cnt = ++g_entry_init_counter;
assert(cnt > 0);
@@ -391,16 +403,21 @@ static bool entry_init_guard(const char *plugin_path) {
g_entry_init_counter = 0;
}
+#ifdef CLAP_HAS_THREAD
mtx_unlock(&g_entry_lock);
+#endif
return succeed;
}
// Thread safe deinit counter
static void entry_deinit_guard(void) {
+#ifdef CLAP_HAS_THREAD
call_once(&g_entry_once, entry_init_guard_init);
mtx_lock(&g_entry_lock);
+#endif
+
const int cnt = --g_entry_init_counter;
assert(cnt > 0);
@@ -408,11 +425,15 @@ static void entry_deinit_guard(void) {
if (cnt == 0)
entry_deinit();
+#ifdef CLAP_HAS_THREAD
mtx_unlock(&g_entry_lock);
+#endif
}
static const void *entry_get_factory(const char *factory_id) {
+#ifdef CLAP_HAS_THREAD
call_once(&g_entry_once, entry_init_guard_init);
+#endif
assert(g_entry_init_counter > 0);
if (g_entry_init_counter <= 0)