2005-11-19 22:00:23 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// The Loki Library
|
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
|
// This code is from the article:
|
|
|
|
|
// "Generic<Programming>: volatile <20> Multithreaded Programmer<65>s Best Friend
|
|
|
|
|
// Volatile-Correctness or How to Have Your Compiler Detect Race Conditions
|
|
|
|
|
// for You" by Alexandrescu, Andrei.
|
|
|
|
|
// Published in the February 2001 issue of the C/C++ Users Journal.
|
|
|
|
|
// http://www.cuj.com/documents/s=7998/cujcexp1902alexandr/
|
2011-09-23 00:46:21 +00:00
|
|
|
|
//
|
|
|
|
|
// Code covered by the MIT License
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
//
|
2005-11-19 22:00:23 +00:00
|
|
|
|
// Prepared for Loki library by Richard Sposato
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
#ifndef LOKI_LOCKING_PTR_INC_
|
|
|
|
|
#define LOKI_LOCKING_PTR_INC_
|
|
|
|
|
|
2006-10-17 19:49:08 +00:00
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 22:04:28 +00:00
|
|
|
|
#include <loki/ConstPolicy.h>
|
2008-03-19 19:44:38 +00:00
|
|
|
|
#include <loki/Threads.h>
|
|
|
|
|
|
2006-01-30 20:33:01 +00:00
|
|
|
|
|
2005-11-19 22:00:23 +00:00
|
|
|
|
namespace Loki
|
|
|
|
|
{
|
|
|
|
|
/** @class LockingPtr
|
|
|
|
|
Locks a volatile object and casts away volatility so that the object
|
|
|
|
|
can be safely used in a single-threaded region of code.
|
|
|
|
|
Original version of LockingPtr had only one template - for the shared
|
|
|
|
|
object, but not the mutex type. This version allows users to specify a
|
|
|
|
|
the mutex type as a LockingPolicy class. The only requirements for a
|
|
|
|
|
LockingPolicy class are to provide Lock and Unlock methods.
|
|
|
|
|
*/
|
2011-09-23 00:46:21 +00:00
|
|
|
|
template < typename SharedObject, typename LockingPolicy = LOKI_DEFAULT_MUTEX,
|
2006-01-30 20:33:01 +00:00
|
|
|
|
template<class> class ConstPolicy = LOKI_DEFAULT_CONSTNESS >
|
2005-11-19 22:00:23 +00:00
|
|
|
|
class LockingPtr
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2006-01-21 14:13:48 +00:00
|
|
|
|
typedef typename ConstPolicy<SharedObject>::Type ConstOrNotType;
|
|
|
|
|
|
2005-11-19 22:00:23 +00:00
|
|
|
|
/** Constructor locks mutex associated with an object.
|
2006-03-08 18:22:42 +00:00
|
|
|
|
@param object Reference to object.
|
|
|
|
|
@param mutex Mutex used to control thread access to object.
|
2005-11-19 22:00:23 +00:00
|
|
|
|
*/
|
2006-01-21 14:13:48 +00:00
|
|
|
|
LockingPtr( volatile ConstOrNotType & object, LockingPolicy & mutex )
|
2005-11-19 22:00:23 +00:00
|
|
|
|
: pObject_( const_cast< SharedObject * >( &object ) ),
|
|
|
|
|
pMutex_( &mutex )
|
|
|
|
|
{
|
|
|
|
|
mutex.Lock();
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-11 08:51:40 +00:00
|
|
|
|
typedef typename std::pair<volatile ConstOrNotType *, LockingPolicy *> Pair;
|
|
|
|
|
|
|
|
|
|
/** Constructor locks mutex associated with an object.
|
|
|
|
|
@param lockpair a std::pair of pointers to the object and the mutex
|
|
|
|
|
*/
|
2009-09-26 20:28:24 +00:00
|
|
|
|
explicit LockingPtr( Pair lockpair )
|
2006-10-11 08:51:40 +00:00
|
|
|
|
: pObject_( const_cast< SharedObject * >( lockpair.first ) ),
|
|
|
|
|
pMutex_( lockpair.second )
|
|
|
|
|
{
|
|
|
|
|
lockpair.second->Lock();
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-19 22:00:23 +00:00
|
|
|
|
/// Destructor unlocks the mutex.
|
|
|
|
|
~LockingPtr()
|
|
|
|
|
{
|
|
|
|
|
pMutex_->Unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Star-operator dereferences pointer.
|
2006-01-21 14:13:48 +00:00
|
|
|
|
ConstOrNotType & operator * ()
|
2005-11-19 22:00:23 +00:00
|
|
|
|
{
|
|
|
|
|
return *pObject_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Point-operator returns pointer to object.
|
2006-01-21 14:13:48 +00:00
|
|
|
|
ConstOrNotType * operator -> ()
|
2005-11-19 22:00:23 +00:00
|
|
|
|
{
|
|
|
|
|
return pObject_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
/// Default constructor is not implemented.
|
|
|
|
|
LockingPtr();
|
|
|
|
|
|
|
|
|
|
/// Copy-constructor is not implemented.
|
|
|
|
|
LockingPtr( const LockingPtr & );
|
|
|
|
|
|
|
|
|
|
/// Copy-assignment-operator is not implemented.
|
|
|
|
|
LockingPtr & operator = ( const LockingPtr & );
|
|
|
|
|
|
|
|
|
|
/// Pointer to the shared object.
|
2006-01-21 14:13:48 +00:00
|
|
|
|
ConstOrNotType * pObject_;
|
2005-11-19 22:00:23 +00:00
|
|
|
|
|
|
|
|
|
/// Pointer to the mutex.
|
|
|
|
|
LockingPolicy * pMutex_;
|
|
|
|
|
|
|
|
|
|
}; // end class LockingPtr
|
|
|
|
|
|
|
|
|
|
} // namespace Loki
|
|
|
|
|
|
2006-10-17 19:31:12 +00:00
|
|
|
|
#endif // end file guardian
|
|
|
|
|
|