Re: Cocoa & signal handlers
Re: Cocoa & signal handlers
- Subject: Re: Cocoa & signal handlers
- From: Ingvar Nedrebo <email@hidden>
- Date: Mon, 10 Dec 2001 22:56:42 +0000
On Monday, December 10, 2001, at 10:04 , Greg Parker wrote:
On Wednesday, December 5, 2001, at 10:02 AM, Ingvar Nedrebo wrote:
Greg,
Thanks! I had come to suspect as much, so what I'll do is to
use a global variable, set my NSTimer to 2 secs and just count
up to my period of ~15 minutes. The CPU overhead should still
be neglible.
But out of curiosity and because of an upcoming project where
this could be more of an issue: One of my thoughts was to open
an NSPipe and use -readInBackgroundAndNotify; then in the
signal handler do a non-blocking raw write(2) directly on
the pipe. Would that have a chance of working?
You may be able to use raw write() on a pipe from a signal
handler. I haven't looked at Darwin's implementation, so I
can't say whether it's completely safe.
Well I implemented this approach and so far it seems to be
working fine -- in fact there was a recommendation on the
Darwin developer's list to use this technique as a work-around
for Darwin's lack of per-thread signals.
You may also be able to use CFRunLoop APIs from CoreFoundation to
wake up the run loop directly. -readInBackgroundAndNotify will
need to do something like this internally, so if this isn't safe
then neither is the pipe.
My understanding from the documentation of readInBackgroundAndNotify
was that it does the read in a separate thread -- on the face
of it it looks fairly safe to do a single system call in a signal
handler, provided of course that the write doesn't block, but
I'm only writing 4 bytes (the signal number).
1. Create a CFRunLoopObserver that runs on kCFRunLoopAfterWaiting
and checks the global variable. Attach the observer to the
app's run loop.
2. In the signal handler, set the global variable and then
call CFRunLoopWakeUp(), which will waken the run loop and run
the observer.
(This is not official Apple advice. The above system has not been
tested. Your mileage may vary.)
--
Greg Parker email@hidden Java & Objective C