Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:33

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_SOFT_MUTEX_HPP
0011 #define BOOST_BEAST_WEBSOCKET_DETAIL_SOFT_MUTEX_HPP
0012 
0013 #include <boost/assert.hpp>
0014 
0015 namespace boost {
0016 namespace beast {
0017 namespace websocket {
0018 namespace detail {
0019 
0020 // used to order reads, writes in websocket streams
0021 
0022 class soft_mutex
0023 {
0024     int id_ = 0;
0025 
0026 public:
0027     soft_mutex() = default;
0028     soft_mutex(soft_mutex const&) = delete;
0029     soft_mutex& operator=(soft_mutex const&) = delete;
0030 
0031     soft_mutex(soft_mutex&& other) noexcept
0032         : id_(boost::exchange(other.id_, 0))
0033     {
0034     }
0035 
0036     soft_mutex& operator=(soft_mutex&& other) noexcept
0037     {
0038         id_ = other.id_;
0039         other.id_ = 0;
0040         return *this;
0041     }
0042 
0043     // VFALCO I'm not too happy that this function is needed
0044     void
0045     reset()
0046     {
0047         id_ = 0;
0048     }
0049 
0050     bool
0051     is_locked() const noexcept
0052     {
0053         return id_ != 0;
0054     }
0055 
0056     template<class T>
0057     bool
0058     is_locked(T const*) const noexcept
0059     {
0060         return id_ == T::id;
0061     }
0062 
0063     template<class T>
0064     void
0065     lock(T const*)
0066     {
0067         BOOST_ASSERT(id_ == 0);
0068         id_ = T::id;
0069     }
0070 
0071     template<class T>
0072     void
0073     unlock(T const*)
0074     {
0075         BOOST_ASSERT(id_ == T::id);
0076         id_ = 0;
0077     }
0078 
0079     template<class T>
0080     bool
0081     try_lock(T const*)
0082     {
0083         // If this assert goes off it means you are attempting to
0084         // simultaneously initiate more than one of same asynchronous
0085         // operation, which is not allowed. For example, you must wait
0086         // for an async_read to complete before performing another
0087         // async_read.
0088         //
0089         BOOST_ASSERT(id_ != T::id);
0090         if(id_ != 0)
0091             return false;
0092         id_ = T::id;
0093         return true;
0094     }
0095 
0096     template<class T>
0097     bool
0098     try_unlock(T const*) noexcept
0099     {
0100         if(id_ != T::id)
0101             return false;
0102         id_ = 0;
0103         return true;
0104     }
0105 };
0106 
0107 } // detail
0108 } // websocket
0109 } // beast
0110 } // boost
0111 
0112 #endif