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 arraay */ |
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; |
… | |
… | |
163 | |
169 | |
164 | /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */ |
170 | /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */ |
165 | /* which is fortunately easy to do for us. */ |
171 | /* which is fortunately easy to do for us. */ |
166 | 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)) |
167 | { |
173 | { |
168 | postfork = 1; /* an error occured, recreate kernel state */ |
174 | postfork = 1; /* an error occurred, recreate kernel state */ |
169 | continue; |
175 | continue; |
170 | } |
176 | } |
171 | } |
177 | } |
172 | |
178 | |
173 | fd_event (EV_A_ fd, got); |
179 | fd_event (EV_A_ fd, got); |