blob: fc0c7c6314199bc7e1ae7f29a0d4eb891b7741a4 (
plain)
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
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/objectlock.hpp"
#include <thread>
using namespace icinga;
#define I2MUTEX_UNLOCKED 0
#define I2MUTEX_LOCKED 1
ObjectLock::~ObjectLock()
{
Unlock();
}
ObjectLock::ObjectLock(const Object::Ptr& object)
: ObjectLock(object.get())
{
}
ObjectLock::ObjectLock(const Object *object)
: m_Object(object), m_Locked(false)
{
if (m_Object)
Lock();
}
void ObjectLock::Lock()
{
ASSERT(!m_Locked && m_Object);
m_Object->m_Mutex.lock();
m_Locked = true;
#ifdef I2_DEBUG
if (++m_Object->m_LockCount == 1u) {
m_Object->m_LockOwner.store(std::this_thread::get_id());
}
#endif /* I2_DEBUG */
}
void ObjectLock::Unlock()
{
#ifdef I2_DEBUG
if (m_Locked && !--m_Object->m_LockCount) {
m_Object->m_LockOwner.store(decltype(m_Object->m_LockOwner.load())());
}
#endif /* I2_DEBUG */
if (m_Locked) {
m_Object->m_Mutex.unlock();
m_Locked = false;
}
}
|