1 /* Select()-based ae.c module.
2  *
3  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   * Neither the name of Redis nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 #include <sys/select.h>
33 #include <string.h>
34 
35 typedef struct aeApiState {
36     fd_set rfds, wfds;
37     /* We need to have a copy of the fd sets as it's not safe to reuse
38      * FD sets after select(). */
39     fd_set _rfds, _wfds;
40 } aeApiState;
41 
aeApiCreate(aeEventLoop * eventLoop)42 static int aeApiCreate(aeEventLoop *eventLoop) {
43     aeApiState *state = zmalloc(sizeof(aeApiState));
44 
45     if (!state) return -1;
46     FD_ZERO(&state->rfds);
47     FD_ZERO(&state->wfds);
48     eventLoop->apidata = state;
49     return 0;
50 }
51 
aeApiResize(aeEventLoop * eventLoop,int setsize)52 static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
53     /* Just ensure we have enough room in the fd_set type. */
54     if (setsize >= FD_SETSIZE) return -1;
55     return 0;
56 }
57 
aeApiFree(aeEventLoop * eventLoop)58 static void aeApiFree(aeEventLoop *eventLoop) {
59     zfree(eventLoop->apidata);
60 }
61 
aeApiAddEvent(aeEventLoop * eventLoop,int fd,int mask)62 static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
63     aeApiState *state = eventLoop->apidata;
64 
65     if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
66     if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
67     return 0;
68 }
69 
aeApiDelEvent(aeEventLoop * eventLoop,int fd,int mask)70 static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
71     aeApiState *state = eventLoop->apidata;
72 
73     if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);
74     if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);
75 }
76 
aeApiPoll(aeEventLoop * eventLoop,struct timeval * tvp)77 static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
78     aeApiState *state = eventLoop->apidata;
79     int retval, j, numevents = 0;
80 
81     memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
82     memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));
83 
84     retval = select(eventLoop->maxfd+1,
85                 &state->_rfds,&state->_wfds,NULL,tvp);
86     if (retval > 0) {
87         for (j = 0; j <= eventLoop->maxfd; j++) {
88             int mask = 0;
89             aeFileEvent *fe = &eventLoop->events[j];
90 
91             if (fe->mask == AE_NONE) continue;
92             if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
93                 mask |= AE_READABLE;
94             if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
95                 mask |= AE_WRITABLE;
96             eventLoop->fired[numevents].fd = j;
97             eventLoop->fired[numevents].mask = mask;
98             numevents++;
99         }
100     }
101     return numevents;
102 }
103 
aeApiName(void)104 static char *aeApiName(void) {
105     return "select";
106 }
107