| 122 | } |
| 123 | |
| 124 | int start(int epoll_size) { |
| 125 | if (started()) { |
| 126 | return -1; |
| 127 | } |
| 128 | _start_mutex.lock(); |
| 129 | // Double check |
| 130 | if (started()) { |
| 131 | _start_mutex.unlock(); |
| 132 | return -1; |
| 133 | } |
| 134 | #if defined(OS_LINUX) |
| 135 | _epfd = epoll_create(epoll_size); |
| 136 | #elif defined(OS_MACOSX) |
| 137 | _epfd = kqueue(); |
| 138 | #endif |
| 139 | _start_mutex.unlock(); |
| 140 | if (_epfd < 0) { |
| 141 | PLOG(FATAL) << "Fail to epoll_create/kqueue"; |
| 142 | return -1; |
| 143 | } |
| 144 | bthread_attr_t attr = BTHREAD_ATTR_NORMAL; |
| 145 | bthread_attr_set_name(&attr, "EpollThread::run_this"); |
| 146 | if (bthread_start_background( |
| 147 | &_tid, &attr, EpollThread::run_this, this) != 0) { |
| 148 | close(_epfd); |
| 149 | _epfd = -1; |
| 150 | LOG(FATAL) << "Fail to create epoll bthread"; |
| 151 | return -1; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | // Note: This function does not wake up suspended fd_wait. This is fine |
| 157 | // since stop_and_join is only called on program's termination |
no test coverage detected