Skip to content

Commit

Permalink
Implement entry init counter for the plugin template
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Nov 30, 2023
1 parent b8c2e1d commit 9a6e997
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes in 1.1.11

* [latency.h](include/clap/ext/latency.h): require the plugin to be activated to get the latency
* [plugin-template.c](src/plugin-template.c): implement thread-safe entry init counter

# Changes in 1.1.10

Expand Down
60 changes: 56 additions & 4 deletions src/plugin-template.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <threads.h>

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

View workflow job for this annotation

GitHub Actions / macos-latest-build

'threads.h' file not found

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

View workflow job for this annotation

GitHub Actions / windows-latest-build

threads.h: No such file or directory
#include <assert.h>

#include <clap/clap.h>

Expand Down Expand Up @@ -357,15 +359,65 @@ static const clap_plugin_factory_t s_plugin_factory = {
////////////////

static bool entry_init(const char *plugin_path) {
// called only once, and very first
// perform the plugin initialization
return true;
}

static void entry_deinit(void) {
// called before unloading the DSO
// perform the plugin de-initialization
}

static mtx_t g_entry_lock;
static once_flag g_entry_once = ONCE_FLAG_INIT;
static int g_entry_init_counter = 0;

// Initializes the necessary mutex for the entry guard
static void entry_init_guard_init(void) {
mtx_init(&g_entry_lock, mtx_plain);
}

// Thread safe init counter
static bool entry_init_guard(const char *plugin_path) {
call_once(&g_entry_once, entry_init_guard_init);

mtx_lock(&g_entry_lock);
const int cnt = ++g_entry_init_counter;
assert(cnt > 0);

bool succeed = true;
if (cnt == 1) {
succeed = entry_init(plugin_path);
if (!succeed)
g_entry_init_counter = 0;
}

mtx_unlock(&g_entry_lock);

return succeed;
}

// Thread safe deinit counter
static void entry_deinit_guard(void) {
call_once(&g_entry_once, entry_init_guard_init);

mtx_lock(&g_entry_lock);
const int cnt = --g_entry_init_counter;
assert(cnt > 0);

bool succeed = true;
if (cnt == 0)
entry_deinit();

mtx_unlock(&g_entry_lock);
}

static const void *entry_get_factory(const char *factory_id) {
call_once(&g_entry_once, entry_init_guard_init);

assert(g_entry_init_counter > 0);
if (g_entry_init_counter <= 0)
return NULL;

if (!strcmp(factory_id, CLAP_PLUGIN_FACTORY_ID))
return &s_plugin_factory;
return NULL;
Expand All @@ -374,7 +426,7 @@ static const void *entry_get_factory(const char *factory_id) {
// This symbol will be resolved by the host
CLAP_EXPORT const clap_plugin_entry_t clap_entry = {
.clap_version = CLAP_VERSION_INIT,
.init = entry_init,
.deinit = entry_deinit,
.init = entry_init_guard,
.deinit = entry_deinit_guard,
.get_factory = entry_get_factory,
};

0 comments on commit 9a6e997

Please sign in to comment.