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