1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
4 |
* * |
5 |
* 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 * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This library is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this library; if not, write to the Free Software * |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
* MA 02111-1307 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include "InstrumentManagerThread.h" |
22 |
|
23 |
namespace LinuxSampler { |
24 |
|
25 |
InstrumentManagerThread::InstrumentManagerThread() : Thread(true, false, 0, -4) { |
26 |
} |
27 |
|
28 |
InstrumentManagerThread::~InstrumentManagerThread() { |
29 |
} |
30 |
|
31 |
/** |
32 |
* @brief Order loading of a new instrument. |
33 |
* |
34 |
* The request will go into a queue waiting to be processed by the |
35 |
* class internal task thread. This method will immediately return and |
36 |
* the instrument will be loaded in the background. |
37 |
* |
38 |
* @param Filename - file name of the instrument |
39 |
* @param uiInstrumentIndex - index of the instrument within the file |
40 |
* @param pEngineChannel - engine channel on which the instrument should be loaded |
41 |
*/ |
42 |
void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) { |
43 |
dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex)); |
44 |
// already tell the engine which instrument to load |
45 |
pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrumentIndex); |
46 |
|
47 |
command_t cmd; |
48 |
cmd.type = command_t::DIRECT_LOAD; |
49 |
cmd.pEngineChannel = pEngineChannel; |
50 |
|
51 |
mutex.Lock(); |
52 |
queue.push_back(cmd); |
53 |
mutex.Unlock(); |
54 |
|
55 |
StartThread(); // ensure thread is running |
56 |
conditionJobsLeft.Set(true); // wake up thread |
57 |
} |
58 |
|
59 |
/** |
60 |
* @brief Order changing the life-time strategy of an instrument. |
61 |
* |
62 |
* The request will go into a queue waiting to be processed by the |
63 |
* class internal task thread. This method will immediately return and |
64 |
* in case the instrument has to be loaded due to a mode change to |
65 |
* PERSISTENT, it will be loaded in the background. |
66 |
* |
67 |
* @param pManager - InstrumentManager which manages the instrument |
68 |
* @param ID - unique ID of the instrument |
69 |
* @param Mode - life-time strategy to set for this instrument |
70 |
*/ |
71 |
void InstrumentManagerThread::StartSettingMode(InstrumentManager* pManager, const InstrumentManager::instrument_id_t& ID, InstrumentManager::mode_t Mode) { |
72 |
command_t cmd; |
73 |
cmd.type = command_t::INSTR_MODE; |
74 |
cmd.pManager = pManager; |
75 |
cmd.instrumentId = ID; |
76 |
cmd.mode = Mode; |
77 |
|
78 |
mutex.Lock(); |
79 |
queue.push_back(cmd); |
80 |
mutex.Unlock(); |
81 |
|
82 |
StartThread(); // ensure thread is running |
83 |
conditionJobsLeft.Set(true); // wake up thread |
84 |
} |
85 |
|
86 |
// Entry point for the task thread. |
87 |
int InstrumentManagerThread::Main() { |
88 |
while (true) { |
89 |
while (!queue.empty()) { |
90 |
command_t cmd; |
91 |
|
92 |
// grab a new command from the queue |
93 |
mutex.Lock(); |
94 |
cmd = queue.front(); |
95 |
mutex.Unlock(); |
96 |
|
97 |
try { |
98 |
switch (cmd.type) { |
99 |
case command_t::DIRECT_LOAD: |
100 |
cmd.pEngineChannel->LoadInstrument(); |
101 |
break; |
102 |
case command_t::INSTR_MODE: |
103 |
cmd.pManager->SetMode(cmd.instrumentId, cmd.mode); |
104 |
break; |
105 |
default: |
106 |
std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush; |
107 |
} |
108 |
} catch (Exception e) { |
109 |
e.PrintMessage(); |
110 |
} catch (...) { |
111 |
std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush; |
112 |
} |
113 |
|
114 |
// remove processed command from queue |
115 |
mutex.Lock(); |
116 |
queue.pop_front(); |
117 |
mutex.Unlock(); |
118 |
} |
119 |
|
120 |
// nothing left to do, sleep until new jobs arrive |
121 |
conditionJobsLeft.WaitIf(false); |
122 |
// reset flag |
123 |
conditionJobsLeft.Set(false); |
124 |
// unlock condition object so it can be turned again by other thread |
125 |
conditionJobsLeft.Unlock(); |
126 |
} |
127 |
} |
128 |
|
129 |
} // namespace LinuxSampler |