/usr/include/sigx-2.0/sigx/lockable.h is in libsigx-2.0-dev 2.0.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | #ifndef _SIGX_LOCKABLE_H_
#define _SIGX_LOCKABLE_H_
/*
* Copyright 2008 Klaus Triendl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* Inspired by Andrei Alexandrescu's article "volatile - Multithreaded
* Programmer's Best Friend":
* http://www.ddj.com/dept/cpp/184403766
*/
#include <tr1/type_traits>
#include <sigx/noncopyable.h>
#include <sigx/lockable_fwddecl.h>
#include <sigx/const_trait.h>
#include <sigx/volatile_trait.h>
namespace sigx
{
/** @addtogroup threadsafety
* @{
*/
/** @short The base for all lockables, template specialized for a specific
* lock, e.g. a boost::mutex.
*
* Lockables are wrapper objects pairing a certain type together with a mutex type.
* Mutex objects of type T_mutex must be default constructible.
*
*/
template<typename T_mutex>
struct lockable_base: noncopyable
{
typedef T_mutex mutex_type;
mutex_type& mutex() const throw()
{
return m_mutex;
}
protected:
lockable_base():
m_mutex()
{}
/** @note mutable in case that lockable_base is const.
*/
mutable mutex_type m_mutex;
};
/** @short Makes @e T_type %lockable.
*
* The "safe" in safe_lockable means that access to the mutex and the locked type is denied, they are only accessible through a lock_acquirer.
*
* The following template arguments are used:
* - @e T_type The type to be protected, e.g. an int
* - @e T_mutex The mutex type to protect @e T_type, e.g. a boost::mutex
*
* @code
* typedef lockable<int, boost::mutex> mutex_lockable_int;
* @endcode
*
* @note lockables are inseparably tied together and constness for lockables and the locked type is transitive, i.e
* no matter whether the type to protect (@e T_type) or the lockable itself is somewhere declared const you get only
* const access to the variable to protect
*/
template<typename T_type, typename T_mutex>
struct safe_lockable: public lockable_base<T_mutex>
{
// lock_acquirer can use interface methods
template<locking_policy I_policy, typename T_type1, typename T_mutex1, typename T_islockable> friend class lock_acquirer;
typedef lockable_base<T_mutex> parent_type;
// acquired_type = type to protect, 1:1 from T_type
typedef T_type acquired_type;
// volatile_type = make T_type volatile, even if T_type is a reference
// volatile T_type or volatile T_type&
typedef typename volatile_trait<acquired_type>::add volatile_type;
// reference_type = reference to volatile-stripped T_type
// T_type&
typedef typename std::tr1::add_reference<typename volatile_trait<acquired_type>::remove>::type reference_type;
// volatile_reference_type = reference to volatile T_type, even if T_type is a reference
// volatile T_type&
typedef typename std::tr1::add_reference<volatile_type>::type volatile_reference_type;
// reference_type = reference to volatile-stripped T_type, even if T_type is a reference
// const T_type&
typedef typename std::tr1::add_reference<typename const_trait<reference_type>::add>::type const_reference_type;
// cv_reference_type = reference to cv-qualified T_type, even if T_type is a reference
// const volatile T_type&
typedef typename std::tr1::add_reference<typename const_trait<volatile_type>::add>::type cv_reference_type;
// apply const qualifier and reference to toplevel type, unchanged if toplevel type is a reference
typedef typename std::tr1::add_reference<typename std::tr1::add_const<acquired_type>::type>::type toplevel_const_reference_type;
/** @short Default constructor.
*
* @e T_type is initialized with its default ctor or its default value
*/
safe_lockable():
parent_type(),
m_obj()
{}
/** @short Constructs a lockable initializing @e T_type with @e _a_value
*/
safe_lockable(toplevel_const_reference_type _a_value):
parent_type(),
m_obj(_a_value)
{}
protected:
/** @return reference to volatile @e T_type
*/
volatile_reference_type access_volatile() throw()
{
return m_obj;
}
/** @return reference to non-volatile @e T_type
*/
reference_type access_nonvolatile() throw()
{
// volatile_cast m_obj
return const_cast<reference_type>(m_obj);
}
/** @return reference to volatile @e T_type
*/
cv_reference_type access_volatile() const throw()
{
return m_obj;
}
/** @return reference to non-volatile @e T_type
*/
const_reference_type access_nonvolatile() const throw()
{
// volatile_cast m_obj
return const_cast<const_reference_type>(m_obj);
}
private:
/** @short store volatile @e T_type
*/
volatile_type m_obj;
};
/** @short Refinement of safe_lockable, open access to mutex and locked type.
*/
template<typename T_type, typename T_mutex>
struct lockable: public safe_lockable<T_type, T_mutex>
{
typedef safe_lockable<T_type, T_mutex> parent_type;
typedef typename parent_type::toplevel_const_reference_type toplevel_const_reference_type;
public:
/** @short Default constructor.
*
* @e T_type is initialized with its default ctor or its default value
*/
lockable():
parent_type()
{}
/** @short Constructs a lockable initializing @e T_type with @e _a_value
*/
lockable(toplevel_const_reference_type _a_value):
parent_type(_a_value)
{}
// make safe_lockable's interface publicly available
using parent_type::access_volatile;
using parent_type::access_nonvolatile;
};
#if 0 // specializations for pointers
/** @short Makes a void pointer %lockable.
*
* The following template arguments are used:
* - @e T_mutex The lock to protect @e void*, e.g. a boost::mutex
*
* @code
* typedef lockable<void*, boost::mutex> mutex_lockable_void_ptr;
* @endcode
*/
template<typename T_mutex>
struct lockable<void*, T_mutex>: public lockable_base<T_mutex>
{
typedef void* acquired_type;
typedef T_mutex mutex_type;
typedef lockable_base<mutex_type> parent_type;
typedef lockable<acquired_type, mutex_type> type;
typedef typename volatile_trait<acquired_type>::add volatile_type;
typedef typename std::tr1::add_reference<typename volatile_trait<acquired_type>::remove>::type reference_type;
typedef typename std::tr1::add_reference<typename volatile_trait<acquired_type>::add>::type volatile_reference_type;
typedef typename std::tr1::add_reference<typename std::tr1::add_const<acquired_type>::type>::type take_type;
/** @short Default constructor.
*
* The void pointer is initialized with the provided pointer or to 0
*/
lockable(take_type _a_value = 0):
parent_type(),
m_obj(_a_value)
{}
/** @return reference to volatile void*
*/
volatile_reference_type access_volatile()
{
return m_obj;
}
/** @return reference to volatile @e T_type
*/
reference_type access_nonvolatile()
{
// volatile_cast m_obj
return const_cast<reference_type>(m_obj);
}
private:
/** @short volatile void*
*/
volatile_type m_obj;
};
/** @short Makes any type of pointer %lockable.
*
* The following template arguments are used:
* - @e T_type The pointer type to be protected, e.g. an int*
* - @e T_mutex The lock to protect @e T_type, e.g. a boost::mutex
*
* @code
* typedef lockable<int*, boost::mutex> mutex_lockable_int_ptr;
* @endcode
*/
template<typename T_type, typename T_mutex>
struct lockable<T_type*, T_mutex>: public lockable<void*, T_mutex>
{
typedef lockable<void*, T_mutex> parent_type;
typedef T_type* acquired_type;
typedef lockable<acquired_type, mutex_type> type;
typedef typename volatile_trait<acquired_type>::add volatile_type;
typedef typename std::tr1::add_reference<typename volatile_trait<acquired_type>::remove>::type reference_type;
typedef typename std::tr1::add_reference<typename volatile_trait<acquired_type>::add>::type volatile_reference_type;
typedef typename std::tr1::add_reference<typename std::tr1::add_const<acquired_type>::type>::type take_type;
/** @short default ctor.
*
* @e T_type is initialized with the provided pointer or to 0
*/
lockable(take_type _a_value = 0):
parent_type((void*&) _a_value)
{}
/** @return reference to volatile @e T_type
problem by now:
void* volatile p = 0;
const int* volatile& p1 = reinterpret_cast<const int* volatile&>(p);
*/
volatile_reference_type access_volatile()
{
return (volatile_reference_type) parent_type::access_volatile();
}
/** @return reference to volatile @e T_type
*/
reference_type access_acquiree()
{
return (reference_type) parent_type::access_acquiree();
}
};
#endif
// @addtogroup threadsafety
/** @}
*/
} // namespace sigx
#endif // end file guard
|