]> git.saurik.com Git - redis.git/blob - src/ae_select.c
Query the archive to provide a complete KEYS list.
[redis.git] / src / ae_select.c
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 <string.h>
33
34 typedef struct aeApiState {
35 fd_set rfds, wfds;
36 /* We need to have a copy of the fd sets as it's not safe to reuse
37 * FD sets after select(). */
38 fd_set _rfds, _wfds;
39 } aeApiState;
40
41 static int aeApiCreate(aeEventLoop *eventLoop) {
42 aeApiState *state = zmalloc(sizeof(aeApiState));
43
44 if (!state) return -1;
45 FD_ZERO(&state->rfds);
46 FD_ZERO(&state->wfds);
47 eventLoop->apidata = state;
48 return 0;
49 }
50
51 static void aeApiFree(aeEventLoop *eventLoop) {
52 zfree(eventLoop->apidata);
53 }
54
55 static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
56 aeApiState *state = eventLoop->apidata;
57
58 if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
59 if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
60 return 0;
61 }
62
63 static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
64 aeApiState *state = eventLoop->apidata;
65
66 if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);
67 if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);
68 }
69
70 static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
71 aeApiState *state = eventLoop->apidata;
72 int retval, j, numevents = 0;
73
74 memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
75 memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));
76
77 retval = select(eventLoop->maxfd+1,
78 &state->_rfds,&state->_wfds,NULL,tvp);
79 if (retval > 0) {
80 for (j = 0; j <= eventLoop->maxfd; j++) {
81 int mask = 0;
82 aeFileEvent *fe = &eventLoop->events[j];
83
84 if (fe->mask == AE_NONE) continue;
85 if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
86 mask |= AE_READABLE;
87 if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
88 mask |= AE_WRITABLE;
89 eventLoop->fired[numevents].fd = j;
90 eventLoop->fired[numevents].mask = mask;
91 numevents++;
92 }
93 }
94 return numevents;
95 }
96
97 static char *aeApiName(void) {
98 return "select";
99 }