REORG/MEDIUM: fd: remove FD_STCLOSE from struct fdtab

In an attempt to get rid of fdtab[].state, and to move the relevant
parts to the connection struct, we remove the FD_STCLOSE state which
can easily be deduced from the <owner> pointer as there is a 1:1 match.
This commit is contained in:
Willy Tarreau 2012-07-05 23:19:22 +02:00
parent 801a0a353a
commit db3b32610f
9 changed files with 18 additions and 24 deletions

View File

@ -32,7 +32,6 @@
#include <types/protocols.h> #include <types/protocols.h>
/* different possible states for the fd */ /* different possible states for the fd */
#define FD_STCLOSE 0
#define FD_STLISTEN 1 #define FD_STLISTEN 1
#define FD_STCONN 2 #define FD_STCONN 2
#define FD_STREADY 3 #define FD_STREADY 3
@ -70,7 +69,7 @@ struct fdtab {
struct { struct {
int (*f)(int fd); /* read/write function */ int (*f)(int fd); /* read/write function */
} cb[DIR_SIZE]; } cb[DIR_SIZE];
void *owner; /* the session (or proxy) associated with this fd */ void *owner; /* the session (or proxy) associated with this fd, NULL if closed */
struct { /* used by pollers which support speculative polling */ struct { /* used by pollers which support speculative polling */
unsigned char e; /* read and write events status. 4 bits, may be merged into flags' lower bits */ unsigned char e; /* read and write events status. 4 bits, may be merged into flags' lower bits */
unsigned int s1; /* Position in spec list+1. 0=not in list. */ unsigned int s1; /* Position in spec list+1. 0=not in list. */

View File

@ -257,14 +257,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp)
((e & EPOLLHUP) ? FD_POLL_HUP : 0); ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) { if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
fdtab[fd].cb[DIR_RD].f(fd); fdtab[fd].cb[DIR_RD].f(fd);
} }
if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) { if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP))
fdtab[fd].cb[DIR_WR].f(fd); fdtab[fd].cb[DIR_WR].f(fd);

View File

@ -140,14 +140,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp)
fd = kev[count].ident; fd = kev[count].ident;
if (kev[count].filter == EVFILT_READ) { if (kev[count].filter == EVFILT_READ) {
if (FD_ISSET(fd, fd_evts[DIR_RD])) { if (FD_ISSET(fd, fd_evts[DIR_RD])) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
fdtab[fd].ev |= FD_POLL_IN; fdtab[fd].ev |= FD_POLL_IN;
fdtab[fd].cb[DIR_RD].f(fd); fdtab[fd].cb[DIR_RD].f(fd);
} }
} else if (kev[count].filter == EVFILT_WRITE) { } else if (kev[count].filter == EVFILT_WRITE) {
if (FD_ISSET(fd, fd_evts[DIR_WR])) { if (FD_ISSET(fd, fd_evts[DIR_WR])) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
fdtab[fd].ev |= FD_POLL_OUT; fdtab[fd].ev |= FD_POLL_OUT;
fdtab[fd].cb[DIR_WR].f(fd); fdtab[fd].cb[DIR_WR].f(fd);

View File

@ -160,14 +160,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp)
status--; status--;
if (FD_ISSET(fd, fd_evts[DIR_RD])) { if (FD_ISSET(fd, fd_evts[DIR_RD])) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
fdtab[fd].cb[DIR_RD].f(fd); fdtab[fd].cb[DIR_RD].f(fd);
} }
if (FD_ISSET(fd, fd_evts[DIR_WR])) { if (FD_ISSET(fd, fd_evts[DIR_WR])) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP))
fdtab[fd].cb[DIR_WR].f(fd); fdtab[fd].cb[DIR_WR].f(fd);

View File

@ -147,14 +147,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp)
* seen first. Moreover, system buffers will be flushed faster. * seen first. Moreover, system buffers will be flushed faster.
*/ */
if (FD_ISSET(fd, tmp_evts[DIR_RD])) { if (FD_ISSET(fd, tmp_evts[DIR_RD])) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
fdtab[fd].ev |= FD_POLL_IN; fdtab[fd].ev |= FD_POLL_IN;
fdtab[fd].cb[DIR_RD].f(fd); fdtab[fd].cb[DIR_RD].f(fd);
} }
if (FD_ISSET(fd, tmp_evts[DIR_WR])) { if (FD_ISSET(fd, tmp_evts[DIR_WR])) {
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
fdtab[fd].ev |= FD_POLL_OUT; fdtab[fd].ev |= FD_POLL_OUT;
fdtab[fd].cb[DIR_WR].f(fd); fdtab[fd].cb[DIR_WR].f(fd);

View File

@ -202,7 +202,7 @@ REGPRM2 static int __fd_is_set(const int fd, int dir)
int ret; int ret;
#if DEBUG_DEV #if DEBUG_DEV
if (fdtab[fd].state == FD_STCLOSE) { if (!fdtab[fd].owner) {
fprintf(stderr, "sepoll.fd_isset called on closed fd #%d.\n", fd); fprintf(stderr, "sepoll.fd_isset called on closed fd #%d.\n", fd);
ABORT_NOW(); ABORT_NOW();
} }
@ -220,7 +220,7 @@ REGPRM2 static int __fd_set(const int fd, int dir)
unsigned int i; unsigned int i;
#if DEBUG_DEV #if DEBUG_DEV
if (fdtab[fd].state == FD_STCLOSE) { if (!fdtab[fd].owner) {
fprintf(stderr, "sepoll.fd_set called on closed fd #%d.\n", fd); fprintf(stderr, "sepoll.fd_set called on closed fd #%d.\n", fd);
ABORT_NOW(); ABORT_NOW();
} }
@ -242,7 +242,7 @@ REGPRM2 static int __fd_clr(const int fd, int dir)
unsigned int i; unsigned int i;
#if DEBUG_DEV #if DEBUG_DEV
if (fdtab[fd].state == FD_STCLOSE) { if (!fdtab[fd].owner) {
fprintf(stderr, "sepoll.fd_clr called on closed fd #%d.\n", fd); fprintf(stderr, "sepoll.fd_clr called on closed fd #%d.\n", fd);
ABORT_NOW(); ABORT_NOW();
} }
@ -410,14 +410,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp)
((e & EPOLLHUP) ? FD_POLL_HUP : 0); ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
if ((fdtab[fd].spec.e & FD_EV_MASK_R) == FD_EV_WAIT_R) { if ((fdtab[fd].spec.e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) if (!fdtab[fd].owner || fdtab[fd].state == FD_STERROR)
continue; continue;
if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
fdtab[fd].cb[DIR_RD].f(fd); fdtab[fd].cb[DIR_RD].f(fd);
} }
if ((fdtab[fd].spec.e & FD_EV_MASK_W) == FD_EV_WAIT_W) { if ((fdtab[fd].spec.e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) if (!fdtab[fd].owner || fdtab[fd].state == FD_STERROR)
continue; continue;
if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR)) if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR))
fdtab[fd].cb[DIR_WR].f(fd); fdtab[fd].cb[DIR_WR].f(fd);
@ -471,7 +471,7 @@ REGPRM2 static void _do_poll(struct poller *p, int exp)
} }
/* one callback might already have closed the fd by itself */ /* one callback might already have closed the fd by itself */
if (fdtab[fd].state == FD_STCLOSE) if (!fdtab[fd].owner)
continue; continue;
if (!(fdtab[fd].spec.e & (FD_EV_RW_SL|FD_EV_RW_PL))) { if (!(fdtab[fd].spec.e & (FD_EV_RW_SL|FD_EV_RW_PL))) {

View File

@ -41,9 +41,9 @@ void fd_delete(int fd)
port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
fdinfo[fd].port_range = NULL; fdinfo[fd].port_range = NULL;
close(fd); close(fd);
fdtab[fd].state = FD_STCLOSE; fdtab[fd].owner = NULL;
while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) while ((maxfd-1 >= 0) && !fdtab[maxfd-1].owner)
maxfd--; maxfd--;
} }

View File

@ -351,7 +351,6 @@ void dump(struct sig_handler *sh)
*/ */
void init(int argc, char **argv) void init(int argc, char **argv)
{ {
int i;
int arg_mode = 0; /* MODE_DEBUG, ... */ int arg_mode = 0; /* MODE_DEBUG, ... */
char *tmp; char *tmp;
char *cfg_pidfile = NULL; char *cfg_pidfile = NULL;
@ -691,10 +690,6 @@ void init(int argc, char **argv)
sizeof(struct fdinfo) * (global.maxsock)); sizeof(struct fdinfo) * (global.maxsock));
fdtab = (struct fdtab *)calloc(1, fdtab = (struct fdtab *)calloc(1,
sizeof(struct fdtab) * (global.maxsock)); sizeof(struct fdtab) * (global.maxsock));
for (i = 0; i < global.maxsock; i++) {
fdtab[i].state = FD_STCLOSE;
}
/* /*
* Note: we could register external pollers here. * Note: we could register external pollers here.
* Built-in pollers have been registered before main(). * Built-in pollers have been registered before main().

View File

@ -320,7 +320,7 @@ int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL); send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
} }
if (fdtab[cfd].state != FD_STCLOSE) if (fdtab[cfd].owner)
fd_delete(cfd); fd_delete(cfd);
else else
close(cfd); close(cfd);