]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModKqueue.cc
4887214aca60c39ca33bddfdeb24809e5d4aba85
2 * Copyright (C) 1996-2023 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 "StatCounters.h"
43 #include <sys/event.h>
48 static void kq_update_events(int, short, PF
*);
51 static struct timespec zero_timespec
;
53 static struct kevent
*kqlst
; /* kevent buffer */
54 static int kqmax
; /* max structs to buffer */
55 static int kqoff
; /* offset into the buffer */
56 static int max_poll_time
= 1000;
58 static void commKQueueRegisterWithCacheManager(void);
60 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
61 /* Private functions */
64 kq_update_events(int fd
, short filter
, PF
* handler
)
72 cur_handler
= fd_table
[fd
].read_handler
;
76 cur_handler
= fd_table
[fd
].write_handler
;
80 /* XXX bad! -- adrian */
85 if ((cur_handler
== NULL
&& handler
!= NULL
)
86 || (cur_handler
!= NULL
&& handler
== NULL
)) {
92 if (handler
!= NULL
) {
93 kep_flags
= (EV_ADD
| EV_ONESHOT
);
95 kep_flags
= EV_DELETE
;
98 EV_SET(kep
, (uintptr_t) fd
, filter
, kep_flags
, 0, 0, 0);
100 /* Check if we've used the last one. If we have then submit them all */
101 if (kqoff
== kqmax
- 1) {
104 ret
= kevent(kq
, kqlst
, kqmax
, NULL
, 0, &zero_timespec
);
105 /* jdc -- someone needs to do error checking... */
108 perror("kq_update_events(): kevent()");
119 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
120 /* Public functions */
125 * This is a needed exported function which will be called to initialise
126 * the network loop code.
129 Comm::SelectLoopInit(void)
134 fatal("comm_select_init: Couldn't open kqueue fd!\n");
137 kqmax
= getdtablesize();
139 kqlst
= (struct kevent
*)xmalloc(sizeof(*kqlst
) * kqmax
);
140 zero_timespec
.tv_sec
= 0;
141 zero_timespec
.tv_nsec
= 0;
143 commKQueueRegisterWithCacheManager();
149 * This is a needed exported function which will be called to register
150 * and deregister interest in a pending IO state for a given FD.
153 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
155 fde
*F
= &fd_table
[fd
];
157 assert(F
->flags
.open
|| (!handler
&& !client_data
&& !timeout
));
158 debugs(5, 5, "FD " << fd
<< ", type=" << type
<<
159 ", handler=" << handler
<< ", client_data=" << client_data
<<
160 ", timeout=" << timeout
);
162 if (type
& COMM_SELECT_READ
) {
163 if (F
->flags
.read_pending
)
164 kq_update_events(fd
, EVFILT_WRITE
, handler
);
166 kq_update_events(fd
, EVFILT_READ
, handler
);
168 F
->read_handler
= handler
;
169 F
->read_data
= client_data
;
172 if (type
& COMM_SELECT_WRITE
) {
173 kq_update_events(fd
, EVFILT_WRITE
, handler
);
174 F
->write_handler
= handler
;
175 F
->write_data
= client_data
;
179 F
->timeout
= squid_curtime
+ timeout
;
184 * Check all connections for new connections and input data that is to be
185 * processed. Also check for connections with data queued and whether we can
192 * Called to do the new-style IO, courtesy of of squid (like most of this
193 * new IO code). This routine handles the stuff we've hidden in
194 * comm_setselect and fd_table[] and calls callbacks for IO ready
199 Comm::DoSelect(int msec
)
203 static struct kevent ke
[KE_LENGTH
];
205 struct timespec poll_time
;
207 if (msec
> max_poll_time
)
208 msec
= max_poll_time
;
210 poll_time
.tv_sec
= msec
/ 1000;
212 poll_time
.tv_nsec
= (msec
% 1000) * 1000000;
215 num
= kevent(kq
, kqlst
, kqoff
, ke
, KE_LENGTH
, &poll_time
);
216 ++statCounter
.select_loops
;
222 if (ignoreErrno(errno
))
227 return Comm::COMM_ERROR
;
235 return Comm::OK
; /* No error.. */
237 for (i
= 0; i
< num
; ++i
) {
238 int fd
= (int) ke
[i
].ident
;
240 fde
*F
= &fd_table
[fd
];
242 if (ke
[i
].flags
& EV_ERROR
) {
244 /* XXX error == bad! -- adrian */
248 if (ke
[i
].filter
== EVFILT_READ
|| F
->flags
.read_pending
) {
249 if ((hdl
= F
->read_handler
) != NULL
) {
250 F
->read_handler
= NULL
;
251 hdl(fd
, F
->read_data
);
255 if (ke
[i
].filter
== EVFILT_WRITE
) {
256 if ((hdl
= F
->write_handler
) != NULL
) {
257 F
->write_handler
= NULL
;
258 hdl(fd
, F
->write_data
);
262 if (ke
[i
].filter
!= EVFILT_WRITE
&& ke
[i
].filter
!= EVFILT_READ
) {
264 debugs(5, DBG_IMPORTANT
, "comm_select: kevent returned " << ke
[i
].filter
<< "!");
272 Comm::QuickPollRequired(void)
278 commKQueueRegisterWithCacheManager(void)
282 #endif /* USE_KQUEUE */