#import <Foundation/Foundation.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSTimer.h>
#include <signal.h>
@interface MyDelegate : NSObject
- (void) callee: (NSTimer *)timer;
@end
@implementation MyDelegate
- (void) callee: (NSTimer *)timer
{
NSLog(@"Timer called %@ with userInfo: %@", timer, [timer userInfo]);
}
@end
static volatile BOOL caughtSIGINT = NO;
void
handle_SIGINT (int signum)
{
void (*oldsig)(int);
caughtSIGINT = YES;
if ((oldsig = signal (signum, handle_SIGINT)) != handle_SIGINT) {
signal (signum, oldsig);
}
}
int
main (int argc, char *argv[])
{
signal (SIGHUP, SIG_IGN);
signal (SIGUSR1, SIG_IGN);
signal (SIGUSR2, SIG_IGN);
signal (SIGINT, handle_SIGINT);
CREATE_AUTORELEASE_POOL(pool);
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
MyDelegate *foo = [[[MyDelegate alloc] init] autorelease];
NSLog (@"Starting");
[[[NSTimer
scheduledTimerWithTimeInterval: 5.0
target: foo
selector: @selector(callee:)
userInfo: @"payload"
repeats: YES ] autorelease] fire];
BOOL isRunning;
do {
NSLog(@"looping");
isRunning = [runLoop runMode: NSDefaultRunLoopMode
beforeDate: [NSDate distantFuture]];
if (caughtSIGINT) {
NSLog (@"caught SIGINT - exiting...");
exit (1);
}
} while (isRunning);
NSLog (@"Normal exit");
RELEASE(pool);
exit (0);
}