… | |
… | |
40 | ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */ |
40 | ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */ |
41 | ev.events = |
41 | ev.events = |
42 | (nev & EV_READ ? EPOLLIN : 0) |
42 | (nev & EV_READ ? EPOLLIN : 0) |
43 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
43 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
44 | |
44 | |
45 | epoll_ctl (epoll_fd, mode, fd, &ev); |
45 | if (epoll_ctl (backend_fd, mode, fd, &ev)) |
46 | } |
46 | if (errno != ENOENT /* on ENOENT the fd went away, so try to do the right thing */ |
47 | |
47 | || (nev && epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev))) |
48 | static void |
48 | fd_kill (EV_A_ fd); |
49 | epoll_postfork_child (EV_P) |
|
|
50 | { |
|
|
51 | int fd; |
|
|
52 | |
|
|
53 | epoll_fd = epoll_create (256); |
|
|
54 | fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); |
|
|
55 | |
|
|
56 | /* re-register interest in fds */ |
|
|
57 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
58 | if (anfds [fd].events)//D |
|
|
59 | epoll_modify (EV_A_ fd, EV_NONE, anfds [fd].events); |
|
|
60 | } |
49 | } |
61 | |
50 | |
62 | static void |
51 | static void |
63 | epoll_poll (EV_P_ ev_tstamp timeout) |
52 | epoll_poll (EV_P_ ev_tstamp timeout) |
64 | { |
53 | { |
65 | int eventcnt = epoll_wait (epoll_fd, epoll_events, epoll_eventmax, ceil (timeout * 1000.)); |
|
|
66 | int i; |
54 | int i; |
|
|
55 | int eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.)); |
67 | |
56 | |
68 | if (eventcnt < 0) |
57 | if (eventcnt < 0) |
|
|
58 | { |
|
|
59 | if (errno != EINTR) |
|
|
60 | syserr ("(libev) epoll_wait"); |
|
|
61 | |
69 | return; |
62 | return; |
|
|
63 | } |
70 | |
64 | |
71 | for (i = 0; i < eventcnt; ++i) |
65 | for (i = 0; i < eventcnt; ++i) |
72 | fd_event ( |
66 | fd_event ( |
73 | EV_A_ |
67 | EV_A_ |
74 | epoll_events [i].data.u64, |
68 | epoll_events [i].data.u64, |
… | |
… | |
77 | ); |
71 | ); |
78 | |
72 | |
79 | /* if the receive array was full, increase its size */ |
73 | /* if the receive array was full, increase its size */ |
80 | if (expect_false (eventcnt == epoll_eventmax)) |
74 | if (expect_false (eventcnt == epoll_eventmax)) |
81 | { |
75 | { |
82 | free (epoll_events); |
76 | ev_free (epoll_events); |
83 | epoll_eventmax = array_roundsize (epoll_events, epoll_eventmax << 1); |
77 | epoll_eventmax = array_roundsize (epoll_events, epoll_eventmax << 1); |
84 | epoll_events = malloc (sizeof (struct epoll_event) * epoll_eventmax); |
78 | epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); |
85 | } |
79 | } |
86 | } |
80 | } |
87 | |
81 | |
88 | static int |
82 | static int |
89 | epoll_init (EV_P_ int flags) |
83 | epoll_init (EV_P_ int flags) |
90 | { |
84 | { |
91 | epoll_fd = epoll_create (256); |
85 | backend_fd = epoll_create (256); |
92 | |
86 | |
93 | if (epoll_fd < 0) |
87 | if (backend_fd < 0) |
94 | return 0; |
88 | return 0; |
95 | |
89 | |
96 | fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); |
90 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
97 | |
91 | |
98 | method_fudge = 1e-3; /* needed to compensate for epoll returning early */ |
92 | backend_fudge = 1e-3; /* needed to compensate for epoll returning early */ |
99 | method_modify = epoll_modify; |
93 | backend_modify = epoll_modify; |
100 | method_poll = epoll_poll; |
94 | backend_poll = epoll_poll; |
101 | |
95 | |
102 | epoll_eventmax = 64; /* intiial number of events receivable per poll */ |
96 | epoll_eventmax = 64; /* intiial number of events receivable per poll */ |
103 | epoll_events = malloc (sizeof (struct epoll_event) * epoll_eventmax); |
97 | epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); |
104 | |
98 | |
105 | return EVMETHOD_EPOLL; |
99 | return EVBACKEND_EPOLL; |
106 | } |
100 | } |
107 | |
101 | |
|
|
102 | static void |
|
|
103 | epoll_destroy (EV_P) |
|
|
104 | { |
|
|
105 | close (backend_fd); |
|
|
106 | |
|
|
107 | ev_free (epoll_events); |
|
|
108 | } |
|
|
109 | |
|
|
110 | static void |
|
|
111 | epoll_fork (EV_P) |
|
|
112 | { |
|
|
113 | close (backend_fd); |
|
|
114 | |
|
|
115 | while ((backend_fd = epoll_create (256)) < 0) |
|
|
116 | syserr ("(libev) epoll_create"); |
|
|
117 | |
|
|
118 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
|
|
119 | |
|
|
120 | fd_rearm_all (EV_A); |
|
|
121 | } |
|
|
122 | |