diff options
Diffstat (limited to 'src/worker.h')
-rw-r--r-- | src/worker.h | 85 |
1 files changed, 64 insertions, 21 deletions
diff --git a/src/worker.h b/src/worker.h index 5956e23..09d35ca 100644 --- a/src/worker.h +++ b/src/worker.h @@ -6,9 +6,7 @@ #include "attributes.h" -#include "zix/ring.h" #include "zix/sem.h" -#include "zix/thread.h" #include "lv2/core/lv2.h" #include "lv2/worker/worker.h" @@ -18,40 +16,85 @@ JALV_BEGIN_DECLS -// Worker for running non-realtime tasks for plugins - -typedef struct { - ZixRing* requests; ///< Requests to the worker - ZixRing* responses; ///< Responses from the worker - void* response; ///< Worker response buffer - ZixSem* lock; ///< Lock for plugin work() method - bool* exit; ///< Pointer to exit flag - ZixSem sem; ///< Worker semaphore - ZixThread thread; ///< Worker thread - LV2_Handle handle; ///< Plugin handle - const LV2_Worker_Interface* iface; ///< Plugin worker interface - bool threaded; ///< Run work in another thread -} JalvWorker; +/** + A worker for running non-realtime tasks for plugins. + The worker can be used in threaded mode, which allows non-realtime work to be + done with latency when running realtime, or non-threaded mode, which performs + work immediately for state restoration or offline rendering. +*/ +typedef struct JalvWorkerImpl JalvWorker; + +/** + Allocate a new worker and launch its thread if necessary. + + @param lock Pointer to lock used to guard doing work. + @param threaded If true, launch a thread to perform work in. + @return A newly allocated worker, or null on error. +*/ +JalvWorker* +jalv_worker_new(ZixSem* lock, bool threaded); + +/** + Start performing work for a plugin, launching the thread if necessary. + + This must be called before scheduling any work. + + @param iface Worker interface from plugin. + @param handle Handle to the LV2 plugin this worker is for. +*/ void -jalv_worker_init(JalvWorker* worker, - const LV2_Worker_Interface* iface, - bool threaded); +jalv_worker_start(JalvWorker* worker, + const LV2_Worker_Interface* iface, + LV2_Handle handle); + +/** + Terminate the worker's thread if necessary. + For threaded workers, this blocks until the thread has exited. For + non-threaded workers, this does nothing. +*/ void -jalv_worker_finish(JalvWorker* worker); +jalv_worker_exit(JalvWorker* worker); +/** + Free a worker allocated with jalv_worker_new(). + + Calls jalv_worker_exit() to terminate the running thread if necessary. +*/ void -jalv_worker_destroy(JalvWorker* worker); +jalv_worker_free(JalvWorker* worker); + +/** + Schedule work to be performed by the worker in the audio thread. + For threaded workers, this enqueues a request that will be handled by the + worker thread asynchronously. For non-threaded workers, the work is + performed immediately before returning. +*/ LV2_Worker_Status jalv_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data); +/** + Emit any pending responses to the plugin in the audio thread. + + This dispatches responses from work that has been completed since the last + call, so the plugin knows it is finished and can apply the changes. +*/ void jalv_worker_emit_responses(JalvWorker* worker, LV2_Handle lv2_handle); +/** + Notify the plugin that the run() cycle is finished. + + This must be called near the end of every cycle, after any potential calls + to jalv_worker_emit_responses(). +*/ +void +jalv_worker_end_run(JalvWorker* worker); + JALV_END_DECLS #endif // JALV_WORKER_H |