/[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 361 - (show annotations) (download)
Wed Feb 9 01:22:18 2005 UTC (19 years, 2 months ago) by schoenebeck
File size: 15472 byte(s)
* bunch of fixes for OSX (patch by Stephane Letz)

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include <sstream>
24
25 #include "DiskThread.h"
26
27 namespace LinuxSampler { namespace gig {
28
29 // *********** DiskThread **************
30 // *
31
32
33 // just a placeholder to mark a cell in the pickup array as 'reserved'
34 Stream* DiskThread::SLOT_RESERVED = (Stream*) &SLOT_RESERVED;
35
36
37 // #########################################################################
38 // # Foreign Thread Section
39 // # (following code intended to be interface for audio thread)
40
41
42 /**
43 * Suspend disk thread, kill all active streams, clear all queues and the
44 * pickup array and reset all streams. Call this method to bring everything
45 * in the disk thread to day one. If the disk thread was running, it will be
46 * respawned right after everything was reset.
47 */
48 void DiskThread::Reset() {
49 bool running = this->IsRunning();
50 if (running) this->StopThread();
51 for (int i = 0; i < MAX_INPUT_STREAMS; i++) {
52 pStreams[i]->Kill();
53 }
54 for (int i = 1; i <= MAX_INPUT_STREAMS; i++) {
55 pCreatedStreams[i] = NULL;
56 }
57 GhostQueue->init();
58 CreationQueue->init();
59 DeletionQueue->init();
60 ActiveStreamCount = 0;
61 ActiveStreamCountMax = 0;
62 if (running) this->StartThread(); // start thread only if it was running before
63 }
64
65 String DiskThread::GetBufferFillBytes() {
66 bool activestreams = false;
67 std::stringstream ss;
68 for (uint i = 0; i < this->Streams; i++) {
69 if (pStreams[i]->GetState() == Stream::state_unused) continue;
70 uint bufferfill = pStreams[i]->GetReadSpace() * sizeof(sample_t);
71 uint streamid = (uint) pStreams[i]->GetHandle();
72 if (!streamid) continue;
73
74 if (activestreams) ss << ",[" << streamid << ']' << bufferfill;
75 else {
76 ss << '[' << streamid << ']' << bufferfill;
77 activestreams = true;
78 }
79 }
80 return ss.str();
81 }
82
83 String DiskThread::GetBufferFillPercentage() {
84 bool activestreams = false;
85 std::stringstream ss;
86 for (uint i = 0; i < this->Streams; i++) {
87 if (pStreams[i]->GetState() == Stream::state_unused) continue;
88 uint bufferfill = (uint) ((float) pStreams[i]->GetReadSpace() / (float) STREAM_BUFFER_SIZE * 100);
89 uint streamid = (uint) pStreams[i]->GetHandle();
90 if (!streamid) continue;
91
92 if (activestreams) ss << ",[" << streamid << ']' << bufferfill << '%';
93 else {
94 ss << '[' << streamid << ']' << bufferfill;
95 activestreams = true;
96 }
97 }
98 return ss.str();
99 }
100
101 /**
102 * Returns -1 if command queue or pickup pool is full, 0 on success (will be
103 * called by audio thread within the voice class).
104 */
105 int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::Sample* pSample, unsigned long SampleOffset, bool DoLoop) {
106 dmsg(4,("Disk Thread: new stream ordered\n"));
107 if (CreationQueue->write_space() < 1) {
108 dmsg(1,("DiskThread: Order queue full!\n"));
109 return -1;
110 }
111
112 const Stream::OrderID_t newOrder = CreateOrderID();
113 if (!newOrder) {
114 dmsg(1,("DiskThread: there was no free slot\n"));
115 return -1; // there was no free slot
116 }
117
118 pStreamRef->State = Stream::state_active;
119 pStreamRef->OrderID = newOrder;
120 pStreamRef->hStream = CreateHandle();
121 pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first
122
123 create_command_t cmd;
124 cmd.OrderID = pStreamRef->OrderID;
125 cmd.hStream = pStreamRef->hStream;
126 cmd.pStreamRef = pStreamRef;
127 cmd.pSample = pSample;
128 cmd.SampleOffset = SampleOffset;
129 cmd.DoLoop = DoLoop;
130
131 CreationQueue->push(&cmd);
132 return 0;
133 }
134
135 /**
136 * Returns -1 if command queue is full, 0 on success (will be called by audio
137 * thread within the voice class).
138 */
139 int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef) {
140 dmsg(4,("Disk Thread: stream deletion ordered\n"));
141 if (DeletionQueue->write_space() < 1) {
142 dmsg(1,("DiskThread: Deletion queue full!\n"));
143 return -1;
144 }
145
146 delete_command_t cmd;
147 cmd.pStream = pStreamRef->pStream;
148 cmd.hStream = pStreamRef->hStream;
149 cmd.OrderID = pStreamRef->OrderID;
150
151 DeletionQueue->push(&cmd);
152 return 0;
153 }
154
155 /**
156 * Returns the pointer to a disk stream if the ordered disk stream
157 * represented by the \a StreamOrderID was already activated by the disk
158 * thread, returns NULL otherwise. If the call was successful, thus if it
159 * returned a valid stream pointer, the caller has to the store that pointer
160 * by himself, because it's not possible to call this method again with the
161 * same used order ID; this method is just intended for picking up an ordered
162 * disk stream. This method will usually be called by the voice class (within
163 * the audio thread).
164 *
165 * @param StreamOrderID - ID previously returned by OrderNewStream()
166 * @returns pointer to created stream object, NULL otherwise
167 */
168 Stream* DiskThread::AskForCreatedStream(Stream::OrderID_t StreamOrderID) {
169 dmsg(4,("Disk Thread: been asked if stream already created, OrderID=%x ", StreamOrderID));
170 Stream* pStream = pCreatedStreams[StreamOrderID];
171 if (pStream && pStream != SLOT_RESERVED) {
172 dmsg(4,("(yes created)\n"));
173 pCreatedStreams[StreamOrderID] = NULL; // free the slot for a new order
174 return pStream;
175 }
176 dmsg(4,("(no not yet created)\n"));
177 return NULL;
178 }
179
180
181
182 // #########################################################################
183 // # Disk Thread Only Section
184 // # (following code should only be executed by the disk thread)
185
186
187 DiskThread::DiskThread(uint BufferWrapElements) : Thread(false, 1, -2) {
188 CreationQueue = new RingBuffer<create_command_t>(1024);
189 DeletionQueue = new RingBuffer<delete_command_t>(1024);
190 GhostQueue = new RingBuffer<Stream::Handle>(MAX_INPUT_STREAMS);
191 Streams = MAX_INPUT_STREAMS;
192 RefillStreamsPerRun = REFILL_STREAMS_PER_RUN;
193 for (int i = 0; i < MAX_INPUT_STREAMS; i++) {
194 pStreams[i] = new Stream(STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words
195 }
196 for (int i = 1; i <= MAX_INPUT_STREAMS; i++) {
197 pCreatedStreams[i] = NULL;
198 }
199 }
200
201 DiskThread::~DiskThread() {
202 for (int i = 0; i < MAX_INPUT_STREAMS; i++) {
203 if (pStreams[i]) delete pStreams[i];
204 }
205 if (CreationQueue) delete CreationQueue;
206 if (DeletionQueue) delete DeletionQueue;
207 if (GhostQueue) delete GhostQueue;
208 }
209
210 int DiskThread::Main() {
211 dmsg(3,("Disk thread running\n"));
212 while (true) {
213 pthread_testcancel(); // mandatory for OSX
214 IsIdle = true; // will be set to false if a stream got filled
215
216 // if there are ghost streams, delete them
217 for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient
218 Stream::Handle hGhostStream;
219 GhostQueue->pop(&hGhostStream);
220 bool found = false;
221 for (int i = 0; i < this->Streams; i++) {
222 if (pStreams[i]->GetHandle() == hGhostStream) {
223 pStreams[i]->Kill();
224 found = true;
225 break;
226 }
227 }
228 if (!found) GhostQueue->push(&hGhostStream); // put ghost stream handle back to the queue
229 }
230
231 // if there are creation commands, create new streams
232 while (Stream::UnusedStreams > 0 && CreationQueue->read_space() > 0) {
233 create_command_t command;
234 CreationQueue->pop(&command);
235 CreateStream(command);
236 }
237
238 // if there are deletion commands, delete those streams
239 while (Stream::UnusedStreams < Stream::TotalStreams && DeletionQueue->read_space() > 0) {
240 delete_command_t command;
241 DeletionQueue->pop(&command);
242 DeleteStream(command);
243 }
244
245 RefillStreams(); // refill the most empty streams
246
247 // if nothing was done during this iteration (eg no streambuffer
248 // filled with data) then sleep for 50ms
249 if (IsIdle) usleep(30000);
250
251 int streamsInUsage = 0;
252 for (int i = Streams - 1; i >= 0; i--) {
253 if (pStreams[i]->GetState() != Stream::state_unused) streamsInUsage++;
254 }
255 ActiveStreamCount = streamsInUsage;
256 if (streamsInUsage > ActiveStreamCountMax) ActiveStreamCountMax = streamsInUsage;
257 }
258
259 return EXIT_FAILURE;
260 }
261
262 void DiskThread::CreateStream(create_command_t& Command) {
263 // search for unused stream
264 Stream* newstream = NULL;
265 for (int i = Streams - 1; i >= 0; i--) {
266 if (pStreams[i]->GetState() == Stream::state_unused) {
267 newstream = pStreams[i];
268 break;
269 }
270 }
271 if (!newstream) {
272 std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;
273 return;
274 }
275 newstream->Launch(Command.hStream, Command.pStreamRef, Command.pSample, Command.SampleOffset, Command.DoLoop);
276 dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));
277 if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {
278 std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;
279 newstream->Kill();
280 return;
281 }
282 pCreatedStreams[Command.OrderID] = newstream;
283 }
284
285 void DiskThread::DeleteStream(delete_command_t& Command) {
286 if (Command.pStream) Command.pStream->Kill();
287 else { // the stream wasn't created by disk thread or picked up by audio thread yet
288
289 // if stream was created but not picked up yet
290 Stream* pStream = pCreatedStreams[Command.OrderID];
291 if (pStream && pStream != SLOT_RESERVED) {
292 pStream->Kill();
293 pCreatedStreams[Command.OrderID] = NULL; // free slot for new order
294 return;
295 }
296
297 // the stream was not created yet
298 if (GhostQueue->write_space() > 0) {
299 GhostQueue->push(&Command.hStream);
300 }
301 else dmsg(1,("DiskThread: GhostQueue full!\n"));
302 }
303 }
304
305 void DiskThread::RefillStreams() {
306 // sort the streams by most empty stream
307 qsort(pStreams, Streams, sizeof(Stream*), CompareStreamWriteSpace);
308
309 // refill the most empty streams
310 for (uint i = 0; i < RefillStreamsPerRun; i++) {
311 if (pStreams[i]->GetState() == Stream::state_active) {
312
313 //float filledpercentage = (float) pStreams[i]->GetReadSpace() / 131072.0 * 100.0;
314 //dmsg(("\nbuffer fill: %.1f%\n", filledpercentage));
315
316 int writespace = pStreams[i]->GetWriteSpaceToEnd();
317 if (writespace == 0) break;
318
319 int capped_writespace = writespace;
320 // if there is too much buffer space available then cut the read/write
321 // size to MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes
322 if (writespace > MAX_REFILL_SIZE) capped_writespace = MAX_REFILL_SIZE;
323
324 // adjust the amount to read in order to ensure that the buffer wraps correctly
325 int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace);
326 // if we wasn't able to refill one of the stream buffers by more than
327 // MIN_REFILL_SIZE we'll send the disk thread to sleep later
328 if (pStreams[i]->ReadAhead(read_amount) > MIN_REFILL_SIZE) this->IsIdle = false;
329 }
330 }
331 }
332
333 /// Handle Generator
334 Stream::Handle DiskThread::CreateHandle() {
335 static uint32_t counter = 0;
336 if (counter == 0xffffffff) counter = 1; // we use '0' as 'invalid handle' only, so we skip 0
337 else counter++;
338 return counter;
339 }
340
341 /// order ID Generator
342 Stream::OrderID_t DiskThread::CreateOrderID() {
343 static Stream::OrderID_t counter(0);
344 for (int i = 0; i < MAX_INPUT_STREAMS; i++) {
345 if (counter == MAX_INPUT_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0
346 else counter++;
347 if (!pCreatedStreams[counter]) {
348 pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved
349 return counter; // found empty slot
350 }
351 }
352 return 0; // no free slot
353 }
354
355
356
357 // *********** C functions **************
358 // *
359
360 /**
361 * This is the comparison function the qsort algo uses to determine if a value is
362 * bigger than another one or special in our case; if the writespace of a stream
363 * is bigger than another one.
364 */
365 int CompareStreamWriteSpace(const void* A, const void* B) {
366 Stream* a = *(Stream**) A;
367 Stream* b = *(Stream**) B;
368 return b->GetWriteSpace() - a->GetWriteSpace();
369 }
370
371 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC