/[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 1424 - (hide annotations) (download)
Sun Oct 14 22:00:17 2007 UTC (16 years, 6 months ago) by schoenebeck
File size: 5659 byte(s)
* code cleanup:
- global.h now only covers global definitions that are needed for the C++
  API header files, all implementation internal global definitions are now
  in global_private.h
- atomic.h is not exposed to the C++ API anymore (replaced the references
  in SynchronizedConfig.h for this with local definitions)
- no need to include config.h anymore for using LS's API header files
- DB instruments classes are not exposed to the C++ API
- POSIX callback functions of Thread.h are hidden
- the (optional) gig Engine benchmark compiles again
- updated Doxyfile.in
- fixed warnings in API doc generation
* preparations for release 0.5.0

1 schoenebeck 947 /***************************************************************************
2     * *
3 schoenebeck 1040 * Copyright (C) 2005 - 2007 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    
25 schoenebeck 947 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 persson 1038 dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex));
46 schoenebeck 947 // 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 schoenebeck 989
53     mutex.Lock();
54     queue.push_back(cmd);
55     mutex.Unlock();
56    
57 schoenebeck 947 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 schoenebeck 989
80     mutex.Lock();
81     queue.push_back(cmd);
82     mutex.Unlock();
83    
84 schoenebeck 947 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 schoenebeck 989 while (!queue.empty()) {
92 schoenebeck 947 command_t cmd;
93 schoenebeck 989
94     // grab a new command from the queue
95     mutex.Lock();
96     cmd = queue.front();
97     mutex.Unlock();
98    
99 schoenebeck 947 try {
100     switch (cmd.type) {
101     case command_t::DIRECT_LOAD:
102     cmd.pEngineChannel->LoadInstrument();
103     break;
104     case command_t::INSTR_MODE:
105     cmd.pManager->SetMode(cmd.instrumentId, cmd.mode);
106     break;
107     default:
108     std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush;
109     }
110 schoenebeck 1040 } catch (Exception e) {
111 schoenebeck 947 e.PrintMessage();
112 schoenebeck 1040 } catch (...) {
113     std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush;
114 schoenebeck 947 }
115 schoenebeck 989
116     // remove processed command from queue
117     mutex.Lock();
118     queue.pop_front();
119     mutex.Unlock();
120 schoenebeck 947 }
121    
122     // nothing left to do, sleep until new jobs arrive
123     conditionJobsLeft.WaitIf(false);
124     // reset flag
125     conditionJobsLeft.Set(false);
126     // unlock condition object so it can be turned again by other thread
127     conditionJobsLeft.Unlock();
128     }
129     }
130    
131     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC