ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_epoll.c
(Generate patch)

Comparing libev/ev_epoll.c (file contents):
Revision 1.48 by root, Sat Jul 25 10:14:35 2009 UTC vs.
Revision 1.54 by sf-exg, Fri Oct 22 10:50:24 2010 UTC

1/* 1/*
2 * libev epoll fd activity backend 2 * libev epoll fd activity backend
3 * 3 *
4 * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
47 * b) the fact that ADD != MOD creates a lot of extra syscalls due to a) 47 * b) the fact that ADD != MOD creates a lot of extra syscalls due to a)
48 * and seems not to have any advantage. 48 * and seems not to have any advantage.
49 * c) the inability to handle fork or file descriptors (think dup) 49 * c) the inability to handle fork or file descriptors (think dup)
50 * limits the applicability over poll, so this is not a generic 50 * limits the applicability over poll, so this is not a generic
51 * poll replacement. 51 * poll replacement.
52 * d) epoll doesn't work the same as select with many file descriptors
53 * (such as files). while not critical, no other advanced interface
54 * seems to share this (rather non-unixy) limitation.
55 * e) epoll claims to be embeddable, but in practise you never get
56 * a ready event for the epoll fd.
52 * 57 *
53 * lots of "weird code" and complication handling in this file is due 58 * lots of "weird code" and complication handling in this file is due
54 * to these design problems with epoll, as we try very hard to avoid 59 * to these design problems with epoll, as we try very hard to avoid
55 * epoll_ctl syscalls for common usage patterns and handle the breakage 60 * epoll_ctl syscalls for common usage patterns and handle the breakage
56 * ensuing from receiving events for closed and otherwise long gone 61 * ensuing from receiving events for closed and otherwise long gone
83 ev.data.u64 = (uint64_t)(uint32_t)fd 88 ev.data.u64 = (uint64_t)(uint32_t)fd
84 | ((uint64_t)(uint32_t)++anfds [fd].egen << 32); 89 | ((uint64_t)(uint32_t)++anfds [fd].egen << 32);
85 ev.events = (nev & EV_READ ? EPOLLIN : 0) 90 ev.events = (nev & EV_READ ? EPOLLIN : 0)
86 | (nev & EV_WRITE ? EPOLLOUT : 0); 91 | (nev & EV_WRITE ? EPOLLOUT : 0);
87 92
88 if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) 93 if (expect_true (!epoll_ctl (backend_fd, oev && oldmask != nev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev)))
89 return; 94 return;
90 95
91 if (expect_true (errno == ENOENT)) 96 if (expect_true (errno == ENOENT))
92 { 97 {
93 /* if ENOENT then the fd went away, so try to do the right thing */ 98 /* if ENOENT then the fd went away, so try to do the right thing */
143 int want = anfds [fd].events; 148 int want = anfds [fd].events;
144 int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) 149 int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
145 | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); 150 | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0);
146 151
147 /* check for spurious notification */ 152 /* check for spurious notification */
153 /* we assume that fd is always in range, as we never shrink the anfds array */
148 if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32))) 154 if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32)))
149 { 155 {
150 /* recreate kernel state */ 156 /* recreate kernel state */
151 postfork = 1; 157 postfork = 1;
152 continue; 158 continue;
159 /* we received an event but are not interested in it, try mod or del */ 165 /* we received an event but are not interested in it, try mod or del */
160 /* I don't think we ever need MOD, but let's handle it anyways */ 166 /* I don't think we ever need MOD, but let's handle it anyways */
161 ev->events = (want & EV_READ ? EPOLLIN : 0) 167 ev->events = (want & EV_READ ? EPOLLIN : 0)
162 | (want & EV_WRITE ? EPOLLOUT : 0); 168 | (want & EV_WRITE ? EPOLLOUT : 0);
163 169
170 /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */
171 /* which is fortunately easy to do for us. */
164 if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev)) 172 if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev))
165 { 173 {
166 postfork = 1; /* an error occured, recreate kernel state */ 174 postfork = 1; /* an error occurred, recreate kernel state */
167 continue; 175 continue;
168 } 176 }
169 } 177 }
170 178
171 fd_event (EV_A_ fd, got); 179 fd_event (EV_A_ fd, got);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines