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