Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:37

0001 //
0002 // detail/conditionally_enabled_event.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
0012 #define BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
0020 #include <boost/asio/detail/event.hpp>
0021 #include <boost/asio/detail/noncopyable.hpp>
0022 #include <boost/asio/detail/null_event.hpp>
0023 #include <boost/asio/detail/scoped_lock.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace detail {
0030 
0031 // Mutex adapter used to conditionally enable or disable locking.
0032 class conditionally_enabled_event
0033   : private noncopyable
0034 {
0035 public:
0036   // Constructor.
0037   conditionally_enabled_event()
0038   {
0039   }
0040 
0041   // Destructor.
0042   ~conditionally_enabled_event()
0043   {
0044   }
0045 
0046   // Signal the event. (Retained for backward compatibility.)
0047   void signal(conditionally_enabled_mutex::scoped_lock& lock)
0048   {
0049     if (lock.mutex_.enabled_)
0050       event_.signal(lock);
0051   }
0052 
0053   // Signal all waiters.
0054   void signal_all(conditionally_enabled_mutex::scoped_lock& lock)
0055   {
0056     if (lock.mutex_.enabled_)
0057       event_.signal_all(lock);
0058   }
0059 
0060   // Unlock the mutex and signal one waiter.
0061   void unlock_and_signal_one(
0062       conditionally_enabled_mutex::scoped_lock& lock)
0063   {
0064     if (lock.mutex_.enabled_)
0065       event_.unlock_and_signal_one(lock);
0066   }
0067 
0068   // Unlock the mutex and signal one waiter who may destroy us.
0069   void unlock_and_signal_one_for_destruction(
0070       conditionally_enabled_mutex::scoped_lock& lock)
0071   {
0072     if (lock.mutex_.enabled_)
0073       event_.unlock_and_signal_one(lock);
0074   }
0075 
0076   // If there's a waiter, unlock the mutex and signal it.
0077   bool maybe_unlock_and_signal_one(
0078       conditionally_enabled_mutex::scoped_lock& lock)
0079   {
0080     if (lock.mutex_.enabled_)
0081       return event_.maybe_unlock_and_signal_one(lock);
0082     else
0083       return false;
0084   }
0085 
0086   // Reset the event.
0087   void clear(conditionally_enabled_mutex::scoped_lock& lock)
0088   {
0089     if (lock.mutex_.enabled_)
0090       event_.clear(lock);
0091   }
0092 
0093   // Wait for the event to become signalled.
0094   void wait(conditionally_enabled_mutex::scoped_lock& lock)
0095   {
0096     if (lock.mutex_.enabled_)
0097       event_.wait(lock);
0098     else
0099       null_event().wait(lock);
0100   }
0101 
0102   // Timed wait for the event to become signalled.
0103   bool wait_for_usec(
0104       conditionally_enabled_mutex::scoped_lock& lock, long usec)
0105   {
0106     if (lock.mutex_.enabled_)
0107       return event_.wait_for_usec(lock, usec);
0108     else
0109       return null_event().wait_for_usec(lock, usec);
0110   }
0111 
0112 private:
0113   boost::asio::detail::event event_;
0114 };
0115 
0116 } // namespace detail
0117 } // namespace asio
0118 } // namespace boost
0119 
0120 #include <boost/asio/detail/pop_options.hpp>
0121 
0122 #endif // BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP