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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 970 by schoenebeck, Wed Dec 6 22:28:17 2006 UTC revision 1761 by iliev, Fri Aug 29 15:42:06 2008 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2005, 2006 Christian Schoenebeck                        *   *   Copyright (C) 2005 - 2007 Christian Schoenebeck                       *
4   *                                                                         *   *                                                                         *
5   *   This library is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 20  Line 20 
20    
21  #include "InstrumentManagerThread.h"  #include "InstrumentManagerThread.h"
22    
23    #include "../common/global_private.h"
24    
25  namespace LinuxSampler {  namespace LinuxSampler {
26    
27      InstrumentManagerThread::InstrumentManagerThread() : Thread(true, false, 0, -4) {      InstrumentManagerThread::InstrumentManagerThread() : Thread(true, false, 0, -4) {
28          pQueue = new RingBuffer<command_t,true>(INSTRUMENT_LOADER_QUEUE_SIZE);          eventHandler.pThread = this;
29      }      }
30    
31      InstrumentManagerThread::~InstrumentManagerThread() {      InstrumentManagerThread::~InstrumentManagerThread() {
         if (pQueue) delete pQueue;  
32      }      }
33    
34      /**      /**
# Line 42  namespace LinuxSampler { Line 43  namespace LinuxSampler {
43       * @param pEngineChannel - engine channel on which the instrument should be loaded       * @param pEngineChannel - engine channel on which the instrument should be loaded
44       */       */
45      void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) {      void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) {
46          dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).",Filename.c_str(),uiInstrumentIndex));          dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex));
47    
48            static bool listenerRegistered = false;
49            if (!listenerRegistered) {
50                pEngineChannel->GetSampler()->AddChannelCountListener(&eventHandler);
51                listenerRegistered = true;
52            }
53            
54            
55          // already tell the engine which instrument to load          // already tell the engine which instrument to load
56          pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrumentIndex);          pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrumentIndex);
57    
58          command_t cmd;          command_t cmd;
59          cmd.type           = command_t::DIRECT_LOAD;          cmd.type           = command_t::DIRECT_LOAD;
60          cmd.pEngineChannel = pEngineChannel;          cmd.pEngineChannel = pEngineChannel;
61          pQueue->push(&cmd);  
62            mutex.Lock();
63            queue.push_back(cmd);
64            mutex.Unlock();
65    
66          StartThread(); // ensure thread is running          StartThread(); // ensure thread is running
67          conditionJobsLeft.Set(true); // wake up thread          conditionJobsLeft.Set(true); // wake up thread
68      }      }
# Line 72  namespace LinuxSampler { Line 85  namespace LinuxSampler {
85          cmd.pManager     = pManager;          cmd.pManager     = pManager;
86          cmd.instrumentId = ID;          cmd.instrumentId = ID;
87          cmd.mode         = Mode;          cmd.mode         = Mode;
88          pQueue->push(&cmd);  
89            mutex.Lock();
90            queue.push_back(cmd);
91            mutex.Unlock();
92    
93          StartThread(); // ensure thread is running          StartThread(); // ensure thread is running
94          conditionJobsLeft.Set(true); // wake up thread          conditionJobsLeft.Set(true); // wake up thread
95      }      }
# Line 80  namespace LinuxSampler { Line 97  namespace LinuxSampler {
97      // Entry point for the task thread.      // Entry point for the task thread.
98      int InstrumentManagerThread::Main() {      int InstrumentManagerThread::Main() {
99          while (true) {          while (true) {
100              while (pQueue->read_space()) {  
101                            #if CONFIG_PTHREAD_TESTCANCEL
102                            TestCancel();
103                            #endif
104    
105                while (!queue.empty()) {
106                  command_t cmd;                  command_t cmd;
107                  pQueue->pop(&cmd);  
108                    // grab a new command from the queue
109                    mutex.Lock();
110                    cmd = queue.front();
111                    queue.pop_front();
112                    mutex.Unlock();
113    
114                  try {                  try {
115                      switch (cmd.type) {                      switch (cmd.type) {
116                          case command_t::DIRECT_LOAD:                          case command_t::DIRECT_LOAD:
# Line 94  namespace LinuxSampler { Line 122  namespace LinuxSampler {
122                          default:                          default:
123                              std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush;                              std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush;
124                      }                      }
125                  }                  } catch (Exception e) {
                 catch (Exception e) {  
126                      e.PrintMessage();                      e.PrintMessage();
127                    } catch (...) {
128                        std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush;
129                  }                  }
130              }              }
131    
# Line 108  namespace LinuxSampler { Line 137  namespace LinuxSampler {
137              conditionJobsLeft.Unlock();              conditionJobsLeft.Unlock();
138          }          }
139      }      }
140        
141        void InstrumentManagerThread::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
142            /*
143               Removing from the queue an eventual scheduled loading of an instrument
144               to a sampler channel which is going to be removed.
145            */
146            pThread->mutex.Lock();
147            std::list<command_t>::iterator it;
148            for (it = pThread->queue.begin(); it != pThread->queue.end();){
149                if ((*it).type != command_t::DIRECT_LOAD) { ++it; continue; }
150                if ((*it).pEngineChannel == pChannel->GetEngineChannel()) {
151                    it = pThread->queue.erase(it);
152                    // we don't break here because the same engine channel could
153                    // occur more than once in the queue, so don't make optimizations
154                } else {
155                    ++it;
156                }
157            }
158            pThread->mutex.Unlock();
159        }
160  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.970  
changed lines
  Added in v.1761

  ViewVC Help
Powered by ViewVC