EventManager: avoid erasing an event handler while it may be in use

This commit is contained in:
Maxime Coste 2012-11-26 14:08:27 +01:00
parent 52ee8b46ae
commit 91d2cc38e5
4 changed files with 35 additions and 24 deletions

View File

@ -26,8 +26,6 @@ void ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
catch (Kakoune::client_removed&) catch (Kakoune::client_removed&)
{ {
ClientManager::instance().remove_client_by_context(*context); ClientManager::instance().remove_client_by_context(*context);
// do not forget, after this line, the current closure
// is dead, do not access captured data.
EventManager::instance().unwatch(fd); EventManager::instance().unwatch(fd);
close(fd); close(fd);
} }

View File

@ -260,8 +260,6 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
{ {
assert(buffer->flags() & Buffer::Flags::Fifo); assert(buffer->flags() & Buffer::Flags::Fifo);
buffer->flags() &= ~Buffer::Flags::Fifo; buffer->flags() &= ~Buffer::Flags::Fifo;
// after that this closure will be dead, do not access
// any captured data.
EventManager::instance().unwatch(fd); EventManager::instance().unwatch(fd);
close(fd); close(fd);
} }

View File

@ -5,8 +5,6 @@
namespace Kakoune namespace Kakoune
{ {
EventManager::EventManager() { m_forced.reserve(4); }
void EventManager::watch(int fd, EventHandler handler) void EventManager::watch(int fd, EventHandler handler)
{ {
auto event = std::find_if(m_events.begin(), m_events.end(), auto event = std::find_if(m_events.begin(), m_events.end(),
@ -20,27 +18,32 @@ void EventManager::watch(int fd, EventHandler handler)
void EventManager::unwatch(int fd) void EventManager::unwatch(int fd)
{ {
for (size_t i = 0; i < m_events.size(); ++i) // do not unwatch now, do that at the end of handle_next_events,
{ // so that if unwatch(fd) is called from fd event handler,
if (m_events[i].fd == fd) // it is not deleted now.
{ m_unwatched.push_back(fd);
m_events.erase(m_events.begin() + i);
m_handlers.erase(fd);
return;
}
}
} }
void EventManager::handle_next_events() void EventManager::handle_next_events()
{ {
const int timeout_ms = 100; const int timeout_ms = 100;
int res = poll(m_events.data(), m_events.size(), timeout_ms); poll(m_events.data(), m_events.size(), timeout_ms);
for (size_t i = 0; i < m_events.size(); ++i) for (auto& event : m_events)
{ {
if ((res > 0 and m_events[i].revents) or const int fd = event.fd;
contains(m_forced, m_events[i].fd)) if ((event.revents or contains(m_forced, fd)) and not contains(m_unwatched, fd))
m_handlers[m_events[i].fd](m_events[i].fd); m_handlers[fd](fd);
} }
// remove unwatched.
for (auto fd : m_unwatched)
{
auto it = std::find_if(m_events.begin(), m_events.end(),
[fd](pollfd& p) { return p.fd == fd; });
m_events.erase(it);
m_handlers.erase(fd);
}
m_unwatched.clear();
m_forced.clear(); m_forced.clear();
} }

View File

@ -11,22 +11,34 @@ namespace Kakoune
using EventHandler = std::function<void (int fd)>; using EventHandler = std::function<void (int fd)>;
// The EventManager provides an interface to file descriptor
// based event handling.
//
// The program main loop should call handle_next_events()
// until it's time to quit.
class EventManager : public Singleton<EventManager> class EventManager : public Singleton<EventManager>
{ {
public: public:
EventManager(); // Watch the given file descriptor, when data becomes
// ready, handler will be called with fd as parameter.
// It is an error to register multiple handlers on the
// same file descriptor.
void watch(int fd, EventHandler handler); void watch(int fd, EventHandler handler);
// stop watching fd
void unwatch(int fd); void unwatch(int fd);
void handle_next_events(); void handle_next_events();
// force the handler associated with fd to be executed
// on next handle_next_events call.
void force_signal(int fd); void force_signal(int fd);
private: private:
std::vector<pollfd> m_events; std::vector<pollfd> m_events;
std::unordered_map<int, EventHandler> m_handlers; std::unordered_map<int, EventHandler> m_handlers;
std::vector<int> m_forced; std::vector<int> m_forced;
std::vector<int> m_unwatched;
}; };
} }