mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-28 16:15:23 +08:00
Problem: duplication in implementation zmq_poller_poll
Solution: remove duplication
This commit is contained in:
parent
89e5f15ac4
commit
3db3bbfbbb
183
src/zmq.cpp
183
src/zmq.cpp
@ -800,7 +800,7 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|||||||
// if poller is present, use that.
|
// if poller is present, use that.
|
||||||
return zmq_poller_poll (items_, nitems_, timeout_);
|
return zmq_poller_poll (items_, nitems_, timeout_);
|
||||||
#else
|
#else
|
||||||
#if defined ZMQ_POLL_BASED_ON_POLL
|
#if defined ZMQ_POLL_BASED_ON_POLL || defined ZMQ_POLL_BASED_ON_SELECT
|
||||||
if (unlikely (nitems_ < 0)) {
|
if (unlikely (nitems_ < 0)) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -811,22 +811,25 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|||||||
#if defined ZMQ_HAVE_WINDOWS
|
#if defined ZMQ_HAVE_WINDOWS
|
||||||
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
|
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
|
||||||
return 0;
|
return 0;
|
||||||
#elif defined ZMQ_HAVE_ANDROID
|
#elif defined ZMQ_HAVE_VXWORKS
|
||||||
usleep (timeout_ * 1000);
|
struct timespec ns_;
|
||||||
return 0;
|
ns_.tv_sec = timeout_ / 1000;
|
||||||
|
ns_.tv_nsec = timeout_ % 1000 * 1000000;
|
||||||
|
return nanosleep (&ns_, 0);
|
||||||
#else
|
#else
|
||||||
return usleep (timeout_ * 1000);
|
return usleep (timeout_ * 1000);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zmq::clock_t clock;
|
||||||
|
uint64_t now = 0;
|
||||||
|
uint64_t end = 0;
|
||||||
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
||||||
if (!items_) {
|
if (!items_) {
|
||||||
errno = EFAULT;
|
errno = EFAULT;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
zmq::clock_t clock;
|
|
||||||
uint64_t now = 0;
|
|
||||||
uint64_t end = 0;
|
|
||||||
pollfd spollfds[ZMQ_POLLITEMS_DFLT];
|
pollfd spollfds[ZMQ_POLLITEMS_DFLT];
|
||||||
pollfd *pollfds = spollfds;
|
pollfd *pollfds = spollfds;
|
||||||
|
|
||||||
@ -860,11 +863,60 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|||||||
| (items_[i].events & ZMQ_POLLPRI ? POLLPRI : 0);
|
| (items_[i].events & ZMQ_POLLPRI ? POLLPRI : 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
// Ensure we do not attempt to select () on more than FD_SETSIZE
|
||||||
|
// file descriptors.
|
||||||
|
zmq_assert (nitems_ <= FD_SETSIZE);
|
||||||
|
|
||||||
|
fd_set pollset_in;
|
||||||
|
FD_ZERO (&pollset_in);
|
||||||
|
fd_set pollset_out;
|
||||||
|
FD_ZERO (&pollset_out);
|
||||||
|
fd_set pollset_err;
|
||||||
|
FD_ZERO (&pollset_err);
|
||||||
|
|
||||||
|
zmq::fd_t maxfd = 0;
|
||||||
|
|
||||||
|
// Build the fd_sets for passing to select ().
|
||||||
|
for (int i = 0; i != nitems_; i++) {
|
||||||
|
// If the poll item is a 0MQ socket we are interested in input on the
|
||||||
|
// notification file descriptor retrieved by the ZMQ_FD socket option.
|
||||||
|
if (items_[i].socket) {
|
||||||
|
size_t zmq_fd_size = sizeof (zmq::fd_t);
|
||||||
|
zmq::fd_t notify_fd;
|
||||||
|
if (zmq_getsockopt (items_[i].socket, ZMQ_FD, ¬ify_fd,
|
||||||
|
&zmq_fd_size)
|
||||||
|
== -1)
|
||||||
|
return -1;
|
||||||
|
if (items_[i].events) {
|
||||||
|
FD_SET (notify_fd, &pollset_in);
|
||||||
|
if (maxfd < notify_fd)
|
||||||
|
maxfd = notify_fd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Else, the poll item is a raw file descriptor. Convert the poll item
|
||||||
|
// events to the appropriate fd_sets.
|
||||||
|
else {
|
||||||
|
if (items_[i].events & ZMQ_POLLIN)
|
||||||
|
FD_SET (items_[i].fd, &pollset_in);
|
||||||
|
if (items_[i].events & ZMQ_POLLOUT)
|
||||||
|
FD_SET (items_[i].fd, &pollset_out);
|
||||||
|
if (items_[i].events & ZMQ_POLLERR)
|
||||||
|
FD_SET (items_[i].fd, &pollset_err);
|
||||||
|
if (maxfd < items_[i].fd)
|
||||||
|
maxfd = items_[i].fd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fd_set inset, outset, errset;
|
||||||
|
#endif
|
||||||
|
|
||||||
bool first_pass = true;
|
bool first_pass = true;
|
||||||
int nevents = 0;
|
int nevents = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
||||||
|
|
||||||
// Compute the timeout for the subsequent poll.
|
// Compute the timeout for the subsequent poll.
|
||||||
int timeout;
|
int timeout;
|
||||||
if (first_pass)
|
if (first_pass)
|
||||||
@ -925,119 +977,8 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|||||||
nevents++;
|
nevents++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If timeout is zero, exit immediately whether there are events or not.
|
|
||||||
if (timeout_ == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
// If there are events to return, we can exit immediately.
|
|
||||||
if (nevents)
|
|
||||||
break;
|
|
||||||
|
|
||||||
// At this point we are meant to wait for events but there are none.
|
|
||||||
// If timeout is infinite we can just loop until we get some events.
|
|
||||||
if (timeout_ < 0) {
|
|
||||||
if (first_pass)
|
|
||||||
first_pass = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The timeout is finite and there are no events. In the first pass
|
|
||||||
// we get a timestamp of when the polling have begun. (We assume that
|
|
||||||
// first pass have taken negligible time). We also compute the time
|
|
||||||
// when the polling should time out.
|
|
||||||
if (first_pass) {
|
|
||||||
now = clock.now_ms ();
|
|
||||||
end = now + timeout_;
|
|
||||||
if (now == end)
|
|
||||||
break;
|
|
||||||
first_pass = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find out whether timeout have expired.
|
|
||||||
now = clock.now_ms ();
|
|
||||||
if (now >= end)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pollfds != spollfds)
|
|
||||||
free (pollfds);
|
|
||||||
return nevents;
|
|
||||||
|
|
||||||
#elif defined ZMQ_POLL_BASED_ON_SELECT
|
|
||||||
|
|
||||||
if (unlikely (nitems_ < 0)) {
|
|
||||||
errno = EINVAL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (unlikely (nitems_ == 0)) {
|
|
||||||
if (timeout_ == 0)
|
|
||||||
return 0;
|
|
||||||
#if defined ZMQ_HAVE_WINDOWS
|
|
||||||
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
|
|
||||||
return 0;
|
|
||||||
#elif defined ZMQ_HAVE_VXWORKS
|
|
||||||
struct timespec ns_;
|
|
||||||
ns_.tv_sec = timeout_ / 1000;
|
|
||||||
ns_.tv_nsec = timeout_ % 1000 * 1000000;
|
|
||||||
return nanosleep (&ns_, 0);
|
|
||||||
#else
|
#else
|
||||||
return usleep (timeout_ * 1000);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
zmq::clock_t clock;
|
|
||||||
uint64_t now = 0;
|
|
||||||
uint64_t end = 0;
|
|
||||||
|
|
||||||
// Ensure we do not attempt to select () on more than FD_SETSIZE
|
|
||||||
// file descriptors.
|
|
||||||
zmq_assert (nitems_ <= FD_SETSIZE);
|
|
||||||
|
|
||||||
fd_set pollset_in;
|
|
||||||
FD_ZERO (&pollset_in);
|
|
||||||
fd_set pollset_out;
|
|
||||||
FD_ZERO (&pollset_out);
|
|
||||||
fd_set pollset_err;
|
|
||||||
FD_ZERO (&pollset_err);
|
|
||||||
|
|
||||||
zmq::fd_t maxfd = 0;
|
|
||||||
|
|
||||||
// Build the fd_sets for passing to select ().
|
|
||||||
for (int i = 0; i != nitems_; i++) {
|
|
||||||
// If the poll item is a 0MQ socket we are interested in input on the
|
|
||||||
// notification file descriptor retrieved by the ZMQ_FD socket option.
|
|
||||||
if (items_[i].socket) {
|
|
||||||
size_t zmq_fd_size = sizeof (zmq::fd_t);
|
|
||||||
zmq::fd_t notify_fd;
|
|
||||||
if (zmq_getsockopt (items_[i].socket, ZMQ_FD, ¬ify_fd,
|
|
||||||
&zmq_fd_size)
|
|
||||||
== -1)
|
|
||||||
return -1;
|
|
||||||
if (items_[i].events) {
|
|
||||||
FD_SET (notify_fd, &pollset_in);
|
|
||||||
if (maxfd < notify_fd)
|
|
||||||
maxfd = notify_fd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Else, the poll item is a raw file descriptor. Convert the poll item
|
|
||||||
// events to the appropriate fd_sets.
|
|
||||||
else {
|
|
||||||
if (items_[i].events & ZMQ_POLLIN)
|
|
||||||
FD_SET (items_[i].fd, &pollset_in);
|
|
||||||
if (items_[i].events & ZMQ_POLLOUT)
|
|
||||||
FD_SET (items_[i].fd, &pollset_out);
|
|
||||||
if (items_[i].events & ZMQ_POLLERR)
|
|
||||||
FD_SET (items_[i].fd, &pollset_err);
|
|
||||||
if (maxfd < items_[i].fd)
|
|
||||||
maxfd = items_[i].fd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool first_pass = true;
|
|
||||||
int nevents = 0;
|
|
||||||
fd_set inset, outset, errset;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
// Compute the timeout for the subsequent poll.
|
// Compute the timeout for the subsequent poll.
|
||||||
timeval timeout;
|
timeval timeout;
|
||||||
timeval *ptimeout;
|
timeval *ptimeout;
|
||||||
@ -1125,6 +1066,7 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|||||||
if (items_[i].revents)
|
if (items_[i].revents)
|
||||||
nevents++;
|
nevents++;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// If timeout is zero, exit immediately whether there are events or not.
|
// If timeout is zero, exit immediately whether there are events or not.
|
||||||
if (timeout_ == 0)
|
if (timeout_ == 0)
|
||||||
@ -1160,9 +1102,12 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|||||||
if (now >= end)
|
if (now >= end)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
||||||
|
if (pollfds != spollfds)
|
||||||
|
free (pollfds);
|
||||||
|
#endif
|
||||||
|
|
||||||
return nevents;
|
return nevents;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
// Exotic platforms that support neither poll() nor select().
|
// Exotic platforms that support neither poll() nor select().
|
||||||
errno = ENOTSUP;
|
errno = ENOTSUP;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user