fw4spl
ObjectReadToWriteLock.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwData/mt/ObjectReadToWriteLock.hpp"
8 
9 #include <boost/thread/locks.hpp>
10 
11 namespace fwData
12 {
13 namespace mt
14 {
15 
16 //-----------------------------------------------------------------------------
17 
18 ObjectReadToWriteLock::ObjectReadToWriteLock(::fwData::Object::sptr obj, bool lock)
19 {
20  if (lock)
21  {
22  m_lock = ::fwCore::mt::ReadToWriteLock(obj->getMutex());
23  }
24  else
25  {
26  m_lock = ::fwCore::mt::ReadToWriteLock(obj->getMutex(), ::boost::defer_lock_t());
27  }
28 }
29 
30 //-----------------------------------------------------------------------------
31 
33 {
34 }
35 
36 //-----------------------------------------------------------------------------
37 
39 {
40  m_lock.lock();
41 }
42 
43 //-----------------------------------------------------------------------------
44 
46 {
47  m_lock.unlock();
48 }
49 
50 //-----------------------------------------------------------------------------
51 
53 {
54  m_upgradedLock = std::make_shared< ::fwCore::mt::UpgradeToWriteLock >(std::ref(m_lock));
55 }
56 
57 //-----------------------------------------------------------------------------
58 
60 {
61  m_upgradedLock.reset();
62 }
63 
64 //-----------------------------------------------------------------------------
65 
66 } // mt
67 } // fwData
FWDATA_API void downgrade()
Downgrade exclusive lock to upgradable lock.
::boost::upgrade_lock< ReadWriteMutex > ReadToWriteLock
Defines an upgradable lock type for read/write mutex.
FWDATA_API void upgrade()
Upgrade lock to exclusive.
FWDATA_API void lock()
Adds upgradable lock on object mutex.
FWDATA_API void unlock()
Releases upgradable lock on object mutex.
FWDATA_API ~ObjectReadToWriteLock()
Destructor. Does nothing.
Contains the representation of the data objects used in the framework.
FWDATA_API ObjectReadToWriteLock(::fwData::Object::sptr obj, bool lock=true)
Constructor : owns an upgradable lock on object mutex. If adopt_lock==false : the mutex is not locked...