/[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 1321 by schoenebeck, Tue Sep 4 01:12:49 2007 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 - 2007 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            DeleteDimregQueue->init();
63          ActiveStreamCount = 0;          ActiveStreamCount = 0;
64          ActiveStreamCountMax = 0;          ActiveStreamCountMax = 0;
65          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 105  namespace LinuxSampler { namespace gig {
105       * 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
106       * called by audio thread within the voice class).       * called by audio thread within the voice class).
107       */       */
108      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) {
109          dmsg(4,("Disk Thread: new stream ordered\n"));          dmsg(4,("Disk Thread: new stream ordered\n"));
110          if (CreationQueue->write_space() < 1) {          if (CreationQueue->write_space() < 1) {
111              dmsg(1,("DiskThread: Order queue full!\n"));              dmsg(1,("DiskThread: Order queue full!\n"));
# Line 125  namespace LinuxSampler { namespace gig { Line 127  namespace LinuxSampler { namespace gig {
127          cmd.OrderID      = pStreamRef->OrderID;          cmd.OrderID      = pStreamRef->OrderID;
128          cmd.hStream      = pStreamRef->hStream;          cmd.hStream      = pStreamRef->hStream;
129          cmd.pStreamRef   = pStreamRef;          cmd.pStreamRef   = pStreamRef;
130          cmd.pSample      = pSample;          cmd.pDimRgn      = pDimRgn;
131          cmd.SampleOffset = SampleOffset;          cmd.SampleOffset = SampleOffset;
132          cmd.DoLoop       = DoLoop;          cmd.DoLoop       = DoLoop;
133    
# Line 134  namespace LinuxSampler { namespace gig { Line 136  namespace LinuxSampler { namespace gig {
136      }      }
137    
138      /**      /**
139       * 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
140       * thread within the voice class).       * will return immediately, thus it won't block until the respective voice
141         * was actually deleted. (Called by audio thread within the Voice class).
142         *
143         * @param pStreamRef           - stream that shall be deleted
144         * @param bRequestNotification - set to true in case you want to receive a
145         *                               notification once the stream has actually
146         *                               been deleted
147         * @returns 0 on success, -1 if command queue is full
148         * @see AskForDeletedStream()
149       */       */
150      int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef) {      int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef, bool bRequestNotification) {
151          dmsg(4,("Disk Thread: stream deletion ordered\n"));          dmsg(4,("Disk Thread: stream deletion ordered\n"));
152          if (DeletionQueue->write_space() < 1) {          if (DeletionQueue->write_space() < 1) {
153              dmsg(1,("DiskThread: Deletion queue full!\n"));              dmsg(1,("DiskThread: Deletion queue full!\n"));
# Line 148  namespace LinuxSampler { namespace gig { Line 158  namespace LinuxSampler { namespace gig {
158          cmd.pStream = pStreamRef->pStream;          cmd.pStream = pStreamRef->pStream;
159          cmd.hStream = pStreamRef->hStream;          cmd.hStream = pStreamRef->hStream;
160          cmd.OrderID = pStreamRef->OrderID;          cmd.OrderID = pStreamRef->OrderID;
161            cmd.bNotify = bRequestNotification;
162    
163          DeletionQueue->push(&cmd);          DeletionQueue->push(&cmd);
164          return 0;          return 0;
165      }      }
166    
167      /**      /**
168         * Tell the disk thread to release a dimension region that belongs
169         * to an instrument which isn't loaded anymore. The disk thread
170         * will hand back the dimension region to the instrument resource
171         * manager. (OrderDeletionOfDimreg is called from the audio thread
172         * when a voice dies.)
173         */
174        int DiskThread::OrderDeletionOfDimreg(::gig::DimensionRegion* dimreg) {
175            dmsg(4,("Disk Thread: dimreg deletion ordered\n"));
176            if (DeleteDimregQueue->write_space() < 1) {
177                dmsg(1,("DiskThread: DeleteDimreg queue full!\n"));
178                return -1;
179            }
180            DeleteDimregQueue->push(&dimreg);
181            return 0;
182        }
183    
184        /**
185       * Returns the pointer to a disk stream if the ordered disk stream       * Returns the pointer to a disk stream if the ordered disk stream
186       * represented by the \a StreamOrderID was already activated by the disk       * represented by the \a StreamOrderID was already activated by the disk
187       * 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 206  namespace LinuxSampler { namespace gig {
206          return NULL;          return NULL;
207      }      }
208    
209        /**
210         * In case the original sender requested a notification with his stream
211         * deletion order, he can use this method to poll if the respective stream
212         * has actually been deleted.
213         *
214         * @returns handle / identifier of the deleted stream, or
215         *          Stream::INVALID_HANDLE if no notification present
216         */
217        Stream::Handle DiskThread::AskForDeletedStream() {
218            if (DeletionNotificationQueue.read_space()) {
219                Stream::Handle hStream;
220                DeletionNotificationQueue.pop(&hStream);
221                return hStream;
222            } else return Stream::INVALID_HANDLE; // no notification received yet
223        }
224    
225    
226    
227      // #########################################################################      // #########################################################################
# Line 185  namespace LinuxSampler { namespace gig { Line 229  namespace LinuxSampler { namespace gig {
229      // #         (following code should only be executed by the disk thread)      // #         (following code should only be executed by the disk thread)
230    
231    
232      DiskThread::DiskThread(uint BufferWrapElements) : Thread(true, false, 1, -2) {      DiskThread::DiskThread(uint BufferWrapElements, InstrumentResourceManager* pInstruments) :
233            Thread(true, false, 1, -2),
234            pInstruments(pInstruments),
235            DeletionNotificationQueue(4*CONFIG_MAX_STREAMS)
236        {
237          DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE);          DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE);
238          CreationQueue       = new RingBuffer<create_command_t>(1024);          CreationQueue       = new RingBuffer<create_command_t,false>(4*CONFIG_MAX_STREAMS);
239          DeletionQueue       = new RingBuffer<delete_command_t>(1024);          DeletionQueue       = new RingBuffer<delete_command_t,false>(4*CONFIG_MAX_STREAMS);
240          GhostQueue          = new RingBuffer<Stream::Handle>(CONFIG_MAX_STREAMS);          GhostQueue          = new RingBuffer<delete_command_t,false>(CONFIG_MAX_STREAMS);
241            DeleteDimregQueue   = new RingBuffer< ::gig::DimensionRegion*,false>(4*CONFIG_MAX_STREAMS);
242          Streams             = CONFIG_MAX_STREAMS;          Streams             = CONFIG_MAX_STREAMS;
243          RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN;          RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN;
244          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 257  namespace LinuxSampler { namespace gig {
257          if (CreationQueue) delete CreationQueue;          if (CreationQueue) delete CreationQueue;
258          if (DeletionQueue) delete DeletionQueue;          if (DeletionQueue) delete DeletionQueue;
259          if (GhostQueue)    delete GhostQueue;          if (GhostQueue)    delete GhostQueue;
260            if (DeleteDimregQueue) delete DeleteDimregQueue;
261          ::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer);          ::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer);
262      }      }
263    
# Line 219  namespace LinuxSampler { namespace gig { Line 269  namespace LinuxSampler { namespace gig {
269    
270              // if there are ghost streams, delete them              // if there are ghost streams, delete them
271              for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient              for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient
272                  Stream::Handle hGhostStream;                  delete_command_t ghostStream;
273                  GhostQueue->pop(&hGhostStream);                  GhostQueue->pop(&ghostStream);
274                  bool found = false;                  bool found = false;
275                  for (int i = 0; i < this->Streams; i++) {                  for (int i = 0; i < this->Streams; i++) {
276                      if (pStreams[i]->GetHandle() == hGhostStream) {                      if (pStreams[i]->GetHandle() == ghostStream.hStream) {
277                          pStreams[i]->Kill();                          pStreams[i]->Kill();
278                          found = true;                          found = true;
279                            // if original sender requested a notification, let him know now
280                            if (ghostStream.bNotify)
281                                DeletionNotificationQueue.push(&ghostStream.hStream);
282                          break;                          break;
283                      }                      }
284                  }                  }
285                  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
286              }              }
287    
288              // if there are creation commands, create new streams              // if there are creation commands, create new streams
# Line 246  namespace LinuxSampler { namespace gig { Line 299  namespace LinuxSampler { namespace gig {
299                  DeleteStream(command);                  DeleteStream(command);
300              }              }
301    
302                // release DimensionRegions that belong to instruments
303                // that are no longer loaded
304                while (DeleteDimregQueue->read_space() > 0) {
305                    ::gig::DimensionRegion* dimreg;
306                    DeleteDimregQueue->pop(&dimreg);
307                    pInstruments->HandBackDimReg(dimreg);
308                }
309    
310              RefillStreams(); // refill the most empty streams              RefillStreams(); // refill the most empty streams
311    
312              // if nothing was done during this iteration (eg no streambuffer              // if nothing was done during this iteration (eg no streambuffer
313              // filled with data) then sleep for 50ms              // filled with data) then sleep for 30ms
314              if (IsIdle) usleep(30000);              if (IsIdle) usleep(30000);
315    
316              int streamsInUsage = 0;              int streamsInUsage = 0;
# Line 276  namespace LinuxSampler { namespace gig { Line 337  namespace LinuxSampler { namespace gig {
337              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;
338              return;              return;
339          }          }
340          newstream->Launch(Command.hStream, Command.pStreamRef, Command.pSample, Command.SampleOffset, Command.DoLoop);          newstream->Launch(Command.hStream, Command.pStreamRef, Command.pDimRgn, Command.SampleOffset, Command.DoLoop);
341          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));
342          if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {          if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {
343              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 356  namespace LinuxSampler { namespace gig {
356              if (pStream && pStream != SLOT_RESERVED) {              if (pStream && pStream != SLOT_RESERVED) {
357                  pStream->Kill();                  pStream->Kill();
358                  pCreatedStreams[Command.OrderID] = NULL; // free slot for new order                  pCreatedStreams[Command.OrderID] = NULL; // free slot for new order
359                    // if original sender requested a notification, let him know now
360                    if (Command.bNotify)
361                        DeletionNotificationQueue.push(&Command.hStream);
362                  return;                  return;
363              }              }
364    
365              // the stream was not created yet              // the stream was not created yet
366              if (GhostQueue->write_space() > 0) {              if (GhostQueue->write_space() > 0) {
367                  GhostQueue->push(&Command.hStream);                  GhostQueue->push(&Command);
368                } else { // error, queue full
369                    if (Command.bNotify) {
370                        dmsg(1,("DiskThread: GhostQueue full! (might lead to dead lock with instrument editor!)\n"));
371                    } else {
372                        dmsg(1,("DiskThread: GhostQueue full!\n"));
373                    }
374              }              }
             else dmsg(1,("DiskThread: GhostQueue full!\n"));  
375          }          }
376      }      }
377    

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

  ViewVC Help
Powered by ViewVC