/[svn]/linuxsampler/trunk/src/diskthread.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/diskthread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (show annotations) (download)
Sun Apr 11 17:25:38 2004 UTC (19 years, 11 months ago) by senkov
File size: 14056 byte(s)
* Fixed bug in AskForCreatedStream(). Cored if stream was not created.

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

  ViewVC Help
Powered by ViewVC