MainLoop.cpp revision 318384
1//===-- MainLoop.cpp --------------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "llvm/Config/llvm-config.h" 11 12#include "lldb/Host/MainLoop.h" 13#include "lldb/Utility/Status.h" 14#include <algorithm> 15#include <cassert> 16#include <cerrno> 17#include <csignal> 18#include <time.h> 19#include <vector> 20 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). 25 26#if HAVE_SYS_EVENT_H 27#include <sys/event.h> 28#elif defined(LLVM_ON_WIN32) 29#include <winsock2.h> 30#else 31#include <poll.h> 32#endif 33 34#ifdef LLVM_ON_WIN32 35#define POLL WSAPoll 36#else 37#define POLL poll 38#endif 39 40#ifdef __ANDROID__ 41#define FORCE_PSELECT 42#endif 43 44#if SIGNAL_POLLING_UNSUPPORTED 45#ifdef LLVM_ON_WIN32 46typedef int sigset_t; 47typedef int siginfo_t; 48#endif 49 50int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout_ts, 51 const sigset_t *) { 52 int timeout = 53 (timeout_ts == nullptr) 54 ? -1 55 : (timeout_ts->tv_sec * 1000 + timeout_ts->tv_nsec / 1000000); 56 return POLL(fds, nfds, timeout); 57} 58 59#endif 60 61using namespace lldb; 62using namespace lldb_private; 63 64static sig_atomic_t g_signal_flags[NSIG]; 65 66static void SignalHandler(int signo, siginfo_t *info, void *) { 67 assert(signo < NSIG); 68 g_signal_flags[signo] = 1; 69} 70 71class MainLoop::RunImpl { 72public: 73 RunImpl(MainLoop &loop); 74 ~RunImpl() = default; 75 76 Status Poll(); 77 void ProcessEvents(); 78 79private: 80 MainLoop &loop; 81 82#if HAVE_SYS_EVENT_H 83 std::vector<struct kevent> in_events; 84 struct kevent out_events[4]; 85 int num_events = -1; 86 87#else 88#ifdef FORCE_PSELECT 89 fd_set read_fd_set; 90#else 91 std::vector<struct pollfd> read_fds; 92#endif 93 94 sigset_t get_sigmask(); 95#endif 96}; 97 98#if HAVE_SYS_EVENT_H 99MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) { 100 in_events.reserve(loop.m_read_fds.size()); 101} 102 103Status MainLoop::RunImpl::Poll() { 104 in_events.resize(loop.m_read_fds.size()); 105 unsigned i = 0; 106 for (auto &fd : loop.m_read_fds) 107 EV_SET(&in_events[i++], fd.first, EVFILT_READ, EV_ADD, 0, 0, 0); 108 109 num_events = kevent(loop.m_kqueue, in_events.data(), in_events.size(), 110 out_events, llvm::array_lengthof(out_events), nullptr); 111 112 if (num_events < 0) 113 return Status("kevent() failed with error %d\n", num_events); 114 return Status(); 115} 116 117void MainLoop::RunImpl::ProcessEvents() { 118 assert(num_events >= 0); 119 for (int i = 0; i < num_events; ++i) { 120 if (loop.m_terminate_request) 121 return; 122 switch (out_events[i].filter) { 123 case EVFILT_READ: 124 loop.ProcessReadObject(out_events[i].ident); 125 break; 126 case EVFILT_SIGNAL: 127 loop.ProcessSignal(out_events[i].ident); 128 break; 129 default: 130 llvm_unreachable("Unknown event"); 131 } 132 } 133} 134#else 135MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) { 136#ifndef FORCE_PSELECT 137 read_fds.reserve(loop.m_read_fds.size()); 138#endif 139} 140 141sigset_t MainLoop::RunImpl::get_sigmask() { 142#if SIGNAL_POLLING_UNSUPPORTED 143 return 0; 144#else 145 sigset_t sigmask; 146 int ret = pthread_sigmask(SIG_SETMASK, nullptr, &sigmask); 147 assert(ret == 0); 148 (void) ret; 149 150 for (const auto &sig : loop.m_signals) 151 sigdelset(&sigmask, sig.first); 152 return sigmask; 153#endif 154} 155 156#ifdef FORCE_PSELECT 157Status MainLoop::RunImpl::Poll() { 158 FD_ZERO(&read_fd_set); 159 int nfds = 0; 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); 163 } 164 165 sigset_t sigmask = get_sigmask(); 166 if (pselect(nfds, &read_fd_set, nullptr, nullptr, nullptr, &sigmask) == -1 && 167 errno != EINTR) 168 return Status(errno, eErrorTypePOSIX); 169 170 return Status(); 171} 172#else 173Status MainLoop::RunImpl::Poll() { 174 read_fds.clear(); 175 176 sigset_t sigmask = get_sigmask(); 177 178 for (const auto &fd : loop.m_read_fds) { 179 struct pollfd pfd; 180 pfd.fd = fd.first; 181 pfd.events = POLLIN; 182 pfd.revents = 0; 183 read_fds.push_back(pfd); 184 } 185 186 if (ppoll(read_fds.data(), read_fds.size(), nullptr, &sigmask) == -1 && 187 errno != EINTR) 188 return Status(errno, eErrorTypePOSIX); 189 190 return Status(); 191} 192#endif 193 194void MainLoop::RunImpl::ProcessEvents() { 195#ifdef FORCE_PSELECT 196 for (const auto &fd : loop.m_read_fds) { 197 if (!FD_ISSET(fd.first, &read_fd_set)) 198 continue; 199 IOObject::WaitableHandle handle = fd.first; 200#else 201 for (const auto &fd : read_fds) { 202 if ((fd.revents & POLLIN) == 0) 203 continue; 204 IOObject::WaitableHandle handle = fd.fd; 205#endif 206 if (loop.m_terminate_request) 207 return; 208 209 loop.ProcessReadObject(handle); 210 } 211 212 for (const auto &entry : loop.m_signals) { 213 if (loop.m_terminate_request) 214 return; 215 if (g_signal_flags[entry.first] == 0) 216 continue; // No signal 217 g_signal_flags[entry.first] = 0; 218 loop.ProcessSignal(entry.first); 219 } 220} 221#endif 222 223MainLoop::MainLoop() { 224#if HAVE_SYS_EVENT_H 225 m_kqueue = kqueue(); 226 assert(m_kqueue >= 0); 227#endif 228} 229MainLoop::~MainLoop() { 230#if HAVE_SYS_EVENT_H 231 close(m_kqueue); 232#endif 233 assert(m_read_fds.size() == 0); 234 assert(m_signals.size() == 0); 235} 236 237MainLoop::ReadHandleUP MainLoop::RegisterReadObject(const IOObjectSP &object_sp, 238 const Callback &callback, 239 Status &error) { 240#ifdef LLVM_ON_WIN32 241 if (object_sp->GetFdType() != IOObject:: eFDTypeSocket) { 242 error.SetErrorString("MainLoop: non-socket types unsupported on Windows"); 243 return nullptr; 244 } 245#endif 246 if (!object_sp || !object_sp->IsValid()) { 247 error.SetErrorString("IO object is not valid."); 248 return nullptr; 249 } 250 251 const bool inserted = 252 m_read_fds.insert({object_sp->GetWaitableHandle(), callback}).second; 253 if (!inserted) { 254 error.SetErrorStringWithFormat("File descriptor %d already monitored.", 255 object_sp->GetWaitableHandle()); 256 return nullptr; 257 } 258 259 return CreateReadHandle(object_sp); 260} 261 262// We shall block the signal, then install the signal handler. The signal will 263// be unblocked in 264// the Run() function to check for signal delivery. 265MainLoop::SignalHandleUP 266MainLoop::RegisterSignal(int signo, const Callback &callback, Status &error) { 267#ifdef SIGNAL_POLLING_UNSUPPORTED 268 error.SetErrorString("Signal polling is not supported on this platform."); 269 return nullptr; 270#else 271 if (m_signals.find(signo) != m_signals.end()) { 272 error.SetErrorStringWithFormat("Signal %d already monitored.", signo); 273 return nullptr; 274 } 275 276 SignalInfo info; 277 info.callback = callback; 278 struct sigaction new_action; 279 new_action.sa_sigaction = &SignalHandler; 280 new_action.sa_flags = SA_SIGINFO; 281 sigemptyset(&new_action.sa_mask); 282 sigaddset(&new_action.sa_mask, signo); 283 sigset_t old_set; 284 285 g_signal_flags[signo] = 0; 286 287 // Even if using kqueue, the signal handler will still be invoked, so it's 288 // important to replace it with our "bening" handler. 289 int ret = sigaction(signo, &new_action, &info.old_action); 290 assert(ret == 0 && "sigaction failed"); 291 292#if HAVE_SYS_EVENT_H 293 struct kevent ev; 294 EV_SET(&ev, signo, EVFILT_SIGNAL, EV_ADD, 0, 0, 0); 295 ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr); 296 assert(ret == 0); 297#endif 298 299 // If we're using kqueue, the signal needs to be unblocked in order to recieve 300 // it. If using pselect/ppoll, we need to block it, and later unblock it as a 301 // part of the system call. 302 ret = pthread_sigmask(HAVE_SYS_EVENT_H ? SIG_UNBLOCK : SIG_BLOCK, 303 &new_action.sa_mask, &old_set); 304 assert(ret == 0 && "pthread_sigmask failed"); 305 info.was_blocked = sigismember(&old_set, signo); 306 m_signals.insert({signo, info}); 307 308 return SignalHandleUP(new SignalHandle(*this, signo)); 309#endif 310} 311 312void MainLoop::UnregisterReadObject(IOObject::WaitableHandle handle) { 313 bool erased = m_read_fds.erase(handle); 314 UNUSED_IF_ASSERT_DISABLED(erased); 315 assert(erased); 316} 317 318void MainLoop::UnregisterSignal(int signo) { 319#if SIGNAL_POLLING_UNSUPPORTED 320 Status("Signal polling is not supported on this platform."); 321#else 322 auto it = m_signals.find(signo); 323 assert(it != m_signals.end()); 324 325 sigaction(signo, &it->second.old_action, nullptr); 326 327 sigset_t set; 328 sigemptyset(&set); 329 sigaddset(&set, signo); 330 int ret = pthread_sigmask(it->second.was_blocked ? SIG_BLOCK : SIG_UNBLOCK, 331 &set, nullptr); 332 assert(ret == 0); 333 (void)ret; 334 335#if HAVE_SYS_EVENT_H 336 struct kevent ev; 337 EV_SET(&ev, signo, EVFILT_SIGNAL, EV_DELETE, 0, 0, 0); 338 ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr); 339 assert(ret == 0); 340#endif 341 342 m_signals.erase(it); 343#endif 344} 345 346Status MainLoop::Run() { 347 m_terminate_request = false; 348 349 Status error; 350 RunImpl impl(*this); 351 352 // run until termination or until we run out of things to listen to 353 while (!m_terminate_request && (!m_read_fds.empty() || !m_signals.empty())) { 354 355 error = impl.Poll(); 356 if (error.Fail()) 357 return error; 358 359 impl.ProcessEvents(); 360 361 if (m_terminate_request) 362 return Status(); 363 } 364 return Status(); 365} 366 367void MainLoop::ProcessSignal(int signo) { 368 auto it = m_signals.find(signo); 369 if (it != m_signals.end()) 370 it->second.callback(*this); // Do the work 371} 372 373void MainLoop::ProcessReadObject(IOObject::WaitableHandle handle) { 374 auto it = m_read_fds.find(handle); 375 if (it != m_read_fds.end()) 376 it->second(*this); // Do the work 377} 378