Subject: Re: interrupting kevent()
To: Antti Kantee <pooka@cs.hut.fi>
From: Andrew Doran <ad@netbsd.org>
List: tech-kern
Date: 10/24/2007 02:36:32
On Tue, Oct 23, 2007 at 04:30:14PM +0300, Antti Kantee wrote:

> On Tue Oct 23 2007 at 14:23:40 +0100, Andrew Doran wrote:
> > On Tue, Oct 23, 2007 at 03:45:49PM +0300, Antti Kantee wrote:
> > 
> > > I have a dual eventloop/thread model and I need to interrupt the event
> > > call from a thread sometimes.  I didn't find any sensible way of doing
> > > this, so I added a KEVENT_TICKLE ioctl() to kqueue for interrupting
> > > the wait.
> > > 
> > > Did I miss something or does the following look ok?

I haven't read kern_event.c without doing a runner yet, but the wakeup()
should be done while holding the lock to avoid a sleep/wakeup race.

> > Other ideas: use socketpair() or pthread_kill(thread, SIGIO).
> 
> I considered writing to a socket, but couldn't figure out how to do it
> without having to drain the signal byte from the socket therefore taking
> two syscalls for signalling instead of one.

So it's likely to happen often?
 
> Signals are problematic because they are global resources for a process
> and this is a library.

One other idea: provided that you know which thread is in kevent, you could
mimic pthread cancellation but without the exit. That would not be portable
though. Basically, test some kind of flag before and after the syscall, and
in all cases do an _lwp_wakeup() on the kevent thread. (As an aside, it
would probably be useful to have a pthread_getlwpid_np() call or similar.)

Andrew