]>
Commit | Line | Data |
---|---|---|
64f36a58 | 1 | /* Linux epoll(2) 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. */ | |
4 | ||
5 | #include <sys/epoll.h> | |
6 | ||
7 | typedef struct aeApiState { | |
8 | int epfd; | |
9 | struct epoll_event events[AE_SETSIZE]; | |
10 | } aeApiState; | |
11 | ||
12 | static int aeApiCreate(aeEventLoop *eventLoop) { | |
13 | aeApiState *state = zmalloc(sizeof(aeApiState)); | |
14 | ||
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; | |
19 | return 0; | |
20 | } | |
21 | ||
22 | static void aeApiFree(aeEventLoop *eventLoop) { | |
23 | aeApiState *state = eventLoop->apidata; | |
24 | ||
25 | close(state->epfd); | |
26 | zfree(state); | |
27 | } | |
28 | ||
29 | static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) { | |
30 | aeApiState *state = eventLoop->apidata; | |
31 | struct epoll_event ee; | |
32 | int op = eventLoop->events[fd].mask == AE_NONE ? | |
33 | EPOLL_CTL_ADD : EPOLL_CTL_MOD; | |
34 | ||
35 | ee.events = 0; | |
36 | if (mask & AE_READABLE) ee.events |= EPOLLIN; | |
37 | if (mask & AE_WRITABLE) ee.events |= EPOLLOUT; | |
38 | if (mask & AE_EXCEPTION) ee.events |= EPOLLPRI; | |
39 | ee.data.fd = fd; | |
40 | if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1; | |
41 | return 0; | |
42 | } | |
43 | ||
44 | static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) { | |
45 | aeApiState *state = eventLoop->apidata; | |
46 | struct epoll_event ee; | |
47 | int mask = eventLoop->events[fd].mask & (~delmask); | |
48 | ||
49 | ee.events = 0; | |
50 | if (mask & AE_READABLE) ee.events |= EPOLLIN; | |
51 | if (mask & AE_WRITABLE) ee.events |= EPOLLOUT; | |
52 | if (mask & AE_EXCEPTION) ee.events |= EPOLLPRI; | |
53 | ee.data.fd = fd; | |
54 | if (mask != AE_NONE) { | |
55 | epoll_ctl(state->epfd,EPOLL_CTL_MOD,fd,&ee); | |
56 | } else { | |
57 | /* Note, Kernel < 2.6.9 requires a non null event pointer even for | |
58 | * EPOLL_CTL_DEL. */ | |
59 | epoll_ctl(state->epfd,EPOLL_CTL_DEL,fd,&ee); | |
60 | } | |
61 | } | |
62 | ||
63 | static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) { | |
64 | aeApiState *state = eventLoop->apidata; | |
65 | int retval, numevents = 0; | |
66 | ||
67 | retval = epoll_wait(state->epfd,state->events,AE_SETSIZE, | |
68 | tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1); | |
69 | if (retval > 0) { | |
70 | int j; | |
71 | ||
72 | numevents = retval; | |
73 | for (j = 0; j < numevents; j++) { | |
74 | int mask = 0; | |
75 | struct epoll_event *e = state->events+j; | |
76 | ||
77 | if (e->events & EPOLLIN) mask |= AE_READABLE; | |
78 | if (e->events & EPOLLOUT) mask |= AE_WRITABLE; | |
79 | if (e->events & EPOLLPRI) mask |= AE_EXCEPTION; | |
80 | eventLoop->fired[j].fd = e->data.fd; | |
81 | eventLoop->fired[j].mask = mask; | |
82 | } | |
83 | } | |
84 | return numevents; | |
85 | } |