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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2427 - (hide annotations) (download)
Sat Mar 2 07:03:04 2013 UTC (11 years, 3 months ago) by persson
File size: 8304 byte(s)
* code refactoring: added a lock guard class for exception safe mutex
  handling and used it everywhere appropriate

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

  ViewVC Help
Powered by ViewVC