1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2001 by Andrei Alexandrescu
4 // This code is from the article:
5 // "Generic<Programming>: volatile
\97 Multithreaded Programmer
\92s Best Friend
6 // Volatile-Correctness or How to Have Your Compiler Detect Race Conditions
7 // for You" by Alexandrescu, Andrei.
8 // Published in the February 2001 issue of the C/C++ Users Journal.
9 // http://www.cuj.com/documents/s=7998/cujcexp1902alexandr/
10 // Permission to use, copy, modify, distribute and sell this software for any
11 // purpose is hereby granted without fee, provided that the above copyright
12 // notice appear in all copies and that both that copyright notice and this
13 // permission notice appear in supporting documentation.
14 // The author makes no representations about the
15 // suitability of this software for any purpose. It is provided "as is"
16 // without express or implied warranty.
17 // Prepared for Loki library by Richard Sposato
18 ////////////////////////////////////////////////////////////////////////////////
19 #ifndef LOKI_LOCKING_PTR_INC_
20 #define LOKI_LOCKING_PTR_INC_
22 // $Id: LockingPtr.h 840 2008-03-19 19:44:38Z rich_sposato $
25 #include <loki/ConstPolicy.h>
26 #include <loki/Threads.h>
32 Locks a volatile object and casts away volatility so that the object
33 can be safely used in a single-threaded region of code.
34 Original version of LockingPtr had only one template - for the shared
35 object, but not the mutex type. This version allows users to specify a
36 the mutex type as a LockingPolicy class. The only requirements for a
37 LockingPolicy class are to provide Lock and Unlock methods.
39 template < typename SharedObject, typename LockingPolicy = LOKI_DEFAULT_MUTEX,
40 template<class> class ConstPolicy = LOKI_DEFAULT_CONSTNESS >
45 typedef typename ConstPolicy<SharedObject>::Type ConstOrNotType;
47 /** Constructor locks mutex associated with an object.
48 @param object Reference to object.
49 @param mutex Mutex used to control thread access to object.
51 LockingPtr( volatile ConstOrNotType &object, LockingPolicy &mutex )
52 : pObject_( const_cast< SharedObject * >( &object ) ),
58 typedef typename std::pair<volatile ConstOrNotType *, LockingPolicy *> Pair;
60 /** Constructor locks mutex associated with an object.
61 @param lockpair a std::pair of pointers to the object and the mutex
63 LockingPtr( Pair lockpair )
64 : pObject_( const_cast< SharedObject * >( lockpair.first ) ),
65 pMutex_( lockpair.second )
67 lockpair.second->Lock();
70 /// Destructor unlocks the mutex.
76 /// Star-operator dereferences pointer.
77 ConstOrNotType &operator * ()
82 /// Point-operator returns pointer to object.
83 ConstOrNotType *operator -> ()
90 /// Default constructor is not implemented.
93 /// Copy-constructor is not implemented.
94 LockingPtr( const LockingPtr & );
96 /// Copy-assignment-operator is not implemented.
97 LockingPtr &operator = ( const LockingPtr & );
99 /// Pointer to the shared object.
100 ConstOrNotType *pObject_;
102 /// Pointer to the mutex.
103 LockingPolicy *pMutex_;
105 }; // end class LockingPtr
109 #endif // end file guardian