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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1038 - (show annotations) (download)
Sat Feb 3 15:33:00 2007 UTC (17 years, 2 months ago) by persson
File size: 17180 byte(s)
* playback is no longer disabled during instrument loading
* all notes playing on a channel that changes its instrument keep
  playing with the old instrument until they get a note off command
* new thread safety fix for lscp "load engine" and "set channel audio
  output device"

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005, 2006 Christian Schoenebeck *
7 * *
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 for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
53 pStreams[i]->Kill();
54 }
55 for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) {
56 pCreatedStreams[i] = NULL;
57 }
58 GhostQueue->init();
59 CreationQueue->init();
60 DeletionQueue->init();
61 DeleteDimregQueue->init();
62 ActiveStreamCount = 0;
63 ActiveStreamCountMax = 0;
64 if (running) this->StartThread(); // start thread only if it was running before
65 }
66
67 String DiskThread::GetBufferFillBytes() {
68 bool activestreams = false;
69 std::stringstream ss;
70 for (uint i = 0; i < this->Streams; i++) {
71 if (pStreams[i]->GetState() == Stream::state_unused) continue;
72 uint bufferfill = pStreams[i]->GetReadSpace() * sizeof(sample_t);
73 uint streamid = (uint) pStreams[i]->GetHandle();
74 if (!streamid) continue;
75
76 if (activestreams) ss << ",[" << streamid << ']' << bufferfill;
77 else {
78 ss << '[' << streamid << ']' << bufferfill;
79 activestreams = true;
80 }
81 }
82 return ss.str();
83 }
84
85 String DiskThread::GetBufferFillPercentage() {
86 bool activestreams = false;
87 std::stringstream ss;
88 for (uint i = 0; i < this->Streams; i++) {
89 if (pStreams[i]->GetState() == Stream::state_unused) continue;
90 uint bufferfill = (uint) ((float) pStreams[i]->GetReadSpace() / (float) CONFIG_STREAM_BUFFER_SIZE * 100);
91 uint streamid = (uint) pStreams[i]->GetHandle();
92 if (!streamid) continue;
93
94 if (activestreams) ss << ",[" << streamid << ']' << bufferfill << '%';
95 else {
96 ss << '[' << streamid << ']' << bufferfill;
97 activestreams = true;
98 }
99 }
100 return ss.str();
101 }
102
103 /**
104 * Returns -1 if command queue or pickup pool is full, 0 on success (will be
105 * called by audio thread within the voice class).
106 */
107 int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop) {
108 dmsg(4,("Disk Thread: new stream ordered\n"));
109 if (CreationQueue->write_space() < 1) {
110 dmsg(1,("DiskThread: Order queue full!\n"));
111 return -1;
112 }
113
114 const Stream::OrderID_t newOrder = CreateOrderID();
115 if (!newOrder) {
116 dmsg(1,("DiskThread: there was no free slot\n"));
117 return -1; // there was no free slot
118 }
119
120 pStreamRef->State = Stream::state_active;
121 pStreamRef->OrderID = newOrder;
122 pStreamRef->hStream = CreateHandle();
123 pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first
124
125 create_command_t cmd;
126 cmd.OrderID = pStreamRef->OrderID;
127 cmd.hStream = pStreamRef->hStream;
128 cmd.pStreamRef = pStreamRef;
129 cmd.pDimRgn = pDimRgn;
130 cmd.SampleOffset = SampleOffset;
131 cmd.DoLoop = DoLoop;
132
133 CreationQueue->push(&cmd);
134 return 0;
135 }
136
137 /**
138 * Returns -1 if command queue is full, 0 on success (will be called by audio
139 * thread within the voice class).
140 */
141 int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef) {
142 dmsg(4,("Disk Thread: stream deletion ordered\n"));
143 if (DeletionQueue->write_space() < 1) {
144 dmsg(1,("DiskThread: Deletion queue full!\n"));
145 return -1;
146 }
147
148 delete_command_t cmd;
149 cmd.pStream = pStreamRef->pStream;
150 cmd.hStream = pStreamRef->hStream;
151 cmd.OrderID = pStreamRef->OrderID;
152
153 DeletionQueue->push(&cmd);
154 return 0;
155 }
156
157 /**
158 * Tell the disk thread to release a dimension region that belong
159 * to an instrument which isn't loaded anymore. The disk thread
160 * will hand back the dimension region to the instrument resource
161 * manager. (OrderDeletionOfDimreg is called from the audio thread
162 * when a voice dies.)
163 */
164 int DiskThread::OrderDeletionOfDimreg(::gig::DimensionRegion* dimreg) {
165 dmsg(4,("Disk Thread: dimreg deletion ordered\n"));
166 if (DeleteDimregQueue->write_space() < 1) {
167 dmsg(1,("DiskThread: DeleteDimreg queue full!\n"));
168 return -1;
169 }
170 DeleteDimregQueue->push(&dimreg);
171 return 0;
172 }
173
174 /**
175 * Returns the pointer to a disk stream if the ordered disk stream
176 * represented by the \a StreamOrderID was already activated by the disk
177 * thread, returns NULL otherwise. If the call was successful, thus if it
178 * returned a valid stream pointer, the caller has to the store that pointer
179 * by himself, because it's not possible to call this method again with the
180 * same used order ID; this method is just intended for picking up an ordered
181 * disk stream. This method will usually be called by the voice class (within
182 * the audio thread).
183 *
184 * @param StreamOrderID - ID previously returned by OrderNewStream()
185 * @returns pointer to created stream object, NULL otherwise
186 */
187 Stream* DiskThread::AskForCreatedStream(Stream::OrderID_t StreamOrderID) {
188 dmsg(4,("Disk Thread: been asked if stream already created, OrderID=%x ", StreamOrderID));
189 Stream* pStream = pCreatedStreams[StreamOrderID];
190 if (pStream && pStream != SLOT_RESERVED) {
191 dmsg(4,("(yes created)\n"));
192 pCreatedStreams[StreamOrderID] = NULL; // free the slot for a new order
193 return pStream;
194 }
195 dmsg(4,("(no not yet created)\n"));
196 return NULL;
197 }
198
199
200
201 // #########################################################################
202 // # Disk Thread Only Section
203 // # (following code should only be executed by the disk thread)
204
205
206 DiskThread::DiskThread(uint BufferWrapElements, InstrumentResourceManager* pInstruments) :
207 Thread(true, false, 1, -2),
208 pInstruments(pInstruments) {
209 DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE);
210 CreationQueue = new RingBuffer<create_command_t,false>(1024);
211 DeletionQueue = new RingBuffer<delete_command_t,false>(1024);
212 GhostQueue = new RingBuffer<Stream::Handle,false>(CONFIG_MAX_STREAMS);
213 DeleteDimregQueue = new RingBuffer< ::gig::DimensionRegion*,false>(1024);
214 Streams = CONFIG_MAX_STREAMS;
215 RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN;
216 for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
217 pStreams[i] = new Stream(&DecompressionBuffer, CONFIG_STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words
218 }
219 for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) {
220 pCreatedStreams[i] = NULL;
221 }
222 ActiveStreamCountMax = 0;
223 }
224
225 DiskThread::~DiskThread() {
226 for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
227 if (pStreams[i]) delete pStreams[i];
228 }
229 if (CreationQueue) delete CreationQueue;
230 if (DeletionQueue) delete DeletionQueue;
231 if (GhostQueue) delete GhostQueue;
232 if (DeleteDimregQueue) delete DeleteDimregQueue;
233 ::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer);
234 }
235
236 int DiskThread::Main() {
237 dmsg(3,("Disk thread running\n"));
238 while (true) {
239 pthread_testcancel(); // mandatory for OSX
240 IsIdle = true; // will be set to false if a stream got filled
241
242 // if there are ghost streams, delete them
243 for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient
244 Stream::Handle hGhostStream;
245 GhostQueue->pop(&hGhostStream);
246 bool found = false;
247 for (int i = 0; i < this->Streams; i++) {
248 if (pStreams[i]->GetHandle() == hGhostStream) {
249 pStreams[i]->Kill();
250 found = true;
251 break;
252 }
253 }
254 if (!found) GhostQueue->push(&hGhostStream); // put ghost stream handle back to the queue
255 }
256
257 // if there are creation commands, create new streams
258 while (Stream::UnusedStreams > 0 && CreationQueue->read_space() > 0) {
259 create_command_t command;
260 CreationQueue->pop(&command);
261 CreateStream(command);
262 }
263
264 // if there are deletion commands, delete those streams
265 while (Stream::UnusedStreams < Stream::TotalStreams && DeletionQueue->read_space() > 0) {
266 delete_command_t command;
267 DeletionQueue->pop(&command);
268 DeleteStream(command);
269 }
270
271 // release DimensionRegions that belong to instruments
272 // that are no longer loaded
273 while (DeleteDimregQueue->read_space() > 0) {
274 ::gig::DimensionRegion* dimreg;
275 DeleteDimregQueue->pop(&dimreg);
276 pInstruments->HandBackDimReg(dimreg);
277 }
278
279 RefillStreams(); // refill the most empty streams
280
281 // if nothing was done during this iteration (eg no streambuffer
282 // filled with data) then sleep for 30ms
283 if (IsIdle) usleep(30000);
284
285 int streamsInUsage = 0;
286 for (int i = Streams - 1; i >= 0; i--) {
287 if (pStreams[i]->GetState() != Stream::state_unused) streamsInUsage++;
288 }
289 ActiveStreamCount = streamsInUsage;
290 if (streamsInUsage > ActiveStreamCountMax) ActiveStreamCountMax = streamsInUsage;
291 }
292
293 return EXIT_FAILURE;
294 }
295
296 void DiskThread::CreateStream(create_command_t& Command) {
297 // search for unused stream
298 Stream* newstream = NULL;
299 for (int i = Streams - 1; i >= 0; i--) {
300 if (pStreams[i]->GetState() == Stream::state_unused) {
301 newstream = pStreams[i];
302 break;
303 }
304 }
305 if (!newstream) {
306 std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;
307 return;
308 }
309 newstream->Launch(Command.hStream, Command.pStreamRef, Command.pDimRgn, Command.SampleOffset, Command.DoLoop);
310 dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));
311 if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {
312 std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;
313 newstream->Kill();
314 return;
315 }
316 pCreatedStreams[Command.OrderID] = newstream;
317 }
318
319 void DiskThread::DeleteStream(delete_command_t& Command) {
320 if (Command.pStream) Command.pStream->Kill();
321 else { // the stream wasn't created by disk thread or picked up by audio thread yet
322
323 // if stream was created but not picked up yet
324 Stream* pStream = pCreatedStreams[Command.OrderID];
325 if (pStream && pStream != SLOT_RESERVED) {
326 pStream->Kill();
327 pCreatedStreams[Command.OrderID] = NULL; // free slot for new order
328 return;
329 }
330
331 // the stream was not created yet
332 if (GhostQueue->write_space() > 0) {
333 GhostQueue->push(&Command.hStream);
334 }
335 else dmsg(1,("DiskThread: GhostQueue full!\n"));
336 }
337 }
338
339 void DiskThread::RefillStreams() {
340 // sort the streams by most empty stream
341 qsort(pStreams, Streams, sizeof(Stream*), CompareStreamWriteSpace);
342
343 // refill the most empty streams
344 for (uint i = 0; i < RefillStreamsPerRun; i++) {
345 if (pStreams[i]->GetState() == Stream::state_active) {
346
347 //float filledpercentage = (float) pStreams[i]->GetReadSpace() / 131072.0 * 100.0;
348 //dmsg(("\nbuffer fill: %.1f%\n", filledpercentage));
349
350 int writespace = pStreams[i]->GetWriteSpaceToEnd();
351 if (writespace == 0) break;
352
353 int capped_writespace = writespace;
354 // if there is too much buffer space available then cut the read/write
355 // size to CONFIG_STREAM_MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes
356 if (writespace > CONFIG_STREAM_MAX_REFILL_SIZE) capped_writespace = CONFIG_STREAM_MAX_REFILL_SIZE;
357
358 // adjust the amount to read in order to ensure that the buffer wraps correctly
359 int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace);
360 // if we wasn't able to refill one of the stream buffers by more than
361 // CONFIG_STREAM_MIN_REFILL_SIZE we'll send the disk thread to sleep later
362 if (pStreams[i]->ReadAhead(read_amount) > CONFIG_STREAM_MIN_REFILL_SIZE) this->IsIdle = false;
363 }
364 }
365 }
366
367 /// Handle Generator
368 Stream::Handle DiskThread::CreateHandle() {
369 static uint32_t counter = 0;
370 if (counter == 0xffffffff) counter = 1; // we use '0' as 'invalid handle' only, so we skip 0
371 else counter++;
372 return counter;
373 }
374
375 /// order ID Generator
376 Stream::OrderID_t DiskThread::CreateOrderID() {
377 static Stream::OrderID_t counter(0);
378 for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
379 if (counter == CONFIG_MAX_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0
380 else counter++;
381 if (!pCreatedStreams[counter]) {
382 pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved
383 return counter; // found empty slot
384 }
385 }
386 return 0; // no free slot
387 }
388
389
390
391 // *********** C functions **************
392 // *
393
394 /**
395 * This is the comparison function the qsort algo uses to determine if a value is
396 * bigger than another one or special in our case; if the writespace of a stream
397 * is bigger than another one.
398 */
399 int CompareStreamWriteSpace(const void* A, const void* B) {
400 Stream* a = *(Stream**) A;
401 Stream* b = *(Stream**) B;
402 return b->GetWriteSpace() - a->GetWriteSpace();
403 }
404
405 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC