1f37b6182SDimitry Andric //===-- MainLoop.cpp --------------------------------------------*- C++ -*-===//
2f37b6182SDimitry Andric //
3f37b6182SDimitry Andric // The LLVM Compiler Infrastructure
4f37b6182SDimitry Andric //
5f37b6182SDimitry Andric // This file is distributed under the University of Illinois Open Source
6f37b6182SDimitry Andric // License. See LICENSE.TXT for details.
7f37b6182SDimitry Andric //
8f37b6182SDimitry Andric //===----------------------------------------------------------------------===//
9f37b6182SDimitry Andric
10f37b6182SDimitry Andric #include "llvm/Config/llvm-config.h"
11f37b6182SDimitry Andric
12f37b6182SDimitry Andric #include "lldb/Host/MainLoop.h"
13b40b48b8SDimitry Andric #include "lldb/Host/PosixApi.h"
145517e702SDimitry Andric #include "lldb/Utility/Status.h"
15f37b6182SDimitry Andric #include <algorithm>
16f37b6182SDimitry Andric #include <cassert>
17f37b6182SDimitry Andric #include <cerrno>
18f37b6182SDimitry Andric #include <csignal>
19f37b6182SDimitry Andric #include <time.h>
205517e702SDimitry Andric #include <vector>
21f37b6182SDimitry Andric
220f5676f4SDimitry Andric // Multiplexing is implemented using kqueue on systems that support it (BSD
230f5676f4SDimitry Andric // variants including OSX). On linux we use ppoll, while android uses pselect
240f5676f4SDimitry Andric // (ppoll is present but not implemented properly). On windows we use WSApoll
250f5676f4SDimitry Andric // (which does not support signals).
260f5676f4SDimitry Andric
27f37b6182SDimitry Andric #if HAVE_SYS_EVENT_H
28f37b6182SDimitry Andric #include <sys/event.h>
294ba319b5SDimitry Andric #elif defined(_WIN32)
30f37b6182SDimitry Andric #include <winsock2.h>
31acac075bSDimitry Andric #elif defined(__ANDROID__)
32acac075bSDimitry Andric #include <sys/syscall.h>
33f37b6182SDimitry Andric #else
34f37b6182SDimitry Andric #include <poll.h>
35f37b6182SDimitry Andric #endif
36f37b6182SDimitry Andric
374ba319b5SDimitry Andric #ifdef _WIN32
38f37b6182SDimitry Andric #define POLL WSAPoll
39f37b6182SDimitry Andric #else
40f37b6182SDimitry Andric #define POLL poll
41f37b6182SDimitry Andric #endif
42f37b6182SDimitry Andric
43f37b6182SDimitry Andric #if SIGNAL_POLLING_UNSUPPORTED
444ba319b5SDimitry Andric #ifdef _WIN32
45f37b6182SDimitry Andric typedef int sigset_t;
46f37b6182SDimitry Andric typedef int siginfo_t;
47f37b6182SDimitry Andric #endif
48f37b6182SDimitry Andric
ppoll(struct pollfd * fds,size_t nfds,const struct timespec * timeout_ts,const sigset_t *)49f37b6182SDimitry Andric int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout_ts,
50f37b6182SDimitry Andric const sigset_t *) {
51f37b6182SDimitry Andric int timeout =
52f37b6182SDimitry Andric (timeout_ts == nullptr)
53f37b6182SDimitry Andric ? -1
54f37b6182SDimitry Andric : (timeout_ts->tv_sec * 1000 + timeout_ts->tv_nsec / 1000000);
55f37b6182SDimitry Andric return POLL(fds, nfds, timeout);
56f37b6182SDimitry Andric }
57f37b6182SDimitry Andric
58f37b6182SDimitry Andric #endif
59f37b6182SDimitry Andric
60f37b6182SDimitry Andric using namespace lldb;
61f37b6182SDimitry Andric using namespace lldb_private;
62f37b6182SDimitry Andric
63f37b6182SDimitry Andric static sig_atomic_t g_signal_flags[NSIG];
64f37b6182SDimitry Andric
SignalHandler(int signo,siginfo_t * info,void *)65f37b6182SDimitry Andric static void SignalHandler(int signo, siginfo_t *info, void *) {
66f37b6182SDimitry Andric assert(signo < NSIG);
67f37b6182SDimitry Andric g_signal_flags[signo] = 1;
68f37b6182SDimitry Andric }
69f37b6182SDimitry Andric
70f37b6182SDimitry Andric class MainLoop::RunImpl {
71f37b6182SDimitry Andric public:
720f5676f4SDimitry Andric RunImpl(MainLoop &loop);
730f5676f4SDimitry Andric ~RunImpl() = default;
74f37b6182SDimitry Andric
755517e702SDimitry Andric Status Poll();
760f5676f4SDimitry Andric void ProcessEvents();
77f37b6182SDimitry Andric
78f37b6182SDimitry Andric private:
79f37b6182SDimitry Andric MainLoop &loop;
80f37b6182SDimitry Andric
81f37b6182SDimitry Andric #if HAVE_SYS_EVENT_H
82f37b6182SDimitry Andric std::vector<struct kevent> in_events;
83f37b6182SDimitry Andric struct kevent out_events[4];
84f37b6182SDimitry Andric int num_events = -1;
85f37b6182SDimitry Andric
86f37b6182SDimitry Andric #else
87acac075bSDimitry Andric #ifdef __ANDROID__
88f37b6182SDimitry Andric fd_set read_fd_set;
89f37b6182SDimitry Andric #else
90f37b6182SDimitry Andric std::vector<struct pollfd> read_fds;
91f37b6182SDimitry Andric #endif
92f37b6182SDimitry Andric
93f37b6182SDimitry Andric sigset_t get_sigmask();
94f37b6182SDimitry Andric #endif
95f37b6182SDimitry Andric };
96f37b6182SDimitry Andric
97f37b6182SDimitry Andric #if HAVE_SYS_EVENT_H
RunImpl(MainLoop & loop)980f5676f4SDimitry Andric MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) {
990f5676f4SDimitry Andric in_events.reserve(loop.m_read_fds.size());
100f37b6182SDimitry Andric }
101f37b6182SDimitry Andric
Poll()1025517e702SDimitry Andric Status MainLoop::RunImpl::Poll() {
1030f5676f4SDimitry Andric in_events.resize(loop.m_read_fds.size());
104f37b6182SDimitry Andric unsigned i = 0;
105f37b6182SDimitry Andric for (auto &fd : loop.m_read_fds)
106f37b6182SDimitry Andric EV_SET(&in_events[i++], fd.first, EVFILT_READ, EV_ADD, 0, 0, 0);
107f37b6182SDimitry Andric
1080f5676f4SDimitry Andric num_events = kevent(loop.m_kqueue, in_events.data(), in_events.size(),
1090f5676f4SDimitry Andric out_events, llvm::array_lengthof(out_events), nullptr);
110f37b6182SDimitry Andric
111*b5893f02SDimitry Andric if (num_events < 0) {
112*b5893f02SDimitry Andric if (errno == EINTR) {
113*b5893f02SDimitry Andric // in case of EINTR, let the main loop run one iteration
114*b5893f02SDimitry Andric // we need to zero num_events to avoid assertions failing
115*b5893f02SDimitry Andric num_events = 0;
116*b5893f02SDimitry Andric } else
117*b5893f02SDimitry Andric return Status(errno, eErrorTypePOSIX);
118*b5893f02SDimitry Andric }
1195517e702SDimitry Andric return Status();
120f37b6182SDimitry Andric }
121f37b6182SDimitry Andric
ProcessEvents()1220f5676f4SDimitry Andric void MainLoop::RunImpl::ProcessEvents() {
123f37b6182SDimitry Andric assert(num_events >= 0);
124f37b6182SDimitry Andric for (int i = 0; i < num_events; ++i) {
125f37b6182SDimitry Andric if (loop.m_terminate_request)
126f37b6182SDimitry Andric return;
1270f5676f4SDimitry Andric switch (out_events[i].filter) {
1280f5676f4SDimitry Andric case EVFILT_READ:
1290f5676f4SDimitry Andric loop.ProcessReadObject(out_events[i].ident);
1300f5676f4SDimitry Andric break;
1310f5676f4SDimitry Andric case EVFILT_SIGNAL:
1320f5676f4SDimitry Andric loop.ProcessSignal(out_events[i].ident);
1330f5676f4SDimitry Andric break;
1340f5676f4SDimitry Andric default:
1350f5676f4SDimitry Andric llvm_unreachable("Unknown event");
136f37b6182SDimitry Andric }
137f37b6182SDimitry Andric }
1380f5676f4SDimitry Andric }
139f37b6182SDimitry Andric #else
RunImpl(MainLoop & loop)1400f5676f4SDimitry Andric MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) {
141acac075bSDimitry Andric #ifndef __ANDROID__
1420f5676f4SDimitry Andric read_fds.reserve(loop.m_read_fds.size());
1430f5676f4SDimitry Andric #endif
144f37b6182SDimitry Andric }
145f37b6182SDimitry Andric
get_sigmask()146f37b6182SDimitry Andric sigset_t MainLoop::RunImpl::get_sigmask() {
147f37b6182SDimitry Andric #if SIGNAL_POLLING_UNSUPPORTED
148f37b6182SDimitry Andric return 0;
149f37b6182SDimitry Andric #else
150f37b6182SDimitry Andric sigset_t sigmask;
151f37b6182SDimitry Andric int ret = pthread_sigmask(SIG_SETMASK, nullptr, &sigmask);
152f37b6182SDimitry Andric assert(ret == 0);
153f37b6182SDimitry Andric (void) ret;
154f37b6182SDimitry Andric
1550f5676f4SDimitry Andric for (const auto &sig : loop.m_signals)
156f37b6182SDimitry Andric sigdelset(&sigmask, sig.first);
157f37b6182SDimitry Andric return sigmask;
158f37b6182SDimitry Andric #endif
159f37b6182SDimitry Andric }
160f37b6182SDimitry Andric
161acac075bSDimitry Andric #ifdef __ANDROID__
Poll()1625517e702SDimitry Andric Status MainLoop::RunImpl::Poll() {
163acac075bSDimitry Andric // ppoll(2) is not supported on older all android versions. Also, older
164acac075bSDimitry Andric // versions android (API <= 19) implemented pselect in a non-atomic way, as a
165acac075bSDimitry Andric // combination of pthread_sigmask and select. This is not sufficient for us,
166acac075bSDimitry Andric // as we rely on the atomicity to correctly implement signal polling, so we
167acac075bSDimitry Andric // call the underlying syscall ourselves.
168acac075bSDimitry Andric
169f37b6182SDimitry Andric FD_ZERO(&read_fd_set);
170f37b6182SDimitry Andric int nfds = 0;
171f37b6182SDimitry Andric for (const auto &fd : loop.m_read_fds) {
172f37b6182SDimitry Andric FD_SET(fd.first, &read_fd_set);
173f37b6182SDimitry Andric nfds = std::max(nfds, fd.first + 1);
174f37b6182SDimitry Andric }
175f37b6182SDimitry Andric
176acac075bSDimitry Andric union {
177acac075bSDimitry Andric sigset_t set;
178acac075bSDimitry Andric uint64_t pad;
179acac075bSDimitry Andric } kernel_sigset;
180acac075bSDimitry Andric memset(&kernel_sigset, 0, sizeof(kernel_sigset));
181acac075bSDimitry Andric kernel_sigset.set = get_sigmask();
182acac075bSDimitry Andric
183acac075bSDimitry Andric struct {
184acac075bSDimitry Andric void *sigset_ptr;
185acac075bSDimitry Andric size_t sigset_len;
186acac075bSDimitry Andric } extra_data = {&kernel_sigset, sizeof(kernel_sigset)};
187acac075bSDimitry Andric if (syscall(__NR_pselect6, nfds, &read_fd_set, nullptr, nullptr, nullptr,
188acac075bSDimitry Andric &extra_data) == -1 &&
189f37b6182SDimitry Andric errno != EINTR)
1905517e702SDimitry Andric return Status(errno, eErrorTypePOSIX);
191f37b6182SDimitry Andric
1925517e702SDimitry Andric return Status();
193f37b6182SDimitry Andric }
194f37b6182SDimitry Andric #else
Poll()1955517e702SDimitry Andric Status MainLoop::RunImpl::Poll() {
196f37b6182SDimitry Andric read_fds.clear();
197f37b6182SDimitry Andric
198f37b6182SDimitry Andric sigset_t sigmask = get_sigmask();
199f37b6182SDimitry Andric
200f37b6182SDimitry Andric for (const auto &fd : loop.m_read_fds) {
201f37b6182SDimitry Andric struct pollfd pfd;
202f37b6182SDimitry Andric pfd.fd = fd.first;
203f37b6182SDimitry Andric pfd.events = POLLIN;
204f37b6182SDimitry Andric pfd.revents = 0;
205f37b6182SDimitry Andric read_fds.push_back(pfd);
206f37b6182SDimitry Andric }
207f37b6182SDimitry Andric
208f37b6182SDimitry Andric if (ppoll(read_fds.data(), read_fds.size(), nullptr, &sigmask) == -1 &&
209f37b6182SDimitry Andric errno != EINTR)
2105517e702SDimitry Andric return Status(errno, eErrorTypePOSIX);
211f37b6182SDimitry Andric
2125517e702SDimitry Andric return Status();
213f37b6182SDimitry Andric }
2140f5676f4SDimitry Andric #endif
215f37b6182SDimitry Andric
ProcessEvents()2160f5676f4SDimitry Andric void MainLoop::RunImpl::ProcessEvents() {
217acac075bSDimitry Andric #ifdef __ANDROID__
2184ba319b5SDimitry Andric // Collect first all readable file descriptors into a separate vector and
2194ba319b5SDimitry Andric // then iterate over it to invoke callbacks. Iterating directly over
220c4394386SDimitry Andric // loop.m_read_fds is not possible because the callbacks can modify the
221c4394386SDimitry Andric // container which could invalidate the iterator.
222c4394386SDimitry Andric std::vector<IOObject::WaitableHandle> fds;
223c4394386SDimitry Andric for (const auto &fd : loop.m_read_fds)
224c4394386SDimitry Andric if (FD_ISSET(fd.first, &read_fd_set))
225c4394386SDimitry Andric fds.push_back(fd.first);
226c4394386SDimitry Andric
227c4394386SDimitry Andric for (const auto &handle : fds) {
2280f5676f4SDimitry Andric #else
229f37b6182SDimitry Andric for (const auto &fd : read_fds) {
230acac075bSDimitry Andric if ((fd.revents & (POLLIN | POLLHUP)) == 0)
231f37b6182SDimitry Andric continue;
2320f5676f4SDimitry Andric IOObject::WaitableHandle handle = fd.fd;
2330f5676f4SDimitry Andric #endif
234f37b6182SDimitry Andric if (loop.m_terminate_request)
235f37b6182SDimitry Andric return;
236f37b6182SDimitry Andric
2370f5676f4SDimitry Andric loop.ProcessReadObject(handle);
2380f5676f4SDimitry Andric }
2390f5676f4SDimitry Andric
240c4394386SDimitry Andric std::vector<int> signals;
241c4394386SDimitry Andric for (const auto &entry : loop.m_signals)
242c4394386SDimitry Andric if (g_signal_flags[entry.first] != 0)
243c4394386SDimitry Andric signals.push_back(entry.first);
244c4394386SDimitry Andric
245c4394386SDimitry Andric for (const auto &signal : signals) {
2460f5676f4SDimitry Andric if (loop.m_terminate_request)
2470f5676f4SDimitry Andric return;
248c4394386SDimitry Andric g_signal_flags[signal] = 0;
249c4394386SDimitry Andric loop.ProcessSignal(signal);
250f37b6182SDimitry Andric }
251f37b6182SDimitry Andric }
252f37b6182SDimitry Andric #endif
253f37b6182SDimitry Andric
2540f5676f4SDimitry Andric MainLoop::MainLoop() {
2550f5676f4SDimitry Andric #if HAVE_SYS_EVENT_H
2560f5676f4SDimitry Andric m_kqueue = kqueue();
2570f5676f4SDimitry Andric assert(m_kqueue >= 0);
2580f5676f4SDimitry Andric #endif
2590f5676f4SDimitry Andric }
260f37b6182SDimitry Andric MainLoop::~MainLoop() {
2610f5676f4SDimitry Andric #if HAVE_SYS_EVENT_H
2620f5676f4SDimitry Andric close(m_kqueue);
2630f5676f4SDimitry Andric #endif
264f37b6182SDimitry Andric assert(m_read_fds.size() == 0);
265f37b6182SDimitry Andric assert(m_signals.size() == 0);
266f37b6182SDimitry Andric }
267f37b6182SDimitry Andric
2685517e702SDimitry Andric MainLoop::ReadHandleUP MainLoop::RegisterReadObject(const IOObjectSP &object_sp,
2695517e702SDimitry Andric const Callback &callback,
2705517e702SDimitry Andric Status &error) {
2714ba319b5SDimitry Andric #ifdef _WIN32
272f37b6182SDimitry Andric if (object_sp->GetFdType() != IOObject:: eFDTypeSocket) {
273f37b6182SDimitry Andric error.SetErrorString("MainLoop: non-socket types unsupported on Windows");
274f37b6182SDimitry Andric return nullptr;
275f37b6182SDimitry Andric }
276f37b6182SDimitry Andric #endif
277f37b6182SDimitry Andric if (!object_sp || !object_sp->IsValid()) {
278f37b6182SDimitry Andric error.SetErrorString("IO object is not valid.");
279f37b6182SDimitry Andric return nullptr;
280f37b6182SDimitry Andric }
281f37b6182SDimitry Andric
282f37b6182SDimitry Andric const bool inserted =
283f37b6182SDimitry Andric m_read_fds.insert({object_sp->GetWaitableHandle(), callback}).second;
284f37b6182SDimitry Andric if (!inserted) {
285f37b6182SDimitry Andric error.SetErrorStringWithFormat("File descriptor %d already monitored.",
286f37b6182SDimitry Andric object_sp->GetWaitableHandle());
287f37b6182SDimitry Andric return nullptr;
288f37b6182SDimitry Andric }
289f37b6182SDimitry Andric
290f37b6182SDimitry Andric return CreateReadHandle(object_sp);
291f37b6182SDimitry Andric }
292f37b6182SDimitry Andric
293f37b6182SDimitry Andric // We shall block the signal, then install the signal handler. The signal will
2944ba319b5SDimitry Andric // be unblocked in the Run() function to check for signal delivery.
295f37b6182SDimitry Andric MainLoop::SignalHandleUP
2965517e702SDimitry Andric MainLoop::RegisterSignal(int signo, const Callback &callback, Status &error) {
297f37b6182SDimitry Andric #ifdef SIGNAL_POLLING_UNSUPPORTED
298f37b6182SDimitry Andric error.SetErrorString("Signal polling is not supported on this platform.");
299f37b6182SDimitry Andric return nullptr;
300f37b6182SDimitry Andric #else
301f37b6182SDimitry Andric if (m_signals.find(signo) != m_signals.end()) {
302f37b6182SDimitry Andric error.SetErrorStringWithFormat("Signal %d already monitored.", signo);
303f37b6182SDimitry Andric return nullptr;
304f37b6182SDimitry Andric }
305f37b6182SDimitry Andric
306f37b6182SDimitry Andric SignalInfo info;
307f37b6182SDimitry Andric info.callback = callback;
308f37b6182SDimitry Andric struct sigaction new_action;
309f37b6182SDimitry Andric new_action.sa_sigaction = &SignalHandler;
310f37b6182SDimitry Andric new_action.sa_flags = SA_SIGINFO;
311f37b6182SDimitry Andric sigemptyset(&new_action.sa_mask);
312f37b6182SDimitry Andric sigaddset(&new_action.sa_mask, signo);
313f37b6182SDimitry Andric sigset_t old_set;
314f37b6182SDimitry Andric
315f37b6182SDimitry Andric g_signal_flags[signo] = 0;
316f37b6182SDimitry Andric
3170f5676f4SDimitry Andric // Even if using kqueue, the signal handler will still be invoked, so it's
318*b5893f02SDimitry Andric // important to replace it with our "benign" handler.
3190f5676f4SDimitry Andric int ret = sigaction(signo, &new_action, &info.old_action);
3200f5676f4SDimitry Andric assert(ret == 0 && "sigaction failed");
3210f5676f4SDimitry Andric
3220f5676f4SDimitry Andric #if HAVE_SYS_EVENT_H
3230f5676f4SDimitry Andric struct kevent ev;
3240f5676f4SDimitry Andric EV_SET(&ev, signo, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
3250f5676f4SDimitry Andric ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr);
3260f5676f4SDimitry Andric assert(ret == 0);
3270f5676f4SDimitry Andric #endif
3280f5676f4SDimitry Andric
3294ba319b5SDimitry Andric // If we're using kqueue, the signal needs to be unblocked in order to
330*b5893f02SDimitry Andric // receive it. If using pselect/ppoll, we need to block it, and later unblock
3314ba319b5SDimitry Andric // it as a part of the system call.
3320f5676f4SDimitry Andric ret = pthread_sigmask(HAVE_SYS_EVENT_H ? SIG_UNBLOCK : SIG_BLOCK,
3330f5676f4SDimitry Andric &new_action.sa_mask, &old_set);
3340f5676f4SDimitry Andric assert(ret == 0 && "pthread_sigmask failed");
3350f5676f4SDimitry Andric info.was_blocked = sigismember(&old_set, signo);
3360f5676f4SDimitry Andric m_signals.insert({signo, info});
3370f5676f4SDimitry Andric
338f37b6182SDimitry Andric return SignalHandleUP(new SignalHandle(*this, signo));
339f37b6182SDimitry Andric #endif
340f37b6182SDimitry Andric }
341f37b6182SDimitry Andric
342f37b6182SDimitry Andric void MainLoop::UnregisterReadObject(IOObject::WaitableHandle handle) {
343f37b6182SDimitry Andric bool erased = m_read_fds.erase(handle);
344f37b6182SDimitry Andric UNUSED_IF_ASSERT_DISABLED(erased);
345f37b6182SDimitry Andric assert(erased);
346f37b6182SDimitry Andric }
347f37b6182SDimitry Andric
348f37b6182SDimitry Andric void MainLoop::UnregisterSignal(int signo) {
349f37b6182SDimitry Andric #if SIGNAL_POLLING_UNSUPPORTED
3505517e702SDimitry Andric Status("Signal polling is not supported on this platform.");
351f37b6182SDimitry Andric #else
352f37b6182SDimitry Andric auto it = m_signals.find(signo);
353f37b6182SDimitry Andric assert(it != m_signals.end());
354f37b6182SDimitry Andric
355f37b6182SDimitry Andric sigaction(signo, &it->second.old_action, nullptr);
356f37b6182SDimitry Andric
357f37b6182SDimitry Andric sigset_t set;
358f37b6182SDimitry Andric sigemptyset(&set);
359f37b6182SDimitry Andric sigaddset(&set, signo);
3600f5676f4SDimitry Andric int ret = pthread_sigmask(it->second.was_blocked ? SIG_BLOCK : SIG_UNBLOCK,
3610f5676f4SDimitry Andric &set, nullptr);
3620f5676f4SDimitry Andric assert(ret == 0);
3630f5676f4SDimitry Andric (void)ret;
3640f5676f4SDimitry Andric
3650f5676f4SDimitry Andric #if HAVE_SYS_EVENT_H
3660f5676f4SDimitry Andric struct kevent ev;
3670f5676f4SDimitry Andric EV_SET(&ev, signo, EVFILT_SIGNAL, EV_DELETE, 0, 0, 0);
3680f5676f4SDimitry Andric ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr);
3690f5676f4SDimitry Andric assert(ret == 0);
3700f5676f4SDimitry Andric #endif
371f37b6182SDimitry Andric
372f37b6182SDimitry Andric m_signals.erase(it);
373f37b6182SDimitry Andric #endif
374f37b6182SDimitry Andric }
375f37b6182SDimitry Andric
3765517e702SDimitry Andric Status MainLoop::Run() {
377f37b6182SDimitry Andric m_terminate_request = false;
378f37b6182SDimitry Andric
3795517e702SDimitry Andric Status error;
3800f5676f4SDimitry Andric RunImpl impl(*this);
381f37b6182SDimitry Andric
382f37b6182SDimitry Andric // run until termination or until we run out of things to listen to
383f37b6182SDimitry Andric while (!m_terminate_request && (!m_read_fds.empty() || !m_signals.empty())) {
384f37b6182SDimitry Andric
3850f5676f4SDimitry Andric error = impl.Poll();
386f37b6182SDimitry Andric if (error.Fail())
387f37b6182SDimitry Andric return error;
388f37b6182SDimitry Andric
3890f5676f4SDimitry Andric impl.ProcessEvents();
390f37b6182SDimitry Andric
391f37b6182SDimitry Andric if (m_terminate_request)
3925517e702SDimitry Andric return Status();
393f37b6182SDimitry Andric }
3945517e702SDimitry Andric return Status();
395f37b6182SDimitry Andric }
3960f5676f4SDimitry Andric
3970f5676f4SDimitry Andric void MainLoop::ProcessSignal(int signo) {
3980f5676f4SDimitry Andric auto it = m_signals.find(signo);
3990f5676f4SDimitry Andric if (it != m_signals.end())
4000f5676f4SDimitry Andric it->second.callback(*this); // Do the work
4010f5676f4SDimitry Andric }
4020f5676f4SDimitry Andric
4030f5676f4SDimitry Andric void MainLoop::ProcessReadObject(IOObject::WaitableHandle handle) {
4040f5676f4SDimitry Andric auto it = m_read_fds.find(handle);
4050f5676f4SDimitry Andric if (it != m_read_fds.end())
4060f5676f4SDimitry Andric it->second(*this); // Do the work
4070f5676f4SDimitry Andric }
408