sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable > Class Template Reference
[Threadsafety]

Locks the given mutex and ensures threadsafe write access to the given locked type. More...

#include <lock_acquirer.h>

Inheritance diagram for sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >:

sigx::noncopyable sigx::nonpointeraliasing

Public Member Functions

 lock_acquirer (volatile_reference_type _a_value, mutex_type &_a_mutex)
 Constructs a lock_acquirer from a volatile type to protect and a lock.
template<typename T_lockfwd_arg1 >
 lock_acquirer (volatile_reference_type _a_value, mutex_type &_a_mutex, T_lockfwd_arg1 lockfwd_arg1)
 Constructs a lock_acquirer from a volatile type to protect, a lock and an additional argument forwarded to the lock constructor.

Protected Types

typedef T_type acquired_type
typedef T_mutex mutex_type
typedef
std::tr1::remove_reference
< acquired_type >::type 
value_type
typedef boost::mpl::eval_if_c
< I_policy==readlock,
std::tr1::add_const
< value_type >
, boost::mpl::identity
< value_type > >::type 
const_or_value_type
typedef
std::tr1::add_reference
< typename
std::tr1::add_volatile
< value_type >::type >::type 
volatile_reference_type
typedef
std::tr1::add_reference
< typename
std::tr1::remove_volatile
< const_or_value_type >::type >
::type 
reference_type

Protected Member Functions

reference_type access_acquiree () throw ()

Protected Attributes

choose_lock< mutex_type,
I_policy >::type 
m_lock
 lock manager appropriate for the lock type
reference_type m_acquiree
 non-const reference to the locked object

Friends

reference_type access_acquiree (lock_acquirer &l) throw ()
 Gives non-volatile access to the locked type.

Detailed Description

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
class sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >

Locks the given mutex and ensures threadsafe write access to the given locked type.

Collects acquisition of a mutex lock and a volatile_cast from a volatile object. A lock_acquirer object is initialized with a lock from an associated mutex and a volatile object. The appropriate lock is chosen by the metafunction choose_lock according to the mutex and the locking policy (read/write). Note that because the lock_acquirer is scope bound choose_lock must only choose scoped lock types.

During its lifetime, a lock_acquirer keeps the lock acquired. Also, lock_acquirer offers read or write access (according to the locking policy) to the volatile-stripped object. Access is granted by a protected friend template function access_acquiree(). The volatile_cast is performed by access_acquiree(). The cast is semantically valid because lock_acquirer keeps the lock acquired for its lifetime.

If the locking policy is readlock then the lock_acquirer grants only const access to the protected variable.

The following template arguments are used:

Note:
The locked type can only be accessed with access_acquiree()
    // somewhere
    boost::mutex mtx;
    int x;
    
    // a scope somewhere else
    {
        lock_acquirer<writelock, int, boost::mutex> l(x, mtx);
        int& i = access_acquiree(l);
        i = 42;
    }

Member Typedef Documentation

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
typedef T_type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::acquired_type [protected]

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
typedef boost::mpl::eval_if_c< I_policy == readlock, std::tr1::add_const<value_type>, boost::mpl::identity<value_type> >::type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::const_or_value_type [protected]

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
typedef T_mutex sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::mutex_type [protected]

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
typedef std::tr1::add_reference<typename std::tr1::remove_volatile<const_or_value_type>::type>::type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::reference_type [protected]

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
typedef std::tr1::remove_reference<acquired_type>::type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::value_type [protected]

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
typedef std::tr1::add_reference<typename std::tr1::add_volatile<value_type>::type>::type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::volatile_reference_type [protected]


Constructor & Destructor Documentation

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::lock_acquirer ( volatile_reference_type  _a_value,
mutex_type _a_mutex 
) [inline]

Constructs a lock_acquirer from a volatile type to protect and a lock.

Note:
Acquires the lock immediately, unlocks when it goes out of scope.
Attention:
We rely here on the fact that members are initialized according to the order in which they are declared in a class, such that the lock is acquired before _a_value is accessed non-volatile.

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
template<typename T_lockfwd_arg1 >
sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::lock_acquirer ( volatile_reference_type  _a_value,
mutex_type _a_mutex,
T_lockfwd_arg1  lockfwd_arg1 
) [inline]

Constructs a lock_acquirer from a volatile type to protect, a lock and an additional argument forwarded to the lock constructor.

Note:
Acquires the lock immediately, unlocks when it goes out of scope


Member Function Documentation

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
reference_type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::access_acquiree (  )  throw () [inline, protected]

Returns:
The locked type with the `volatile" qualifier removed


Friends And Related Function Documentation

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
reference_type access_acquiree ( lock_acquirer< I_policy, T_type, T_mutex, T_islockable > &  l  )  throw () [friend]

Gives non-volatile access to the locked type.

Forces the programmer to pass a previously named lock_acquirer object thus ensuring that the lock is active throughout the usage of the locked object.


Field Documentation

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
reference_type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::m_acquiree [protected]

template<locking_policy I_policy, typename T_type, typename T_mutex, typename T_islockable>
choose_lock<mutex_type, I_policy>::type sigx::lock_acquirer< I_policy, T_type, T_mutex, T_islockable >::m_lock [protected]

lock manager appropriate for the lock type


The documentation for this class was generated from the following file:

Generated on Mon Aug 3 17:35:09 2009 for sigx++ by  doxygen 1.5.9