From 568cc1adaa034fddbbefedf921664237d62c4555 Mon Sep 17 00:00:00 2001 From: Richard Newton Date: Thu, 7 Nov 2013 17:46:19 +0000 Subject: [PATCH] Get maximum number of sockets it can handle from poller_t --- src/ctx.cpp | 12 ++++++++++-- src/devpoll.cpp | 5 +++++ src/devpoll.hpp | 2 ++ src/epoll.cpp | 5 +++++ src/epoll.hpp | 2 ++ src/kqueue.cpp | 5 +++++ src/kqueue.hpp | 2 ++ src/poll.cpp | 5 +++++ src/poll.hpp | 2 ++ src/select.cpp | 5 +++++ src/select.hpp | 2 ++ 11 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/ctx.cpp b/src/ctx.cpp index b078c7c5..071c5722 100644 --- a/src/ctx.cpp +++ b/src/ctx.cpp @@ -38,6 +38,14 @@ #define ZMQ_CTX_TAG_VALUE_GOOD 0xabadcafe #define ZMQ_CTX_TAG_VALUE_BAD 0xdeadbeef +int clipped_maxsocket(int max_requested) +{ + if (max_requested >= zmq::poller_t::max_fds() && zmq::poller_t::max_fds() != -1) + max_requested = zmq::poller_t::max_fds() - 1; // -1 because we need room for the repear mailbox. + + return max_requested; +} + zmq::ctx_t::ctx_t () : tag (ZMQ_CTX_TAG_VALUE_GOOD), starting (true), @@ -45,7 +53,7 @@ zmq::ctx_t::ctx_t () : reaper (NULL), slot_count (0), slots (NULL), - max_sockets (ZMQ_MAX_SOCKETS_DFLT), + max_sockets(clipped_maxsocket(ZMQ_MAX_SOCKETS_DFLT)), io_thread_count (ZMQ_IO_THREADS_DFLT), ipv6 (false) { @@ -161,7 +169,7 @@ int zmq::ctx_t::shutdown () int zmq::ctx_t::set (int option_, int optval_) { int rc = 0; - if (option_ == ZMQ_MAX_SOCKETS && optval_ >= 1) { + if (option_ == ZMQ_MAX_SOCKETS && optval_ >= 1 && optval_ == clipped_maxsocket(optval_)) { opt_sync.lock (); max_sockets = optval_; opt_sync.unlock (); diff --git a/src/devpoll.cpp b/src/devpoll.cpp index dfa3cddf..841e42ab 100644 --- a/src/devpoll.cpp +++ b/src/devpoll.cpp @@ -133,6 +133,11 @@ void zmq::devpoll_t::stop () stopping = true; } +int zmq::devpoll_t::max_fds() +{ + return -1; +} + void zmq::devpoll_t::loop () { while (!stopping) { diff --git a/src/devpoll.hpp b/src/devpoll.hpp index f47691ce..12ed9afd 100644 --- a/src/devpoll.hpp +++ b/src/devpoll.hpp @@ -56,6 +56,8 @@ namespace zmq void start (); void stop (); + static int max_fds(); + private: // Main worker thread routine. diff --git a/src/epoll.cpp b/src/epoll.cpp index bcedd758..92163ef4 100644 --- a/src/epoll.cpp +++ b/src/epoll.cpp @@ -126,6 +126,11 @@ void zmq::epoll_t::stop () stopping = true; } +int zmq::epoll_t::max_fds() +{ + return -1; +} + void zmq::epoll_t::loop () { epoll_event ev_buf [max_io_events]; diff --git a/src/epoll.hpp b/src/epoll.hpp index d8caf9fe..c32f7eb9 100644 --- a/src/epoll.hpp +++ b/src/epoll.hpp @@ -58,6 +58,8 @@ namespace zmq void start (); void stop (); + static int max_fds(); + private: // Main worker thread routine. diff --git a/src/kqueue.cpp b/src/kqueue.cpp index 266c418c..e834f3d4 100644 --- a/src/kqueue.cpp +++ b/src/kqueue.cpp @@ -152,6 +152,11 @@ void zmq::kqueue_t::stop () stopping = true; } +int zmq::kqueue_t::max_fds() +{ + return -1; +} + void zmq::kqueue_t::loop () { while (!stopping) { diff --git a/src/kqueue.hpp b/src/kqueue.hpp index 04e24776..50599958 100644 --- a/src/kqueue.hpp +++ b/src/kqueue.hpp @@ -58,6 +58,8 @@ namespace zmq void start (); void stop (); + static int max_fds(); + private: // Main worker thread routine. diff --git a/src/poll.cpp b/src/poll.cpp index f8fe198f..fff088e7 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -114,6 +114,11 @@ void zmq::poll_t::stop () stopping = true; } +int zmq::poll_t::max_fds() +{ + return -1; +} + void zmq::poll_t::loop () { while (!stopping) { diff --git a/src/poll.hpp b/src/poll.hpp index 3bf23ba6..0a34ec4b 100644 --- a/src/poll.hpp +++ b/src/poll.hpp @@ -59,6 +59,8 @@ namespace zmq void start (); void stop (); + static int max_fds(); + private: // Main worker thread routine. diff --git a/src/select.cpp b/src/select.cpp index ba60e8e8..755d443e 100644 --- a/src/select.cpp +++ b/src/select.cpp @@ -144,6 +144,11 @@ void zmq::select_t::stop () stopping = true; } +int zmq::select_t::max_fds() +{ + return FD_SETSIZE; +} + void zmq::select_t::loop () { while (!stopping) { diff --git a/src/select.hpp b/src/select.hpp index cb38489f..26ff2550 100644 --- a/src/select.hpp +++ b/src/select.hpp @@ -69,6 +69,8 @@ namespace zmq void start (); void stop (); + static int max_fds(); + private: // Main worker thread routine.