1 //===-- MainLoop.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Config/llvm-config.h"
12 #include "lldb/Host/MainLoop.h"
13 #include "lldb/Utility/Error.h"
21 // Multiplexing is implemented using kqueue on systems that support it (BSD
22 // variants including OSX). On linux we use ppoll, while android uses pselect
23 // (ppoll is present but not implemented properly). On windows we use WSApoll
24 // (which does not support signals).
27 #include <sys/event.h>
28 #elif defined(LLVM_ON_WIN32)
44 #if SIGNAL_POLLING_UNSUPPORTED
47 typedef int siginfo_t;
50 int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout_ts,
53 (timeout_ts == nullptr)
55 : (timeout_ts->tv_sec * 1000 + timeout_ts->tv_nsec / 1000000);
56 return POLL(fds, nfds, timeout);
62 using namespace lldb_private;
64 static sig_atomic_t g_signal_flags[NSIG];
66 static void SignalHandler(int signo, siginfo_t *info, void *) {
68 g_signal_flags[signo] = 1;
71 class MainLoop::RunImpl {
73 RunImpl(MainLoop &loop);
83 std::vector<struct kevent> in_events;
84 struct kevent out_events[4];
91 std::vector<struct pollfd> read_fds;
94 sigset_t get_sigmask();
99 MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) {
100 in_events.reserve(loop.m_read_fds.size());
103 Error MainLoop::RunImpl::Poll() {
104 in_events.resize(loop.m_read_fds.size());
106 for (auto &fd : loop.m_read_fds)
107 EV_SET(&in_events[i++], fd.first, EVFILT_READ, EV_ADD, 0, 0, 0);
109 num_events = kevent(loop.m_kqueue, in_events.data(), in_events.size(),
110 out_events, llvm::array_lengthof(out_events), nullptr);
113 return Error("kevent() failed with error %d\n", num_events);
117 void MainLoop::RunImpl::ProcessEvents() {
118 assert(num_events >= 0);
119 for (int i = 0; i < num_events; ++i) {
120 if (loop.m_terminate_request)
122 switch (out_events[i].filter) {
124 loop.ProcessReadObject(out_events[i].ident);
127 loop.ProcessSignal(out_events[i].ident);
130 llvm_unreachable("Unknown event");
135 MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) {
136 #ifndef FORCE_PSELECT
137 read_fds.reserve(loop.m_read_fds.size());
141 sigset_t MainLoop::RunImpl::get_sigmask() {
142 #if SIGNAL_POLLING_UNSUPPORTED
146 int ret = pthread_sigmask(SIG_SETMASK, nullptr, &sigmask);
150 for (const auto &sig : loop.m_signals)
151 sigdelset(&sigmask, sig.first);
157 Error MainLoop::RunImpl::Poll() {
158 FD_ZERO(&read_fd_set);
160 for (const auto &fd : loop.m_read_fds) {
161 FD_SET(fd.first, &read_fd_set);
162 nfds = std::max(nfds, fd.first + 1);
165 sigset_t sigmask = get_sigmask();
166 if (pselect(nfds, &read_fd_set, nullptr, nullptr, nullptr, &sigmask) == -1 &&
168 return Error(errno, eErrorTypePOSIX);
173 Error MainLoop::RunImpl::Poll() {
176 sigset_t sigmask = get_sigmask();
178 for (const auto &fd : loop.m_read_fds) {
183 read_fds.push_back(pfd);
186 if (ppoll(read_fds.data(), read_fds.size(), nullptr, &sigmask) == -1 &&
188 return Error(errno, eErrorTypePOSIX);
194 void MainLoop::RunImpl::ProcessEvents() {
196 for (const auto &fd : loop.m_read_fds) {
197 if (!FD_ISSET(fd.first, &read_fd_set))
199 IOObject::WaitableHandle handle = fd.first;
201 for (const auto &fd : read_fds) {
202 if ((fd.revents & POLLIN) == 0)
204 IOObject::WaitableHandle handle = fd.fd;
206 if (loop.m_terminate_request)
209 loop.ProcessReadObject(handle);
212 for (const auto &entry : loop.m_signals) {
213 if (loop.m_terminate_request)
215 if (g_signal_flags[entry.first] == 0)
216 continue; // No signal
217 g_signal_flags[entry.first] = 0;
218 loop.ProcessSignal(entry.first);
223 MainLoop::MainLoop() {
226 assert(m_kqueue >= 0);
229 MainLoop::~MainLoop() {
233 assert(m_read_fds.size() == 0);
234 assert(m_signals.size() == 0);
237 MainLoop::ReadHandleUP
238 MainLoop::RegisterReadObject(const IOObjectSP &object_sp,
239 const Callback &callback, Error &error) {
241 if (object_sp->GetFdType() != IOObject:: eFDTypeSocket) {
242 error.SetErrorString("MainLoop: non-socket types unsupported on Windows");
246 if (!object_sp || !object_sp->IsValid()) {
247 error.SetErrorString("IO object is not valid.");
251 const bool inserted =
252 m_read_fds.insert({object_sp->GetWaitableHandle(), callback}).second;
254 error.SetErrorStringWithFormat("File descriptor %d already monitored.",
255 object_sp->GetWaitableHandle());
259 return CreateReadHandle(object_sp);
262 // We shall block the signal, then install the signal handler. The signal will
264 // the Run() function to check for signal delivery.
265 MainLoop::SignalHandleUP
266 MainLoop::RegisterSignal(int signo, const Callback &callback,
268 #ifdef SIGNAL_POLLING_UNSUPPORTED
269 error.SetErrorString("Signal polling is not supported on this platform.");
272 if (m_signals.find(signo) != m_signals.end()) {
273 error.SetErrorStringWithFormat("Signal %d already monitored.", signo);
278 info.callback = callback;
279 struct sigaction new_action;
280 new_action.sa_sigaction = &SignalHandler;
281 new_action.sa_flags = SA_SIGINFO;
282 sigemptyset(&new_action.sa_mask);
283 sigaddset(&new_action.sa_mask, signo);
286 g_signal_flags[signo] = 0;
288 // Even if using kqueue, the signal handler will still be invoked, so it's
289 // important to replace it with our "bening" handler.
290 int ret = sigaction(signo, &new_action, &info.old_action);
291 assert(ret == 0 && "sigaction failed");
295 EV_SET(&ev, signo, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
296 ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr);
300 // If we're using kqueue, the signal needs to be unblocked in order to recieve
301 // it. If using pselect/ppoll, we need to block it, and later unblock it as a
302 // part of the system call.
303 ret = pthread_sigmask(HAVE_SYS_EVENT_H ? SIG_UNBLOCK : SIG_BLOCK,
304 &new_action.sa_mask, &old_set);
305 assert(ret == 0 && "pthread_sigmask failed");
306 info.was_blocked = sigismember(&old_set, signo);
307 m_signals.insert({signo, info});
309 return SignalHandleUP(new SignalHandle(*this, signo));
313 void MainLoop::UnregisterReadObject(IOObject::WaitableHandle handle) {
314 bool erased = m_read_fds.erase(handle);
315 UNUSED_IF_ASSERT_DISABLED(erased);
319 void MainLoop::UnregisterSignal(int signo) {
320 #if SIGNAL_POLLING_UNSUPPORTED
321 Error("Signal polling is not supported on this platform.");
323 auto it = m_signals.find(signo);
324 assert(it != m_signals.end());
326 sigaction(signo, &it->second.old_action, nullptr);
330 sigaddset(&set, signo);
331 int ret = pthread_sigmask(it->second.was_blocked ? SIG_BLOCK : SIG_UNBLOCK,
338 EV_SET(&ev, signo, EVFILT_SIGNAL, EV_DELETE, 0, 0, 0);
339 ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr);
347 Error MainLoop::Run() {
348 m_terminate_request = false;
353 // run until termination or until we run out of things to listen to
354 while (!m_terminate_request && (!m_read_fds.empty() || !m_signals.empty())) {
360 impl.ProcessEvents();
362 if (m_terminate_request)
368 void MainLoop::ProcessSignal(int signo) {
369 auto it = m_signals.find(signo);
370 if (it != m_signals.end())
371 it->second.callback(*this); // Do the work
374 void MainLoop::ProcessReadObject(IOObject::WaitableHandle handle) {
375 auto it = m_read_fds.find(handle);
376 if (it != m_read_fds.end())
377 it->second(*this); // Do the work