Skip to content

Commit

Permalink
update jalv sources.
Browse files Browse the repository at this point in the history
It is an attempt to fix #16
but this change itself does not automatically fix the issue.
Needs more investigation.
  • Loading branch information
atsushieno committed Feb 16, 2022
1 parent 5373d1c commit d12d5d9
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 193 deletions.
3 changes: 2 additions & 1 deletion androidaudioplugin-lv2/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ set (androidaudioplugin-lv2_SOURCES
"src/android-audio-plugin-lv2-bridge.cpp"
"src/AudioPluginLV2LocalHost_jni.cpp"
"src/symap.cpp"
"src/zix/ring.cpp"
"src/zix/ring.c"

"src/std_workaround.c"
"src/abstract_io.c"
Expand Down Expand Up @@ -141,6 +141,7 @@ target_compile_options (androidaudioplugin-lv2
PRIVATE
-Wall
-Wshadow
-DHAVE_MLOCK=1
# uncomment this if you want to enable AddressSanitizer
#-fsanitize=address -fno-omit-frame-pointer
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ typedef struct {
} AAPLV2PluginContextStatics;

// imported from jalv
class AAPLV2PluginContext;
typedef AAPLV2PluginContext Jalv;

typedef struct {
void* ctx; ///< Pointer back to AAPLV2PluginContext
Jalv* ctx; ///< Pointer back to AAPLV2PluginContext
ZixRing* requests{nullptr}; ///< Requests to the worker
ZixRing* responses{nullptr}; ///< Responses from the worker
void* response; ///< Worker response buffer
Expand Down Expand Up @@ -161,7 +164,7 @@ class AAPLV2PluginContext {
JalvWorker state_worker; ///< Synchronous worker for state restore
ZixSem work_lock; ///< Lock for plugin work() method
bool safe_restore{false}; ///< Plugin restore() is thread-safe
bool terminate{false};
bool exit{false};
};

static LV2_URID
Expand Down Expand Up @@ -211,8 +214,7 @@ PORTCHECKER_AND (IS_ATOM_IN, IS_ATOM_PORT, IS_INPUT_PORT)

PORTCHECKER_AND (IS_ATOM_OUT, IS_ATOM_PORT, IS_OUTPUT_PORT)

// The code below (jalv_xxx) is imported from jalv and then modified.

// The code below (jalv_worker_xxx) is copied from jalv worker.c and then made minimum required changes.

static LV2_Worker_Status
jalv_worker_respond(LV2_Worker_Respond_Handle handle,
Expand All @@ -229,11 +231,11 @@ static void*
worker_func(void* data)
{
JalvWorker* worker = (JalvWorker*)data;
auto ctx = (AAPLV2PluginContext*) worker->ctx;
Jalv* jalv = worker->ctx;
void* buf = NULL;
while (true) {
zix_sem_wait(&worker->sem);
if (ctx->terminate) {
if (jalv->exit) {
break;
}

Expand All @@ -248,21 +250,22 @@ worker_func(void* data)

zix_ring_read(worker->requests, (char*)buf, size);

zix_sem_wait(&ctx->work_lock);
zix_sem_wait(&jalv->work_lock);
worker->iface->work(
ctx->instance->lv2_handle, jalv_worker_respond, worker, size, buf);
zix_sem_post(&ctx->work_lock);
jalv->instance->lv2_handle, jalv_worker_respond, worker, size, buf);
zix_sem_post(&jalv->work_lock);
}

free(buf);
return NULL;
}

void
jalv_worker_init(AAPLV2PluginContext*,
JalvWorker* worker,
const LV2_Worker_Interface* iface,
bool threaded) {
jalv_worker_init(Jalv* ZIX_UNUSED(jalv),
JalvWorker* worker,
const LV2_Worker_Interface* iface,
bool threaded)
{
worker->iface = iface;
worker->threaded = threaded;
if (threaded) {
Expand All @@ -271,7 +274,7 @@ bool threaded) {
zix_ring_mlock(worker->requests);
}
worker->responses = zix_ring_new(4096);
worker->response = malloc(4096);
worker->response = malloc(4096);
zix_ring_mlock(worker->responses);
}

Expand Down Expand Up @@ -302,18 +305,18 @@ jalv_worker_schedule(LV2_Worker_Schedule_Handle handle,
const void* data)
{
JalvWorker* worker = (JalvWorker*)handle;
auto ctx = (AAPLV2PluginContext*) worker->ctx;
Jalv* jalv = worker->ctx;
if (worker->threaded) {
// Schedule a request to be executed by the worker thread
zix_ring_write(worker->requests, (const char*)&size, sizeof(size));
zix_ring_write(worker->requests, (const char*)data, size);
zix_sem_post(&worker->sem);
} else {
// Execute work immediately in this thread
zix_sem_wait(&ctx->work_lock);
zix_sem_wait(&jalv->work_lock);
worker->iface->work(
ctx->instance->lv2_handle, jalv_worker_respond, worker, size, data);
zix_sem_post(&ctx->work_lock);
jalv->instance->lv2_handle, jalv_worker_respond, worker, size, data);
zix_sem_post(&jalv->work_lock);
}
return LV2_WORKER_SUCCESS;
}
Expand Down Expand Up @@ -344,7 +347,7 @@ void aap_lv2_plugin_delete(
AndroidAudioPlugin *plugin) {
auto l = (AAPLV2PluginContext *) plugin->plugin_specific;

l->terminate = true;
l->exit = true;

// Terminate the worker
jalv_worker_finish(&l->worker);
Expand Down
8 changes: 4 additions & 4 deletions androidaudioplugin-lv2/src/main/cpp/src/symap.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2011-2014 David Robillard <http://drobilla.net>
Copyright 2011-2014 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down Expand Up @@ -107,7 +107,7 @@ symap_search(const Symap* map, const char* sym, bool* exact)
uint32_t lower = 0;
uint32_t upper = map->size - 1;
uint32_t i = upper;
int cmp;
int cmp = 0;

while (upper >= lower) {
i = lower + ((upper - lower) / 2);
Expand All @@ -133,7 +133,7 @@ symap_search(const Symap* map, const char* sym, bool* exact)
uint32_t
symap_try_map(Symap* map, const char* sym)
{
bool exact;
bool exact = false;
const uint32_t index = symap_search(map, sym, &exact);
if (exact) {
assert(!strcmp(map->symbols[map->index[index]], sym));
Expand All @@ -146,7 +146,7 @@ symap_try_map(Symap* map, const char* sym)
uint32_t
symap_map(Symap* map, const char* sym)
{
bool exact;
bool exact = false;
const uint32_t index = symap_search(map, sym, &exact);
if (exact) {
assert(!strcmp(map->symbols[map->index[index] - 1], sym));
Expand Down
4 changes: 1 addition & 3 deletions androidaudioplugin-lv2/src/main/cpp/src/symap.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2011-2014 David Robillard <http://drobilla.net>
Copyright 2011-2014 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down Expand Up @@ -27,8 +27,6 @@

#include <stdint.h>

struct SymapImpl;

typedef struct SymapImpl Symap;

/**
Expand Down
104 changes: 63 additions & 41 deletions androidaudioplugin-lv2/src/main/cpp/src/zix/common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2016 David Robillard <http://drobilla.net>
Copyright 2016-2020 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand All @@ -17,45 +17,65 @@
#ifndef ZIX_COMMON_H
#define ZIX_COMMON_H

#include <stdbool.h>

/**
@addtogroup zix
@{
*/

/** @cond */
#ifdef ZIX_SHARED
# ifdef _WIN32
# define ZIX_LIB_IMPORT __declspec(dllimport)
# define ZIX_LIB_EXPORT __declspec(dllexport)
# else
# define ZIX_LIB_IMPORT __attribute__((visibility("default")))
# define ZIX_LIB_EXPORT __attribute__((visibility("default")))
# endif
# ifdef ZIX_INTERNAL
# define ZIX_API ZIX_LIB_EXPORT
# else
# define ZIX_API ZIX_LIB_IMPORT
# endif
# define ZIX_PRIVATE static
#elif defined(ZIX_INLINE)
# define ZIX_API static inline
# define ZIX_PRIVATE static inline
#if defined(_WIN32) && !defined(ZIX_STATIC) && defined(ZIX_INTERNAL)
# define ZIX_API __declspec(dllexport)
#elif defined(_WIN32) && !defined(ZIX_STATIC)
# define ZIX_API __declspec(dllimport)
#elif defined(__GNUC__)
# define ZIX_API __attribute__((visibility("default")))
#else
# define ZIX_API
# define ZIX_PRIVATE static
# define ZIX_API
#endif

#ifdef __GNUC__
# define ZIX_PURE_FUNC __attribute__((pure))
# define ZIX_CONST_FUNC __attribute__((const))
# define ZIX_MALLOC_FUNC __attribute__((malloc))
#else
# define ZIX_PURE_FUNC
# define ZIX_CONST_FUNC
# define ZIX_MALLOC_FUNC
#endif

#define ZIX_PURE_API \
ZIX_API \
ZIX_PURE_FUNC

#define ZIX_CONST_API \
ZIX_API \
ZIX_CONST_FUNC

#define ZIX_MALLOC_API \
ZIX_API \
ZIX_MALLOC_FUNC

/** @endcond */

#ifdef __cplusplus
extern "C" {
#else
# include <stdbool.h>
#endif

#ifdef __GNUC__
#define ZIX_UNUSED __attribute__((__unused__))
# define ZIX_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
#else
# define ZIX_LOG_FUNC(fmt, arg1)
#endif

// Unused parameter macro to suppresses warnings and make it impossible to use
#if defined(__cplusplus)
# define ZIX_UNUSED(name)
#elif defined(__GNUC__)
# define ZIX_UNUSED(name) name##_unused __attribute__((__unused__))
#else
#define ZIX_UNUSED
# define ZIX_UNUSED(name) name
#endif

typedef enum {
Expand All @@ -72,28 +92,30 @@ static inline const char*
zix_strerror(const ZixStatus status)
{
switch (status) {
case ZIX_STATUS_SUCCESS:
return "Success";
case ZIX_STATUS_ERROR:
return "Unknown error";
case ZIX_STATUS_NO_MEM:
return "Out of memory";
case ZIX_STATUS_NOT_FOUND:
return "Not found";
case ZIX_STATUS_EXISTS:
return "Exists";
case ZIX_STATUS_BAD_ARG:
return "Bad argument";
case ZIX_STATUS_BAD_PERMS:
return "Bad permissions";
case ZIX_STATUS_SUCCESS:
return "Success";
case ZIX_STATUS_ERROR:
return "Unknown error";
case ZIX_STATUS_NO_MEM:
return "Out of memory";
case ZIX_STATUS_NOT_FOUND:
return "Not found";
case ZIX_STATUS_EXISTS:
return "Exists";
case ZIX_STATUS_BAD_ARG:
return "Bad argument";
case ZIX_STATUS_BAD_PERMS:
return "Bad permissions";
}
return "Unknown error";
}

/**
Function for comparing two elements.
*/
typedef int (*ZixComparator)(const void* a, const void* b, void* user_data);
typedef int (*ZixComparator)(const void* a,
const void* b,
const void* user_data);

/**
Function for testing equality of two elements.
Expand All @@ -110,7 +132,7 @@ typedef void (*ZixDestroyFunc)(void* ptr);
*/

#ifdef __cplusplus
} /* extern "C" */
} /* extern "C" */
#endif

#endif /* ZIX_COMMON_H */
#endif /* ZIX_COMMON_H */
Loading

0 comments on commit d12d5d9

Please sign in to comment.