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

Diff of /linuxsampler/trunk/src/engines/gig/DiskThread.cpp

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

revision 835 by persson, Mon Feb 6 18:07:17 2006 UTC revision 1644 by persson, Sat Jan 19 16:55:03 2008 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 Christian Schoenebeck                              *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   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 58  namespace LinuxSampler { namespace gig { Line 58  namespace LinuxSampler { namespace gig {
58          GhostQueue->init();          GhostQueue->init();
59          CreationQueue->init();          CreationQueue->init();
60          DeletionQueue->init();          DeletionQueue->init();
61            DeletionNotificationQueue.init();
62    
63            // make sure that all DimensionRegions are released
64            while (DeleteDimregQueue->read_space() > 0) {
65                ::gig::DimensionRegion* dimreg;
66                DeleteDimregQueue->pop(&dimreg);
67                pInstruments->HandBackDimReg(dimreg);
68            }
69            DeleteDimregQueue->init();
70          ActiveStreamCount = 0;          ActiveStreamCount = 0;
71          ActiveStreamCountMax = 0;          ActiveStreamCountMax = 0;
72          if (running) this->StartThread(); // start thread only if it was running before          if (running) this->StartThread(); // start thread only if it was running before
# Line 103  namespace LinuxSampler { namespace gig { Line 112  namespace LinuxSampler { namespace gig {
112       * Returns -1 if command queue or pickup pool is full, 0 on success (will be       * Returns -1 if command queue or pickup pool is full, 0 on success (will be
113       * called by audio thread within the voice class).       * called by audio thread within the voice class).
114       */       */
115      int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::Sample* pSample, unsigned long SampleOffset, bool DoLoop) {      int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop) {
116          dmsg(4,("Disk Thread: new stream ordered\n"));          dmsg(4,("Disk Thread: new stream ordered\n"));
117          if (CreationQueue->write_space() < 1) {          if (CreationQueue->write_space() < 1) {
118              dmsg(1,("DiskThread: Order queue full!\n"));              dmsg(1,("DiskThread: Order queue full!\n"));
# Line 125  namespace LinuxSampler { namespace gig { Line 134  namespace LinuxSampler { namespace gig {
134          cmd.OrderID      = pStreamRef->OrderID;          cmd.OrderID      = pStreamRef->OrderID;
135          cmd.hStream      = pStreamRef->hStream;          cmd.hStream      = pStreamRef->hStream;
136          cmd.pStreamRef   = pStreamRef;          cmd.pStreamRef   = pStreamRef;
137          cmd.pSample      = pSample;          cmd.pDimRgn      = pDimRgn;
138          cmd.SampleOffset = SampleOffset;          cmd.SampleOffset = SampleOffset;
139          cmd.DoLoop       = DoLoop;          cmd.DoLoop       = DoLoop;
140    
# Line 134  namespace LinuxSampler { namespace gig { Line 143  namespace LinuxSampler { namespace gig {
143      }      }
144    
145      /**      /**
146       * Returns -1 if command queue is full, 0 on success (will be called by audio       * Request the disk thread to delete the given disk stream. This method
147       * thread within the voice class).       * will return immediately, thus it won't block until the respective voice
148         * was actually deleted. (Called by audio thread within the Voice class).
149         *
150         * @param pStreamRef           - stream that shall be deleted
151         * @param bRequestNotification - set to true in case you want to receive a
152         *                               notification once the stream has actually
153         *                               been deleted
154         * @returns 0 on success, -1 if command queue is full
155         * @see AskForDeletedStream()
156       */       */
157      int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef) {      int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef, bool bRequestNotification) {
158          dmsg(4,("Disk Thread: stream deletion ordered\n"));          dmsg(4,("Disk Thread: stream deletion ordered\n"));
159          if (DeletionQueue->write_space() < 1) {          if (DeletionQueue->write_space() < 1) {
160              dmsg(1,("DiskThread: Deletion queue full!\n"));              dmsg(1,("DiskThread: Deletion queue full!\n"));
# Line 148  namespace LinuxSampler { namespace gig { Line 165  namespace LinuxSampler { namespace gig {
165          cmd.pStream = pStreamRef->pStream;          cmd.pStream = pStreamRef->pStream;
166          cmd.hStream = pStreamRef->hStream;          cmd.hStream = pStreamRef->hStream;
167          cmd.OrderID = pStreamRef->OrderID;          cmd.OrderID = pStreamRef->OrderID;
168            cmd.bNotify = bRequestNotification;
169    
170          DeletionQueue->push(&cmd);          DeletionQueue->push(&cmd);
171          return 0;          return 0;
172      }      }
173    
174      /**      /**
175         * Tell the disk thread to release a dimension region that belongs
176         * to an instrument which isn't loaded anymore. The disk thread
177         * will hand back the dimension region to the instrument resource
178         * manager. (OrderDeletionOfDimreg is called from the audio thread
179         * when a voice dies.)
180         */
181        int DiskThread::OrderDeletionOfDimreg(::gig::DimensionRegion* dimreg) {
182            dmsg(4,("Disk Thread: dimreg deletion ordered\n"));
183            if (DeleteDimregQueue->write_space() < 1) {
184                dmsg(1,("DiskThread: DeleteDimreg queue full!\n"));
185                return -1;
186            }
187            DeleteDimregQueue->push(&dimreg);
188            return 0;
189        }
190    
191        /**
192       * Returns the pointer to a disk stream if the ordered disk stream       * Returns the pointer to a disk stream if the ordered disk stream
193       * represented by the \a StreamOrderID was already activated by the disk       * represented by the \a StreamOrderID was already activated by the disk
194       * thread, returns NULL otherwise. If the call was successful, thus if it       * thread, returns NULL otherwise. If the call was successful, thus if it
# Line 178  namespace LinuxSampler { namespace gig { Line 213  namespace LinuxSampler { namespace gig {
213          return NULL;          return NULL;
214      }      }
215    
216        /**
217         * In case the original sender requested a notification with his stream
218         * deletion order, he can use this method to poll if the respective stream
219         * has actually been deleted.
220         *
221         * @returns handle / identifier of the deleted stream, or
222         *          Stream::INVALID_HANDLE if no notification present
223         */
224        Stream::Handle DiskThread::AskForDeletedStream() {
225            if (DeletionNotificationQueue.read_space()) {
226                Stream::Handle hStream;
227                DeletionNotificationQueue.pop(&hStream);
228                return hStream;
229            } else return Stream::INVALID_HANDLE; // no notification received yet
230        }
231    
232    
233    
234      // #########################################################################      // #########################################################################
# Line 185  namespace LinuxSampler { namespace gig { Line 236  namespace LinuxSampler { namespace gig {
236      // #         (following code should only be executed by the disk thread)      // #         (following code should only be executed by the disk thread)
237    
238    
239      DiskThread::DiskThread(uint BufferWrapElements) : Thread(true, false, 1, -2) {      DiskThread::DiskThread(uint BufferWrapElements, InstrumentResourceManager* pInstruments) :
240            Thread(true, false, 1, -2),
241            pInstruments(pInstruments),
242            DeletionNotificationQueue(4*CONFIG_MAX_STREAMS)
243        {
244          DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE);          DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE);
245          CreationQueue       = new RingBuffer<create_command_t>(1024);          CreationQueue       = new RingBuffer<create_command_t,false>(4*CONFIG_MAX_STREAMS);
246          DeletionQueue       = new RingBuffer<delete_command_t>(1024);          DeletionQueue       = new RingBuffer<delete_command_t,false>(4*CONFIG_MAX_STREAMS);
247          GhostQueue          = new RingBuffer<Stream::Handle>(CONFIG_MAX_STREAMS);          GhostQueue          = new RingBuffer<delete_command_t,false>(CONFIG_MAX_STREAMS);
248            DeleteDimregQueue   = new RingBuffer< ::gig::DimensionRegion*,false>(4*CONFIG_MAX_STREAMS);
249          Streams             = CONFIG_MAX_STREAMS;          Streams             = CONFIG_MAX_STREAMS;
250          RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN;          RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN;
251          for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {          for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
# Line 208  namespace LinuxSampler { namespace gig { Line 264  namespace LinuxSampler { namespace gig {
264          if (CreationQueue) delete CreationQueue;          if (CreationQueue) delete CreationQueue;
265          if (DeletionQueue) delete DeletionQueue;          if (DeletionQueue) delete DeletionQueue;
266          if (GhostQueue)    delete GhostQueue;          if (GhostQueue)    delete GhostQueue;
267            if (DeleteDimregQueue) delete DeleteDimregQueue;
268          ::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer);          ::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer);
269      }      }
270    
271      int DiskThread::Main() {      int DiskThread::Main() {
272          dmsg(3,("Disk thread running\n"));          dmsg(3,("Disk thread running\n"));
273          while (true) {          while (true) {
274                #if !defined(WIN32)
275              pthread_testcancel(); // mandatory for OSX              pthread_testcancel(); // mandatory for OSX
276                #endif
277              IsIdle = true; // will be set to false if a stream got filled              IsIdle = true; // will be set to false if a stream got filled
278    
279              // if there are ghost streams, delete them              // if there are ghost streams, delete them
280              for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient              for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient
281                  Stream::Handle hGhostStream;                  delete_command_t ghostStream;
282                  GhostQueue->pop(&hGhostStream);                  GhostQueue->pop(&ghostStream);
283                  bool found = false;                  bool found = false;
284                  for (int i = 0; i < this->Streams; i++) {                  for (int i = 0; i < this->Streams; i++) {
285                      if (pStreams[i]->GetHandle() == hGhostStream) {                      if (pStreams[i]->GetHandle() == ghostStream.hStream) {
286                          pStreams[i]->Kill();                          pStreams[i]->Kill();
287                          found = true;                          found = true;
288                            // if original sender requested a notification, let him know now
289                            if (ghostStream.bNotify)
290                                DeletionNotificationQueue.push(&ghostStream.hStream);
291                          break;                          break;
292                      }                      }
293                  }                  }
294                  if (!found) GhostQueue->push(&hGhostStream); // put ghost stream handle back to the queue                  if (!found) GhostQueue->push(&ghostStream); // put ghost stream handle back to the queue
295              }              }
296    
297              // if there are creation commands, create new streams              // if there are creation commands, create new streams
# Line 246  namespace LinuxSampler { namespace gig { Line 308  namespace LinuxSampler { namespace gig {
308                  DeleteStream(command);                  DeleteStream(command);
309              }              }
310    
311                // release DimensionRegions that belong to instruments
312                // that are no longer loaded
313                while (DeleteDimregQueue->read_space() > 0) {
314                    ::gig::DimensionRegion* dimreg;
315                    DeleteDimregQueue->pop(&dimreg);
316                    pInstruments->HandBackDimReg(dimreg);
317                }
318    
319              RefillStreams(); // refill the most empty streams              RefillStreams(); // refill the most empty streams
320    
321              // if nothing was done during this iteration (eg no streambuffer              // if nothing was done during this iteration (eg no streambuffer
322              // filled with data) then sleep for 50ms              // filled with data) then sleep for 30ms
323              if (IsIdle) usleep(30000);              if (IsIdle) usleep(30000);
324    
325              int streamsInUsage = 0;              int streamsInUsage = 0;
# Line 276  namespace LinuxSampler { namespace gig { Line 346  namespace LinuxSampler { namespace gig {
346              std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;              std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;
347              return;              return;
348          }          }
349          newstream->Launch(Command.hStream, Command.pStreamRef, Command.pSample, Command.SampleOffset, Command.DoLoop);          newstream->Launch(Command.hStream, Command.pStreamRef, Command.pDimRgn, Command.SampleOffset, Command.DoLoop);
350          dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));          dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));
351          if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {          if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {
352              std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;              std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;
# Line 295  namespace LinuxSampler { namespace gig { Line 365  namespace LinuxSampler { namespace gig {
365              if (pStream && pStream != SLOT_RESERVED) {              if (pStream && pStream != SLOT_RESERVED) {
366                  pStream->Kill();                  pStream->Kill();
367                  pCreatedStreams[Command.OrderID] = NULL; // free slot for new order                  pCreatedStreams[Command.OrderID] = NULL; // free slot for new order
368                    // if original sender requested a notification, let him know now
369                    if (Command.bNotify)
370                        DeletionNotificationQueue.push(&Command.hStream);
371                  return;                  return;
372              }              }
373    
374              // the stream was not created yet              // the stream was not created yet
375              if (GhostQueue->write_space() > 0) {              if (GhostQueue->write_space() > 0) {
376                  GhostQueue->push(&Command.hStream);                  GhostQueue->push(&Command);
377                } else { // error, queue full
378                    if (Command.bNotify) {
379                        dmsg(1,("DiskThread: GhostQueue full! (might lead to dead lock with instrument editor!)\n"));
380                    } else {
381                        dmsg(1,("DiskThread: GhostQueue full!\n"));
382                    }
383              }              }
             else dmsg(1,("DiskThread: GhostQueue full!\n"));  
384          }          }
385      }      }
386    

Legend:
Removed from v.835  
changed lines
  Added in v.1644

  ViewVC Help
Powered by ViewVC