Boost logo

Boost-Commit :

From: chris_at_[hidden]
Date: 2008-05-03 07:36:17


Author: chris_kohlhoff
Date: 2008-05-03 07:36:16 EDT (Sat, 03 May 2008)
New Revision: 45059
URL: http://svn.boost.org/trac/boost/changeset/45059

Log:
Add fast-pathing of speculative reads and writes to the kqueue_reactor.

Text files modified:
   trunk/boost/asio/detail/kqueue_reactor.hpp | 56 +++++++++++++++++++++++++++++++++++----
   1 files changed, 50 insertions(+), 6 deletions(-)

Modified: trunk/boost/asio/detail/kqueue_reactor.hpp
==============================================================================
--- trunk/boost/asio/detail/kqueue_reactor.hpp (original)
+++ trunk/boost/asio/detail/kqueue_reactor.hpp 2008-05-03 07:36:16 EDT (Sat, 03 May 2008)
@@ -64,6 +64,8 @@
   // Per-descriptor data.
   struct per_descriptor_data
   {
+ bool allow_speculative_read;
+ bool allow_speculative_write;
   };
 
   // Constructor.
@@ -132,17 +134,30 @@
 
   // Register a socket with the reactor. Returns 0 on success, system error
   // code on failure.
- int register_descriptor(socket_type, per_descriptor_data&)
+ int register_descriptor(socket_type, per_descriptor_data& descriptor_data)
   {
+ descriptor_data.allow_speculative_read = true;
+ descriptor_data.allow_speculative_write = true;
+
     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, per_descriptor_data&,
- Handler handler, bool allow_speculative_read = true)
+ void start_read_op(socket_type descriptor,
+ per_descriptor_data& descriptor_data, Handler handler,
+ bool allow_speculative_read = true)
   {
+ if (allow_speculative_read && descriptor_data.allow_speculative_read)
+ {
+ if (handler(boost::system::error_code()))
+ 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_)
@@ -151,8 +166,16 @@
     if (!allow_speculative_read)
       need_kqueue_wait_ = true;
     else if (!read_op_queue_.has_operation(descriptor))
+ {
+ // Speculative reads are ok as there are no queued read operations.
+ descriptor_data.allow_speculative_read = true;
+
       if (handler(boost::system::error_code()))
         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))
     {
@@ -170,9 +193,19 @@
   // 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&,
- Handler handler, bool allow_speculative_write = true)
+ void start_write_op(socket_type descriptor,
+ per_descriptor_data& descriptor_data, Handler handler,
+ bool allow_speculative_write = true)
   {
+ if (allow_speculative_write && descriptor_data.allow_speculative_write)
+ {
+ if (handler(boost::system::error_code()))
+ 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_);
 
     if (shutdown_)
@@ -181,8 +214,16 @@
     if (!allow_speculative_write)
       need_kqueue_wait_ = true;
     else if (!write_op_queue_.has_operation(descriptor))
+ {
+ // Speculative writes are ok as there are no queued write operations.
+ descriptor_data.allow_speculative_write = true;
+
       if (handler(boost::system::error_code()))
         return;
+ }
+
+ // 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))
     {
@@ -228,13 +269,16 @@
   // given descriptor is ready to be written, or an error has occurred.
   template <typename Handler>
   void start_connect_op(socket_type descriptor,
- per_descriptor_data&, Handler handler)
+ per_descriptor_data& descriptor_data, Handler handler)
   {
     boost::asio::detail::mutex::scoped_lock lock(mutex_);
 
     if (shutdown_)
       return;
 
+ // 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))
     {
       struct kevent event;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk