Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 106 lines (89 sloc) 3.804 kb
4365e5b @antirez BSD license added to every C source and header file.
authored
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
a51dcb8 @antirez ae_select module added
authored
31
32 #include <string.h>
33
34 typedef struct aeApiState {
621d5c1 @antirez ae.c event loop does no longer support exception notifications, as th…
authored
35 fd_set rfds, wfds;
a51dcb8 @antirez ae_select module added
authored
36 /* We need to have a copy of the fd sets as it's not safe to reuse
37 * FD sets after select(). */
621d5c1 @antirez ae.c event loop does no longer support exception notifications, as th…
authored
38 fd_set _rfds, _wfds;
a51dcb8 @antirez ae_select module added
authored
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
8e2d082 @antirez ae.c event loop: API to resize the fd set size on the run.
authored
51 static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
52 /* Just ensure we have enough room in the fd_set type. */
53 if (setsize >= FD_SETSIZE) return -1;
54 return 0;
55 }
56
a51dcb8 @antirez ae_select module added
authored
57 static void aeApiFree(aeEventLoop *eventLoop) {
58 zfree(eventLoop->apidata);
59 }
60
61 static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
62 aeApiState *state = eventLoop->apidata;
63
64 if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
65 if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
66 return 0;
67 }
68
69 static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
70 aeApiState *state = eventLoop->apidata;
71
72 if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);
73 if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);
74 }
75
76 static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
77 aeApiState *state = eventLoop->apidata;
78 int retval, j, numevents = 0;
79
80 memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
81 memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));
82
83 retval = select(eventLoop->maxfd+1,
621d5c1 @antirez ae.c event loop does no longer support exception notifications, as th…
authored
84 &state->_rfds,&state->_wfds,NULL,tvp);
a51dcb8 @antirez ae_select module added
authored
85 if (retval > 0) {
86 for (j = 0; j <= eventLoop->maxfd; j++) {
87 int mask = 0;
88 aeFileEvent *fe = &eventLoop->events[j];
89
90 if (fe->mask == AE_NONE) continue;
91 if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
92 mask |= AE_READABLE;
93 if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
94 mask |= AE_WRITABLE;
95 eventLoop->fired[numevents].fd = j;
96 eventLoop->fired[numevents].mask = mask;
97 numevents++;
98 }
99 }
100 return numevents;
101 }
7a932b7 @antirez ifdefs added to use kevent on Free Open and Net BSD as well. INFO and…
authored
102
103 static char *aeApiName(void) {
104 return "select";
105 }
Something went wrong with that request. Please try again.