]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModKqueue.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 05 Socket Functions */
12 * This code was originally written by Benno Rice and hacked on quite
13 * a bit by Adrian. Adrian then took it to the hybrid-ircd project to use
14 * in their new IO subsystem. After a year of modifications and some
15 * rather interesting changes (event aggregation) its back in squid.
16 * Thanks to the ircd-hybrid guys.
20 * XXX Currently not implemented / supported by this module XXX
24 * - flags.read_pending
26 * So, its not entirely useful in a production setup since if a read
27 * is meant to be deferred it isn't (we're not even throwing the event
28 * away here). Eventually the rest of the code will be rewritten
29 * so deferred reads aren't required.
35 #include "comm/Loops.h"
38 #include "SquidTime.h"
39 #include "StatCounters.h"
44 #include <sys/event.h>
49 /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */
52 #define EV_SET(kevp, a, b, c, d, e, f) do { \
53 (kevp)->ident = (a); \
54 (kevp)->filter = (b); \
55 (kevp)->flags = (c); \
56 (kevp)->fflags = (d); \
58 (kevp)->udata = (f); \
62 static void kq_update_events(int, short, PF
*);
65 static struct timespec zero_timespec
;
67 static struct kevent
*kqlst
; /* kevent buffer */
68 static int kqmax
; /* max structs to buffer */
69 static int kqoff
; /* offset into the buffer */
70 static int max_poll_time
= 1000;
72 static void commKQueueRegisterWithCacheManager(void);
74 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
75 /* Private functions */
78 kq_update_events(int fd
, short filter
, PF
* handler
)
86 cur_handler
= fd_table
[fd
].read_handler
;
90 cur_handler
= fd_table
[fd
].write_handler
;
94 /* XXX bad! -- adrian */
99 if ((cur_handler
== NULL
&& handler
!= NULL
)
100 || (cur_handler
!= NULL
&& handler
== NULL
)) {
106 if (handler
!= NULL
) {
107 kep_flags
= (EV_ADD
| EV_ONESHOT
);
109 kep_flags
= EV_DELETE
;
112 EV_SET(kep
, (uintptr_t) fd
, filter
, kep_flags
, 0, 0, 0);
114 /* Check if we've used the last one. If we have then submit them all */
115 if (kqoff
== kqmax
- 1) {
118 ret
= kevent(kq
, kqlst
, kqmax
, NULL
, 0, &zero_timespec
);
119 /* jdc -- someone needs to do error checking... */
122 perror("kq_update_events(): kevent()");
133 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
134 /* Public functions */
139 * This is a needed exported function which will be called to initialise
140 * the network loop code.
143 Comm::SelectLoopInit(void)
148 fatal("comm_select_init: Couldn't open kqueue fd!\n");
151 kqmax
= getdtablesize();
153 kqlst
= (struct kevent
*)xmalloc(sizeof(*kqlst
) * kqmax
);
154 zero_timespec
.tv_sec
= 0;
155 zero_timespec
.tv_nsec
= 0;
157 commKQueueRegisterWithCacheManager();
163 * This is a needed exported function which will be called to register
164 * and deregister interest in a pending IO state for a given FD.
167 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
169 fde
*F
= &fd_table
[fd
];
171 assert(F
->flags
.open
);
172 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
173 ", handler=" << handler
<< ", client_data=" << client_data
<<
174 ", timeout=" << timeout
);
176 if (type
& COMM_SELECT_READ
) {
177 if (F
->flags
.read_pending
)
178 kq_update_events(fd
, EVFILT_WRITE
, handler
);
180 kq_update_events(fd
, EVFILT_READ
, handler
);
182 F
->read_handler
= handler
;
183 F
->read_data
= client_data
;
186 if (type
& COMM_SELECT_WRITE
) {
187 kq_update_events(fd
, EVFILT_WRITE
, handler
);
188 F
->write_handler
= handler
;
189 F
->write_data
= client_data
;
193 F
->timeout
= squid_curtime
+ timeout
;
198 Comm::ResetSelect(int fd
)
200 fde
*F
= &fd_table
[fd
];
201 if (F
->read_handler
) {
202 kq_update_events(fd
, EVFILT_READ
, (PF
*)1);
204 if (F
->write_handler
) {
205 kq_update_events(fd
, EVFILT_WRITE
, (PF
*)1);
210 * Check all connections for new connections and input data that is to be
211 * processed. Also check for connections with data queued and whether we can
218 * Called to do the new-style IO, courtesy of of squid (like most of this
219 * new IO code). This routine handles the stuff we've hidden in
220 * comm_setselect and fd_table[] and calls callbacks for IO ready
225 Comm::DoSelect(int msec
)
229 static struct kevent ke
[KE_LENGTH
];
231 struct timespec poll_time
;
233 if (msec
> max_poll_time
)
234 msec
= max_poll_time
;
236 poll_time
.tv_sec
= msec
/ 1000;
238 poll_time
.tv_nsec
= (msec
% 1000) * 1000000;
241 num
= kevent(kq
, kqlst
, kqoff
, ke
, KE_LENGTH
, &poll_time
);
242 ++statCounter
.select_loops
;
248 if (ignoreErrno(errno
))
253 return Comm::COMM_ERROR
;
261 return Comm::OK
; /* No error.. */
263 for (i
= 0; i
< num
; ++i
) {
264 int fd
= (int) ke
[i
].ident
;
266 fde
*F
= &fd_table
[fd
];
268 if (ke
[i
].flags
& EV_ERROR
) {
270 /* XXX error == bad! -- adrian */
274 if (ke
[i
].filter
== EVFILT_READ
|| F
->flags
.read_pending
) {
275 if ((hdl
= F
->read_handler
) != NULL
) {
276 F
->read_handler
= NULL
;
277 F
->flags
.read_pending
= 0;
278 hdl(fd
, F
->read_data
);
282 if (ke
[i
].filter
== EVFILT_WRITE
) {
283 if ((hdl
= F
->write_handler
) != NULL
) {
284 F
->write_handler
= NULL
;
285 hdl(fd
, F
->write_data
);
289 if (ke
[i
].filter
!= EVFILT_WRITE
&& ke
[i
].filter
!= EVFILT_READ
) {
291 debugs(5, DBG_IMPORTANT
, "comm_select: kevent returned " << ke
[i
].filter
<< "!");
299 Comm::QuickPollRequired(void)
305 commKQueueRegisterWithCacheManager(void)
309 #endif /* USE_KQUEUE */