]>
git.saurik.com Git - redis.git/blob - src/ae_select.c
1 /* Select()-based ae.c module.
3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
34 typedef struct aeApiState
{
36 /* We need to have a copy of the fd sets as it's not safe to reuse
37 * FD sets after select(). */
41 static int aeApiCreate(aeEventLoop
*eventLoop
) {
42 aeApiState
*state
= zmalloc(sizeof(aeApiState
));
44 if (!state
) return -1;
45 FD_ZERO(&state
->rfds
);
46 FD_ZERO(&state
->wfds
);
47 eventLoop
->apidata
= state
;
51 static void aeApiFree(aeEventLoop
*eventLoop
) {
52 zfree(eventLoop
->apidata
);
55 static int aeApiAddEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
56 aeApiState
*state
= eventLoop
->apidata
;
58 if (mask
& AE_READABLE
) FD_SET(fd
,&state
->rfds
);
59 if (mask
& AE_WRITABLE
) FD_SET(fd
,&state
->wfds
);
63 static void aeApiDelEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
64 aeApiState
*state
= eventLoop
->apidata
;
66 if (mask
& AE_READABLE
) FD_CLR(fd
,&state
->rfds
);
67 if (mask
& AE_WRITABLE
) FD_CLR(fd
,&state
->wfds
);
70 static int aeApiPoll(aeEventLoop
*eventLoop
, struct timeval
*tvp
) {
71 aeApiState
*state
= eventLoop
->apidata
;
72 int retval
, j
, numevents
= 0;
74 memcpy(&state
->_rfds
,&state
->rfds
,sizeof(fd_set
));
75 memcpy(&state
->_wfds
,&state
->wfds
,sizeof(fd_set
));
77 retval
= select(eventLoop
->maxfd
+1,
78 &state
->_rfds
,&state
->_wfds
,NULL
,tvp
);
80 for (j
= 0; j
<= eventLoop
->maxfd
; j
++) {
82 aeFileEvent
*fe
= &eventLoop
->events
[j
];
84 if (fe
->mask
== AE_NONE
) continue;
85 if (fe
->mask
& AE_READABLE
&& FD_ISSET(j
,&state
->_rfds
))
87 if (fe
->mask
& AE_WRITABLE
&& FD_ISSET(j
,&state
->_wfds
))
89 eventLoop
->fired
[numevents
].fd
= j
;
90 eventLoop
->fired
[numevents
].mask
= mask
;
97 static char *aeApiName(void) {