]>
git.saurik.com Git - redis.git/blob - src/ae_epoll.c
1 /* Linux epoll(2) based ae.c module
2 * Copyright (C) 2009-2010 Salvatore Sanfilippo - antirez@gmail.com
3 * Released under the BSD license. See the COPYING file for more info. */
7 typedef struct aeApiState
{
9 struct epoll_event events
[AE_SETSIZE
];
12 static int aeApiCreate(aeEventLoop
*eventLoop
) {
13 aeApiState
*state
= zmalloc(sizeof(aeApiState
));
15 if (!state
) return -1;
16 state
->epfd
= epoll_create(1024); /* 1024 is just an hint for the kernel */
17 if (state
->epfd
== -1) return -1;
18 eventLoop
->apidata
= state
;
22 static void aeApiFree(aeEventLoop
*eventLoop
) {
23 aeApiState
*state
= eventLoop
->apidata
;
29 static int aeApiAddEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
30 aeApiState
*state
= eventLoop
->apidata
;
31 struct epoll_event ee
;
32 /* If the fd was already monitored for some event, we need a MOD
33 * operation. Otherwise we need an ADD operation. */
34 int op
= eventLoop
->events
[fd
].mask
== AE_NONE
?
35 EPOLL_CTL_ADD
: EPOLL_CTL_MOD
;
38 mask
|= eventLoop
->events
[fd
].mask
; /* Merge old events */
39 if (mask
& AE_READABLE
) ee
.events
|= EPOLLIN
;
40 if (mask
& AE_WRITABLE
) ee
.events
|= EPOLLOUT
;
41 ee
.data
.u64
= 0; /* avoid valgrind warning */
43 if (epoll_ctl(state
->epfd
,op
,fd
,&ee
) == -1) return -1;
47 static void aeApiDelEvent(aeEventLoop
*eventLoop
, int fd
, int delmask
) {
48 aeApiState
*state
= eventLoop
->apidata
;
49 struct epoll_event ee
;
50 int mask
= eventLoop
->events
[fd
].mask
& (~delmask
);
53 if (mask
& AE_READABLE
) ee
.events
|= EPOLLIN
;
54 if (mask
& AE_WRITABLE
) ee
.events
|= EPOLLOUT
;
55 ee
.data
.u64
= 0; /* avoid valgrind warning */
57 if (mask
!= AE_NONE
) {
58 epoll_ctl(state
->epfd
,EPOLL_CTL_MOD
,fd
,&ee
);
60 /* Note, Kernel < 2.6.9 requires a non null event pointer even for
62 epoll_ctl(state
->epfd
,EPOLL_CTL_DEL
,fd
,&ee
);
66 static int aeApiPoll(aeEventLoop
*eventLoop
, struct timeval
*tvp
) {
67 aeApiState
*state
= eventLoop
->apidata
;
68 int retval
, numevents
= 0;
70 retval
= epoll_wait(state
->epfd
,state
->events
,AE_SETSIZE
,
71 tvp
? (tvp
->tv_sec
*1000 + tvp
->tv_usec
/1000) : -1);
76 for (j
= 0; j
< numevents
; j
++) {
78 struct epoll_event
*e
= state
->events
+j
;
80 if (e
->events
& EPOLLIN
) mask
|= AE_READABLE
;
81 if (e
->events
& EPOLLOUT
) mask
|= AE_WRITABLE
;
82 eventLoop
->fired
[j
].fd
= e
->data
.fd
;
83 eventLoop
->fired
[j
].mask
= mask
;
89 static char *aeApiName(void) {