File: configobjectslock.cpp

package info (click to toggle)
icinga2 2.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,040 kB
  • sloc: cpp: 97,870; sql: 3,261; cs: 1,636; yacc: 1,584; sh: 1,009; ansic: 890; lex: 420; python: 80; makefile: 62; javascript: 12
file content (61 lines) | stat: -rw-r--r-- 1,846 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
/* Icinga 2 | (c) 2022 Icinga GmbH | GPLv2+ */

#include "remote/configobjectslock.hpp"

#ifndef _WIN32
#include "base/shared-memory.hpp"
#include <boost/interprocess/sync/lock_options.hpp>
#endif /* _WIN32 */

using namespace icinga;

#ifndef _WIN32

// On *nix one process may write config objects while another is loading the config, so this uses IPC.
static SharedMemory<boost::interprocess::interprocess_sharable_mutex> l_ConfigObjectsMutex;

ConfigObjectsExclusiveLock::ConfigObjectsExclusiveLock()
	: m_Lock(l_ConfigObjectsMutex.Get())
{
}

ConfigObjectsSharedLock::ConfigObjectsSharedLock(std::try_to_lock_t)
	: m_Lock(l_ConfigObjectsMutex.Get(), boost::interprocess::try_to_lock)
{
}

#endif /* _WIN32 */

std::mutex ObjectNameLock::m_Mutex;
std::condition_variable ObjectNameLock::m_CV;
std::map<Type*, std::set<String>> ObjectNameLock::m_LockedObjectNames;

/**
 * Locks the specified object name of the given type and unlocks it upon destruction of the instance of this class.
 *
 * If it is already locked, the call blocks until the lock is released.
 *
 * @param Type::Ptr ptype The type of the object you want to lock
 * @param String objName The object name you want to lock
 */
ObjectNameLock::ObjectNameLock(const Type::Ptr& ptype, const String& objName): m_ObjectName{objName}, m_Type{ptype}
{
	std::unique_lock<std::mutex> lock(m_Mutex);
	m_CV.wait(lock, [this]{
		auto& locked = m_LockedObjectNames[m_Type.get()];
		return locked.find(m_ObjectName) == locked.end();
	});

	// Add the object name to the locked list to block all other threads that try
	// to process a message affecting the same object.
	m_LockedObjectNames[ptype.get()].emplace(objName);
}

ObjectNameLock::~ObjectNameLock()
{
	{
		std::unique_lock<std::mutex> lock(m_Mutex);
		m_LockedObjectNames[m_Type.get()].erase(m_ObjectName);
	}
	m_CV.notify_all();
}