Skip to content

Commit

Permalink
allow duplicate entries in zmq_poller_poll
Browse files Browse the repository at this point in the history
This should restore full compatibility with earlier zmq_poll behavior.

It complicates things a little bit, as collisions must be detected, and when collisions are found:

- event masks must be merged
- pollitems, events arrays are no longer co-ordered

Reverts the recent zmq_proxy patch to workaround the lack of repeat-item support in zmq_poll that is now fixed.
  • Loading branch information
minrk committed Sep 29, 2016
1 parent ae0676e commit fb5a04e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
11 changes: 1 addition & 10 deletions src/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,6 @@ int zmq::proxy (
{ backend_, 0, ZMQ_POLLOUT, 0 }
};

int control_idx = 2;
if (frontend_ == backend_) {
// when frontend & backend are the same,
// avoid duplicate poll entries
qt_poll_items -= 1;
items[1] = items[2];
control_idx = 1;
}

// Proxy can be in these three states
enum {
active,
Expand All @@ -163,7 +154,7 @@ int zmq::proxy (
}

// Process a control command if any
if (control_ && items [control_idx].revents & ZMQ_POLLIN) {
if (control_ && items [2].revents & ZMQ_POLLIN) {
rc = control_->recv (&msg, 0);
if (unlikely (rc < 0))
return -1;
Expand Down
66 changes: 53 additions & 13 deletions src/zmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,20 +753,48 @@ inline int zmq_poller_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
void *poller = zmq_poller_new ();
alloc_assert(poller);

bool repeat_items = false;
// Register sockets with poller
for (int i = 0; i < nitems_; i++) {
items_[i].revents = 0;

bool modify = false;
short e = items_[i].events;
if (items_[i].socket) {
// Poll item is a 0MQ socket.
rc = zmq_poller_add (poller, items_[i].socket, NULL, items_[i].events);
for (int j = 0; j < i; ++j) {
// Check for repeat entries
if (items_[j].socket == items_[i].socket) {
repeat_items = true;
modify = true;
e |= items_[j].events;
}
}
if (modify) {
rc = zmq_poller_modify (poller, items_[i].socket, e);
} else {
rc = zmq_poller_add (poller, items_[i].socket, NULL, e);
}
if (rc < 0) {
zmq_poller_destroy (&poller);
delete [] events;
return rc;
}
} else {
// Poll item is a raw file descriptor.
rc = zmq_poller_add_fd (poller, items_[i].fd, NULL, items_[i].events);
for (int j = 0; j < i; ++j) {
// Check for repeat entries
if (items_[j].fd == items_[i].fd) {
repeat_items = true;
modify = true;
e |= items_[j].events;
}
}
if (modify) {
rc = zmq_poller_modify_fd (poller, items_[i].fd, e);
} else {
rc = zmq_poller_add_fd (poller, items_[i].fd, NULL, e);
}
if (rc < 0) {
zmq_poller_destroy (&poller);
delete [] events;
Expand All @@ -786,18 +814,30 @@ inline int zmq_poller_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
return rc;
}

// Transform poller events into zmq_pollitem events
// Transform poller events into zmq_pollitem events.
// items_ contains all items, while events only contains fired events.
// The two are still co-ordered, so the step through items
// Checking for matches only on the first event
int found_events = rc;
for (int i = 0, j = 0; i < nitems_ && j < found_events; i++) {
if (
(items_[i].socket && items_[i].socket == events[j].socket) ||
(items_[i].fd && items_[i].fd == events[j].fd)
) {
items_[i].revents = events[j].events;
j++;
// If no sockets are repeated (likely), the two are still co-ordered, so the step through items
// Checking for matches only on the first event.
// If there are repeat items, they cannot be assumed to be co-ordered,
// so each pollitem must check fired events from the beginning.
int j_start = 0, found_events = rc;
for (int i = 0; i < nitems_; i++) {
for (int j = j_start; j < found_events; ++j) {
if (
(items_[i].socket && items_[i].socket == events[j].socket) ||
(items_[i].fd && items_[i].fd == events[j].fd)
) {
items_[i].revents = events[j].events & items_[i].events;
if (!repeat_items) {
// no repeats, we can ignore events we've already seen
j_start++;
}
break;
}
if (!repeat_items) {
// no repeats, never have to look at j > j_start
break;
}
}
}

Expand Down

0 comments on commit fb5a04e

Please sign in to comment.