Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 2 | * include/proto/fd.h |
| 3 | * File descriptors states. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_FD_H |
| 23 | #define _PROTO_FD_H |
| 24 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 30 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <types/fd.h> |
| 32 | |
| 33 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 34 | * The file descriptor is also closed. |
| 35 | */ |
| 36 | void fd_delete(int fd); |
| 37 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 38 | /* disable the specified poller */ |
| 39 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 41 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 42 | * Initialize the pollers till the best one is found. |
| 43 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 44 | * The pollers register themselves just before main() is called. |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 45 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 46 | int init_pollers(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 48 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 49 | * Deinitialize the pollers. |
| 50 | */ |
| 51 | void deinit_pollers(); |
| 52 | |
| 53 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 54 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 55 | * to create initialize part of them again. Returns 0 in case of failure, |
| 56 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 57 | * the the current poller is destroyed and the caller is responsible for trying |
| 58 | * another one by calling init_pollers() again. |
| 59 | */ |
| 60 | int fork_poller(); |
| 61 | |
| 62 | /* |
| 63 | * Lists the known pollers on <out>. |
| 64 | * Should be performed only before initialization. |
| 65 | */ |
| 66 | int list_pollers(FILE *out); |
| 67 | |
| 68 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 69 | * Runs the polling loop |
| 70 | */ |
| 71 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 72 | |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 73 | #define EV_FD_ISSET(fd, ev) (cur_poller.is_set((fd), (ev))) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 74 | |
| 75 | /* event manipulation primitives for use by I/O callbacks */ |
| 76 | static inline void fd_want_recv(int fd) |
| 77 | { |
| 78 | cur_poller.set(fd, DIR_RD); |
| 79 | } |
| 80 | |
| 81 | static inline void fd_stop_recv(int fd) |
| 82 | { |
| 83 | cur_poller.clr(fd, DIR_RD); |
| 84 | } |
| 85 | |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 86 | static inline void fd_poll_recv(int fd) |
| 87 | { |
| 88 | cur_poller.wai(fd, DIR_RD); |
| 89 | } |
| 90 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 91 | static inline void fd_want_send(int fd) |
| 92 | { |
| 93 | cur_poller.set(fd, DIR_WR); |
| 94 | } |
| 95 | |
| 96 | static inline void fd_stop_send(int fd) |
| 97 | { |
| 98 | cur_poller.clr(fd, DIR_WR); |
| 99 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 101 | static inline void fd_poll_send(int fd) |
| 102 | { |
| 103 | cur_poller.wai(fd, DIR_WR); |
| 104 | } |
| 105 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 106 | static inline void fd_stop_both(int fd) |
| 107 | { |
| 108 | cur_poller.rem(fd); |
| 109 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 111 | /* Prepares <fd> for being polled */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | static inline void fd_insert(int fd) |
| 113 | { |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 114 | fdtab[fd].ev = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | if (fd + 1 > maxfd) |
| 116 | maxfd = fd + 1; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | #endif /* _PROTO_FD_H */ |
| 121 | |
| 122 | /* |
| 123 | * Local variables: |
| 124 | * c-indent-level: 8 |
| 125 | * c-basic-offset: 8 |
| 126 | * End: |
| 127 | */ |