summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '3rdParty/Boost/src/boost/asio/detail/epoll_reactor.hpp')
-rw-r--r--3rdParty/Boost/src/boost/asio/detail/epoll_reactor.hpp747
1 files changed, 263 insertions, 484 deletions
diff --git a/3rdParty/Boost/src/boost/asio/detail/epoll_reactor.hpp b/3rdParty/Boost/src/boost/asio/detail/epoll_reactor.hpp
index 6c6bfe3..4684944 100644
--- a/3rdParty/Boost/src/boost/asio/detail/epoll_reactor.hpp
+++ b/3rdParty/Boost/src/boost/asio/detail/epoll_reactor.hpp
@@ -23,115 +23,126 @@
#include <boost/asio/detail/push_options.hpp>
#include <cstddef>
-#include <vector>
#include <sys/epoll.h>
#include <boost/config.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/throw_exception.hpp>
#include <boost/system/system_error.hpp>
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/io_service.hpp>
-#include <boost/asio/detail/bind_handler.hpp>
#include <boost/asio/detail/hash_map.hpp>
#include <boost/asio/detail/mutex.hpp>
-#include <boost/asio/detail/task_io_service.hpp>
-#include <boost/asio/detail/thread.hpp>
-#include <boost/asio/detail/reactor_op_queue.hpp>
+#include <boost/asio/detail/op_queue.hpp>
+#include <boost/asio/detail/reactor_op.hpp>
#include <boost/asio/detail/select_interrupter.hpp>
#include <boost/asio/detail/service_base.hpp>
-#include <boost/asio/detail/signal_blocker.hpp>
#include <boost/asio/detail/socket_types.hpp>
-#include <boost/asio/detail/timer_queue.hpp>
+#include <boost/asio/detail/timer_op.hpp>
+#include <boost/asio/detail/timer_queue_base.hpp>
+#include <boost/asio/detail/timer_queue_fwd.hpp>
+#include <boost/asio/detail/timer_queue_set.hpp>
+
+#if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8)
+# define BOOST_ASIO_HAS_TIMERFD 1
+#endif // (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8)
+
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+# include <boost/asio/detail/push_options.hpp>
+# include <sys/timerfd.h>
+# include <boost/asio/detail/pop_options.hpp>
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
namespace boost {
namespace asio {
namespace detail {
-template <bool Own_Thread>
class epoll_reactor
- : public boost::asio::detail::service_base<epoll_reactor<Own_Thread> >
+ : public boost::asio::detail::service_base<epoll_reactor>
{
public:
- // Per-descriptor data.
- struct per_descriptor_data
+ enum { read_op = 0, write_op = 1,
+ connect_op = 1, except_op = 2, max_ops = 3 };
+
+ // Per-descriptor queues.
+ struct descriptor_state
{
- bool allow_speculative_read;
- bool allow_speculative_write;
+ descriptor_state() {}
+ descriptor_state(const descriptor_state&) {}
+ void operator=(const descriptor_state&) {}
+
+ mutex mutex_;
+ op_queue<reactor_op> op_queue_[max_ops];
+ bool shutdown_;
};
+ // Per-descriptor data.
+ typedef descriptor_state* per_descriptor_data;
+
// Constructor.
epoll_reactor(boost::asio::io_service& io_service)
- : boost::asio::detail::service_base<epoll_reactor<Own_Thread> >(io_service),
+ : boost::asio::detail::service_base<epoll_reactor>(io_service),
+ io_service_(use_service<io_service_impl>(io_service)),
mutex_(),
epoll_fd_(do_epoll_create()),
- wait_in_progress_(false),
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+ timer_fd_(timerfd_create(CLOCK_MONOTONIC, 0)),
+#else // defined(BOOST_ASIO_HAS_TIMERFD)
+ timer_fd_(-1),
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
interrupter_(),
- read_op_queue_(),
- write_op_queue_(),
- except_op_queue_(),
- pending_cancellations_(),
- stop_thread_(false),
- thread_(0),
- shutdown_(false),
- need_epoll_wait_(true)
+ shutdown_(false)
{
- // Start the reactor's internal thread only if needed.
- if (Own_Thread)
- {
- boost::asio::detail::signal_blocker sb;
- thread_ = new boost::asio::detail::thread(
- bind_handler(&epoll_reactor::call_run_thread, this));
- }
-
// Add the interrupter's descriptor to epoll.
epoll_event ev = { 0, { 0 } };
- ev.events = EPOLLIN | EPOLLERR;
- ev.data.fd = interrupter_.read_descriptor();
+ ev.events = EPOLLIN | EPOLLERR | EPOLLET;
+ ev.data.ptr = &interrupter_;
epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, interrupter_.read_descriptor(), &ev);
+ interrupter_.interrupt();
+
+ // Add the timer descriptor to epoll.
+ if (timer_fd_ != -1)
+ {
+ ev.events = EPOLLIN | EPOLLERR;
+ ev.data.ptr = &timer_fd_;
+ epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, timer_fd_, &ev);
+ }
}
// Destructor.
~epoll_reactor()
{
- shutdown_service();
close(epoll_fd_);
+ if (timer_fd_ != -1)
+ close(timer_fd_);
}
// Destroy all user-defined handler objects owned by the service.
void shutdown_service()
{
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
+ mutex::scoped_lock lock(mutex_);
shutdown_ = true;
- stop_thread_ = true;
lock.unlock();
- if (thread_)
+ op_queue<operation> ops;
+
+ descriptor_map::iterator iter = registered_descriptors_.begin();
+ descriptor_map::iterator end = registered_descriptors_.end();
+ while (iter != end)
{
- interrupter_.interrupt();
- thread_->join();
- delete thread_;
- thread_ = 0;
+ for (int i = 0; i < max_ops; ++i)
+ ops.push(iter->second.op_queue_[i]);
+ iter->second.shutdown_ = true;
+ ++iter;
}
- read_op_queue_.destroy_operations();
- write_op_queue_.destroy_operations();
- except_op_queue_.destroy_operations();
-
- for (std::size_t i = 0; i < timer_queues_.size(); ++i)
- timer_queues_[i]->destroy_timers();
- timer_queues_.clear();
+ timer_queues_.get_all_timers(ops);
}
- // Initialise the task, but only if the reactor is not in its own thread.
+ // Initialise the task.
void init_task()
{
- if (!Own_Thread)
- {
- typedef task_io_service<epoll_reactor<Own_Thread> > task_io_service_type;
- use_service<task_io_service_type>(this->get_io_service()).init_task();
- }
+ io_service_.init_task();
}
// Register a socket with the reactor. Returns 0 on success, system error
@@ -139,454 +150,274 @@ public:
int register_descriptor(socket_type descriptor,
per_descriptor_data& descriptor_data)
{
- // No need to lock according to epoll documentation.
+ mutex::scoped_lock lock(registered_descriptors_mutex_);
- descriptor_data.allow_speculative_read = true;
- descriptor_data.allow_speculative_write = true;
+ descriptor_map::iterator new_entry = registered_descriptors_.insert(
+ std::make_pair(descriptor, descriptor_state())).first;
+ descriptor_data = &new_entry->second;
epoll_event ev = { 0, { 0 } };
- ev.events = 0;
- ev.data.fd = descriptor;
+ ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLOUT | EPOLLPRI | EPOLLET;
+ ev.data.ptr = descriptor_data;
int result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
if (result != 0)
return errno;
+
+ descriptor_data->shutdown_ = false;
+
return 0;
}
- // Start a new read operation. The handler object will be invoked when the
- // given descriptor is ready to be read, or an error has occurred.
- template <typename Handler>
- void start_read_op(socket_type descriptor,
+ // Start a new operation. The reactor operation will be performed when the
+ // given descriptor is flagged as ready, or an error has occurred.
+ void start_op(int op_type, socket_type descriptor,
per_descriptor_data& descriptor_data,
- Handler handler, bool allow_speculative_read = true)
+ reactor_op* op, bool allow_speculative)
{
- if (allow_speculative_read && descriptor_data.allow_speculative_read)
- {
- boost::system::error_code ec;
- std::size_t bytes_transferred = 0;
- if (handler.perform(ec, bytes_transferred))
- {
- handler.complete(ec, bytes_transferred);
- return;
- }
-
- // We only get one shot at a speculative read in this function.
- allow_speculative_read = false;
- }
-
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
-
- if (shutdown_)
+ mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
+ if (descriptor_data->shutdown_)
return;
- if (!allow_speculative_read)
- need_epoll_wait_ = true;
- else if (!read_op_queue_.has_operation(descriptor))
+ if (descriptor_data->op_queue_[op_type].empty())
{
- // Speculative reads are ok as there are no queued read operations.
- descriptor_data.allow_speculative_read = true;
-
- boost::system::error_code ec;
- std::size_t bytes_transferred = 0;
- if (handler.perform(ec, bytes_transferred))
+ if (allow_speculative
+ && (op_type != read_op
+ || descriptor_data->op_queue_[except_op].empty()))
{
- handler.complete(ec, bytes_transferred);
- return;
+ if (op->perform())
+ {
+ descriptor_lock.unlock();
+ io_service_.post_immediate_completion(op);
+ return;
+ }
}
- }
-
- // Speculative reads are not ok as there will be queued read operations.
- descriptor_data.allow_speculative_read = false;
-
- if (read_op_queue_.enqueue_operation(descriptor, handler))
- {
- epoll_event ev = { 0, { 0 } };
- ev.events = EPOLLIN | EPOLLERR | EPOLLHUP;
- if (write_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLOUT;
- if (except_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLPRI;
- ev.data.fd = descriptor;
-
- int result = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
- if (result != 0 && errno == ENOENT)
- result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
- if (result != 0)
+ else
{
- boost::system::error_code ec(errno,
- boost::asio::error::get_system_category());
- read_op_queue_.perform_all_operations(descriptor, ec);
+ epoll_event ev = { 0, { 0 } };
+ ev.events = EPOLLIN | EPOLLERR | EPOLLHUP
+ | EPOLLOUT | EPOLLPRI | EPOLLET;
+ ev.data.ptr = descriptor_data;
+ epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
}
}
+
+ descriptor_data->op_queue_[op_type].push(op);
+ io_service_.work_started();
}
- // Start a new write operation. The handler object will be invoked when the
- // given descriptor is ready to be written, or an error has occurred.
- template <typename Handler>
- void start_write_op(socket_type descriptor,
- per_descriptor_data& descriptor_data,
- Handler handler, bool allow_speculative_write = true)
+ // Cancel all operations associated with the given descriptor. The
+ // handlers associated with the descriptor will be invoked with the
+ // operation_aborted error.
+ void cancel_ops(socket_type descriptor, per_descriptor_data& descriptor_data)
{
- if (allow_speculative_write && descriptor_data.allow_speculative_write)
- {
- boost::system::error_code ec;
- std::size_t bytes_transferred = 0;
- if (handler.perform(ec, bytes_transferred))
- {
- handler.complete(ec, bytes_transferred);
- return;
- }
-
- // We only get one shot at a speculative write in this function.
- allow_speculative_write = false;
- }
-
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
+ mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
- if (shutdown_)
- return;
-
- if (!allow_speculative_write)
- need_epoll_wait_ = true;
- else if (!write_op_queue_.has_operation(descriptor))
+ op_queue<operation> ops;
+ for (int i = 0; i < max_ops; ++i)
{
- // Speculative writes are ok as there are no queued write operations.
- descriptor_data.allow_speculative_write = true;
-
- boost::system::error_code ec;
- std::size_t bytes_transferred = 0;
- if (handler.perform(ec, bytes_transferred))
+ while (reactor_op* op = descriptor_data->op_queue_[i].front())
{
- handler.complete(ec, bytes_transferred);
- return;
+ op->ec_ = boost::asio::error::operation_aborted;
+ descriptor_data->op_queue_[i].pop();
+ ops.push(op);
}
}
- // Speculative writes are not ok as there will be queued write operations.
- descriptor_data.allow_speculative_write = false;
+ descriptor_lock.unlock();
- if (write_op_queue_.enqueue_operation(descriptor, handler))
- {
- epoll_event ev = { 0, { 0 } };
- ev.events = EPOLLOUT | EPOLLERR | EPOLLHUP;
- if (read_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLIN;
- if (except_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLPRI;
- ev.data.fd = descriptor;
-
- int result = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
- if (result != 0 && errno == ENOENT)
- result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
- if (result != 0)
- {
- boost::system::error_code ec(errno,
- boost::asio::error::get_system_category());
- write_op_queue_.perform_all_operations(descriptor, ec);
- }
- }
+ io_service_.post_deferred_completions(ops);
}
- // Start a new exception operation. The handler object will be invoked when
- // the given descriptor has exception information, or an error has occurred.
- template <typename Handler>
- void start_except_op(socket_type descriptor,
- per_descriptor_data&, Handler handler)
+ // Cancel any operations that are running against the descriptor and remove
+ // its registration from the reactor.
+ void close_descriptor(socket_type descriptor,
+ per_descriptor_data& descriptor_data)
{
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
+ mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
+ mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
- if (shutdown_)
- return;
+ // Remove the descriptor from the set of known descriptors. The descriptor
+ // will be automatically removed from the epoll set when it is closed.
+ descriptor_data->shutdown_ = true;
- if (except_op_queue_.enqueue_operation(descriptor, handler))
+ op_queue<operation> ops;
+ for (int i = 0; i < max_ops; ++i)
{
- epoll_event ev = { 0, { 0 } };
- ev.events = EPOLLPRI | EPOLLERR | EPOLLHUP;
- if (read_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLIN;
- if (write_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLOUT;
- ev.data.fd = descriptor;
-
- int result = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
- if (result != 0 && errno == ENOENT)
- result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
- if (result != 0)
+ while (reactor_op* op = descriptor_data->op_queue_[i].front())
{
- boost::system::error_code ec(errno,
- boost::asio::error::get_system_category());
- except_op_queue_.perform_all_operations(descriptor, ec);
+ op->ec_ = boost::asio::error::operation_aborted;
+ descriptor_data->op_queue_[i].pop();
+ ops.push(op);
}
}
- }
- // Start a new write operation. The handler object will be invoked when the
- // given descriptor is ready for writing or an error has occurred. Speculative
- // writes are not allowed.
- template <typename Handler>
- void start_connect_op(socket_type descriptor,
- per_descriptor_data& descriptor_data, Handler handler)
- {
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
+ descriptor_lock.unlock();
- if (shutdown_)
- return;
+ registered_descriptors_.erase(descriptor);
- // Speculative writes are not ok as there will be queued write operations.
- descriptor_data.allow_speculative_write = false;
-
- if (write_op_queue_.enqueue_operation(descriptor, handler))
- {
- epoll_event ev = { 0, { 0 } };
- ev.events = EPOLLOUT | EPOLLERR | EPOLLHUP;
- if (read_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLIN;
- if (except_op_queue_.has_operation(descriptor))
- ev.events |= EPOLLPRI;
- ev.data.fd = descriptor;
-
- int result = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
- if (result != 0 && errno == ENOENT)
- result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
- if (result != 0)
- {
- boost::system::error_code ec(errno,
- boost::asio::error::get_system_category());
- write_op_queue_.perform_all_operations(descriptor, ec);
- }
- }
- }
+ descriptors_lock.unlock();
- // Cancel all operations associated with the given descriptor. The
- // handlers associated with the descriptor will be invoked with the
- // operation_aborted error.
- void cancel_ops(socket_type descriptor, per_descriptor_data&)
- {
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
- cancel_ops_unlocked(descriptor);
- }
-
- // Cancel any operations that are running against the descriptor and remove
- // its registration from the reactor.
- void close_descriptor(socket_type descriptor, per_descriptor_data&)
- {
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
-
- // Remove the descriptor from epoll.
- epoll_event ev = { 0, { 0 } };
- epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, descriptor, &ev);
-
- // Cancel any outstanding operations associated with the descriptor.
- cancel_ops_unlocked(descriptor);
+ io_service_.post_deferred_completions(ops);
}
// Add a new timer queue to the reactor.
template <typename Time_Traits>
void add_timer_queue(timer_queue<Time_Traits>& timer_queue)
{
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
- timer_queues_.push_back(&timer_queue);
+ mutex::scoped_lock lock(mutex_);
+ timer_queues_.insert(&timer_queue);
}
// Remove a timer queue from the reactor.
template <typename Time_Traits>
void remove_timer_queue(timer_queue<Time_Traits>& timer_queue)
{
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
- for (std::size_t i = 0; i < timer_queues_.size(); ++i)
- {
- if (timer_queues_[i] == &timer_queue)
- {
- timer_queues_.erase(timer_queues_.begin() + i);
- return;
- }
- }
+ mutex::scoped_lock lock(mutex_);
+ timer_queues_.erase(&timer_queue);
}
- // Schedule a timer in the given timer queue to expire at the specified
- // absolute time. The handler object will be invoked when the timer expires.
- template <typename Time_Traits, typename Handler>
+ // Schedule a new operation in the given timer queue to expire at the
+ // specified absolute time.
+ template <typename Time_Traits>
void schedule_timer(timer_queue<Time_Traits>& timer_queue,
- const typename Time_Traits::time_type& time, Handler handler, void* token)
+ const typename Time_Traits::time_type& time, timer_op* op, void* token)
{
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
+ mutex::scoped_lock lock(mutex_);
if (!shutdown_)
- if (timer_queue.enqueue_timer(time, handler, token))
+ {
+ bool earliest = timer_queue.enqueue_timer(time, op, token);
+ io_service_.work_started();
+ if (earliest)
+ {
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+ if (timer_fd_ != -1)
+ {
+ itimerspec new_timeout;
+ itimerspec old_timeout;
+ int flags = get_timeout(new_timeout);
+ timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
+ return;
+ }
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
interrupter_.interrupt();
+ }
+ }
}
- // Cancel the timer associated with the given token. Returns the number of
- // handlers that have been posted or dispatched.
+ // Cancel the timer operations associated with the given token. Returns the
+ // number of operations that have been posted or dispatched.
template <typename Time_Traits>
std::size_t cancel_timer(timer_queue<Time_Traits>& timer_queue, void* token)
{
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
- std::size_t n = timer_queue.cancel_timer(token);
- if (n > 0)
- interrupter_.interrupt();
+ mutex::scoped_lock lock(mutex_);
+ op_queue<operation> ops;
+ std::size_t n = timer_queue.cancel_timer(token, ops);
+ lock.unlock();
+ io_service_.post_deferred_completions(ops);
return n;
}
-private:
- friend class task_io_service<epoll_reactor<Own_Thread> >;
-
// Run epoll once until interrupted or events are ready to be dispatched.
- void run(bool block)
+ void run(bool block, op_queue<operation>& ops)
{
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
-
- // Dispatch any operation cancellations that were made while the select
- // loop was not running.
- read_op_queue_.perform_cancellations();
- write_op_queue_.perform_cancellations();
- except_op_queue_.perform_cancellations();
- for (std::size_t i = 0; i < timer_queues_.size(); ++i)
- timer_queues_[i]->dispatch_cancellations();
-
- // Check if the thread is supposed to stop.
- if (stop_thread_)
- {
- complete_operations_and_timers(lock);
- return;
- }
-
- // We can return immediately if there's no work to do and the reactor is
- // not supposed to block.
- if (!block && read_op_queue_.empty() && write_op_queue_.empty()
- && except_op_queue_.empty() && all_timer_queues_are_empty())
+ // Calculate a timeout only if timerfd is not used.
+ int timeout;
+ if (timer_fd_ != -1)
+ timeout = block ? -1 : 0;
+ else
{
- complete_operations_and_timers(lock);
- return;
+ mutex::scoped_lock lock(mutex_);
+ timeout = block ? get_timeout() : 0;
}
- int timeout = block ? get_timeout() : 0;
- wait_in_progress_ = true;
- lock.unlock();
-
// Block on the epoll descriptor.
epoll_event events[128];
- int num_events = (block || need_epoll_wait_)
- ? epoll_wait(epoll_fd_, events, 128, timeout)
- : 0;
+ int num_events = epoll_wait(epoll_fd_, events, 128, timeout);
- lock.lock();
- wait_in_progress_ = false;
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+ bool check_timers = (timer_fd_ == -1);
+#else // defined(BOOST_ASIO_HAS_TIMERFD)
+ bool check_timers = true;
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
// Dispatch the waiting events.
for (int i = 0; i < num_events; ++i)
{
- int descriptor = events[i].data.fd;
- if (descriptor == interrupter_.read_descriptor())
+ void* ptr = events[i].data.ptr;
+ if (ptr == &interrupter_)
{
- interrupter_.reset();
+ // No need to reset the interrupter since we're leaving the descriptor
+ // in a ready-to-read state and relying on edge-triggered notifications
+ // to make it so that we only get woken up when the descriptor's epoll
+ // registration is updated.
+
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+ if (timer_fd_ == -1)
+ check_timers = true;
+#else // defined(BOOST_ASIO_HAS_TIMERFD)
+ check_timers = true;
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
}
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+ else if (ptr == &timer_fd_)
+ {
+ check_timers = true;
+ }
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
else
{
- bool more_reads = false;
- bool more_writes = false;
- bool more_except = false;
- boost::system::error_code ec;
+ descriptor_state* descriptor_data = static_cast<descriptor_state*>(ptr);
+ mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
// Exception operations must be processed first to ensure that any
// out-of-band data is read before normal data.
- if (events[i].events & (EPOLLPRI | EPOLLERR | EPOLLHUP))
- more_except = except_op_queue_.perform_operation(descriptor, ec);
- else
- more_except = except_op_queue_.has_operation(descriptor);
-
- if (events[i].events & (EPOLLIN | EPOLLERR | EPOLLHUP))
- more_reads = read_op_queue_.perform_operation(descriptor, ec);
- else
- more_reads = read_op_queue_.has_operation(descriptor);
-
- if (events[i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP))
- more_writes = write_op_queue_.perform_operation(descriptor, ec);
- else
- more_writes = write_op_queue_.has_operation(descriptor);
-
- if ((events[i].events & (EPOLLERR | EPOLLHUP)) != 0
- && (events[i].events & ~(EPOLLERR | EPOLLHUP)) == 0
- && !more_except && !more_reads && !more_writes)
+ static const int flag[max_ops] = { EPOLLIN, EPOLLOUT, EPOLLPRI };
+ for (int j = max_ops - 1; j >= 0; --j)
{
- // If we have an event and no operations associated with the
- // descriptor then we need to delete the descriptor from epoll. The
- // epoll_wait system call can produce EPOLLHUP or EPOLLERR events
- // when there is no operation pending, so if we do not remove the
- // descriptor we can end up in a tight loop of repeated
- // calls to epoll_wait.
- epoll_event ev = { 0, { 0 } };
- epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, descriptor, &ev);
- }
- else
- {
- epoll_event ev = { 0, { 0 } };
- ev.events = EPOLLERR | EPOLLHUP;
- if (more_reads)
- ev.events |= EPOLLIN;
- if (more_writes)
- ev.events |= EPOLLOUT;
- if (more_except)
- ev.events |= EPOLLPRI;
- ev.data.fd = descriptor;
- int result = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
- if (result != 0 && errno == ENOENT)
- result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
- if (result != 0)
+ if (events[i].events & (flag[j] | EPOLLERR | EPOLLHUP))
{
- ec = boost::system::error_code(errno,
- boost::asio::error::get_system_category());
- read_op_queue_.perform_all_operations(descriptor, ec);
- write_op_queue_.perform_all_operations(descriptor, ec);
- except_op_queue_.perform_all_operations(descriptor, ec);
+ while (reactor_op* op = descriptor_data->op_queue_[j].front())
+ {
+ if (op->perform())
+ {
+ descriptor_data->op_queue_[j].pop();
+ ops.push(op);
+ }
+ else
+ break;
+ }
}
}
}
}
- read_op_queue_.perform_cancellations();
- write_op_queue_.perform_cancellations();
- except_op_queue_.perform_cancellations();
- for (std::size_t i = 0; i < timer_queues_.size(); ++i)
- {
- timer_queues_[i]->dispatch_timers();
- timer_queues_[i]->dispatch_cancellations();
- }
-
- // Issue any pending cancellations.
- for (size_t i = 0; i < pending_cancellations_.size(); ++i)
- cancel_ops_unlocked(pending_cancellations_[i]);
- pending_cancellations_.clear();
-
- // Determine whether epoll_wait should be called when the reactor next runs.
- need_epoll_wait_ = !read_op_queue_.empty()
- || !write_op_queue_.empty() || !except_op_queue_.empty();
-
- complete_operations_and_timers(lock);
- }
- // Run the select loop in the thread.
- void run_thread()
- {
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
- while (!stop_thread_)
+ if (check_timers)
{
- lock.unlock();
- run(true);
- lock.lock();
- }
- }
+ mutex::scoped_lock common_lock(mutex_);
+ timer_queues_.get_ready_timers(ops);
- // Entry point for the select loop thread.
- static void call_run_thread(epoll_reactor* reactor)
- {
- reactor->run_thread();
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+ if (timer_fd_ != -1)
+ {
+ itimerspec new_timeout;
+ itimerspec old_timeout;
+ int flags = get_timeout(new_timeout);
+ timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
+ }
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
+ }
}
// Interrupt the select loop.
void interrupt()
{
- interrupter_.interrupt();
+ epoll_event ev = { 0, { 0 } };
+ ev.events = EPOLLIN | EPOLLERR | EPOLLET;
+ ev.data.ptr = &interrupter_;
+ epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, interrupter_.read_descriptor(), &ev);
}
+private:
// The hint to pass to epoll_create to size its data structures.
enum { epoll_size = 20000 };
@@ -606,117 +437,65 @@ private:
return fd;
}
- // Check if all timer queues are empty.
- bool all_timer_queues_are_empty() const
- {
- for (std::size_t i = 0; i < timer_queues_.size(); ++i)
- if (!timer_queues_[i]->empty())
- return false;
- return true;
- }
-
// Get the timeout value for the epoll_wait call. The timeout value is
// returned as a number of milliseconds. A return value of -1 indicates
// that epoll_wait should block indefinitely.
int get_timeout()
{
- if (all_timer_queues_are_empty())
- return -1;
-
// By default we will wait no longer than 5 minutes. This will ensure that
// any changes to the system clock are detected after no longer than this.
- boost::posix_time::time_duration minimum_wait_duration
- = boost::posix_time::minutes(5);
-
- for (std::size_t i = 0; i < timer_queues_.size(); ++i)
- {
- boost::posix_time::time_duration wait_duration
- = timer_queues_[i]->wait_duration();
- if (wait_duration < minimum_wait_duration)
- minimum_wait_duration = wait_duration;
- }
-
- if (minimum_wait_duration > boost::posix_time::time_duration())
- {
- int milliseconds = minimum_wait_duration.total_milliseconds();
- return milliseconds > 0 ? milliseconds : 1;
- }
- else
- {
- return 0;
- }
+ return timer_queues_.wait_duration_msec(5 * 60 * 1000);
}
- // Cancel all operations associated with the given descriptor. The do_cancel
- // function of the handler objects will be invoked. This function does not
- // acquire the epoll_reactor's mutex.
- void cancel_ops_unlocked(socket_type descriptor)
+#if defined(BOOST_ASIO_HAS_TIMERFD)
+ // Get the timeout value for the timer descriptor. The return value is the
+ // flag argument to be used when calling timerfd_settime.
+ int get_timeout(itimerspec& ts)
{
- bool interrupt = read_op_queue_.cancel_operations(descriptor);
- interrupt = write_op_queue_.cancel_operations(descriptor) || interrupt;
- interrupt = except_op_queue_.cancel_operations(descriptor) || interrupt;
- if (interrupt)
- interrupter_.interrupt();
- }
+ ts.it_interval.tv_sec = 0;
+ ts.it_interval.tv_nsec = 0;
- // Clean up operations and timers. We must not hold the lock since the
- // destructors may make calls back into this reactor. We make a copy of the
- // vector of timer queues since the original may be modified while the lock
- // is not held.
- void complete_operations_and_timers(
- boost::asio::detail::mutex::scoped_lock& lock)
- {
- timer_queues_for_cleanup_ = timer_queues_;
- lock.unlock();
- read_op_queue_.complete_operations();
- write_op_queue_.complete_operations();
- except_op_queue_.complete_operations();
- for (std::size_t i = 0; i < timer_queues_for_cleanup_.size(); ++i)
- timer_queues_for_cleanup_[i]->complete_timers();
+ long usec = timer_queues_.wait_duration_usec(5 * 60 * 1000 * 1000);
+ ts.it_value.tv_sec = usec / 1000000;
+ ts.it_value.tv_nsec = usec ? (usec % 1000000) * 1000 : 1;
+
+ return usec ? 0 : TFD_TIMER_ABSTIME;
}
+#endif // defined(BOOST_ASIO_HAS_TIMERFD)
+
+ // The io_service implementation used to post completions.
+ io_service_impl& io_service_;
// Mutex to protect access to internal data.
- boost::asio::detail::mutex mutex_;
+ mutex mutex_;
// The epoll file descriptor.
int epoll_fd_;
- // Whether the epoll_wait call is currently in progress
- bool wait_in_progress_;
+ // The timer file descriptor.
+ int timer_fd_;
// The interrupter is used to break a blocking epoll_wait call.
select_interrupter interrupter_;
- // The queue of read operations.
- reactor_op_queue<socket_type> read_op_queue_;
-
- // The queue of write operations.
- reactor_op_queue<socket_type> write_op_queue_;
-
- // The queue of except operations.
- reactor_op_queue<socket_type> except_op_queue_;
-
// The timer queues.
- std::vector<timer_queue_base*> timer_queues_;
-
- // A copy of the timer queues, used when cleaning up timers. The copy is
- // stored as a class data member to avoid unnecessary memory allocation.
- std::vector<timer_queue_base*> timer_queues_for_cleanup_;
-
- // The descriptors that are pending cancellation.
- std::vector<socket_type> pending_cancellations_;
-
- // Does the reactor loop thread need to stop.
- bool stop_thread_;
-
- // The thread that is running the reactor loop.
- boost::asio::detail::thread* thread_;
+ timer_queue_set timer_queues_;
// Whether the service has been shut down.
bool shutdown_;
- // Whether we need to call epoll_wait the next time the reactor is run.
- bool need_epoll_wait_;
+ // Mutex to protect access to the registered descriptors.
+ mutex registered_descriptors_mutex_;
+
+ // Keep track of all registered descriptors. This code relies on the fact that
+ // the hash_map implementation pools deleted nodes, meaning that we can assume
+ // our descriptor_state pointer remains valid even after the entry is removed.
+ // Technically this is not true for C++98, as that standard says that spliced
+ // elements in a list are invalidated. However, C++0x fixes this shortcoming
+ // so we'll just assume that C++98 std::list implementations will do the right
+ // thing anyway.
+ typedef detail::hash_map<socket_type, descriptor_state> descriptor_map;
+ descriptor_map registered_descriptors_;
};
} // namespace detail