Unverified Commit f00f4645 authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #3866 from gummif/gfa/poller-refactoring

Problem: poller item lookup can be simplified
parents d882e807 0b32fb36
......@@ -41,6 +41,18 @@ static bool is_thread_safe (const zmq::socket_base_t &socket_)
return socket_.is_thread_safe ();
}
// compare elements to value
template <class It, class T, class Pred>
static It find_if2 (It b_, It e_, const T &value, Pred pred)
{
for (; b_ != e_; ++b_) {
if (pred (*b_, value)) {
break;
}
}
return b_;
}
zmq::socket_poller_t::socket_poller_t () :
_tag (0xCAFEBABE),
_signaler (NULL)
......@@ -101,12 +113,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
void *user_data_,
short events_)
{
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end;
++it) {
if (it->socket == socket_) {
errno = EINVAL;
return -1;
}
if (find_if2 (_items.begin (), _items.end (), socket_, &is_socket)
!= _items.end ()) {
errno = EINVAL;
return -1;
}
if (is_thread_safe (*socket_)) {
......@@ -151,12 +161,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
{
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end;
++it) {
if (!it->socket && it->fd == fd_) {
errno = EINVAL;
return -1;
}
if (find_if2 (_items.begin (), _items.end (), fd_, &is_fd)
!= _items.end ()) {
errno = EINVAL;
return -1;
}
const item_t item = {
......@@ -183,15 +191,10 @@ int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_)
{
const items_t::iterator end = _items.end ();
items_t::iterator it;
for (it = _items.begin (); it != end; ++it) {
if (it->socket == socket_)
break;
}
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), socket_, &is_socket);
if (it == end) {
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
......@@ -205,15 +208,10 @@ int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_)
int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
{
const items_t::iterator end = _items.end ();
items_t::iterator it;
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), fd_, &is_fd);
for (it = _items.begin (); it != end; ++it) {
if (!it->socket && it->fd == fd_)
break;
}
if (it == end) {
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
......@@ -227,15 +225,10 @@ int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
int zmq::socket_poller_t::remove (socket_base_t *socket_)
{
const items_t::iterator end = _items.end ();
items_t::iterator it;
for (it = _items.begin (); it != end; ++it) {
if (it->socket == socket_)
break;
}
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), socket_, &is_socket);
if (it == end) {
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
......@@ -252,15 +245,10 @@ int zmq::socket_poller_t::remove (socket_base_t *socket_)
int zmq::socket_poller_t::remove_fd (fd_t fd_)
{
const items_t::iterator end = _items.end ();
items_t::iterator it;
for (it = _items.begin (); it != end; ++it) {
if (!it->socket && it->fd == fd_)
break;
}
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), fd_, &is_fd);
if (it == end) {
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
......
......@@ -86,6 +86,17 @@ class socket_poller_t
bool check_tag () const;
private:
typedef struct item_t
{
socket_base_t *socket;
fd_t fd;
void *user_data;
short events;
#if defined ZMQ_POLL_BASED_ON_POLL
int pollfd_index;
#endif
} item_t;
static void zero_trail_events (zmq::socket_poller_t::event_t *events_,
int n_events_,
int found_);
......@@ -103,6 +114,15 @@ class socket_poller_t
uint64_t &now_,
uint64_t &end_,
bool &first_pass_);
static bool is_socket (const item_t &item, const socket_base_t *socket_)
{
return item.socket == socket_;
}
static bool is_fd (const item_t &item, fd_t fd_)
{
return !item.socket && item.fd == fd_;
}
int rebuild ();
// Used to check whether the object is a socket_poller.
......@@ -111,17 +131,6 @@ class socket_poller_t
// Signaler used for thread safe sockets polling
signaler_t *_signaler;
typedef struct item_t
{
socket_base_t *socket;
fd_t fd;
void *user_data;
short events;
#if defined ZMQ_POLL_BASED_ON_POLL
int pollfd_index;
#endif
} item_t;
// List of sockets
typedef std::vector<item_t> items_t;
items_t _items;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment