1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
* Copyright (C) 2005 - 2012 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 * |
21 |
#include "InstrumentManagerThread.h" |
#include "InstrumentManagerThread.h" |
22 |
|
|
23 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
24 |
|
#include "EngineChannelFactory.h" |
25 |
|
|
26 |
namespace LinuxSampler { |
namespace LinuxSampler { |
27 |
|
|
46 |
void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) { |
void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) { |
47 |
dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex)); |
dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex)); |
48 |
|
|
49 |
static bool listenerRegistered = false; |
// the listener only needs to be registered once in the |
50 |
if (!listenerRegistered) { |
// Sampler, but as we don't know if Sampler has been |
51 |
pEngineChannel->GetSampler()->AddChannelCountListener(&eventHandler); |
// recreated, we simply remove and add every time |
52 |
listenerRegistered = true; |
pEngineChannel->GetSampler()->RemoveChannelCountListener(&eventHandler); |
53 |
} |
pEngineChannel->GetSampler()->AddChannelCountListener(&eventHandler); |
|
|
|
54 |
|
|
|
// already tell the engine which instrument to load |
|
|
pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrumentIndex); |
|
|
|
|
55 |
command_t cmd; |
command_t cmd; |
56 |
cmd.type = command_t::DIRECT_LOAD; |
cmd.type = command_t::DIRECT_LOAD; |
57 |
cmd.pEngineChannel = pEngineChannel; |
cmd.pEngineChannel = pEngineChannel; |
58 |
|
cmd.instrumentId.Index = uiInstrumentIndex; |
59 |
|
cmd.instrumentId.FileName = Filename; |
60 |
|
|
61 |
mutex.Lock(); |
mutex.Lock(); |
62 |
queue.push_back(cmd); |
queue.push_back(cmd); |
97 |
int InstrumentManagerThread::Main() { |
int InstrumentManagerThread::Main() { |
98 |
while (true) { |
while (true) { |
99 |
|
|
100 |
#if CONFIG_PTHREAD_TESTCANCEL |
#if CONFIG_PTHREAD_TESTCANCEL |
101 |
TestCancel(); |
TestCancel(); |
102 |
#endif |
#endif |
103 |
|
|
104 |
while (!queue.empty()) { |
while (true) { |
105 |
command_t cmd; |
command_t cmd; |
106 |
|
|
107 |
// grab a new command from the queue |
// grab a new command from the queue |
108 |
mutex.Lock(); |
mutex.Lock(); |
109 |
cmd = queue.front(); |
bool empty = queue.empty(); |
110 |
queue.pop_front(); |
if (!empty) { |
111 |
|
cmd = queue.front(); |
112 |
|
queue.pop_front(); |
113 |
|
|
114 |
|
if (cmd.type == command_t::DIRECT_LOAD) { |
115 |
|
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, false); |
116 |
|
} |
117 |
|
} |
118 |
mutex.Unlock(); |
mutex.Unlock(); |
119 |
|
if (empty) break; |
120 |
|
|
121 |
try { |
try { |
122 |
switch (cmd.type) { |
switch (cmd.type) { |
123 |
case command_t::DIRECT_LOAD: |
case command_t::DIRECT_LOAD: |
124 |
|
cmd.pEngineChannel->PrepareLoadInstrument(cmd.instrumentId.FileName.c_str(), cmd.instrumentId.Index); |
125 |
cmd.pEngineChannel->LoadInstrument(); |
cmd.pEngineChannel->LoadInstrument(); |
126 |
|
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true); |
127 |
break; |
break; |
128 |
case command_t::INSTR_MODE: |
case command_t::INSTR_MODE: |
129 |
cmd.pManager->SetMode(cmd.instrumentId, cmd.mode); |
cmd.pManager->SetMode(cmd.instrumentId, cmd.mode); |
133 |
} |
} |
134 |
} catch (Exception e) { |
} catch (Exception e) { |
135 |
e.PrintMessage(); |
e.PrintMessage(); |
136 |
|
if (cmd.type == command_t::DIRECT_LOAD) { |
137 |
|
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true); |
138 |
|
} |
139 |
} catch (...) { |
} catch (...) { |
140 |
std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush; |
std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush; |
141 |
|
if (cmd.type == command_t::DIRECT_LOAD) { |
142 |
|
EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true); |
143 |
|
} |
144 |
} |
} |
145 |
} |
} |
146 |
|
|
151 |
// unlock condition object so it can be turned again by other thread |
// unlock condition object so it can be turned again by other thread |
152 |
conditionJobsLeft.Unlock(); |
conditionJobsLeft.Unlock(); |
153 |
} |
} |
154 |
|
return 0; |
155 |
} |
} |
156 |
|
|
157 |
void InstrumentManagerThread::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) { |
void InstrumentManagerThread::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) { |
158 |
/* |
/* |
159 |
Removing from the queue an eventual scheduled loading of an instrument |
Removing from the queue an eventual scheduled loading of an instrument |
173 |
} |
} |
174 |
pThread->mutex.Unlock(); |
pThread->mutex.Unlock(); |
175 |
} |
} |
176 |
|
|
177 |
|
#if defined(__APPLE__) && !defined(__x86_64__) |
178 |
|
int InstrumentManagerThread::StopThread() { |
179 |
|
// This is a fix for Mac OS X 32 bit, where SignalStopThread |
180 |
|
// doesn't wake up a thread waiting for a condition variable. |
181 |
|
SignalStopThread(); // send stop signal, but don't wait |
182 |
|
conditionJobsLeft.Set(true); // wake thread |
183 |
|
return Thread::StopThread(); // then wait for it to cancel |
184 |
|
} |
185 |
|
#endif |
186 |
|
|
187 |
|
#ifdef WIN32 |
188 |
|
int InstrumentManagerThread::StopThread() { |
189 |
|
int res = Thread::StopThread(); |
190 |
|
conditionJobsLeft.Reset(); |
191 |
|
return res; |
192 |
|
} |
193 |
|
#endif |
194 |
|
|
195 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |