File: configobjectslock.hpp

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 (102 lines) | stat: -rw-r--r-- 2,239 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */

#pragma once

#include "base/type.hpp"
#include "base/string.hpp"
#include <condition_variable>
#include <map>
#include <mutex>
#include <set>

#ifndef _WIN32
#include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/sharable_lock.hpp>
#endif /* _WIN32 */

namespace icinga
{

#ifdef _WIN32

class ConfigObjectsSharedLock
{
public:
	inline ConfigObjectsSharedLock(std::try_to_lock_t)
	{
	}

	constexpr explicit operator bool() const
	{
		return true;
	}
};

#else /* _WIN32 */

/**
 * Waits until all ConfigObjects*Lock-s have vanished. For its lifetime disallows such.
 * Keep an instance alive during reload to forbid runtime config changes!
 * This way Icinga reads a consistent config which doesn't suddenly get runtime-changed.
 *
 * @ingroup remote
 */
class ConfigObjectsExclusiveLock
{
public:
	ConfigObjectsExclusiveLock();

private:
	boost::interprocess::scoped_lock<boost::interprocess::interprocess_sharable_mutex> m_Lock;
};

/**
 * Waits until the only ConfigObjectsExclusiveLock has vanished (if any). For its lifetime disallows such.
 * Keep an instance alive during runtime config changes to delay a reload (if any)!
 * This way Icinga reads a consistent config which doesn't suddenly get runtime-changed.
 *
 * @ingroup remote
 */
class ConfigObjectsSharedLock
{
public:
	ConfigObjectsSharedLock(std::try_to_lock_t);

	inline explicit operator bool() const
	{
		return m_Lock.owns();
	}

private:
	boost::interprocess::sharable_lock<boost::interprocess::interprocess_sharable_mutex> m_Lock;
};

#endif /* _WIN32 */


/**
 * Allows you to easily lock/unlock a specific object of a given type by its name.
 *
 * That way, locking an object "this" of type Host does not affect an object "this" of
 * type "Service" nor an object "other" of type "Host".
 *
 * @ingroup remote
 */
class ObjectNameLock
{
public:
	ObjectNameLock(const Type::Ptr& ptype, const String& objName);

	~ObjectNameLock();

private:
	String m_ObjectName;
	Type::Ptr m_Type;

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

}