| 116 | } |
| 117 | |
| 118 | void* epoll_thread(void* arg) { |
| 119 | bthread_usleep(1); |
| 120 | EpollMeta* m = (EpollMeta*)arg; |
| 121 | const int epfd = m->epfd; |
| 122 | #if defined(OS_LINUX) |
| 123 | epoll_event e[32]; |
| 124 | #elif defined(OS_MACOSX) |
| 125 | struct kevent e[32]; |
| 126 | #endif |
| 127 | |
| 128 | while (!stop) { |
| 129 | |
| 130 | #if defined(OS_LINUX) |
| 131 | # ifndef USE_BLOCKING_EPOLL |
| 132 | const int n = epoll_wait(epfd, e, ARRAY_SIZE(e), 0); |
| 133 | if (stop) { |
| 134 | break; |
| 135 | } |
| 136 | if (n == 0) { |
| 137 | bthread_fd_wait(epfd, EPOLLIN); |
| 138 | continue; |
| 139 | } |
| 140 | # else |
| 141 | const int n = epoll_wait(epfd, e, ARRAY_SIZE(e), -1); |
| 142 | if (stop) { |
| 143 | break; |
| 144 | } |
| 145 | if (n == 0) { |
| 146 | continue; |
| 147 | } |
| 148 | # endif |
| 149 | #elif defined(OS_MACOSX) |
| 150 | const int n = kevent(epfd, NULL, 0, e, ARRAY_SIZE(e), NULL); |
| 151 | if (stop) { |
| 152 | break; |
| 153 | } |
| 154 | if (n == 0) { |
| 155 | continue; |
| 156 | } |
| 157 | #endif |
| 158 | if (n < 0) { |
| 159 | if (EINTR == errno) { |
| 160 | continue; |
| 161 | } |
| 162 | #if defined(OS_LINUX) |
| 163 | PLOG(FATAL) << "Fail to epoll_wait"; |
| 164 | #elif defined(OS_MACOSX) |
| 165 | PLOG(FATAL) << "Fail to kevent"; |
| 166 | #endif |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | #ifdef CREATE_THREAD_TO_PROCESS |
| 171 | bthread_fvec vec[n]; |
| 172 | for (int i = 0; i < n; ++i) { |
| 173 | vec[i].fn = process_thread; |
| 174 | # if defined(OS_LINUX) |
| 175 | vec[i].arg = e[i].data.ptr; |
nothing calls this directly
no test coverage detected