Purpose of kqueue_task?

Ryan Stone rysto32 at gmail.com
Fri Mar 15 17:19:20 UTC 2013


On Fri, Mar 15, 2013 at 12:46 PM, Sebastian Huber <
sebastian.huber at embedded-brains.de> wrote:

> Hello,
>
> I want to port the FreeBSD kqueue implementation to another operating
> system (RTEMS in this case) to improve the Erlang support.
>
> I have difficulties to understand the purpose of the kqueue_task.  This
> function runs asynchronously.  It obtains some locks and wakes up the
> normal kqueue channel if (kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN.
>  This state is only set in kqueue_close().  So most of the time the
> kqueue_task only obtains some locks, clears a flag (KQ_TASKSCHED) and
> releases the locks?
>

You missed the most important thing that it does: it calls KNOTE_LOCKED to
wake up any waiters sleeping on this event.  I suspect that it had to be
done in a separate task due to lock ordering problems with the kq_global
lock.

The call to wakeup() is used to synchronize with kqueue_close to ensure
that kqueue_close will not free the kqueue while the task is pending.
Otherwise the task could run after kq was freed and crash the system.


More information about the freebsd-hackers mailing list