- 论坛徽章:
- 0
|
Here is a brief introduction about kqueue/kevent mechanism in FreeBSD. It is just my note. The Operating System I use during this note.
[amd64box:~]$uname -a
FreeBSD amd64box 6.2-RELEASE FreeBSD 6.2-RELEASE #0: Mon Apr 23 21:42:50 CST 2007 root@:/usr/src/sys/amd64/compile/AMD64MP amd64
A kqueue is a container of state for kevent calls.
int kevent(int kq, const struct kevent *changelist, int nchanges,
/* register these kevents on this kqueue */ struct kevent *eventlist, int nevents,
/* get ready kevents from this kqueue */ const struct timespec *timeout);
A kqueue dispatches those kevents in changelist to files they point to, and he knows which ones have been dispatched or not. Every file will report to this kqueue if its status changed (if this kqueue is interested in it). The kqueue will keep these reports and send them to user via kevent when requested.
kqueue_register tell the kqueue to dispatch this kevent (called knote in kernel) to corresponding file, and kqueue will keep this kevent in a private list, so that there are at most one (ident, filter) in one kqueue for any ident and/or filter.
kqueue_scan scan the pending kevents on a kqueue if any and msleep on it if there are no kevents pending on the kqueue
KNOTE_ACTIVATE a kevent is triggered. Insert the kevent into its kqueue's list and tell any monitor that the kqueue is OK. to avoid deadlock, use taskqueue to activate kevents on si_note asynchronously.
This is a common scene.
A registers kevents kev0, kev1 on a kqueue kq1, kq1 dispatches these kevents to files f0, f1, and A msleeps on kq1. B triggers kev0, puts kev0 on the active list of kq1 and wakeups A from kq1 asynchronously. A finds that kev0 is triggered and returns to user mode.
Locks
KQ_LOCK protects the kqueue and knotes on it.
kq_global acquired if we need lock multiple kqueues
KN_INFLUX protects a knote when its kqueue is unlocked
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10543/showart_290779.html |
|