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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1649 - (hide annotations) (download)
Fri Jan 25 15:06:02 2008 UTC (16 years, 3 months ago) by nagata
File size: 19563 byte(s)
* added a new config option --enable-pthread-testcancel, which uses
pthread_testcancel() instead of asynchronous canceling (needed for OSX)

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 persson 1644 * Copyright (C) 2005 - 2008 Christian Schoenebeck *
7 schoenebeck 53 * *
8     * 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 *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #include <sstream>
25    
26     #include "DiskThread.h"
27    
28     namespace LinuxSampler { namespace gig {
29    
30     // *********** DiskThread **************
31     // *
32    
33    
34     // just a placeholder to mark a cell in the pickup array as 'reserved'
35     Stream* DiskThread::SLOT_RESERVED = (Stream*) &SLOT_RESERVED;
36    
37    
38     // #########################################################################
39     // # Foreign Thread Section
40     // # (following code intended to be interface for audio thread)
41    
42    
43     /**
44     * Suspend disk thread, kill all active streams, clear all queues and the
45     * pickup array and reset all streams. Call this method to bring everything
46     * in the disk thread to day one. If the disk thread was running, it will be
47     * respawned right after everything was reset.
48     */
49     void DiskThread::Reset() {
50     bool running = this->IsRunning();
51     if (running) this->StopThread();
52 schoenebeck 554 for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
53 schoenebeck 53 pStreams[i]->Kill();
54     }
55 schoenebeck 554 for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) {
56 schoenebeck 53 pCreatedStreams[i] = NULL;
57     }
58     GhostQueue->init();
59     CreationQueue->init();
60     DeletionQueue->init();
61 schoenebeck 1321 DeletionNotificationQueue.init();
62 persson 1644
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 persson 1038 DeleteDimregQueue->init();
70 schoenebeck 53 ActiveStreamCount = 0;
71     ActiveStreamCountMax = 0;
72     if (running) this->StartThread(); // start thread only if it was running before
73     }
74    
75     String DiskThread::GetBufferFillBytes() {
76     bool activestreams = false;
77     std::stringstream ss;
78     for (uint i = 0; i < this->Streams; i++) {
79     if (pStreams[i]->GetState() == Stream::state_unused) continue;
80     uint bufferfill = pStreams[i]->GetReadSpace() * sizeof(sample_t);
81     uint streamid = (uint) pStreams[i]->GetHandle();
82     if (!streamid) continue;
83    
84     if (activestreams) ss << ",[" << streamid << ']' << bufferfill;
85     else {
86     ss << '[' << streamid << ']' << bufferfill;
87     activestreams = true;
88     }
89     }
90     return ss.str();
91     }
92    
93     String DiskThread::GetBufferFillPercentage() {
94     bool activestreams = false;
95     std::stringstream ss;
96     for (uint i = 0; i < this->Streams; i++) {
97     if (pStreams[i]->GetState() == Stream::state_unused) continue;
98 schoenebeck 554 uint bufferfill = (uint) ((float) pStreams[i]->GetReadSpace() / (float) CONFIG_STREAM_BUFFER_SIZE * 100);
99 schoenebeck 53 uint streamid = (uint) pStreams[i]->GetHandle();
100     if (!streamid) continue;
101    
102     if (activestreams) ss << ",[" << streamid << ']' << bufferfill << '%';
103     else {
104     ss << '[' << streamid << ']' << bufferfill;
105     activestreams = true;
106     }
107     }
108     return ss.str();
109     }
110    
111     /**
112     * Returns -1 if command queue or pickup pool is full, 0 on success (will be
113     * called by audio thread within the voice class).
114     */
115 persson 865 int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop) {
116 schoenebeck 53 dmsg(4,("Disk Thread: new stream ordered\n"));
117     if (CreationQueue->write_space() < 1) {
118     dmsg(1,("DiskThread: Order queue full!\n"));
119     return -1;
120     }
121    
122 senkov 329 const Stream::OrderID_t newOrder = CreateOrderID();
123     if (!newOrder) {
124     dmsg(1,("DiskThread: there was no free slot\n"));
125     return -1; // there was no free slot
126     }
127    
128 schoenebeck 53 pStreamRef->State = Stream::state_active;
129 senkov 329 pStreamRef->OrderID = newOrder;
130 schoenebeck 53 pStreamRef->hStream = CreateHandle();
131     pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first
132    
133     create_command_t cmd;
134     cmd.OrderID = pStreamRef->OrderID;
135     cmd.hStream = pStreamRef->hStream;
136     cmd.pStreamRef = pStreamRef;
137 persson 865 cmd.pDimRgn = pDimRgn;
138 schoenebeck 53 cmd.SampleOffset = SampleOffset;
139     cmd.DoLoop = DoLoop;
140    
141     CreationQueue->push(&cmd);
142     return 0;
143     }
144    
145     /**
146 schoenebeck 1321 * Request the disk thread to delete the given disk stream. This method
147     * 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 schoenebeck 53 */
157 schoenebeck 1321 int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef, bool bRequestNotification) {
158 schoenebeck 53 dmsg(4,("Disk Thread: stream deletion ordered\n"));
159     if (DeletionQueue->write_space() < 1) {
160     dmsg(1,("DiskThread: Deletion queue full!\n"));
161     return -1;
162     }
163    
164     delete_command_t cmd;
165     cmd.pStream = pStreamRef->pStream;
166     cmd.hStream = pStreamRef->hStream;
167     cmd.OrderID = pStreamRef->OrderID;
168 schoenebeck 1321 cmd.bNotify = bRequestNotification;
169 schoenebeck 53
170     DeletionQueue->push(&cmd);
171     return 0;
172     }
173    
174     /**
175 schoenebeck 1321 * Tell the disk thread to release a dimension region that belongs
176 persson 1038 * 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 schoenebeck 53 * Returns the pointer to a disk stream if the ordered disk stream
193     * represented by the \a StreamOrderID was already activated by the disk
194     * thread, returns NULL otherwise. If the call was successful, thus if it
195     * returned a valid stream pointer, the caller has to the store that pointer
196     * by himself, because it's not possible to call this method again with the
197     * same used order ID; this method is just intended for picking up an ordered
198     * disk stream. This method will usually be called by the voice class (within
199     * the audio thread).
200     *
201     * @param StreamOrderID - ID previously returned by OrderNewStream()
202     * @returns pointer to created stream object, NULL otherwise
203     */
204     Stream* DiskThread::AskForCreatedStream(Stream::OrderID_t StreamOrderID) {
205     dmsg(4,("Disk Thread: been asked if stream already created, OrderID=%x ", StreamOrderID));
206     Stream* pStream = pCreatedStreams[StreamOrderID];
207     if (pStream && pStream != SLOT_RESERVED) {
208     dmsg(4,("(yes created)\n"));
209     pCreatedStreams[StreamOrderID] = NULL; // free the slot for a new order
210     return pStream;
211     }
212     dmsg(4,("(no not yet created)\n"));
213     return NULL;
214     }
215    
216 schoenebeck 1321 /**
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 schoenebeck 53
232    
233 schoenebeck 1321
234 schoenebeck 53 // #########################################################################
235     // # Disk Thread Only Section
236     // # (following code should only be executed by the disk thread)
237    
238    
239 persson 1038 DiskThread::DiskThread(uint BufferWrapElements, InstrumentResourceManager* pInstruments) :
240     Thread(true, false, 1, -2),
241 schoenebeck 1321 pInstruments(pInstruments),
242     DeletionNotificationQueue(4*CONFIG_MAX_STREAMS)
243     {
244 schoenebeck 554 DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE);
245 schoenebeck 1321 CreationQueue = new RingBuffer<create_command_t,false>(4*CONFIG_MAX_STREAMS);
246     DeletionQueue = new RingBuffer<delete_command_t,false>(4*CONFIG_MAX_STREAMS);
247     GhostQueue = new RingBuffer<delete_command_t,false>(CONFIG_MAX_STREAMS);
248     DeleteDimregQueue = new RingBuffer< ::gig::DimensionRegion*,false>(4*CONFIG_MAX_STREAMS);
249 schoenebeck 554 Streams = CONFIG_MAX_STREAMS;
250     RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN;
251     for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
252     pStreams[i] = new Stream(&DecompressionBuffer, CONFIG_STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words
253 schoenebeck 53 }
254 schoenebeck 554 for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) {
255 schoenebeck 53 pCreatedStreams[i] = NULL;
256     }
257 persson 835 ActiveStreamCountMax = 0;
258 schoenebeck 53 }
259    
260     DiskThread::~DiskThread() {
261 schoenebeck 554 for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
262 schoenebeck 53 if (pStreams[i]) delete pStreams[i];
263     }
264     if (CreationQueue) delete CreationQueue;
265     if (DeletionQueue) delete DeletionQueue;
266     if (GhostQueue) delete GhostQueue;
267 persson 1038 if (DeleteDimregQueue) delete DeleteDimregQueue;
268 schoenebeck 385 ::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer);
269 schoenebeck 53 }
270    
271     int DiskThread::Main() {
272     dmsg(3,("Disk thread running\n"));
273     while (true) {
274 senoner 1481 #if !defined(WIN32)
275 schoenebeck 361 pthread_testcancel(); // mandatory for OSX
276 senoner 1481 #endif
277 nagata 1649 #if CONFIG_PTHREAD_TESTCANCEL
278     TestCancel();
279     #endif
280 schoenebeck 53 IsIdle = true; // will be set to false if a stream got filled
281    
282     // if there are ghost streams, delete them
283     for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient
284 schoenebeck 1321 delete_command_t ghostStream;
285     GhostQueue->pop(&ghostStream);
286 schoenebeck 53 bool found = false;
287     for (int i = 0; i < this->Streams; i++) {
288 schoenebeck 1321 if (pStreams[i]->GetHandle() == ghostStream.hStream) {
289 schoenebeck 53 pStreams[i]->Kill();
290     found = true;
291 schoenebeck 1321 // if original sender requested a notification, let him know now
292     if (ghostStream.bNotify)
293     DeletionNotificationQueue.push(&ghostStream.hStream);
294 schoenebeck 53 break;
295     }
296     }
297 schoenebeck 1321 if (!found) GhostQueue->push(&ghostStream); // put ghost stream handle back to the queue
298 schoenebeck 53 }
299    
300     // if there are creation commands, create new streams
301     while (Stream::UnusedStreams > 0 && CreationQueue->read_space() > 0) {
302     create_command_t command;
303     CreationQueue->pop(&command);
304     CreateStream(command);
305     }
306    
307     // if there are deletion commands, delete those streams
308 senkov 333 while (Stream::UnusedStreams < Stream::TotalStreams && DeletionQueue->read_space() > 0) {
309 schoenebeck 53 delete_command_t command;
310     DeletionQueue->pop(&command);
311     DeleteStream(command);
312     }
313    
314 persson 1038 // release DimensionRegions that belong to instruments
315     // that are no longer loaded
316     while (DeleteDimregQueue->read_space() > 0) {
317     ::gig::DimensionRegion* dimreg;
318     DeleteDimregQueue->pop(&dimreg);
319     pInstruments->HandBackDimReg(dimreg);
320     }
321    
322 schoenebeck 53 RefillStreams(); // refill the most empty streams
323    
324     // if nothing was done during this iteration (eg no streambuffer
325 persson 840 // filled with data) then sleep for 30ms
326 schoenebeck 53 if (IsIdle) usleep(30000);
327    
328     int streamsInUsage = 0;
329     for (int i = Streams - 1; i >= 0; i--) {
330     if (pStreams[i]->GetState() != Stream::state_unused) streamsInUsage++;
331     }
332     ActiveStreamCount = streamsInUsage;
333     if (streamsInUsage > ActiveStreamCountMax) ActiveStreamCountMax = streamsInUsage;
334     }
335    
336     return EXIT_FAILURE;
337     }
338    
339     void DiskThread::CreateStream(create_command_t& Command) {
340     // search for unused stream
341     Stream* newstream = NULL;
342     for (int i = Streams - 1; i >= 0; i--) {
343     if (pStreams[i]->GetState() == Stream::state_unused) {
344     newstream = pStreams[i];
345     break;
346     }
347     }
348     if (!newstream) {
349     std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;
350     return;
351     }
352 persson 865 newstream->Launch(Command.hStream, Command.pStreamRef, Command.pDimRgn, Command.SampleOffset, Command.DoLoop);
353 schoenebeck 53 dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));
354     if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {
355     std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;
356     newstream->Kill();
357     return;
358     }
359     pCreatedStreams[Command.OrderID] = newstream;
360     }
361    
362     void DiskThread::DeleteStream(delete_command_t& Command) {
363     if (Command.pStream) Command.pStream->Kill();
364     else { // the stream wasn't created by disk thread or picked up by audio thread yet
365    
366     // if stream was created but not picked up yet
367     Stream* pStream = pCreatedStreams[Command.OrderID];
368     if (pStream && pStream != SLOT_RESERVED) {
369     pStream->Kill();
370     pCreatedStreams[Command.OrderID] = NULL; // free slot for new order
371 schoenebeck 1321 // if original sender requested a notification, let him know now
372     if (Command.bNotify)
373     DeletionNotificationQueue.push(&Command.hStream);
374 schoenebeck 53 return;
375     }
376    
377     // the stream was not created yet
378     if (GhostQueue->write_space() > 0) {
379 schoenebeck 1321 GhostQueue->push(&Command);
380     } else { // error, queue full
381     if (Command.bNotify) {
382     dmsg(1,("DiskThread: GhostQueue full! (might lead to dead lock with instrument editor!)\n"));
383     } else {
384     dmsg(1,("DiskThread: GhostQueue full!\n"));
385     }
386 schoenebeck 53 }
387     }
388     }
389    
390     void DiskThread::RefillStreams() {
391     // sort the streams by most empty stream
392     qsort(pStreams, Streams, sizeof(Stream*), CompareStreamWriteSpace);
393    
394     // refill the most empty streams
395     for (uint i = 0; i < RefillStreamsPerRun; i++) {
396     if (pStreams[i]->GetState() == Stream::state_active) {
397    
398     //float filledpercentage = (float) pStreams[i]->GetReadSpace() / 131072.0 * 100.0;
399     //dmsg(("\nbuffer fill: %.1f%\n", filledpercentage));
400    
401     int writespace = pStreams[i]->GetWriteSpaceToEnd();
402     if (writespace == 0) break;
403    
404     int capped_writespace = writespace;
405     // if there is too much buffer space available then cut the read/write
406 schoenebeck 554 // size to CONFIG_STREAM_MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes
407     if (writespace > CONFIG_STREAM_MAX_REFILL_SIZE) capped_writespace = CONFIG_STREAM_MAX_REFILL_SIZE;
408 schoenebeck 53
409     // adjust the amount to read in order to ensure that the buffer wraps correctly
410     int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace);
411     // if we wasn't able to refill one of the stream buffers by more than
412 schoenebeck 554 // CONFIG_STREAM_MIN_REFILL_SIZE we'll send the disk thread to sleep later
413     if (pStreams[i]->ReadAhead(read_amount) > CONFIG_STREAM_MIN_REFILL_SIZE) this->IsIdle = false;
414 schoenebeck 53 }
415     }
416     }
417    
418     /// Handle Generator
419     Stream::Handle DiskThread::CreateHandle() {
420     static uint32_t counter = 0;
421     if (counter == 0xffffffff) counter = 1; // we use '0' as 'invalid handle' only, so we skip 0
422     else counter++;
423     return counter;
424     }
425    
426     /// order ID Generator
427     Stream::OrderID_t DiskThread::CreateOrderID() {
428 senkov 329 static Stream::OrderID_t counter(0);
429 schoenebeck 554 for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
430     if (counter == CONFIG_MAX_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0
431 schoenebeck 53 else counter++;
432     if (!pCreatedStreams[counter]) {
433     pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved
434     return counter; // found empty slot
435     }
436     }
437     return 0; // no free slot
438     }
439    
440    
441    
442     // *********** C functions **************
443     // *
444    
445     /**
446     * This is the comparison function the qsort algo uses to determine if a value is
447     * bigger than another one or special in our case; if the writespace of a stream
448     * is bigger than another one.
449     */
450     int CompareStreamWriteSpace(const void* A, const void* B) {
451     Stream* a = *(Stream**) A;
452     Stream* b = *(Stream**) B;
453     return b->GetWriteSpace() - a->GetWriteSpace();
454     }
455    
456     }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC