2
2
* FD polling functions for FreeBSD kqueue()
4
* Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
4
* Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
6
6
* This program is free software; you can redistribute it and/or
7
7
* modify it under the terms of the GNU General Public License
8
8
* as published by the Free Software Foundation; either version
9
9
* 2 of the License, or (at your option) any later version.
11
* Note: not knowing much about kqueue, I had to rely on OpenBSD's detailed man
12
* page and to check how it was implemented in lighttpd to understand it better.
13
* But it is possible that I got things wrong.
17
13
#include <unistd.h>
34
30
#include <proto/task.h>
37
static fd_set *fd_evts[2];
38
33
static int kqueue_fd;
39
34
static struct kevent *kev = NULL;
41
/* speeds up conversion of DIR_RD/DIR_WR to EVFILT* */
42
static const int dir2filt[2] = { EVFILT_READ, EVFILT_WRITE };
44
/* completes a change list for deletion */
45
REGPRM3 static int kqev_del(struct kevent *kev, const int fd, const int dir)
47
if (FD_ISSET(fd, fd_evts[dir])) {
48
FD_CLR(fd, fd_evts[dir]);
49
EV_SET(kev, fd, dir2filt[dir], EV_DELETE, 0, 0, NULL);
56
* Returns non-zero if direction <dir> is already set for <fd>.
58
REGPRM2 static int __fd_is_set(const int fd, int dir)
60
return FD_ISSET(fd, fd_evts[dir]);
63
REGPRM2 static int __fd_set(const int fd, int dir)
65
/* if the value was set, do nothing */
66
if (FD_ISSET(fd, fd_evts[dir]))
69
FD_SET(fd, fd_evts[dir]);
70
EV_SET(kev, fd, dir2filt[dir], EV_ADD, 0, 0, NULL);
71
kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
75
REGPRM2 static int __fd_clr(const int fd, int dir)
77
if (!kqev_del(kev, fd, dir))
79
kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
83
REGPRM1 static void __fd_rem(int fd)
87
changes += kqev_del(&kev[changes], fd, DIR_RD);
88
changes += kqev_del(&kev[changes], fd, DIR_WR);
91
kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
94
REGPRM1 static void __fd_clo(int fd)
96
FD_CLR(fd, fd_evts[DIR_RD]);
97
FD_CLR(fd, fd_evts[DIR_WR]);
106
42
int count, fd, delta_ms;
107
43
struct timespec timeout;
47
/* first, scan the update list to find changes */
48
for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
49
fd = fd_updt[updt_idx];
50
fdtab[fd].updated = 0;
57
en = fd_compute_new_polled_status(eo);
59
if ((eo ^ en) & FD_EV_POLLED_RW) {
60
/* poll status changed */
63
if ((eo ^ en) & FD_EV_POLLED_R) {
64
/* read poll status changed */
65
if (en & FD_EV_POLLED_R) {
66
EV_SET(&kev[changes], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
70
EV_SET(&kev[changes], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
75
if ((eo ^ en) & FD_EV_POLLED_W) {
76
/* write poll status changed */
77
if (en & FD_EV_POLLED_W) {
78
EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
82
EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
88
fd_alloc_or_release_cache_entry(fd, en);
91
kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
110
95
timeout.tv_sec = 0;
111
96
timeout.tv_nsec = 0;
113
if (!run_queue && !signal_queue_len) {
98
if (!fd_cache_num && !run_queue && !signal_queue_len) {
115
100
delta_ms = MAX_DELAY_MS;
116
101
timeout.tv_sec = (MAX_DELAY_MS / 1000);
133
119
fd, // int nevents
134
120
&timeout); // const struct timespec *timeout
135
121
tv_update_date(delta_ms, status);
137
124
for (count = 0; count < status; count++) {
138
125
fd = kev[count].ident;
127
if (!fdtab[fd].owner)
130
fdtab[fd].ev &= FD_POLL_STICKY;
139
132
if (kev[count].filter == EVFILT_READ) {
140
if (FD_ISSET(fd, fd_evts[DIR_RD])) {
141
if (fdtab[fd].state == FD_STCLOSE)
143
fdtab[fd].cb[DIR_RD].f(fd);
145
} else if (kev[count].filter == EVFILT_WRITE) {
146
if (FD_ISSET(fd, fd_evts[DIR_WR])) {
147
if (fdtab[fd].state == FD_STCLOSE)
149
fdtab[fd].cb[DIR_WR].f(fd);
133
if ((fdtab[fd].state & FD_EV_STATUS_R))
134
fdtab[fd].ev |= FD_POLL_IN;
136
else if (kev[count].filter == EVFILT_WRITE) {
137
if ((fdtab[fd].state & FD_EV_STATUS_W))
138
fdtab[fd].ev |= FD_POLL_OUT;
141
fd_process_polled_events(fd);
160
150
REGPRM1 static int _do_init(struct poller *p)
162
__label__ fail_wevt, fail_revt, fail_fd;
165
152
p->private = NULL;
166
fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
168
154
kqueue_fd = kqueue();
169
155
if (kqueue_fd < 0)
172
kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents);
158
/* we can have up to two events per fd (*/
159
kev = (struct kevent*)calloc(1, sizeof(struct kevent) * 2 * global.maxsock);
177
if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
180
if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
186
free(fd_evts[DIR_RD]);
190
166
close(kqueue_fd);