Worker thread per plugin instance.

This commit is contained in:
Robin E. R. Davies
2025-07-22 16:02:37 -04:00
parent 7ffbee16cf
commit ea134b46a8
8 changed files with 56 additions and 29 deletions
+24 -8
View File
@@ -15,7 +15,7 @@
*/
// (Borrows heavily from worker.c by David Robillard.)
// Copyright (c) 2022 Robin Davies
// Copyright (c) 2024 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
@@ -242,8 +242,19 @@ void HostWorkerThread::ThreadProc() noexcept
HostWorkerThread::HostWorkerThread()
{
this->dataBuffer.resize(16*1024);
pThread = std::make_unique<std::thread>([this]()
{ this->ThreadProc(); });
}
bool HostWorkerThread::StartThread()
{
if (!pThread)
{
if (closed) {
return false;
}
pThread = std::make_unique<std::thread>([this]()
{ this->ThreadProc(); });
}
return true;
}
void HostWorkerThread::Close()
@@ -252,10 +263,12 @@ void HostWorkerThread::Close()
bool sendClose = false;
{
std::lock_guard lock{submitMutex};
if (!closed)
{
closed = true;
sendClose = true;
if (pThread) {
if (!closed)
{
closed = true;
sendClose = true;
}
}
}
if (sendClose)
@@ -278,7 +291,10 @@ LV2_Worker_Status HostWorkerThread::ScheduleWork(Worker *worker, size_t size, co
{
std::lock_guard lock(submitMutex);
if (exiting)
if (!StartThread()) { // ensures thread is running, when plugins haven't declared they want a worker thread.
return LV2_Worker_Status::LV2_WORKER_ERR_NO_SPACE;
}
if (exiting )
{
return LV2_Worker_Status::LV2_WORKER_ERR_NO_SPACE;
}