|
UniSet
2.2.1
|
00001 /* This file is part of the UniSet project 00002 * Copyright (c) 2002 Free Software Foundation, Inc. 00003 * Copyright (c) 2002 Pavel Vainerman 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 // -------------------------------------------------------------------------- 00023 // -------------------------------------------------------------------------- 00024 #ifndef UniSet_MUTEX_H_ 00025 #define UniSet_MUTEX_H_ 00026 // ----------------------------------------------------------------------------------------- 00027 #include <string> 00028 #include <atomic> 00029 #include <chrono> 00030 #include <mutex> 00031 #include <cc++/thread.h> 00032 // ----------------------------------------------------------------------------------------- 00033 namespace UniSetTypes 00034 { 00035 class uniset_mutex 00036 { 00037 public: 00038 uniset_mutex(); 00039 uniset_mutex( const std::string& name ); 00040 ~uniset_mutex(); 00041 00042 void lock(); 00043 void unlock(); 00044 bool try_lock_for( const time_t& msec ); 00045 00046 inline std::string name() 00047 { 00048 return nm; 00049 } 00050 inline void setName( const std::string& name ) 00051 { 00052 nm = name; 00053 } 00054 00055 protected: 00056 00057 private: 00058 friend class uniset_mutex_lock; 00059 uniset_mutex(const uniset_mutex& r) = delete; 00060 uniset_mutex& operator=(const uniset_mutex& r) = delete; 00061 std::string nm; 00062 std::timed_mutex m_lock; 00063 }; 00064 00065 std::ostream& operator<<(std::ostream& os, uniset_mutex& m ); 00066 // ------------------------------------------------------------------------- 00073 class uniset_mutex_lock 00074 { 00075 public: 00076 uniset_mutex_lock( uniset_mutex& m, const time_t timeoutMS = 0 ); 00077 ~uniset_mutex_lock(); 00078 00079 bool lock_ok(); 00080 00081 private: 00082 uniset_mutex* mutex; 00083 std::atomic_bool locked; 00084 00085 uniset_mutex_lock(const uniset_mutex_lock&) = delete; 00086 uniset_mutex_lock& operator=(const uniset_mutex_lock&) = delete; 00087 }; 00088 00089 // ------------------------------------------------------------------------- 00090 // rwmutex.. 00091 class uniset_rwmutex 00092 { 00093 public: 00094 uniset_rwmutex( const std::string& name ); 00095 uniset_rwmutex(); 00096 ~uniset_rwmutex(); 00097 00098 void lock(); 00099 void unlock(); 00100 00101 void wrlock(); 00102 void rlock(); 00103 00104 bool try_lock(); 00105 bool try_rlock(); 00106 bool try_wrlock(); 00107 00108 uniset_rwmutex( const uniset_rwmutex& r ) = delete; 00109 uniset_rwmutex& operator=(const uniset_rwmutex& r) = delete; 00110 00111 uniset_rwmutex( uniset_rwmutex&& r ) = default; 00112 uniset_rwmutex& operator=(uniset_rwmutex&& r) = default; 00113 00114 inline std::string name() 00115 { 00116 return nm; 00117 } 00118 inline void setName( const std::string& name ) 00119 { 00120 nm = name; 00121 } 00122 00123 private: 00124 std::string nm; 00125 friend class uniset_rwmutex_lock; 00126 ost::ThreadLock m; // это рекурсивный mutex (!) 00127 }; 00128 00129 std::ostream& operator<<(std::ostream& os, uniset_rwmutex& m ); 00130 // ------------------------------------------------------------------------- 00131 class uniset_rwmutex_wrlock 00132 { 00133 public: 00134 uniset_rwmutex_wrlock( uniset_rwmutex& m ); 00135 ~uniset_rwmutex_wrlock(); 00136 00137 private: 00138 uniset_rwmutex_wrlock(const uniset_rwmutex_wrlock&) = delete; 00139 uniset_rwmutex_wrlock& operator=(const uniset_rwmutex_wrlock&) = delete; 00140 uniset_rwmutex& m; 00141 }; 00142 00143 class uniset_rwmutex_rlock 00144 { 00145 public: 00146 uniset_rwmutex_rlock( uniset_rwmutex& m ); 00147 ~uniset_rwmutex_rlock(); 00148 00149 private: 00150 uniset_rwmutex_rlock(const uniset_rwmutex_rlock&) = delete; 00151 uniset_rwmutex_rlock& operator=(const uniset_rwmutex_rlock&) = delete; 00152 uniset_rwmutex& m; 00153 }; 00154 // ------------------------------------------------------------------------- 00155 } // end of UniSetTypes namespace 00156 00157 #endif
1.7.6.1