/[svn]/linuxsampler/trunk/src/engines/InstrumentManagerThread.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/InstrumentManagerThread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3290 - (show annotations) (download)
Fri Jun 23 12:24:58 2017 UTC (6 years, 10 months ago) by schoenebeck
File size: 8334 byte(s)
* Revised fundamental C++ classes "Thread", "Mutex" and
  "Condition" which fixes potential undefined behavior
  (note: this addresses mainly the POSIX implementation,
   Win32 is untested yet and would also need an update).
* Bumped version (2.0.0.svn64).

1 /***************************************************************************
2 * *
3 * Copyright (C) 2005 - 2017 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 #include "../common/global_private.h"
24 #include "EngineChannelFactory.h"
25
26 namespace LinuxSampler {
27
28 InstrumentManagerThread::InstrumentManagerThread() : Thread(true, false, 0, -4) {
29 eventHandler.pThread = this;
30 }
31
32 InstrumentManagerThread::~InstrumentManagerThread() {
33 Thread::StopThread();
34 }
35
36 /**
37 * @brief Order loading of a new instrument.
38 *
39 * The request will go into a queue waiting to be processed by the
40 * class internal task thread. This method will immediately return and
41 * the instrument will be loaded in the background.
42 *
43 * @param Filename - file name of the instrument
44 * @param uiInstrumentIndex - index of the instrument within the file
45 * @param pEngineChannel - engine channel on which the instrument should be loaded
46 */
47 void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) {
48 dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex));
49
50 // the listener only needs to be registered once in the
51 // Sampler, but as we don't know if Sampler has been
52 // recreated, we simply remove and add every time
53 pEngineChannel->GetSampler()->RemoveChannelCountListener(&eventHandler);
54 pEngineChannel->GetSampler()->AddChannelCountListener(&eventHandler);
55
56 command_t cmd;
57 cmd.type = command_t::DIRECT_LOAD;
58 cmd.pEngineChannel = pEngineChannel;
59 cmd.instrumentId.Index = uiInstrumentIndex;
60 cmd.instrumentId.FileName = Filename;
61
62 {
63 LockGuard lock(mutex);
64 queue.push_back(cmd);
65 }
66
67 StartThread(); // ensure thread is running
68 conditionJobsLeft.Set(true); // wake up thread
69 }
70
71 /**
72 * @brief Order changing the life-time strategy of an instrument.
73 *
74 * The request will go into a queue waiting to be processed by the
75 * class internal task thread. This method will immediately return and
76 * in case the instrument has to be loaded due to a mode change to
77 * PERSISTENT, it will be loaded in the background.
78 *
79 * @param pManager - InstrumentManager which manages the instrument
80 * @param ID - unique ID of the instrument
81 * @param Mode - life-time strategy to set for this instrument
82 */
83 void InstrumentManagerThread::StartSettingMode(InstrumentManager* pManager, const InstrumentManager::instrument_id_t& ID, InstrumentManager::mode_t Mode) {
84 command_t cmd;
85 cmd.type = command_t::INSTR_MODE;
86 cmd.pManager = pManager;
87 cmd.instrumentId = ID;
88 cmd.mode = Mode;
89
90 {
91 LockGuard lock(mutex);
92 queue.push_back(cmd);
93 }
94
95 StartThread(); // ensure thread is running
96 conditionJobsLeft.Set(true); // wake up thread
97 }
98
99 // Entry point for the task thread.
100 int InstrumentManagerThread::Main() {
101 while (true) {
102
103 #if CONFIG_PTHREAD_TESTCANCEL
104 TestCancel();
105 #endif
106
107 while (true) {
108 command_t cmd;
109
110 // grab a new command from the queue
111 {
112 LockGuard lock(mutex);
113 if (queue.empty()) break;
114
115 cmd = queue.front();
116 queue.pop_front();
117
118 if (cmd.type == command_t::DIRECT_LOAD) {
119 EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, false);
120 }
121 }
122
123 try {
124 switch (cmd.type) {
125 case command_t::DIRECT_LOAD:
126 cmd.pEngineChannel->PrepareLoadInstrument(cmd.instrumentId.FileName.c_str(), cmd.instrumentId.Index);
127 cmd.pEngineChannel->LoadInstrument();
128 EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true);
129 break;
130 case command_t::INSTR_MODE:
131 cmd.pManager->SetMode(cmd.instrumentId, cmd.mode);
132 break;
133 default:
134 std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush;
135 }
136 } catch (Exception e) {
137 e.PrintMessage();
138 if (cmd.type == command_t::DIRECT_LOAD) {
139 EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true);
140 }
141 } catch (...) {
142 std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush;
143 if (cmd.type == command_t::DIRECT_LOAD) {
144 EngineChannelFactory::SetDeleteEnabled(cmd.pEngineChannel, true);
145 }
146 }
147 }
148
149 // nothing left to do, sleep until new jobs arrive
150 conditionJobsLeft.WaitIf(false);
151 // reset flag
152 conditionJobsLeft.Set(false);
153 // unlock condition object so it can be turned again by other thread
154 conditionJobsLeft.Unlock();
155 }
156 return 0;
157 }
158
159 void InstrumentManagerThread::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
160 /*
161 Removing from the queue an eventual scheduled loading of an instrument
162 to a sampler channel which is going to be removed.
163 */
164 LockGuard lock(pThread->mutex);
165 std::list<command_t>::iterator it;
166 for (it = pThread->queue.begin(); it != pThread->queue.end();){
167 if ((*it).type != command_t::DIRECT_LOAD) { ++it; continue; }
168 if ((*it).pEngineChannel == pChannel->GetEngineChannel()) {
169 it = pThread->queue.erase(it);
170 // we don't break here because the same engine channel could
171 // occur more than once in the queue, so don't make optimizations
172 } else {
173 ++it;
174 }
175 }
176 }
177
178 #if defined(__APPLE__) && !defined(__x86_64__)
179 int InstrumentManagerThread::StopThread() {
180 // This is a fix for Mac OS X 32 bit, where SignalStopThread
181 // doesn't wake up a thread waiting for a condition variable.
182 SignalStopThread(); // send stop signal, but don't wait
183 conditionJobsLeft.Set(true); // wake thread
184 return Thread::StopThread(); // then wait for it to cancel
185 }
186 #endif
187
188 #ifdef WIN32
189 int InstrumentManagerThread::StopThread() {
190 int res = Thread::StopThread();
191 conditionJobsLeft.Reset();
192 return res;
193 }
194 #endif
195
196 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC