1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2005 - 2012 Christian Schoenebeck * |
* Copyright (C) 2005 - 2020 Christian Schoenebeck * |
4 |
* * |
* * |
5 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
30 |
} |
} |
31 |
|
|
32 |
InstrumentManagerThread::~InstrumentManagerThread() { |
InstrumentManagerThread::~InstrumentManagerThread() { |
33 |
|
Thread::StopThread(); |
34 |
} |
} |
35 |
|
|
36 |
/** |
/** |
59 |
cmd.instrumentId.Index = uiInstrumentIndex; |
cmd.instrumentId.Index = uiInstrumentIndex; |
60 |
cmd.instrumentId.FileName = Filename; |
cmd.instrumentId.FileName = Filename; |
61 |
|
|
62 |
mutex.Lock(); |
{ |
63 |
queue.push_back(cmd); |
LockGuard lock(mutex); |
64 |
mutex.Unlock(); |
queue.push_back(cmd); |
65 |
|
} |
66 |
|
|
67 |
StartThread(); // ensure thread is running |
StartThread(); // ensure thread is running |
68 |
conditionJobsLeft.Set(true); // wake up thread |
conditionJobsLeft.Set(true); // wake up thread |
87 |
cmd.instrumentId = ID; |
cmd.instrumentId = ID; |
88 |
cmd.mode = Mode; |
cmd.mode = Mode; |
89 |
|
|
90 |
mutex.Lock(); |
{ |
91 |
queue.push_back(cmd); |
LockGuard lock(mutex); |
92 |
mutex.Unlock(); |
queue.push_back(cmd); |
93 |
|
} |
94 |
|
|
95 |
StartThread(); // ensure thread is running |
StartThread(); // ensure thread is running |
96 |
conditionJobsLeft.Set(true); // wake up thread |
conditionJobsLeft.Set(true); // wake up thread |
98 |
|
|
99 |
// Entry point for the task thread. |
// Entry point for the task thread. |
100 |
int InstrumentManagerThread::Main() { |
int InstrumentManagerThread::Main() { |
101 |
|
#if DEBUG |
102 |
|
Thread::setNameOfCaller("InstrumentMngr"); |
103 |
|
#endif |
104 |
|
|
105 |
while (true) { |
while (true) { |
106 |
|
|
|
#if CONFIG_PTHREAD_TESTCANCEL |
|
107 |
TestCancel(); |
TestCancel(); |
108 |
#endif |
|
109 |
|
// prevent thread from being cancelled |
110 |
|
// (e.g. to prevent deadlocks while holding mutex lock(s)) |
111 |
|
pushCancelable(false); |
112 |
|
|
113 |
while (true) { |
while (true) { |
114 |
command_t cmd; |
command_t cmd; |
115 |
|
|
116 |
// grab a new command from the queue |
// grab a new command from the queue |
117 |
mutex.Lock(); |
{ |
118 |
bool empty = queue.empty(); |
LockGuard lock(mutex); |
119 |
if (!empty) { |
if (queue.empty()) break; |
120 |
|
|
121 |
cmd = queue.front(); |
cmd = queue.front(); |
122 |
queue.pop_front(); |
queue.pop_front(); |
123 |
|
|
124 |
|
if (cmd.type == command_t::DIRECT_LOAD) { |
125 |
|
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, false); |
126 |
|
} |
127 |
} |
} |
|
mutex.Unlock(); |
|
|
if (empty) break; |
|
128 |
|
|
129 |
try { |
try { |
130 |
switch (cmd.type) { |
switch (cmd.type) { |
131 |
case command_t::DIRECT_LOAD: |
case command_t::DIRECT_LOAD: |
|
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, false); |
|
132 |
cmd.pEngineChannel->PrepareLoadInstrument(cmd.instrumentId.FileName.c_str(), cmd.instrumentId.Index); |
cmd.pEngineChannel->PrepareLoadInstrument(cmd.instrumentId.FileName.c_str(), cmd.instrumentId.Index); |
133 |
cmd.pEngineChannel->LoadInstrument(); |
cmd.pEngineChannel->LoadInstrument(); |
134 |
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true); |
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true); |
152 |
} |
} |
153 |
} |
} |
154 |
|
|
155 |
|
// now allow thread being cancelled again |
156 |
|
// (since all mutexes are now unlocked) |
157 |
|
popCancelable(); |
158 |
|
|
159 |
// nothing left to do, sleep until new jobs arrive |
// nothing left to do, sleep until new jobs arrive |
160 |
conditionJobsLeft.WaitIf(false); |
conditionJobsLeft.WaitIf(false); |
161 |
// reset flag |
// reset flag |
171 |
Removing from the queue an eventual scheduled loading of an instrument |
Removing from the queue an eventual scheduled loading of an instrument |
172 |
to a sampler channel which is going to be removed. |
to a sampler channel which is going to be removed. |
173 |
*/ |
*/ |
174 |
pThread->mutex.Lock(); |
LockGuard lock(pThread->mutex); |
175 |
std::list<command_t>::iterator it; |
std::list<command_t>::iterator it; |
176 |
for (it = pThread->queue.begin(); it != pThread->queue.end();){ |
for (it = pThread->queue.begin(); it != pThread->queue.end();){ |
177 |
if ((*it).type != command_t::DIRECT_LOAD) { ++it; continue; } |
if ((*it).type != command_t::DIRECT_LOAD) { ++it; continue; } |
183 |
++it; |
++it; |
184 |
} |
} |
185 |
} |
} |
|
pThread->mutex.Unlock(); |
|
186 |
} |
} |
187 |
|
|
188 |
#ifdef __APPLE__ |
#if defined(__APPLE__) && !defined(__x86_64__) |
189 |
int InstrumentManagerThread::StopThread() { |
int InstrumentManagerThread::StopThread() { |
190 |
// This is a fix for Mac OS X, where SignalStopThread doesn't |
// This is a fix for Mac OS X 32 bit, where SignalStopThread |
191 |
// wake up a thread waiting for a condition variable. |
// doesn't wake up a thread waiting for a condition variable. |
192 |
SignalStopThread(); // send stop signal, but don't wait |
SignalStopThread(); // send stop signal, but don't wait |
193 |
conditionJobsLeft.Set(true); // wake thread |
conditionJobsLeft.Set(true); // wake thread |
194 |
return Thread::StopThread(); // then wait for it to cancel |
return Thread::StopThread(); // then wait for it to cancel |