/[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 56 - (show annotations) (download)
Tue Apr 27 09:21:58 2004 UTC (19 years, 11 months ago) by schoenebeck
File size: 15309 byte(s)
updated copyright header for 2004

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 pStreamRef->State = Stream::state_active;
113 pStreamRef->OrderID = CreateOrderID();
114 pStreamRef->hStream = CreateHandle();
115 pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first
116
117 if (!pStreamRef->OrderID) return -1; // there was no free slot
118
119 create_command_t cmd;
120 cmd.OrderID = pStreamRef->OrderID;
121 cmd.hStream = pStreamRef->hStream;
122 cmd.pStreamRef = pStreamRef;
123 cmd.pSample = pSample;
124 cmd.SampleOffset = SampleOffset;
125 cmd.DoLoop = DoLoop;
126
127 CreationQueue->push(&cmd);
128 return 0;
129 }
130
131 /**
132 * Returns -1 if command queue is full, 0 on success (will be called by audio
133 * thread within the voice class).
134 */
135 int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef) {
136 dmsg(4,("Disk Thread: stream deletion ordered\n"));
137 if (DeletionQueue->write_space() < 1) {
138 dmsg(1,("DiskThread: Deletion queue full!\n"));
139 return -1;
140 }
141
142 delete_command_t cmd;
143 cmd.pStream = pStreamRef->pStream;
144 cmd.hStream = pStreamRef->hStream;
145 cmd.OrderID = pStreamRef->OrderID;
146
147 DeletionQueue->push(&cmd);
148 return 0;
149 }
150
151 /**
152 * Returns the pointer to a disk stream if the ordered disk stream
153 * represented by the \a StreamOrderID was already activated by the disk
154 * thread, returns NULL otherwise. If the call was successful, thus if it
155 * returned a valid stream pointer, the caller has to the store that pointer
156 * by himself, because it's not possible to call this method again with the
157 * same used order ID; this method is just intended for picking up an ordered
158 * disk stream. This method will usually be called by the voice class (within
159 * the audio thread).
160 *
161 * @param StreamOrderID - ID previously returned by OrderNewStream()
162 * @returns pointer to created stream object, NULL otherwise
163 */
164 Stream* DiskThread::AskForCreatedStream(Stream::OrderID_t StreamOrderID) {
165 dmsg(4,("Disk Thread: been asked if stream already created, OrderID=%x ", StreamOrderID));
166 Stream* pStream = pCreatedStreams[StreamOrderID];
167 if (pStream && pStream != SLOT_RESERVED) {
168 dmsg(4,("(yes created)\n"));
169 pCreatedStreams[StreamOrderID] = NULL; // free the slot for a new order
170 return pStream;
171 }
172 dmsg(4,("(no not yet created)\n"));
173 return NULL;
174 }
175
176
177
178 // #########################################################################
179 // # Disk Thread Only Section
180 // # (following code should only be executed by the disk thread)
181
182
183 DiskThread::DiskThread(uint BufferWrapElements) : Thread(false, 1, -2) {
184 CreationQueue = new RingBuffer<create_command_t>(1024);
185 DeletionQueue = new RingBuffer<delete_command_t>(1024);
186 GhostQueue = new RingBuffer<Stream::Handle>(MAX_INPUT_STREAMS);
187 Streams = MAX_INPUT_STREAMS;
188 RefillStreamsPerRun = REFILL_STREAMS_PER_RUN;
189 for (int i = 0; i < MAX_INPUT_STREAMS; i++) {
190 pStreams[i] = new Stream(STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words
191 }
192 for (int i = 1; i <= MAX_INPUT_STREAMS; i++) {
193 pCreatedStreams[i] = NULL;
194 }
195 }
196
197 DiskThread::~DiskThread() {
198 for (int i = 0; i < MAX_INPUT_STREAMS; i++) {
199 if (pStreams[i]) delete pStreams[i];
200 }
201 if (CreationQueue) delete CreationQueue;
202 if (DeletionQueue) delete DeletionQueue;
203 if (GhostQueue) delete GhostQueue;
204 }
205
206 int DiskThread::Main() {
207 dmsg(3,("Disk thread running\n"));
208 while (true) {
209 IsIdle = true; // will be set to false if a stream got filled
210
211 // if there are ghost streams, delete them
212 for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient
213 Stream::Handle hGhostStream;
214 GhostQueue->pop(&hGhostStream);
215 bool found = false;
216 for (int i = 0; i < this->Streams; i++) {
217 if (pStreams[i]->GetHandle() == hGhostStream) {
218 pStreams[i]->Kill();
219 found = true;
220 break;
221 }
222 }
223 if (!found) GhostQueue->push(&hGhostStream); // put ghost stream handle back to the queue
224 }
225
226 // if there are creation commands, create new streams
227 while (Stream::UnusedStreams > 0 && CreationQueue->read_space() > 0) {
228 create_command_t command;
229 CreationQueue->pop(&command);
230 CreateStream(command);
231 }
232
233 // if there are deletion commands, delete those streams
234 while (Stream::UnusedStreams < Streams && DeletionQueue->read_space() > 0) {
235 delete_command_t command;
236 DeletionQueue->pop(&command);
237 DeleteStream(command);
238 }
239
240 RefillStreams(); // refill the most empty streams
241
242 // if nothing was done during this iteration (eg no streambuffer
243 // filled with data) then sleep for 50ms
244 if (IsIdle) usleep(30000);
245
246 int streamsInUsage = 0;
247 for (int i = Streams - 1; i >= 0; i--) {
248 if (pStreams[i]->GetState() != Stream::state_unused) streamsInUsage++;
249 }
250 ActiveStreamCount = streamsInUsage;
251 if (streamsInUsage > ActiveStreamCountMax) ActiveStreamCountMax = streamsInUsage;
252 }
253
254 return EXIT_FAILURE;
255 }
256
257 void DiskThread::CreateStream(create_command_t& Command) {
258 // search for unused stream
259 Stream* newstream = NULL;
260 for (int i = Streams - 1; i >= 0; i--) {
261 if (pStreams[i]->GetState() == Stream::state_unused) {
262 newstream = pStreams[i];
263 break;
264 }
265 }
266 if (!newstream) {
267 std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;
268 return;
269 }
270 newstream->Launch(Command.hStream, Command.pStreamRef, Command.pSample, Command.SampleOffset, Command.DoLoop);
271 dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));
272 if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {
273 std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;
274 newstream->Kill();
275 return;
276 }
277 pCreatedStreams[Command.OrderID] = newstream;
278 }
279
280 void DiskThread::DeleteStream(delete_command_t& Command) {
281 if (Command.pStream) Command.pStream->Kill();
282 else { // the stream wasn't created by disk thread or picked up by audio thread yet
283
284 // if stream was created but not picked up yet
285 Stream* pStream = pCreatedStreams[Command.OrderID];
286 if (pStream && pStream != SLOT_RESERVED) {
287 pStream->Kill();
288 pCreatedStreams[Command.OrderID] = NULL; // free slot for new order
289 return;
290 }
291
292 // the stream was not created yet
293 if (GhostQueue->write_space() > 0) {
294 GhostQueue->push(&Command.hStream);
295 }
296 else dmsg(1,("DiskThread: GhostQueue full!\n"));
297 }
298 }
299
300 void DiskThread::RefillStreams() {
301 // sort the streams by most empty stream
302 qsort(pStreams, Streams, sizeof(Stream*), CompareStreamWriteSpace);
303
304 // refill the most empty streams
305 for (uint i = 0; i < RefillStreamsPerRun; i++) {
306 if (pStreams[i]->GetState() == Stream::state_active) {
307
308 //float filledpercentage = (float) pStreams[i]->GetReadSpace() / 131072.0 * 100.0;
309 //dmsg(("\nbuffer fill: %.1f%\n", filledpercentage));
310
311 int writespace = pStreams[i]->GetWriteSpaceToEnd();
312 if (writespace == 0) break;
313
314 int capped_writespace = writespace;
315 // if there is too much buffer space available then cut the read/write
316 // size to MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes
317 if (writespace > MAX_REFILL_SIZE) capped_writespace = MAX_REFILL_SIZE;
318
319 // adjust the amount to read in order to ensure that the buffer wraps correctly
320 int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace);
321 // if we wasn't able to refill one of the stream buffers by more than
322 // MIN_REFILL_SIZE we'll send the disk thread to sleep later
323 if (pStreams[i]->ReadAhead(read_amount) > MIN_REFILL_SIZE) this->IsIdle = false;
324 }
325 }
326 }
327
328 /// Handle Generator
329 Stream::Handle DiskThread::CreateHandle() {
330 static uint32_t counter = 0;
331 if (counter == 0xffffffff) counter = 1; // we use '0' as 'invalid handle' only, so we skip 0
332 else counter++;
333 return counter;
334 }
335
336 /// order ID Generator
337 Stream::OrderID_t DiskThread::CreateOrderID() {
338 static uint32_t counter = 0;
339 for (int i = 0; i < MAX_INPUT_STREAMS; i++) {
340 if (counter == MAX_INPUT_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0
341 else counter++;
342 if (!pCreatedStreams[counter]) {
343 pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved
344 return counter; // found empty slot
345 }
346 }
347 return 0; // no free slot
348 }
349
350
351
352 // *********** C functions **************
353 // *
354
355 /**
356 * This is the comparison function the qsort algo uses to determine if a value is
357 * bigger than another one or special in our case; if the writespace of a stream
358 * is bigger than another one.
359 */
360 int CompareStreamWriteSpace(const void* A, const void* B) {
361 Stream* a = *(Stream**) A;
362 Stream* b = *(Stream**) B;
363 return b->GetWriteSpace() - a->GetWriteSpace();
364 }
365
366 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC