Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: kqueue/kevent project



On Mon, Mar 11, 2002 at 05:06:55PM -0500, Jim Magee wrote:
> On Monday, March 11, 2002, at 04:45 PM, Stefan Arentz wrote:
> > On Mon, Mar 11, 2002 at 09:16:16AM -0500, Jim Magee wrote:
> >> One "problem" the kqueues implementation presents: multiple threads
> >> sharing a kqueue.
> >
> > Are you refering to kernel threads or user threads?
>
> I'm referring to multiple threads (pthreads) in an application waiting
> on a single kqueue. The same problem arises, however, if each thread
> has its own kqueue, but the same event source is added to multiple of
> them. In either case, more than thread will be awakened if the source
> fires.

The documentation does not say anything about multiple threads sharing a
kqueue. I think it was never intended to be used like that. Don't you
typically create multiple queues (multiple kqueues can exist in a single
process) with different event sources and then use worker threads to
serialize the incoming events?

When multiple threads listen on the same queue then an out of order problem
can occur. Say that two threads are listening to events from the same
socket descriptor. It is possible that thread A fires and receives the
EV_READ event that is also marked EOF (socket closed) *before* thread B
is able to process the previous EV_READ.

This is typically solved at the application level. I don't think the
kernel should do anything to prevent this problem. Maybe it's good to
keep a use count on the kqueue and return something like EALREADY or
EINPROGRESS when a second thread tries to call kevent() on the same queue.

However, it might be a good option to be able to change the kqueue from
another thread (adding/removing listeners). Needs more thought :-)

> However, the ONESHOT options allows for only one thread being awakened
> in the first scenario (many threads, one kqueue). I'm not sufficiently
> kqueue-savvy enough to know what the behavior would be in the second
> scenario (one kqueue per thread, each with the same source registered as
> ONESHOT). Does only one thread get awakened, or does one thread from
> each kqueue get awakened?

EV_ONESHOT Causes the event to return only the first occurrence of
the filter being triggered. After the user retrieves the
event from the kqueue, it is deleted.

I'm not yet completely sure how the EV_ONESHOT option is related to the
threading issue. I need to read more code first.

BTW, I'm reading http://stuff.sateh.com/Mach/kernel_principles.pdf now. Is
this document still relevant? It's almost 10 years old now :-/

Regards,

Stefan
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: kqueue/kevent project (From: Stefan Arentz <email@hidden>)
 >Re: kqueue/kevent project (From: Jim Magee <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.