1 | /* |
1 | /* |
2 | * libev poll fd activity backend |
2 | * libev poll fd activity backend |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2016,2019 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 | * |
… | |
… | |
39 | |
39 | |
40 | #include <poll.h> |
40 | #include <poll.h> |
41 | |
41 | |
42 | inline_size |
42 | inline_size |
43 | void |
43 | void |
44 | pollidx_init (int *base, int count) |
44 | array_needsize_pollidx (int *base, int offset, int count) |
45 | { |
45 | { |
46 | /* consider using memset (.., -1, ...), which is practically guaranteed |
46 | /* using memset (.., -1, ...) is tempting, we we try |
47 | * to work on all systems implementing poll */ |
47 | * to be ultraportable |
|
|
48 | */ |
|
|
49 | base += offset; |
48 | while (count--) |
50 | while (count--) |
49 | *base++ = -1; |
51 | *base++ = -1; |
50 | } |
52 | } |
51 | |
53 | |
52 | static void |
54 | static void |
… | |
… | |
55 | int idx; |
57 | int idx; |
56 | |
58 | |
57 | if (oev == nev) |
59 | if (oev == nev) |
58 | return; |
60 | return; |
59 | |
61 | |
60 | array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); |
62 | array_needsize (int, pollidxs, pollidxmax, fd + 1, array_needsize_pollidx); |
61 | |
63 | |
62 | idx = pollidxs [fd]; |
64 | idx = pollidxs [fd]; |
63 | |
65 | |
64 | if (idx < 0) /* need to allocate a new pollfd */ |
66 | if (idx < 0) /* need to allocate a new pollfd */ |
65 | { |
67 | { |
66 | pollidxs [fd] = idx = pollcnt++; |
68 | pollidxs [fd] = idx = pollcnt++; |
67 | array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2); |
69 | array_needsize (struct pollfd, polls, pollmax, pollcnt, array_needsize_noinit); |
68 | polls [idx].fd = fd; |
70 | polls [idx].fd = fd; |
69 | } |
71 | } |
70 | |
72 | |
71 | assert (polls [idx].fd == fd); |
73 | assert (polls [idx].fd == fd); |
72 | |
74 | |
… | |
… | |
76 | | (nev & EV_WRITE ? POLLOUT : 0); |
78 | | (nev & EV_WRITE ? POLLOUT : 0); |
77 | else /* remove pollfd */ |
79 | else /* remove pollfd */ |
78 | { |
80 | { |
79 | pollidxs [fd] = -1; |
81 | pollidxs [fd] = -1; |
80 | |
82 | |
81 | if (expect_true (idx < --pollcnt)) |
83 | if (ecb_expect_true (idx < --pollcnt)) |
82 | { |
84 | { |
83 | polls [idx] = polls [pollcnt]; |
85 | polls [idx] = polls [pollcnt]; |
84 | pollidxs [polls [idx].fd] = idx; |
86 | pollidxs [polls [idx].fd] = idx; |
85 | } |
87 | } |
86 | } |
88 | } |
… | |
… | |
91 | { |
93 | { |
92 | struct pollfd *p; |
94 | struct pollfd *p; |
93 | int res; |
95 | int res; |
94 | |
96 | |
95 | EV_RELEASE_CB; |
97 | EV_RELEASE_CB; |
96 | res = poll (polls, pollcnt, timeout * 1e3); |
98 | res = poll (polls, pollcnt, EV_TS_TO_MS (timeout)); |
97 | EV_ACQUIRE_CB; |
99 | EV_ACQUIRE_CB; |
98 | |
100 | |
99 | if (expect_false (res < 0)) |
101 | if (ecb_expect_false (res < 0)) |
100 | { |
102 | { |
101 | if (errno == EBADF) |
103 | if (errno == EBADF) |
102 | fd_ebadf (EV_A); |
104 | fd_ebadf (EV_A); |
103 | else if (errno == ENOMEM && !syserr_cb) |
105 | else if (errno == ENOMEM && !syserr_cb) |
104 | fd_enomem (EV_A); |
106 | fd_enomem (EV_A); |
… | |
… | |
106 | ev_syserr ("(libev) poll"); |
108 | ev_syserr ("(libev) poll"); |
107 | } |
109 | } |
108 | else |
110 | else |
109 | for (p = polls; res; ++p) |
111 | for (p = polls; res; ++p) |
110 | { |
112 | { |
111 | assert (("libev: poll() returned illegal result, broken BSD kernel?", p < polls + pollcnt)); |
113 | assert (("libev: poll returned illegal result, broken BSD kernel?", p < polls + pollcnt)); |
112 | |
114 | |
113 | if (expect_false (p->revents)) /* this expect is debatable */ |
115 | if (ecb_expect_false (p->revents)) /* this expect is debatable */ |
114 | { |
116 | { |
115 | --res; |
117 | --res; |
116 | |
118 | |
117 | if (expect_false (p->revents & POLLNVAL)) |
119 | if (ecb_expect_false (p->revents & POLLNVAL)) |
|
|
120 | { |
|
|
121 | assert (("libev: poll found invalid fd in poll set", 0)); |
118 | fd_kill (EV_A_ p->fd); |
122 | fd_kill (EV_A_ p->fd); |
|
|
123 | } |
119 | else |
124 | else |
120 | fd_event ( |
125 | fd_event ( |
121 | EV_A_ |
126 | EV_A_ |
122 | p->fd, |
127 | p->fd, |
123 | (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
128 | (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |