… | |
… | |
32 | #include <sys/queue.h> |
32 | #include <sys/queue.h> |
33 | #include <sys/event.h> |
33 | #include <sys/event.h> |
34 | #include <string.h> |
34 | #include <string.h> |
35 | #include <errno.h> |
35 | #include <errno.h> |
36 | |
36 | |
37 | static int kq_fd; |
|
|
38 | static struct kevent *kq_changes; |
|
|
39 | static int kq_changemax, kq_changecnt; |
|
|
40 | static struct kevent *kq_events; |
|
|
41 | static int kq_eventmax; |
|
|
42 | |
|
|
43 | static void |
37 | static void |
44 | kqueue_change (int fd, int filter, int flags, int fflags) |
38 | kqueue_change (EV_P_ int fd, int filter, int flags, int fflags) |
45 | { |
39 | { |
46 | struct kevent *ke; |
40 | struct kevent *ke; |
47 | |
41 | |
48 | array_needsize (kq_changes, kq_changemax, ++kq_changecnt, ); |
42 | array_needsize (kqueue_changes, kqueue_changemax, ++kqueue_changecnt, ); |
49 | |
43 | |
50 | ke = &kq_changes [kq_changecnt - 1]; |
44 | ke = &kqueue_changes [kqueue_changecnt - 1]; |
51 | memset (ke, 0, sizeof (struct kevent)); |
45 | memset (ke, 0, sizeof (struct kevent)); |
52 | ke->ident = fd; |
46 | ke->ident = fd; |
53 | ke->filter = filter; |
47 | ke->filter = filter; |
54 | ke->flags = flags; |
48 | ke->flags = flags; |
55 | ke->fflags = fflags; |
49 | ke->fflags = fflags; |
… | |
… | |
58 | #ifndef NOTE_EOF |
52 | #ifndef NOTE_EOF |
59 | # define NOTE_EOF 0 |
53 | # define NOTE_EOF 0 |
60 | #endif |
54 | #endif |
61 | |
55 | |
62 | static void |
56 | static void |
63 | kqueue_modify (int fd, int oev, int nev) |
57 | kqueue_modify (EV_P_ int fd, int oev, int nev) |
64 | { |
58 | { |
65 | if ((oev ^ nev) & EV_READ) |
59 | if ((oev ^ nev) & EV_READ) |
66 | { |
60 | { |
67 | if (nev & EV_READ) |
61 | if (nev & EV_READ) |
68 | kqueue_change (fd, EVFILT_READ, EV_ADD, NOTE_EOF); |
62 | kqueue_change (fd, EVFILT_READ, EV_ADD, NOTE_EOF); |
… | |
… | |
78 | kqueue_change (fd, EVFILT_WRITE, EV_DELETE, 0); |
72 | kqueue_change (fd, EVFILT_WRITE, EV_DELETE, 0); |
79 | } |
73 | } |
80 | } |
74 | } |
81 | |
75 | |
82 | static void |
76 | static void |
83 | kqueue_poll (ev_tstamp timeout) |
77 | kqueue_poll (EV_P_ ev_tstamp timeout) |
84 | { |
78 | { |
85 | int res, i; |
79 | int res, i; |
86 | struct timespec ts; |
80 | struct timespec ts; |
87 | |
81 | |
88 | ts.tv_sec = (time_t)timeout; |
82 | ts.tv_sec = (time_t)timeout; |
89 | ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; |
83 | ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; |
90 | res = kevent (kq_fd, kq_changes, kq_changecnt, kq_events, kq_eventmax, &ts); |
84 | res = kevent (kqueue_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts); |
91 | kq_changecnt = 0; |
85 | kqueue_changecnt = 0; |
92 | |
86 | |
93 | if (res < 0) |
87 | if (res < 0) |
94 | return; |
88 | return; |
95 | |
89 | |
96 | for (i = 0; i < res; ++i) |
90 | for (i = 0; i < res; ++i) |
97 | { |
91 | { |
98 | if (kq_events [i].flags & EV_ERROR) |
92 | if (kqueue_events [i].flags & EV_ERROR) |
99 | { |
93 | { |
100 | /* |
94 | /* |
101 | * Error messages that can happen, when a delete fails. |
95 | * Error messages that can happen, when a delete fails. |
102 | * EBADF happens when the file discriptor has been |
96 | * EBADF happens when the file discriptor has been |
103 | * closed, |
97 | * closed, |
… | |
… | |
107 | * should not be returned ever; but FreeBSD does :-\ |
101 | * should not be returned ever; but FreeBSD does :-\ |
108 | * An error is also indicated when a callback deletes |
102 | * An error is also indicated when a callback deletes |
109 | * an event we are still processing. In that case |
103 | * an event we are still processing. In that case |
110 | * the data field is set to ENOENT. |
104 | * the data field is set to ENOENT. |
111 | */ |
105 | */ |
112 | if (kq_events [i].data == EBADF) |
106 | if (kqueue_events [i].data == EBADF) |
113 | fd_kill (kq_events [i].ident); |
107 | fd_kill (EV_A_ kqueue_events [i].ident); |
114 | } |
108 | } |
115 | else |
109 | else |
116 | fd_event ( |
110 | fd_event ( |
|
|
111 | EV_A_ |
117 | kq_events [i].ident, |
112 | kqueue_events [i].ident, |
118 | kq_events [i].filter == EVFILT_READ ? EV_READ |
113 | kqueue_events [i].filter == EVFILT_READ ? EV_READ |
119 | : kq_events [i].filter == EVFILT_WRITE ? EV_WRITE |
114 | : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE |
120 | : 0 |
115 | : 0 |
121 | ); |
116 | ); |
122 | } |
117 | } |
123 | |
118 | |
124 | if (expect_false (res == kq_eventmax)) |
119 | if (expect_false (res == kqueue_eventmax)) |
125 | { |
120 | { |
126 | free (kq_events); |
121 | free (kqueue_events); |
127 | kq_eventmax = array_roundsize (kq_events, kq_eventmax << 1); |
122 | kqueue_eventmax = array_roundsize (kqueue_events, kqueue_eventmax << 1); |
128 | kq_events = malloc (sizeof (struct kevent) * kq_eventmax); |
123 | kqueue_events = malloc (sizeof (struct kevent) * kqueue_eventmax); |
129 | } |
124 | } |
130 | } |
125 | } |
131 | |
126 | |
132 | static void |
127 | static int |
133 | kqueue_init (int flags) |
128 | kqueue_init (EV_P_ int flags) |
134 | { |
129 | { |
135 | struct kevent ch, ev; |
130 | struct kevent ch, ev; |
136 | |
131 | |
137 | /* Initalize the kernel queue */ |
132 | /* Initalize the kernel queue */ |
138 | if ((kq_fd = kqueue ()) < 0) |
133 | if ((kqueue_fd = kqueue ()) < 0) |
139 | return; |
134 | return 0; |
140 | |
135 | |
141 | /* Check for Mac OS X kqueue bug. */ |
136 | /* Check for Mac OS X kqueue bug. */ |
142 | ch.ident = -1; |
137 | ch.ident = -1; |
143 | ch.filter = EVFILT_READ; |
138 | ch.filter = EVFILT_READ; |
144 | ch.flags = EV_ADD; |
139 | ch.flags = EV_ADD; |
… | |
… | |
146 | /* |
141 | /* |
147 | * If kqueue works, then kevent will succeed, and it will |
142 | * If kqueue works, then kevent will succeed, and it will |
148 | * stick an error in ev. If kqueue is broken, then |
143 | * stick an error in ev. If kqueue is broken, then |
149 | * kevent will fail. |
144 | * kevent will fail. |
150 | */ |
145 | */ |
151 | if (kevent (kq_fd, &ch, 1, &ev, 1, 0) != 1 |
146 | if (kevent (kqueue_fd, &ch, 1, &ev, 1, 0) != 1 |
152 | || ev.ident != -1 |
147 | || ev.ident != -1 |
153 | || ev.flags != EV_ERROR) |
148 | || ev.flags != EV_ERROR) |
154 | { |
149 | { |
155 | /* detected broken kqueue */ |
150 | /* detected broken kqueue */ |
156 | close (kq_fd); |
151 | close (kqueue_fd); |
157 | return; |
152 | return 0; |
158 | } |
153 | } |
159 | |
154 | |
160 | ev_method = EVMETHOD_KQUEUE; |
|
|
161 | method_fudge = 1e-3; /* needed to compensate for kevent returning early */ |
155 | method_fudge = 1e-3; /* needed to compensate for kevent returning early */ |
162 | method_modify = kqueue_modify; |
156 | method_modify = kqueue_modify; |
163 | method_poll = kqueue_poll; |
157 | method_poll = kqueue_poll; |
164 | |
158 | |
165 | kq_eventmax = 64; /* intiial number of events receivable per poll */ |
159 | kqueue_eventmax = 64; /* intiial number of events receivable per poll */ |
166 | kq_events = malloc (sizeof (struct kevent) * kq_eventmax); |
160 | kqueue_events = malloc (sizeof (struct kevent) * kqueue_eventmax); |
|
|
161 | |
|
|
162 | return EVMETHOD_KQUEUE; |
167 | } |
163 | } |
168 | |
164 | |