/[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 1649 - (show annotations) (download)
Fri Jan 25 15:06:02 2008 UTC (16 years, 2 months ago) by nagata
File size: 5721 byte(s)
* added a new config option --enable-pthread-testcancel, which uses
pthread_testcancel() instead of asynchronous canceling (needed for OSX)

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 #include "../common/global_private.h"
24
25 namespace LinuxSampler {
26
27 InstrumentManagerThread::InstrumentManagerThread() : Thread(true, false, 0, -4) {
28 }
29
30 InstrumentManagerThread::~InstrumentManagerThread() {
31 }
32
33 /**
34 * @brief Order loading of a new instrument.
35 *
36 * The request will go into a queue waiting to be processed by the
37 * class internal task thread. This method will immediately return and
38 * the instrument will be loaded in the background.
39 *
40 * @param Filename - file name of the instrument
41 * @param uiInstrumentIndex - index of the instrument within the file
42 * @param pEngineChannel - engine channel on which the instrument should be loaded
43 */
44 void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) {
45 dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex));
46 // already tell the engine which instrument to load
47 pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrumentIndex);
48
49 command_t cmd;
50 cmd.type = command_t::DIRECT_LOAD;
51 cmd.pEngineChannel = pEngineChannel;
52
53 mutex.Lock();
54 queue.push_back(cmd);
55 mutex.Unlock();
56
57 StartThread(); // ensure thread is running
58 conditionJobsLeft.Set(true); // wake up thread
59 }
60
61 /**
62 * @brief Order changing the life-time strategy of an instrument.
63 *
64 * The request will go into a queue waiting to be processed by the
65 * class internal task thread. This method will immediately return and
66 * in case the instrument has to be loaded due to a mode change to
67 * PERSISTENT, it will be loaded in the background.
68 *
69 * @param pManager - InstrumentManager which manages the instrument
70 * @param ID - unique ID of the instrument
71 * @param Mode - life-time strategy to set for this instrument
72 */
73 void InstrumentManagerThread::StartSettingMode(InstrumentManager* pManager, const InstrumentManager::instrument_id_t& ID, InstrumentManager::mode_t Mode) {
74 command_t cmd;
75 cmd.type = command_t::INSTR_MODE;
76 cmd.pManager = pManager;
77 cmd.instrumentId = ID;
78 cmd.mode = Mode;
79
80 mutex.Lock();
81 queue.push_back(cmd);
82 mutex.Unlock();
83
84 StartThread(); // ensure thread is running
85 conditionJobsLeft.Set(true); // wake up thread
86 }
87
88 // Entry point for the task thread.
89 int InstrumentManagerThread::Main() {
90 while (true) {
91
92 #if CONFIG_PTHREAD_TESTCANCEL
93 TestCancel();
94 #endif
95
96 while (!queue.empty()) {
97 command_t cmd;
98
99 // grab a new command from the queue
100 mutex.Lock();
101 cmd = queue.front();
102 mutex.Unlock();
103
104 try {
105 switch (cmd.type) {
106 case command_t::DIRECT_LOAD:
107 cmd.pEngineChannel->LoadInstrument();
108 break;
109 case command_t::INSTR_MODE:
110 cmd.pManager->SetMode(cmd.instrumentId, cmd.mode);
111 break;
112 default:
113 std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush;
114 }
115 } catch (Exception e) {
116 e.PrintMessage();
117 } catch (...) {
118 std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush;
119 }
120
121 // remove processed command from queue
122 mutex.Lock();
123 queue.pop_front();
124 mutex.Unlock();
125 }
126
127 // nothing left to do, sleep until new jobs arrive
128 conditionJobsLeft.WaitIf(false);
129 // reset flag
130 conditionJobsLeft.Set(false);
131 // unlock condition object so it can be turned again by other thread
132 conditionJobsLeft.Unlock();
133 }
134 }
135
136 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC