File indexing completed on 2025-01-18 09:29:33
0001
0002
0003
0004
0005
0006
0007
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
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
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
0084
0085
0086
0087
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 }
0108 }
0109 }
0110 }
0111
0112 #endif