Skip to content

Commit

Permalink
Add some preprocessor checks before using threads.h
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Nov 30, 2023
1 parent 9a6e997 commit 0eb701a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/plugin-template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Check failure on line 13 in src/plugin-template.c

View workflow job for this annotation

GitHub Actions / windows-latest-build

threads.h: No such file or directory
#endif

#include <clap/clap.h>

static const clap_plugin_descriptor_t s_my_plug_desc = {
Expand Down Expand Up @@ -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);

Expand All @@ -391,28 +403,37 @@ 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);

bool succeed = true;
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)
Expand Down

0 comments on commit 0eb701a

Please sign in to comment.