event_manager.cc (4870B)
1 #include "event_manager.hh" 2 3 #include "flags.hh" 4 #include "ranges.hh" 5 6 #if defined(__sun__) 7 #include <cstring> 8 #endif 9 10 #include <unistd.h> 11 12 namespace Kakoune 13 { 14 15 FDWatcher::FDWatcher(int fd, FdEvents events, EventMode mode, Callback callback) 16 : m_fd{fd}, m_events{events}, m_mode{mode}, m_callback{std::move(callback)} 17 { 18 EventManager::instance().m_fd_watchers.push_back(this); 19 } 20 21 FDWatcher::~FDWatcher() 22 { 23 unordered_erase(EventManager::instance().m_fd_watchers, this); 24 } 25 26 void FDWatcher::run(FdEvents events, EventMode mode) 27 { 28 m_callback(*this, events, mode); 29 } 30 31 void FDWatcher::close_fd() 32 { 33 if (m_fd != -1) 34 { 35 close(m_fd); 36 m_fd = -1; 37 } 38 } 39 40 Timer::Timer(TimePoint date, Callback callback, EventMode mode) 41 : m_date{date}, m_mode(mode), m_callback{std::move(callback)} 42 { 43 if (m_callback and EventManager::has_instance()) 44 EventManager::instance().m_timers.push_back(this); 45 } 46 47 Timer::~Timer() 48 { 49 if (m_callback and EventManager::has_instance()) 50 unordered_erase(EventManager::instance().m_timers, this); 51 } 52 53 void Timer::run(EventMode mode) 54 { 55 kak_assert(m_callback); 56 if (mode == m_mode) 57 { 58 m_date = TimePoint::max(); 59 m_callback(*this); 60 } 61 else // try again a little later 62 m_date = Clock::now() + std::chrono::milliseconds{10}; 63 } 64 65 EventManager::EventManager() 66 { 67 FD_ZERO(&m_forced_fd); 68 } 69 70 EventManager::~EventManager() 71 { 72 kak_assert(m_fd_watchers.empty()); 73 kak_assert(m_timers.empty()); 74 } 75 76 bool EventManager::handle_next_events(EventMode mode, sigset_t* sigmask, Optional<Nanoseconds> timeout) 77 { 78 int max_fd = 0; 79 fd_set rfds, wfds, efds; 80 FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); 81 for (auto& watcher : m_fd_watchers) 82 { 83 if (watcher->mode() == EventMode::Normal and mode == EventMode::Urgent) 84 continue; 85 86 const int fd = watcher->fd(); 87 if (fd == -1) 88 continue; 89 90 max_fd = std::max(fd, max_fd); 91 auto events = watcher->events(); 92 if (events & FdEvents::Read) 93 FD_SET(fd, &rfds); 94 if (events & FdEvents::Write) 95 FD_SET(fd, &wfds); 96 if (events & FdEvents::Except) 97 FD_SET(fd, &efds); 98 } 99 100 if (m_has_forced_fd) 101 timeout.reset(); 102 103 if (not m_timers.empty()) 104 { 105 auto next_date = (*std::min_element( 106 m_timers.begin(), m_timers.end(), [](Timer* lhs, Timer* rhs) { 107 return lhs->next_date() < rhs->next_date(); 108 }))->next_date(); 109 110 if (next_date != TimePoint::max()) 111 { 112 auto remaining = std::max(Nanoseconds(0), 113 std::chrono::duration_cast<Nanoseconds>(next_date - Clock::now())); 114 timeout = timeout ? std::min(*timeout, remaining) : remaining; 115 } 116 } 117 auto ts = timeout.map([](auto nsecs) { 118 auto secs = std::chrono::duration_cast<std::chrono::seconds>(nsecs); 119 return timespec{(time_t)secs.count(), (long)(nsecs - secs).count()}; 120 }); 121 int res = pselect(max_fd + 1, &rfds, &wfds, &efds, ts ? &*ts : nullptr, sigmask); 122 123 // copy forced fds *after* select, so that signal handlers can write to 124 // m_forced_fd, interupt select, and directly be serviced. 125 m_has_forced_fd = false; 126 fd_set forced = m_forced_fd; 127 FD_ZERO(&m_forced_fd); 128 129 for (int fd = 0; fd < max_fd + 1; ++fd) 130 { 131 auto events = FD_ISSET(fd, &forced) ? FdEvents::Read : FdEvents::None; 132 if (res > 0) 133 events |= (FD_ISSET(fd, &rfds) ? FdEvents::Read : FdEvents::None) | 134 (FD_ISSET(fd, &wfds) ? FdEvents::Write : FdEvents::None) | 135 (FD_ISSET(fd, &efds) ? FdEvents::Except : FdEvents::None); 136 137 if (events != FdEvents::None) 138 { 139 auto it = find_if(m_fd_watchers, 140 [fd](const FDWatcher* w){return w->fd() == fd; }); 141 if (it != m_fd_watchers.end()) 142 (*it)->run(events, mode); 143 } 144 } 145 146 TimePoint now = Clock::now(); 147 auto timers = m_timers; // copy timers in case m_timers gets mutated 148 for (auto& timer : timers) 149 { 150 if (contains(m_timers, timer) and timer->next_date() <= now) 151 timer->run(mode); 152 } 153 154 return res > 0; 155 } 156 157 void EventManager::force_signal(int fd) 158 { 159 FD_SET(fd, &m_forced_fd); 160 m_has_forced_fd = true; 161 } 162 163 void EventManager::handle_urgent_events() 164 { 165 if (has_instance()) 166 instance().handle_next_events(EventMode::Urgent, nullptr, Nanoseconds{}); 167 } 168 169 170 SignalHandler set_signal_handler(int signum, SignalHandler handler) 171 { 172 struct sigaction new_action, old_action; 173 174 sigemptyset(&new_action.sa_mask); 175 new_action.sa_handler = handler; 176 new_action.sa_flags = SA_RESTART; 177 sigaction(signum, &new_action, &old_action); 178 return old_action.sa_handler; 179 } 180 181 }