sigx++  2.0.1
glib_lockables.h
Go to the documentation of this file.
1 #ifndef _SIGX_GLIB_LOCKABLES_HPP_
2 #define _SIGX_GLIB_LOCKABLES_HPP_
3 
4 /*
5  * Copyright 2006 Klaus Triendl
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21 */
22 
23 /*
24  * Inspired by Andrei Alexandrescu's article "volatile - Multithreaded
25  * Programmer's Best Friend":
26  * http://www.ddj.com/dept/cpp/184403766
27 */
28 
29 #include <sigx/lockable.h>
30 #include <sigx/choose_lock.h>
31 #include <glibmm/thread.h>
32 
33 
34 namespace sigx
35 {
36 
43 template<typename T_type>
44 struct rw_lockable: public lockable<T_type, Glib::RWLock>
45 {
47 
48 public:
50  parent_type()
51  {}
53  parent_type(v)
54  {}
55 };
56 
59 template<typename T_type>
60 class mutex_lockable: public lockable<T_type, Glib::Mutex>
61 {
63 
64 public:
66  parent_type()
67  {}
69  parent_type(v)
70  {}
71 };
72 
75 template<typename T_type>
76 class static_mutex_lockable: public lockable<T_type, Glib::StaticMutex>
77 {
79 
80 public:
82  parent_type()
83  {
84  g_static_mutex_init(this->m_mutex.gobj());
85  }
86 
88  parent_type(v)
89  {
90  g_static_mutex_init(this->m_mutex.gobj());
91  }
92 };
93 
96 template<typename T_type>
97 class recmutex_lockable: public lockable<T_type, Glib::RecMutex>
98 {
100 
101 public:
103  parent_type()
104  {}
106  parent_type(v)
107  {}
108 };
109 
112 template<typename T_type>
113 class static_recmutex_lockable: public lockable<T_type, Glib::StaticRecMutex>
114 {
116 
117 public:
119  parent_type()
120  {
121  g_static_rec_mutex_init(this->m_mutex.gobj());
122  }
123 
125  parent_type(v)
126  {
127  g_static_rec_mutex_init(this->m_mutex.gobj());
128  }
129 };
130 
131 
132 template<>
133 struct choose_lock<Glib::RWLock, readlock>
134 {
135  typedef Glib::RWLock::ReaderLock type;
136 };
137 
138 template<>
139 struct choose_lock<Glib::RWLock, writelock>
140 {
141  typedef Glib::RWLock::WriterLock type;
142 };
143 
144 template<locking_policy I_policy>
145 struct choose_lock<Glib::Mutex, I_policy>
146 {
147  typedef Glib::Mutex::Lock type;
148 };
149 
150 template<locking_policy I_policy>
151 struct choose_lock<Glib::RecMutex, I_policy>
152 {
153  typedef Glib::RecMutex::Lock type;
154 };
155 
156 template<locking_policy I_policy>
157 struct choose_lock<Glib::StaticMutex, I_policy>
158 {
159  typedef Glib::/*Static*/Mutex::Lock type;
160 };
161 
162 template<locking_policy I_policy>
163 struct choose_lock<Glib::StaticRecMutex, I_policy>
164 {
165  typedef Glib::/*Static*/RecMutex::Lock type;
166 };
167 
168 
169 
170 
171 // @addtogroup threadsafety
175 } // namespace sigx
176 
177 
178 #endif // end file guard
Metafunction that chooses an appropriate scoped lock for a mutex.
Definition: choose_lock.h:56
mutex_type m_mutex
Definition: lockable.h:69
Glib::RecMutex::Lock type
Definition: glib_lockables.h:165
Definition: fwddecl.h:34
recmutex_lockable(typename parent_type::const_reference_type v)
Definition: glib_lockables.h:105
Glib::Mutex::Lock type
Definition: glib_lockables.h:147
Makes T_type lockable with a Glib::StaticRecMutex.
Definition: glib_lockables.h:113
rw_lockable(typename parent_type::const_reference_type v)
Definition: glib_lockables.h:52
Definition: lockable_fwddecl.h:33
lockable< T_type, Glib::RWLock > parent_type
Definition: glib_lockables.h:46
mutex_lockable()
Definition: glib_lockables.h:65
static_mutex_lockable(typename parent_type::const_reference_type v)
Definition: glib_lockables.h:87
rw_lockable()
Definition: glib_lockables.h:49
Makes T_type read/write lockable with a Glib::RWLock.
Definition: glib_lockables.h:44
static_mutex_lockable()
Definition: glib_lockables.h:81
Definition: lockable_fwddecl.h:32
Glib::RWLock::ReaderLock type
Definition: glib_lockables.h:135
Refinement of safe_lockable, open access to mutex and locked type.
Definition: lockable.h:177
Glib::Mutex::Lock type
Definition: glib_lockables.h:159
static_recmutex_lockable(typename parent_type::const_reference_type v)
Definition: glib_lockables.h:124
Glib::RecMutex::Lock type
Definition: glib_lockables.h:153
Definition: auto_dispatchable.h:27
recmutex_lockable()
Definition: glib_lockables.h:102
Makes T_type lockable with a Glib::RecMutex.
Definition: glib_lockables.h:97
std::tr1::add_reference< typename const_trait< reference_type >::add >::type const_reference_type
Definition: lockable.h:110
static_recmutex_lockable()
Definition: glib_lockables.h:118
Makes T_type lockable with a Glib::StaticMutex.
Definition: glib_lockables.h:76
Glib::RWLock::WriterLock type
Definition: glib_lockables.h:141
Makes T_type lockable with a Glib::Mutex.
Definition: glib_lockables.h:60
mutex_lockable(typename parent_type::const_reference_type v)
Definition: glib_lockables.h:68