MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / aeApiPoll

Function aeApiPoll

src/ae_kqueue.c:131–183  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129}
130
131static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
132 aeApiState *state = (aeApiState*)eventLoop->apidata;
133 int retval, numevents = 0;
134
135 if (tvp != NULL) {
136 struct timespec timeout;
137 timeout.tv_sec = tvp->tv_sec;
138 timeout.tv_nsec = tvp->tv_usec * 1000;
139 retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
140 &timeout);
141 } else {
142 retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
143 NULL);
144 }
145
146 if (retval > 0) {
147 int j;
148
149 /* Normally we execute the read event first and then the write event.
150 * When the barrier is set, we will do it reverse.
151 *
152 * However, under kqueue, read and write events would be separate
153 * events, which would make it impossible to control the order of
154 * reads and writes. So we store the event's mask we've got and merge
155 * the same fd events later. */
156 for (j = 0; j < retval; j++) {
157 struct kevent *e = state->events+j;
158 int fd = e->ident;
159 int mask = 0;
160
161 if (e->filter == EVFILT_READ) mask = AE_READABLE;
162 else if (e->filter == EVFILT_WRITE) mask = AE_WRITABLE;
163 addEventMask(state->eventsMask, fd, mask);
164 }
165
166 /* Re-traversal to merge read and write events, and set the fd's mask to
167 * 0 so that events are not added again when the fd is encountered again. */
168 numevents = 0;
169 for (j = 0; j < retval; j++) {
170 struct kevent *e = state->events+j;
171 int fd = e->ident;
172 int mask = getEventMask(state->eventsMask, fd);
173
174 if (mask) {
175 eventLoop->fired[numevents].fd = fd;
176 eventLoop->fired[numevents].mask = mask;
177 resetEventMask(state->eventsMask, fd);
178 numevents++;
179 }
180 }
181 }
182 return numevents;
183}
184
185static const char *aeApiName(void) {
186 return "kqueue";

Callers

nothing calls this directly

Calls 4

addEventMaskFunction · 0.85
getEventMaskFunction · 0.85
resetEventMaskFunction · 0.85
keventClass · 0.70

Tested by

no test coverage detected