Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make poll set interruptable #3644

Merged
merged 5 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 24 additions & 3 deletions Net/src/PollSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#elif defined(POCO_HAVE_FD_POLL)
#ifndef _WIN32
#include <poll.h>
#include "Poco/Pipe.h"
#endif
#endif

Expand Down Expand Up @@ -226,6 +227,17 @@ class PollSetImpl
class PollSetImpl
{
public:
PollSetImpl()
{
pollfd fd{_pipe.readHandle(), POLLIN, 0};
_pollfds.push_back(fd);
}

~PollSetImpl()
{
_pipe.close();
}

void add(const Socket& socket, int mode)
{
Poco::FastMutex::ScopedLock lock(_mutex);
Expand Down Expand Up @@ -280,7 +292,7 @@ class PollSetImpl
_socketMap.clear();
_addMap.clear();
_removeSet.clear();
_pollfds.clear();
_pollfds.reserve(1);
}

PollSet::SocketModeMap poll(const Poco::Timespan& timeout)
Expand Down Expand Up @@ -341,11 +353,17 @@ class PollSetImpl
if (rc < 0) SocketImpl::error();

{
if (_pollfds[0].revents & POLLIN)
{
char c;
_pipe.readBytes(&c, 1);
}

Poco::FastMutex::ScopedLock lock(_mutex);

if (!_socketMap.empty())
{
for (auto it = _pollfds.begin(); it != _pollfds.end(); ++it)
for (auto it = _pollfds.begin() + 1; it != _pollfds.end(); ++it)
{
std::map<poco_socket_t, Socket>::const_iterator its = _socketMap.find(it->fd);
if (its != _socketMap.end())
Expand All @@ -371,7 +389,8 @@ class PollSetImpl

void wakeUp()
{
// TODO
char c = 1;
_pipe.writeBytes(&c, 1);
}

int count() const
Expand All @@ -396,6 +415,8 @@ class PollSetImpl
std::map<poco_socket_t, int> _addMap;
std::set<poco_socket_t> _removeSet;
std::vector<pollfd> _pollfds;
Poco::Pipe _pipe;
/// Add _pipe to head of _pollfds used to wake up poll blocking
};


Expand Down
4 changes: 2 additions & 2 deletions Net/testsuite/src/PollSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void PollSetTest::testPollClosedServer()

void PollSetTest::testPollSetWakeUp()
{
#if defined(POCO_HAVE_FD_EPOLL)
#if defined(POCO_HAVE_FD_EPOLL) || defined (POCO_HAVE_FD_POLL)
PollSet ps;
Timespan timeout(100000000); // 100 seconds
Poller poller(ps, timeout);
Expand All @@ -304,7 +304,7 @@ void PollSetTest::testPollSetWakeUp()
assertTrue(sw.elapsedSeconds() < 1);
#else // TODO: other implementations
std::cout << "not implemented";
#endif // POCO_HAVE_FD_EPOLL
#endif // POCO_HAVE_FD_EPOLL || POCO_HAVE_FD_EPOLL
}


Expand Down