sigx++ 2.0.1
|
00001 #ifndef _SIGX_GLIB_LOCKABLES_HPP_ 00002 #define _SIGX_GLIB_LOCKABLES_HPP_ 00003 00004 /* 00005 * Copyright 2006 Klaus Triendl 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Library General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Library General Public 00018 * License along with this library; if not, write to the Free 00019 * Software Foundation, 51 Franklin Street, Fifth Floor, 00020 * Boston, MA 02110-1301, USA. 00021 */ 00022 00023 /* 00024 * Inspired by Andrei Alexandrescu's article "volatile - Multithreaded 00025 * Programmer's Best Friend": 00026 * http://www.ddj.com/dept/cpp/184403766 00027 */ 00028 00029 #include <sigx/lockable.h> 00030 #include <sigx/choose_lock.h> 00031 #include <glibmm/thread.h> 00032 00033 00034 namespace sigx 00035 { 00036 00043 template<typename T_type> 00044 struct rw_lockable: public lockable<T_type, Glib::RWLock> 00045 { 00046 typedef lockable<T_type, Glib::RWLock> parent_type; 00047 00048 public: 00049 rw_lockable(): 00050 parent_type() 00051 {} 00052 rw_lockable(typename parent_type::const_reference_type v): 00053 parent_type(v) 00054 {} 00055 }; 00056 00059 template<typename T_type> 00060 class mutex_lockable: public lockable<T_type, Glib::Mutex> 00061 { 00062 typedef lockable<T_type, Glib::Mutex> parent_type; 00063 00064 public: 00065 mutex_lockable(): 00066 parent_type() 00067 {} 00068 mutex_lockable(typename parent_type::const_reference_type v): 00069 parent_type(v) 00070 {} 00071 }; 00072 00075 template<typename T_type> 00076 class static_mutex_lockable: public lockable<T_type, Glib::StaticMutex> 00077 { 00078 typedef lockable<T_type, Glib::StaticMutex> parent_type; 00079 00080 public: 00081 static_mutex_lockable(): 00082 parent_type() 00083 { 00084 g_static_mutex_init(this->m_mutex.gobj()); 00085 } 00086 00087 static_mutex_lockable(typename parent_type::const_reference_type v): 00088 parent_type(v) 00089 { 00090 g_static_mutex_init(this->m_mutex.gobj()); 00091 } 00092 }; 00093 00096 template<typename T_type> 00097 class recmutex_lockable: public lockable<T_type, Glib::RecMutex> 00098 { 00099 typedef lockable<T_type, Glib::RecMutex> parent_type; 00100 00101 public: 00102 recmutex_lockable(): 00103 parent_type() 00104 {} 00105 recmutex_lockable(typename parent_type::const_reference_type v): 00106 parent_type(v) 00107 {} 00108 }; 00109 00112 template<typename T_type> 00113 class static_recmutex_lockable: public lockable<T_type, Glib::StaticRecMutex> 00114 { 00115 typedef lockable<T_type, Glib::StaticRecMutex> parent_type; 00116 00117 public: 00118 static_recmutex_lockable(): 00119 parent_type() 00120 { 00121 g_static_rec_mutex_init(this->m_mutex.gobj()); 00122 } 00123 00124 static_recmutex_lockable(typename parent_type::const_reference_type v): 00125 parent_type(v) 00126 { 00127 g_static_rec_mutex_init(this->m_mutex.gobj()); 00128 } 00129 }; 00130 00131 00132 template<> 00133 struct choose_lock<Glib::RWLock, readlock> 00134 { 00135 typedef Glib::RWLock::ReaderLock type; 00136 }; 00137 00138 template<> 00139 struct choose_lock<Glib::RWLock, writelock> 00140 { 00141 typedef Glib::RWLock::WriterLock type; 00142 }; 00143 00144 template<locking_policy I_policy> 00145 struct choose_lock<Glib::Mutex, I_policy> 00146 { 00147 typedef Glib::Mutex::Lock type; 00148 }; 00149 00150 template<locking_policy I_policy> 00151 struct choose_lock<Glib::RecMutex, I_policy> 00152 { 00153 typedef Glib::RecMutex::Lock type; 00154 }; 00155 00156 template<locking_policy I_policy> 00157 struct choose_lock<Glib::StaticMutex, I_policy> 00158 { 00159 typedef Glib::/*Static*/Mutex::Lock type; 00160 }; 00161 00162 template<locking_policy I_policy> 00163 struct choose_lock<Glib::StaticRecMutex, I_policy> 00164 { 00165 typedef Glib::/*Static*/RecMutex::Lock type; 00166 }; 00167 00168 00169 00170 00171 // @addtogroup threadsafety 00175 } // namespace sigx 00176 00177 00178 #endif // end file guard