UniSet  2.0.0
Mutex.h
См. документацию.
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(){ return nm; }
00047             inline void setName( const std::string& name ){ nm = name; }
00048 
00049         protected:
00050 
00051         private:
00052             friend class uniset_mutex_lock;
00053             uniset_mutex(const uniset_mutex& r) = delete;
00054             uniset_mutex &operator=(const uniset_mutex& r) = delete;
00055             std::string nm;
00056             std::timed_mutex m_lock;
00057      };
00058 
00059     std::ostream& operator<<(std::ostream& os, uniset_mutex& m );
00060     // -------------------------------------------------------------------------
00067     class uniset_mutex_lock
00068     {
00069         public:
00070             uniset_mutex_lock( uniset_mutex& m, const time_t timeoutMS=0 );
00071             ~uniset_mutex_lock();
00072 
00073             bool lock_ok();
00074 
00075         private:
00076             uniset_mutex* mutex;
00077             std::atomic_bool locked;
00078 
00079             uniset_mutex_lock(const uniset_mutex_lock&)=delete;
00080             uniset_mutex_lock& operator=(const uniset_mutex_lock&)=delete;
00081     };
00082 
00083     // -------------------------------------------------------------------------
00084     // rwmutex..
00085     class uniset_rwmutex
00086     {
00087         public:
00088             uniset_rwmutex( const std::string& name );
00089             uniset_rwmutex();
00090             ~uniset_rwmutex();
00091 
00092             void lock();
00093             void unlock();
00094 
00095             void wrlock();
00096             void rlock();
00097 
00098             bool trylock();
00099             bool tryrlock();
00100             bool trywrlock();
00101 
00102             uniset_rwmutex( const uniset_rwmutex& r ) = delete;
00103             uniset_rwmutex& operator=(const uniset_rwmutex& r)=delete;
00104 
00105             uniset_rwmutex( uniset_rwmutex&& r ) = default;
00106             uniset_rwmutex& operator=(uniset_rwmutex&& r)=default;
00107 
00108             inline std::string name(){ return nm; }
00109             inline void setName( const std::string& name ){ nm = name; }
00110 
00111         private:
00112             std::string nm;
00113             friend class uniset_rwmutex_lock;
00114             ost::ThreadLock m;
00115     };
00116 
00117     std::ostream& operator<<(std::ostream& os, uniset_rwmutex& m );
00118     // -------------------------------------------------------------------------
00119     class uniset_rwmutex_wrlock
00120     {
00121         public:
00122             uniset_rwmutex_wrlock( uniset_rwmutex& m );
00123             ~uniset_rwmutex_wrlock();
00124 
00125         private:
00126             uniset_rwmutex_wrlock(const uniset_rwmutex_wrlock&)=delete;
00127             uniset_rwmutex_wrlock& operator=(const uniset_rwmutex_wrlock&)=delete;
00128             uniset_rwmutex& m;
00129     };
00130 
00131     class uniset_rwmutex_rlock
00132     {
00133         public:
00134             uniset_rwmutex_rlock( uniset_rwmutex& m );
00135             ~uniset_rwmutex_rlock();
00136 
00137         private:
00138             uniset_rwmutex_rlock(const uniset_rwmutex_rlock&)=delete;
00139             uniset_rwmutex_rlock& operator=(const uniset_rwmutex_rlock&)=delete;
00140             uniset_rwmutex& m;
00141     };
00142     // -------------------------------------------------------------------------
00143 } // end of UniSetTypes namespace
00144 
00145 #endif