Re: kqueue issue in multithreaded environment
Re: kqueue issue in multithreaded environment
- Subject: Re: kqueue issue in multithreaded environment
- From: "A.M." <email@hidden>
- Date: Fri, 10 Jun 2011 10:38:56 -0400
On Jun 10, 2011, at 5:31 AM, Leonardo Bernardini wrote:
>
> To anyone interested, I've solved the issue, It has been very complicated to troubleshoot this, because there's a real lack of documentation on how kqueue should be used in a multi-threaded environment. After struggling and studying the kernel, I've fixed my issue ending with a more concrete knowledge of the topic. What I describe here, applies to a Mac OS X 10.6.7 kernel, I really don't know if this has the same behaviour on other BSD variants and/or other OS X versions....
>
> My problem was that, under certain circumnstances, I was registering events on the same file descriptor, from multiple threads simultaneous, this ended with a race condition that, for a funny reason, is shown only when you close the file descriptor, there's no lock actually during the kevent() call.
>
> So, kqueue listens can be invoked on the same kqueue FD from multiple threads, if you use the ONESHOT flag for the events, you'll get just one message per thread, while if you don't use ONESHOT, you may get the same message on each listening thread. Kevents can be registerd from multiple threads as long as you protect the calls on the same file descriptor, i.e. if you want to register for a WRITE event from a sock, be sure to wrap the kevent call in a mutex or whatever else . Same thing, if you need to react to a EOF condition and/or an error that tells you the socket needs to be closed, lock any kevent() call while you close the socket to again avoid a race condition... Also unless you use the ONESHOT flag, avoid to re-register already registered events because it requires you to provide further syncronizations and it's really useless, I did that because I ported the logic from an epool based linux code, that works in a total different way..
>
> Thanks for the support, hope this helps someone else!
>
> Cheers !
>
> Leonardo Bernardini
>
I use kqueue between multiple threads reading and writing one socket, never lock around kqueue or use ONESHOT and I haven't seen this deadlock. Do you have reproducible test case? The only time I lock is to for the userspace socket buffering.
Cheers,
M _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden