]>
git.saurik.com Git - redis.git/blob - ae_select.c
1 /* Select()-based ae.c module
2 * Copyright (C) 2009 Salvatore Sanfilippo - antirez@gmail.com
3 * Released under the BSD license. See the COPYING file for more info. */
7 typedef struct aeApiState
{
9 /* We need to have a copy of the fd sets as it's not safe to reuse
10 * FD sets after select(). */
14 static int aeApiCreate(aeEventLoop
*eventLoop
) {
15 aeApiState
*state
= zmalloc(sizeof(aeApiState
));
17 if (!state
) return -1;
18 FD_ZERO(&state
->rfds
);
19 FD_ZERO(&state
->wfds
);
20 eventLoop
->apidata
= state
;
24 static void aeApiFree(aeEventLoop
*eventLoop
) {
25 zfree(eventLoop
->apidata
);
28 static int aeApiAddEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
29 aeApiState
*state
= eventLoop
->apidata
;
31 if (mask
& AE_READABLE
) FD_SET(fd
,&state
->rfds
);
32 if (mask
& AE_WRITABLE
) FD_SET(fd
,&state
->wfds
);
36 static void aeApiDelEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
37 aeApiState
*state
= eventLoop
->apidata
;
39 if (mask
& AE_READABLE
) FD_CLR(fd
,&state
->rfds
);
40 if (mask
& AE_WRITABLE
) FD_CLR(fd
,&state
->wfds
);
43 static int aeApiPoll(aeEventLoop
*eventLoop
, struct timeval
*tvp
) {
44 aeApiState
*state
= eventLoop
->apidata
;
45 int retval
, j
, numevents
= 0;
47 memcpy(&state
->_rfds
,&state
->rfds
,sizeof(fd_set
));
48 memcpy(&state
->_wfds
,&state
->wfds
,sizeof(fd_set
));
50 retval
= select(eventLoop
->maxfd
+1,
51 &state
->_rfds
,&state
->_wfds
,NULL
,tvp
);
53 for (j
= 0; j
<= eventLoop
->maxfd
; j
++) {
55 aeFileEvent
*fe
= &eventLoop
->events
[j
];
57 if (fe
->mask
== AE_NONE
) continue;
58 if (fe
->mask
& AE_READABLE
&& FD_ISSET(j
,&state
->_rfds
))
60 if (fe
->mask
& AE_WRITABLE
&& FD_ISSET(j
,&state
->_wfds
))
62 eventLoop
->fired
[numevents
].fd
= j
;
63 eventLoop
->fired
[numevents
].mask
= mask
;
70 static char *aeApiName(void) {