22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
+
|
#import "OFDictionary.h"
#ifdef OF_HAVE_SOCKETS
# import "OFStreamObserver.h"
#endif
#import "OFThread.h"
#ifdef OF_HAVE_THREADS
# import "OFMutex.h"
# import "OFCondition.h"
#endif
#import "OFSortedList.h"
#import "OFTimer.h"
#import "OFDate.h"
#import "autorelease.h"
#import "macros.h"
|
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
|
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
|
-
+
+
+
-
+
+
+
-
+
-
-
-
-
+
+
+
+
|
@try {
_timersQueue = [[OFSortedList alloc] init];
#ifdef OF_HAVE_THREADS
_timersQueueLock = [[OFMutex alloc] init];
#endif
#ifdef OF_HAVE_SOCKETS
#if defined(OF_HAVE_SOCKETS)
_streamObserver = [[OFStreamObserver alloc] init];
[_streamObserver setDelegate: self];
_readQueues = [[OFMutableDictionary alloc] init];
#elif defined(OF_HAVE_THREADS)
_condition = [[OFCondition alloc] init];
#endif
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_timersQueue release];
#ifdef OF_HAVE_THREADS
[_timersQueueLock release];
#endif
#ifdef OF_HAVE_SOCKETS
#if defined(OF_HAVE_SOCKETS)
[_streamObserver release];
[_readQueues release];
#elif defined(OF_HAVE_THREADS)
[_condition release];
#endif
[super dealloc];
}
- (void)addTimer: (OFTimer*)timer
{
#ifdef OF_HAVE_THREADS
[_timersQueueLock lock];
@try {
#endif
[_timersQueue insertObject: timer];
#ifdef OF_HAVE_THREADS
} @finally {
[_timersQueueLock unlock];
}
#endif
[timer OF_setInRunLoop: self];
#ifdef OF_HAVE_SOCKETS
#if defined(OF_HAVE_SOCKETS)
[_streamObserver cancel];
#endif
#if defined(OF_HAVE_THREADS) && !defined(OF_HAVE_SOCKETS)
/* FIXME: No way to cancel waiting! What to do? */
#elif defined(OF_HAVE_THREADS)
[_condition lock];
[_condition signal];
[_condition unlock];
#endif
}
- (void)OF_removeTimer: (OFTimer*)timer
{
#ifdef OF_HAVE_THREADS
[_timersQueueLock lock];
|
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
|
-
-
+
+
+
+
+
+
+
-
+
+
+
+
+
-
+
-
-
-
-
+
+
+
+
|
}
#endif
/* Watch for stream events until the next timer is due */
if (nextTimer != nil) {
double timeout = [nextTimer timeIntervalSinceNow];
if (timeout > 0)
#ifdef OF_HAVE_SOCKETS
if (timeout > 0) {
#if defined(OF_HAVE_SOCKETS)
[_streamObserver
observeForTimeInterval: timeout];
#elif defined(OF_HAVE_THREADS)
[_condition lock];
[_condition waitForTimeInterval: timeout];
[_condition unlock];
#else
[OFThread sleepForTimeInterval: timeout];
#endif
}
} else {
/*
* No more timers: Just watch for streams until we get
* an event. If a timer is added by another thread, it
* cancels the observe.
*/
#ifdef OF_HAVE_SOCKETS
#if defined(OF_HAVE_SOCKETS)
[_streamObserver observe];
#elif defined(OF_HAVE_THREADS)
[_condition lock];
[_condition wait];
[_condition unlock];
#else
[OFThread sleepForTimeInterval: 86400];
#endif
}
objc_autoreleasePoolPop(pool);
}
}
- (void)stop
{
_running = false;
#ifdef OF_HAVE_THREADS
of_memory_write_barrier();
#endif
#ifdef OF_HAVE_SOCKETS
#if defined(OF_HAVE_SOCKETS)
[_streamObserver cancel];
#endif
#if defined(OF_HAVE_THREADS) && !defined(OF_HAVE_SOCKETS)
/* FIXME: No way to cancel waiting! What to do? */
#elif defined(OF_HAVE_THREADS)
[_condition lock];
[_condition signal];
[_condition unlock];
#endif
}
@end
|