4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/socket.h>
38 #include <sys/select.h>
40 #include <sys/types.h>
48 * Returns the index in the fdset for a given fd.
49 * If fd is -1, it means to search for a free entry.
51 * index for the fd, or -1 if fd isn't in the fdset.
54 fdset_find_fd(struct fdset *pfdset, int fd)
61 for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++)
64 return i == MAX_FDS ? -1 : i;
68 fdset_find_free_slot(struct fdset *pfdset)
70 return fdset_find_fd(pfdset, -1);
74 fdset_add_fd(struct fdset *pfdset, int idx, int fd,
75 fd_cb rcb, fd_cb wcb, void *dat)
77 struct fdentry *pfdentry;
79 if (pfdset == NULL || idx >= MAX_FDS)
82 pfdentry = &pfdset->fd[idx];
90 * Fill the read/write fd_set with the fds in the fdset.
92 * the maximum fds filled in the read/write fd_set.
95 fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
97 struct fdentry *pfdentry;
104 for (i = 0; i < num; i++) {
105 pfdentry = &pfdset->fd[i];
106 if (pfdentry->fd != -1) {
108 if (pfdentry->rcb && rfset) {
109 FD_SET(pfdentry->fd, rfset);
112 if (pfdentry->wcb && wfset) {
113 FD_SET(pfdentry->fd, wfset);
117 maxfds = pfdentry->fd < maxfds ?
118 maxfds : pfdentry->fd;
125 fdset_init(struct fdset *pfdset)
132 for (i = 0; i < MAX_FDS; i++)
133 pfdset->fd[i].fd = -1;
138 * Register the fd in the fdset with read/write handler and context.
141 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
145 if (pfdset == NULL || fd == -1)
148 /* Find a free slot in the list. */
149 i = fdset_find_free_slot(pfdset);
153 fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
160 * Unregister the fd from the fdset.
163 fdset_del(struct fdset *pfdset, int fd)
167 i = fdset_find_fd(pfdset, fd);
168 if (i != -1 && fd != -1) {
169 pfdset->fd[i].fd = -1;
170 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
176 * This functions runs in infinite blocking loop until there is no fd in
177 * pfdset. It calls corresponding r/w handler if there is event on the fd.
180 fdset_event_dispatch(struct fdset *pfdset)
184 struct fdentry *pfdentry;
193 maxfds = fdset_fill(&rfds, &wfds, pfdset);
197 select(maxfds + 1, &rfds, &wfds, NULL, NULL);
199 for (i = 0; i < num; i++) {
200 pfdentry = &pfdset->fd[i];
201 if (pfdentry->fd >= 0 && FD_ISSET(pfdentry->fd, &rfds) && pfdentry->rcb)
202 pfdentry->rcb(pfdentry->fd, pfdentry->dat);
203 if (pfdentry->fd >= 0 && FD_ISSET(pfdentry->fd, &wfds) && pfdentry->wcb)
204 pfdentry->wcb(pfdentry->fd, pfdentry->dat);