Run loops
Run loops
- Subject: Run loops
- From: Mike Laster <email@hidden>
- Date: Wed, 03 Jul 2002 17:41:38 -0400
My question isn't directly network related, but CFRunLoop and CFSocket seem
to be tightly inter-related, so I figured someone here would know the
answer.
How do I externally force a run loop to "wake up"?
What I'm trying to do is have my server gracefully shutdown after receiving
a SIGINT.
My main run loop body is:
- (void) run
{
NSRunLoop *rl = [NSRunLoop currentRunLoop];
NSDate *distantFuture = [NSDate distantFuture];
BOOL isRunning = YES;
do
{
if (_caughtSignal != 0)
{
[MLog logWithFormat:@"** a signal was caught %d **",
_caughtSignal];
[self handleCaughtSignal];
}
[MLog logWithFormat:@">>> run loop isRunning:%d endRunLoop:%d",
isRunning, endRunLoop];
isRunning = [rl runMode:NSDefaultRunLoopMode
beforeDate:distantFuture];
[MLog logWithFormat:@"<<< run loop isRunning:%d endRunLoop:%d",
isRunning, endRunLoop];
} while ( endRunLoop == NO && isRunning == YES);
}
My signal handler is:
void _signalHandler(int sig)
{
_caughtSignal = sig;
// This may be dangerous...
NSLog(@"caught #%d", sig);
// [[NSRunLoop currentRunLoop] addTimer:_shutdownTimer
forMode:NSDefaultRunLoopMode];
// [NSTimer scheduledTimerWithTimeInterval:0.1 target:[NSObject class]
selector:@selector(self) userInfo:nil repeats:NO];
/*
NSNotification *notification = [NSNotification
notificationWithName:@"_CAUGHT_SIGNAL_" object:[NSNumber
numberWithInt:sig]];
[[NSNotificationCenter defaultCenter]
performSelector:@selector(postNotification:)
withObject:notification
afterDelay:
//[[NSNotificationCenter defaultCenter]
postNotificationName:@"_CAUGHT_SIGNAL_" object:[NSNumber
numberWithInt:sig]];
*/
}
As you can see, I've tried various approaches.
- Insert a timer configured to call +[NSObject self] in 0.0 seconds
- Try to post a notification after a delay to make the run loop wake up and
make a call.
- Just plain posting a notification.
None of these approaches wake up my run loop.
I want to do an orderly shutdown (post a "ServerShuttingDownNotification")
and then call [self shutdown] to allow subclassers the ability to clean up
any used resources before going away.
What would be REALLY handy is something like a CFSignalRunLoopSource to take
care of all of these ugly issues. That way any signal would automatically
get posted to the main thread's run loop. :-)
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.