| 97 | |
| 98 | |
| 99 | Future<short> poll(int_fd fd, short events) |
| 100 | { |
| 101 | Poll* poll = new Poll(); |
| 102 | |
| 103 | // Have the watchers data point back to the struct. |
| 104 | poll->watcher.async->data = poll; |
| 105 | poll->watcher.io->data = poll; |
| 106 | |
| 107 | // Get a copy of the future to avoid any races with the event loop. |
| 108 | Future<short> future = poll->promise.future(); |
| 109 | |
| 110 | // Initialize and start the async watcher. |
| 111 | ev_async_init(poll->watcher.async.get(), discard_poll); |
| 112 | ev_async_start(loop, poll->watcher.async.get()); |
| 113 | |
| 114 | // Make sure we stop polling if a discard occurs on our future. |
| 115 | // Note that it's possible that we'll invoke '_poll' when someone |
| 116 | // does a discard even after the polling has already completed, but |
| 117 | // in this case while we will interrupt the event loop since the |
| 118 | // async watcher has already been stopped we won't cause |
| 119 | // 'discard_poll' to get invoked. |
| 120 | future.onDiscard(lambda::bind(&_poll, poll->watcher.async)); |
| 121 | |
| 122 | // Initialize and start the I/O watcher. |
| 123 | ev_io_init(poll->watcher.io.get(), polled, fd, events); |
| 124 | ev_io_start(loop, poll->watcher.io.get()); |
| 125 | |
| 126 | return future; |
| 127 | } |
| 128 | |
| 129 | } // namespace internal { |
| 130 | |