1 | /* |
1 | /* |
|
|
2 | * libev epoll fd activity backend |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
43 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
45 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
44 | |
46 | |
45 | epoll_ctl (epoll_fd, mode, fd, &ev); |
47 | epoll_ctl (epoll_fd, mode, fd, &ev); |
46 | } |
48 | } |
47 | |
49 | |
|
|
50 | static void |
48 | void epoll_postfork_child (void) |
51 | epoll_postfork_child (void) |
49 | { |
52 | { |
50 | int fd; |
53 | int fd; |
51 | |
54 | |
52 | epoll_fd = epoll_create (256); |
55 | epoll_fd = epoll_create (256); |
53 | fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); |
56 | fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); |
54 | |
57 | |
55 | /* re-register interest in fds */ |
58 | /* re-register interest in fds */ |
56 | for (fd = 0; fd < anfdmax; ++fd) |
59 | for (fd = 0; fd < anfdmax; ++fd) |
57 | if (anfds [fd].wev) |
60 | if (anfds [fd].events)//D |
58 | epoll_modify (fd, EV_NONE, anfds [fd].wev); |
61 | epoll_modify (fd, EV_NONE, anfds [fd].events); |
59 | } |
62 | } |
60 | |
63 | |
61 | static struct epoll_event *events; |
64 | static struct epoll_event *events; |
62 | static int eventmax; |
65 | static int eventmax; |
63 | |
66 | |
|
|
67 | static void |
64 | static void epoll_poll (ev_tstamp timeout) |
68 | epoll_poll (ev_tstamp timeout) |
65 | { |
69 | { |
66 | int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.)); |
70 | int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.)); |
67 | int i; |
71 | int i; |
68 | |
72 | |
69 | if (eventcnt < 0) |
73 | if (eventcnt < 0) |
… | |
… | |
75 | (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) |
79 | (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) |
76 | | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0) |
80 | | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0) |
77 | ); |
81 | ); |
78 | |
82 | |
79 | /* if the receive array was full, increase its size */ |
83 | /* if the receive array was full, increase its size */ |
80 | if (eventcnt == eventmax) |
84 | if (expect_false (eventcnt == eventmax)) |
81 | { |
85 | { |
82 | free (events); |
86 | free (events); |
83 | eventmax += eventmax >> 1; |
87 | eventmax = array_roundsize (events, eventmax << 1); |
84 | events = malloc (sizeof (struct epoll_event) * eventmax); |
88 | events = malloc (sizeof (struct epoll_event) * eventmax); |
85 | } |
89 | } |
86 | } |
90 | } |
87 | |
91 | |
|
|
92 | static void |
88 | void epoll_init (int flags) |
93 | epoll_init (int flags) |
89 | { |
94 | { |
90 | epoll_fd = epoll_create (256); |
95 | epoll_fd = epoll_create (256); |
91 | |
96 | |
92 | if (epoll_fd < 0) |
97 | if (epoll_fd < 0) |
93 | return; |
98 | return; |
… | |
… | |
100 | method_poll = epoll_poll; |
105 | method_poll = epoll_poll; |
101 | |
106 | |
102 | eventmax = 64; /* intiial number of events receivable per poll */ |
107 | eventmax = 64; /* intiial number of events receivable per poll */ |
103 | events = malloc (sizeof (struct epoll_event) * eventmax); |
108 | events = malloc (sizeof (struct epoll_event) * eventmax); |
104 | } |
109 | } |
|
|
110 | |