Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 104 lines (88 sloc) 3.203 kb
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
1 /* Linux epoll(2) based ae.c module
12d090d Salvatore Sanfilippo A problem with replication with multiple slaves connectiong to a single ...
authored
2 * Copyright (C) 2009-2010 Salvatore Sanfilippo - antirez@gmail.com
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
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;
e074416 Salvatore Sanfilippo Max limit to 10k clients removed, this implements feature request on iss...
authored
9 struct epoll_event *events;
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
10 } aeApiState;
11
12 static int aeApiCreate(aeEventLoop *eventLoop) {
13 aeApiState *state = zmalloc(sizeof(aeApiState));
14
15 if (!state) return -1;
36dda95 Salvatore Sanfilippo ae_epoll.c typo introduced in the previous commit fixed.
authored
16 state->events = zmalloc(sizeof(struct epoll_event)*eventLoop->setsize);
e074416 Salvatore Sanfilippo Max limit to 10k clients removed, this implements feature request on iss...
authored
17 if (!state->events) {
18 zfree(state);
19 return -1;
20 }
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
21 state->epfd = epoll_create(1024); /* 1024 is just an hint for the kernel */
fb293cc Salvatore Sanfilippo Fixed a theoretical memory leak with no practical effects in ae_kqueue.c...
authored
22 if (state->epfd == -1) {
e074416 Salvatore Sanfilippo Max limit to 10k clients removed, this implements feature request on iss...
authored
23 zfree(state->events);
fb293cc Salvatore Sanfilippo Fixed a theoretical memory leak with no practical effects in ae_kqueue.c...
authored
24 zfree(state);
25 return -1;
26 }
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
27 eventLoop->apidata = state;
28 return 0;
29 }
30
31 static void aeApiFree(aeEventLoop *eventLoop) {
32 aeApiState *state = eventLoop->apidata;
33
34 close(state->epfd);
e074416 Salvatore Sanfilippo Max limit to 10k clients removed, this implements feature request on iss...
authored
35 zfree(state->events);
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
36 zfree(state);
37 }
38
39 static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
40 aeApiState *state = eventLoop->apidata;
41 struct epoll_event ee;
67669f0 Salvatore Sanfilippo Fixed a lame epoll issue
authored
42 /* If the fd was already monitored for some event, we need a MOD
43 * operation. Otherwise we need an ADD operation. */
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
44 int op = eventLoop->events[fd].mask == AE_NONE ?
45 EPOLL_CTL_ADD : EPOLL_CTL_MOD;
46
47 ee.events = 0;
67669f0 Salvatore Sanfilippo Fixed a lame epoll issue
authored
48 mask |= eventLoop->events[fd].mask; /* Merge old events */
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
49 if (mask & AE_READABLE) ee.events |= EPOLLIN;
50 if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
214d880 Salvatore Sanfilippo minor fix to avoid a false valgrind warning.
authored
51 ee.data.u64 = 0; /* avoid valgrind warning */
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
52 ee.data.fd = fd;
53 if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;
54 return 0;
55 }
56
57 static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) {
58 aeApiState *state = eventLoop->apidata;
59 struct epoll_event ee;
60 int mask = eventLoop->events[fd].mask & (~delmask);
61
62 ee.events = 0;
63 if (mask & AE_READABLE) ee.events |= EPOLLIN;
64 if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
214d880 Salvatore Sanfilippo minor fix to avoid a false valgrind warning.
authored
65 ee.data.u64 = 0; /* avoid valgrind warning */
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
66 ee.data.fd = fd;
67 if (mask != AE_NONE) {
68 epoll_ctl(state->epfd,EPOLL_CTL_MOD,fd,&ee);
69 } else {
70 /* Note, Kernel < 2.6.9 requires a non null event pointer even for
71 * EPOLL_CTL_DEL. */
72 epoll_ctl(state->epfd,EPOLL_CTL_DEL,fd,&ee);
73 }
74 }
75
76 static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
77 aeApiState *state = eventLoop->apidata;
78 int retval, numevents = 0;
79
e074416 Salvatore Sanfilippo Max limit to 10k clients removed, this implements feature request on iss...
authored
80 retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
81 tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
82 if (retval > 0) {
83 int j;
84
85 numevents = retval;
86 for (j = 0; j < numevents; j++) {
87 int mask = 0;
88 struct epoll_event *e = state->events+j;
89
90 if (e->events & EPOLLIN) mask |= AE_READABLE;
91 if (e->events & EPOLLOUT) mask |= AE_WRITABLE;
93b0075 jokea mark fd as writable when EPOLLERR or EPOLLHUP is returned by epoll_wait.
jokea authored
92 if (e->events & EPOLLERR) mask |= AE_WRITABLE;
93 if (e->events & EPOLLHUP) mask |= AE_WRITABLE;
64f36a5 Salvatore Sanfilippo epoll module for ae.c implemented. Some more testing needed
authored
94 eventLoop->fired[j].fd = e->data.fd;
95 eventLoop->fired[j].mask = mask;
96 }
97 }
98 return numevents;
99 }
7a932b7 Salvatore Sanfilippo ifdefs added to use kevent on Free Open and Net BSD as well. INFO and ae...
authored
100
101 static char *aeApiName(void) {
102 return "epoll";
103 }
Something went wrong with that request. Please try again.