a51dcb8b |
1 | /* Select()-based ae.c module |
12d090d2 |
2 | * Copyright (C) 2009-2010 Salvatore Sanfilippo - antirez@gmail.com |
a51dcb8b |
3 | * Released under the BSD license. See the COPYING file for more info. */ |
4 | |
5 | #include <string.h> |
6 | |
7 | typedef struct aeApiState { |
621d5c19 |
8 | fd_set rfds, wfds; |
a51dcb8b |
9 | /* We need to have a copy of the fd sets as it's not safe to reuse |
10 | * FD sets after select(). */ |
621d5c19 |
11 | fd_set _rfds, _wfds; |
a51dcb8b |
12 | } aeApiState; |
13 | |
14 | static int aeApiCreate(aeEventLoop *eventLoop) { |
15 | aeApiState *state = zmalloc(sizeof(aeApiState)); |
16 | |
17 | if (!state) return -1; |
18 | FD_ZERO(&state->rfds); |
19 | FD_ZERO(&state->wfds); |
a51dcb8b |
20 | eventLoop->apidata = state; |
21 | return 0; |
22 | } |
23 | |
24 | static void aeApiFree(aeEventLoop *eventLoop) { |
25 | zfree(eventLoop->apidata); |
26 | } |
27 | |
28 | static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) { |
29 | aeApiState *state = eventLoop->apidata; |
30 | |
31 | if (mask & AE_READABLE) FD_SET(fd,&state->rfds); |
32 | if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds); |
a51dcb8b |
33 | return 0; |
34 | } |
35 | |
36 | static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) { |
37 | aeApiState *state = eventLoop->apidata; |
38 | |
39 | if (mask & AE_READABLE) FD_CLR(fd,&state->rfds); |
40 | if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds); |
a51dcb8b |
41 | } |
42 | |
43 | static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) { |
44 | aeApiState *state = eventLoop->apidata; |
45 | int retval, j, numevents = 0; |
46 | |
47 | memcpy(&state->_rfds,&state->rfds,sizeof(fd_set)); |
48 | memcpy(&state->_wfds,&state->wfds,sizeof(fd_set)); |
a51dcb8b |
49 | |
50 | retval = select(eventLoop->maxfd+1, |
621d5c19 |
51 | &state->_rfds,&state->_wfds,NULL,tvp); |
a51dcb8b |
52 | if (retval > 0) { |
53 | for (j = 0; j <= eventLoop->maxfd; j++) { |
54 | int mask = 0; |
55 | aeFileEvent *fe = &eventLoop->events[j]; |
56 | |
57 | if (fe->mask == AE_NONE) continue; |
58 | if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds)) |
59 | mask |= AE_READABLE; |
60 | if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds)) |
61 | mask |= AE_WRITABLE; |
a51dcb8b |
62 | eventLoop->fired[numevents].fd = j; |
63 | eventLoop->fired[numevents].mask = mask; |
64 | numevents++; |
65 | } |
66 | } |
67 | return numevents; |
68 | } |
7a932b74 |
69 | |
70 | static char *aeApiName(void) { |
71 | return "select"; |
72 | } |