UniSet  2.6.0
Mutex.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00020 // --------------------------------------------------------------------------
00021 #ifndef UniSet_MUTEX_H_
00022 #define UniSet_MUTEX_H_
00023 // -----------------------------------------------------------------------------------------
00024 #include <string>
00025 #include <memory>
00026 #include <mutex>
00027 #include <Poco/RWLock.h>
00028 // -----------------------------------------------------------------------------------------
00029 namespace uniset
00030 {
00031 // rwmutex..
00032 class uniset_rwmutex
00033 {
00034     public:
00035         uniset_rwmutex( const std::string& name );
00036         uniset_rwmutex();
00037         ~uniset_rwmutex();
00038 
00039         void lock();
00040         void unlock();
00041 
00042         void wrlock();
00043         void rlock();
00044 
00045         bool try_lock();
00046         bool try_rlock();
00047         bool try_wrlock();
00048 
00049         uniset_rwmutex( const uniset_rwmutex& r ) = delete;
00050         uniset_rwmutex& operator=(const uniset_rwmutex& r) = delete;
00051 
00052         uniset_rwmutex( uniset_rwmutex&& r ) = default;
00053         uniset_rwmutex& operator=(uniset_rwmutex&& r) = default;
00054 
00055         inline std::string name()
00056         {
00057             return nm;
00058         }
00059         inline void setName( const std::string& name )
00060         {
00061             nm = name;
00062         }
00063 
00064     private:
00065         std::string nm;
00066         friend class uniset_rwmutex_lock;
00067         std::unique_ptr<Poco::RWLock> m;
00068 };
00069 
00070 std::ostream& operator<<(std::ostream& os, uniset_rwmutex& m );
00071 // -------------------------------------------------------------------------
00072 class uniset_rwmutex_wrlock
00073 {
00074     public:
00075         uniset_rwmutex_wrlock( uniset_rwmutex& m );
00076         ~uniset_rwmutex_wrlock();
00077 
00078     private:
00079         uniset_rwmutex_wrlock(const uniset_rwmutex_wrlock&) = delete;
00080         uniset_rwmutex_wrlock& operator=(const uniset_rwmutex_wrlock&) = delete;
00081         uniset_rwmutex& m;
00082 };
00083 
00084 class uniset_rwmutex_rlock
00085 {
00086     public:
00087         uniset_rwmutex_rlock( uniset_rwmutex& m );
00088         ~uniset_rwmutex_rlock();
00089 
00090     private:
00091         uniset_rwmutex_rlock(const uniset_rwmutex_rlock&) = delete;
00092         uniset_rwmutex_rlock& operator=(const uniset_rwmutex_rlock&) = delete;
00093         uniset_rwmutex& m;
00094 };
00095 // -------------------------------------------------------------------------
00096 } // end of UniSetTypes namespace
00097 
00098 #endif