1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/*
Copyright (C) 2000 Stefan Westerfeld
stefan@space.twc.de
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "iomanager.h"
#include "dispatcher.h"
#include "notification.h"
#include "thread.h"
#include <stdio.h>
#include <fcntl.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> // Needed on some systems.
#endif
// WABA: NOTE!
// sys/select.h is needed on e.g. AIX to define "fd_set".
// However, we can not include config.h in a header file.
// The right solution would be not to use "fd_set" in the
// header file but to use it only in a private datastructure
// defined in the .cc file.
using namespace std;
using namespace Arts;
namespace Arts {
/* internal data structures */
class IOWatchFD {
int _fd, _types;
IONotify *_notify;
public:
int activeTypes;
IOWatchFD(int fd, int types, IONotify *notify);
inline int fd() { return _fd; };
inline int types() { return _types; };
inline IONotify *notify() { return _notify; };
inline void remove(int types) { _types &= ~types; }
};
class TimeWatcher {
int milliseconds;
TimeNotify *_notify;
timeval nextNotify;
bool active, destroyed;
bool earlier(const timeval& reference);
public:
TimeWatcher(int _milliseconds, TimeNotify *notify);
inline TimeNotify *notify() { return _notify; };
timeval advance(const timeval& currentTime);
void destroy();
};
}
/*
* Enable this if you want to debug how long certain plugins / operations
* take to perform. You'll get the times between two select() calls that are
* done by the IOManager, which is equivalent to the time the input/output
* remains unserved. For apps like artsd, it gives the minimum audio latency
* users will need to specify to avoid dropouts.
*/
#undef IOMANAGER_DEBUG_LATENCY
#ifdef IOMANAGER_DEBUG_LATENCY
static timeval iomanager_debug_latency_time = { 0, 0 };
static void iomanager_debug_latency_end()
{
if(iomanager_debug_latency_time.tv_sec)
{
timeval end;
gettimeofday(&end,0);
float diff = (end.tv_usec-iomanager_debug_latency_time.tv_usec)/1000.0
+ (end.tv_sec-iomanager_debug_latency_time.tv_sec)*1000.0;
/* change this value if you get your screen filled up with messages */
if(diff >= 1.5)
fprintf(stderr,"IOManager: latency for operation: %2.3f ms\n",diff);
}
}
static void iomanager_debug_latency_start()
{
gettimeofday(&iomanager_debug_latency_time,0);
}
#else
static inline void iomanager_debug_latency_end()
{
}
static inline void iomanager_debug_latency_start()
{
}
#endif
IOWatchFD::IOWatchFD(int fd, int types, IONotify *notify)
{
_fd = fd;
_types = types;
_notify = notify;
activeTypes = 0;
}
StdIOManager::StdIOManager()
{
// force initialization of the fd_set's
fdListChanged = true;
timeListChanged = false;
level = 0;
}
void StdIOManager::processOneEvent(bool blocking)
{
assert(SystemThreads::the()->isMainThread());
level++;
// we release and acquire the lock only on level 1
if(level == 1)
Dispatcher::lock();
// notifications not carried out reentrant
if(level == 1)
NotificationManager::the()->run();
// FIXME: timers *could* change the file descriptors to select...
//---
if(fdListChanged)
{
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_ZERO(&reentrant_readfds);
FD_ZERO(&reentrant_writefds);
FD_ZERO(&reentrant_exceptfds);
maxfd = 0;
list<IOWatchFD *>::iterator i;
for(i = fdList.begin(); i != fdList.end(); i++)
{
IOWatchFD *w = *i;
if(w->types() & IOType::read) FD_SET(w->fd(),&readfds);
if(w->types() & IOType::write) FD_SET(w->fd(),&writefds);
if(w->types() & IOType::except) FD_SET(w->fd(),&exceptfds);
if(w->types() & IOType::reentrant)
{
if(w->types() & IOType::read)
FD_SET(w->fd(),&reentrant_readfds);
if(w->types() & IOType::write)
FD_SET(w->fd(),&reentrant_writefds);
if(w->types() & IOType::except)
FD_SET(w->fd(),&reentrant_exceptfds);
}
if(w->types() && w->fd() > maxfd) maxfd = w->fd();
}
fdListChanged = false;
}
fd_set rfd,wfd,efd;
if(level == 1)
{
rfd = readfds;
wfd = writefds;
efd = exceptfds;
}
else
{
// watch out, this is reentrant I/O
rfd = reentrant_readfds;
wfd = reentrant_writefds;
efd = reentrant_exceptfds;
}
/* default timeout 5 seconds */
long selectabs;
if(blocking)
selectabs = 5000000;
else
selectabs = 0;
/* prepare timers - only at level 1 */
if(level == 1 && timeList.size())
{
struct timeval currenttime;
gettimeofday(¤ttime,0);
list<TimeWatcher *>::iterator ti;
timeListChanged = false;
ti = timeList.begin();
while(ti != timeList.end())
{
TimeWatcher *w = *ti++;
timeval timertime = w->advance(currenttime);
// if that may happen in the next ten seconds
if(timertime.tv_sec < currenttime.tv_sec+10)
{
long timerabs = (timertime.tv_sec - currenttime.tv_sec)*1000000;
timerabs += (timertime.tv_usec - currenttime.tv_usec);
if(timerabs < selectabs) selectabs = timerabs;
}
if(timeListChanged)
{
ti = timeList.begin();
timeListChanged = false;
}
}
}
timeval select_timeout;
select_timeout.tv_sec = selectabs / 1000000;
select_timeout.tv_usec = selectabs % 1000000;
if(level == 1) iomanager_debug_latency_end();
// we release and acquire the lock only on level 1
if(level == 1)
Dispatcher::unlock();
int retval = select(maxfd+1,&rfd,&wfd,&efd,&select_timeout);
// we release and acquire the lock only on level 1
if(level == 1)
Dispatcher::lock();
if(level == 1) iomanager_debug_latency_start();
if(retval > 0)
{
/*
* the problem is, that objects that are being notified may change
* the watch list, add fds, remove fds, remove objects and whatever
* else
*
* so we can' notify them from the loop - but we can make a stack
* of "notifications to do" and send them as soon as we looked up
* in the list what to send
*/
long tonotify = 0;
list<IOWatchFD *>::iterator i;
for(i = fdList.begin(); i != fdList.end(); i++) {
IOWatchFD *w = *i;
int match = 0;
if(FD_ISSET(w->fd(),&rfd) && (w->types() & IOType::read))
match |= IOType::read;
if(FD_ISSET(w->fd(),&wfd) && (w->types() & IOType::write))
match |= IOType::write;
if(FD_ISSET(w->fd(),&efd) && (w->types() & IOType::except))
match |= IOType::except;
if((w->types() & IOType::reentrant) == 0 && level != 1)
match = 0;
if(match) {
tonotify++;
w->activeTypes = match;
notifyStack.push(w);
}
}
while(tonotify != 0)
{
if(!fdListChanged)
{
IOWatchFD *w = notifyStack.top();
int activeTypes = w->activeTypes;
int fd = w->fd();
IONotify *notify = w->notify();
w->activeTypes = 0;
notify->notifyIO(fd, activeTypes);
// warning: w and notify might no longer exist here
}
notifyStack.pop();
tonotify--;
}
}
/* handle timers - only at level 1 */
if(level == 1 && timeList.size())
{
struct timeval currenttime;
gettimeofday(¤ttime,0);
list<TimeWatcher *>::iterator ti;
timeListChanged = false;
ti = timeList.begin();
while(ti != timeList.end())
{
TimeWatcher *w = *ti++;
w->advance(currenttime);
if (timeListChanged)
{
ti = timeList.begin();
timeListChanged = false;
}
}
}
// notifications not carried out reentrant
if(level == 1)
NotificationManager::the()->run();
// we release and acquire the lock only on level 1
if(level == 1)
Dispatcher::unlock();
level--;
}
void StdIOManager::run()
{
assert(SystemThreads::the()->isMainThread());
assert(level == 0);
// FIXME: this might not be threadsafe, as there is no lock here!
terminated = false;
while(!terminated)
processOneEvent(true);
}
void StdIOManager::terminate()
{
terminated = true;
Dispatcher::wakeUp();
}
void StdIOManager::watchFD(int fd, int types, IONotify *notify)
{
/*
IOWatchFD *watchfd = findWatch(fd,notify);
if(watchfd)
{
watchfd->add(types);
}
else
{
fdList.push_back(new IOWatchFD(fd,types,notify));
}
*/
// FIXME: might want to reuse old watches
fdList.push_back(new IOWatchFD(fd,types,notify));
fdListChanged = true;
Dispatcher::wakeUp();
}
void StdIOManager::remove(IONotify *notify, int types)
{
list<IOWatchFD *>::iterator i;
i = fdList.begin();
while(i != fdList.end())
{
IOWatchFD *w = *i;
if(w->notify() == notify) w->remove(types);
// nothing left to watch?
if(w->types() == 0 || w->types() == IOType::reentrant)
{
i = fdList.erase(i);
delete w; // FIXME: shouldn't we have a destroy() similar
// to the one for timers
}
else i++;
}
fdListChanged = true;
}
void StdIOManager::addTimer(int milliseconds, TimeNotify *notify)
{
if (milliseconds == -1 && notify == 0) {
// HACK: in order to not add a virtual function to IOManager we're calling addTimer with
// magic values. This call tells the ioManager that notifications are pending and
// NotificationManager::run() should get called soon.
} else {
timeList.push_back(new TimeWatcher(milliseconds,notify));
timeListChanged = true;
Dispatcher::wakeUp();
}
}
void StdIOManager::removeTimer(TimeNotify *notify)
{
list<TimeWatcher *>::iterator i;
i = timeList.begin();
while(i != timeList.end())
{
TimeWatcher *w = *i;
if(w->notify() == notify)
{
i = timeList.erase(i);
timeListChanged = true;
w->destroy();
}
else i++;
}
}
TimeWatcher::TimeWatcher(int _milliseconds, TimeNotify *notify)
: milliseconds(_milliseconds),_notify(notify),active(false),destroyed(false)
{
gettimeofday(&nextNotify,0);
nextNotify.tv_usec += (milliseconds%1000)*1000;
nextNotify.tv_sec += (milliseconds/1000)+(nextNotify.tv_usec/1000000);
nextNotify.tv_usec %= 1000000;
}
timeval TimeWatcher::advance(const timeval& currentTime)
{
active = true;
while(earlier(currentTime))
{
nextNotify.tv_usec += (milliseconds%1000)*1000;
nextNotify.tv_sec += (milliseconds/1000)+(nextNotify.tv_usec/1000000);
nextNotify.tv_usec %= 1000000;
_notify->notifyTime();
if(destroyed)
{
delete this;
struct timeval never = { -1, 0 };
return never;
}
}
active = false;
return nextNotify;
}
bool TimeWatcher::earlier(const timeval& reference)
{
if(nextNotify.tv_sec > reference.tv_sec) return false;
if(nextNotify.tv_sec < reference.tv_sec) return true;
return (nextNotify.tv_usec < reference.tv_usec);
}
void TimeWatcher::destroy()
{
if(active)
{
destroyed = true;
}
else
{
delete this;
}
}
|