Skip to content

Commit

Permalink
Make poll set interruptable (#3644)
Browse files Browse the repository at this point in the history
* Make poll set interruptable

* open test for poll set

* fix poll set wake up test

* fix build error
  • Loading branch information
JackyWoo committed Jun 28, 2022
1 parent cc14bcf commit 0d539a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
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

0 comments on commit 0d539a7

Please sign in to comment.