added ability pass thread id for async

This commit is contained in:
Igor Korsukov 2021-05-13 16:19:06 +03:00 committed by pereverzev+v
parent adbf6cfb41
commit 8cc89e0f1b
6 changed files with 21 additions and 10 deletions

View File

@ -31,6 +31,8 @@
using namespace mu::audio;
std::thread::id AudioThread::ID;
AudioThread::AudioThread()
{
m_channel = std::make_shared<rpc::QueuedRpcChannel>();
@ -88,6 +90,8 @@ void AudioThread::main()
mu::runtime::setThreadName("audio_worker");
m_channel->setupWorkerThread();
AudioThread::ID = std::this_thread::get_id();
if (m_onStart) {
m_onStart();
}

View File

@ -39,6 +39,8 @@ public:
AudioThread();
~AudioThread();
static std::thread::id ID;
using OnStart = std::function<void ()>;
using OnFinished = std::function<void ()>;
@ -58,7 +60,6 @@ private:
OnStart m_onStart;
OnFinished m_onFinished;
rpc::QueuedRpcChannelPtr m_channel;
std::shared_ptr<rpc::RpcControllers> m_controller;
std::shared_ptr<IAudioBuffer> m_buffer = nullptr;
std::shared_ptr<std::thread> m_thread = nullptr;
std::atomic<bool> m_running = false;

View File

@ -27,6 +27,9 @@
#include "midiplayer.h"
#include "audioplayer.h"
#include "async/async.h"
#include "internal/audiothread.h"
using namespace mu::audio;
Sequencer::Sequencer()
@ -61,8 +64,10 @@ mu::async::Channel<ISequencer::Status> Sequencer::statusChanged() const
void Sequencer::play()
{
ONLY_AUDIO_WORKER_THREAD;
m_nextStatus = PLAYING;
async::Async::call(this, [this]() {
ONLY_AUDIO_WORKER_THREAD;
m_nextStatus = PLAYING;
}, AudioThread::ID);
}
void Sequencer::pause()

View File

@ -11,15 +11,15 @@ class Async
public:
template<typename F>
static void call(const Asyncable* caller, F f)
static void call(const Asyncable* caller, F f, const std::thread::id& th = std::this_thread::get_id())
{
AsyncImpl::instance()->call(const_cast<Asyncable*>(caller), new AsyncImpl::Functor<F>(f));
AsyncImpl::instance()->call(const_cast<Asyncable*>(caller), new AsyncImpl::Functor<F>(f), th);
}
template<typename F, typename Arg1>
static void call(const Asyncable* caller, F f, Arg1 a1)
static void call(const Asyncable* caller, F f, Arg1 a1, const std::thread::id& th = std::this_thread::get_id())
{
AsyncImpl::instance()->call(const_cast<Asyncable*>(caller), new AsyncImpl::FunctorArg1<F, Arg1>(f, a1));
AsyncImpl::instance()->call(const_cast<Asyncable*>(caller), new AsyncImpl::FunctorArg1<F, Arg1>(f, a1), th);
}
static void disconnectAsync(Asyncable* a)

View File

@ -27,7 +27,7 @@ void AsyncImpl::disconnectAsync(Asyncable* caller)
}
}
void AsyncImpl::call(Asyncable* caller, IFunction* f)
void AsyncImpl::call(Asyncable* caller, IFunction* f, const std::thread::id& th)
{
if (caller) {
caller->connectAsync(this);
@ -40,7 +40,7 @@ void AsyncImpl::call(Asyncable* caller, IFunction* f)
}
auto functor = [this, key]() { onCall(key); };
QueuedInvoker::instance()->invoke(std::this_thread::get_id(), functor, true);
QueuedInvoker::instance()->invoke(th, functor, true);
}
void AsyncImpl::onCall(uint64_t key)

View File

@ -3,6 +3,7 @@
#include <mutex>
#include <map>
#include <thread>
#include "../asyncable.h"
namespace deto {
@ -35,7 +36,7 @@ public:
void call() { functor(arg1); }
};
void call(Asyncable* caller, IFunction* f);
void call(Asyncable* caller, IFunction* f, const std::thread::id& th = std::this_thread::get_id());
void disconnectAsync(Asyncable* caller);
private: