1//
2// NamedMutex.h
3//
4// Library: Foundation
5// Package: Processes
6// Module: NamedMutex
7//
8// Definition of the NamedMutex class.
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_NamedMutex_INCLUDED
18#define Foundation_NamedMutex_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/ScopedLock.h"
23
24
25#if defined(POCO_OS_FAMILY_WINDOWS)
26#include "Poco/NamedMutex_WIN32.h"
27#elif defined(POCO_ANDROID)
28#include "Poco/NamedMutex_Android.h"
29#elif defined(POCO_OS_FAMILY_UNIX)
30#include "Poco/NamedMutex_UNIX.h"
31#else
32#include "Poco/NamedMutex_VMS.h"
33#endif
34
35
36namespace Poco {
37
38
39class Foundation_API NamedMutex: private NamedMutexImpl
40 /// A NamedMutex (mutual exclusion) is a global synchronization
41 /// mechanism used to control access to a shared resource
42 /// in a concurrent (multi process) scenario.
43 /// Using the ScopedLock class is the preferred way to automatically
44 /// lock and unlock a mutex.
45 ///
46 /// Unlike a Mutex or a FastMutex, which itself is the unit of synchronization,
47 /// a NamedMutex refers to a named operating system resource being the
48 /// unit of synchronization.
49 /// In other words, there can be multiple instances of NamedMutex referring
50 /// to the same actual synchronization object.
51 ///
52 ///
53 /// There should not be more than one instance of NamedMutex for
54 /// a given name in a process. Otherwise, the instances may
55 /// interfere with each other.
56{
57public:
58 typedef Poco::ScopedLock<NamedMutex> ScopedLock;
59
60 NamedMutex(const std::string& name);
61 /// creates the Mutex.
62
63 ~NamedMutex();
64 /// destroys the Mutex.
65
66 void lock();
67 /// Locks the mutex. Blocks if the mutex
68 /// is held by another process or thread.
69
70 bool tryLock();
71 /// Tries to lock the mutex. Returns false immediately
72 /// if the mutex is already held by another process or thread.
73 /// Returns true if the mutex was successfully locked.
74
75 void unlock();
76 /// Unlocks the mutex so that it can be acquired by
77 /// other threads.
78
79private:
80 NamedMutex();
81 NamedMutex(const NamedMutex&);
82 NamedMutex& operator = (const NamedMutex&);
83};
84
85
86//
87// inlines
88//
89inline void NamedMutex::lock()
90{
91 lockImpl();
92}
93
94
95inline bool NamedMutex::tryLock()
96{
97 return tryLockImpl();
98}
99
100
101inline void NamedMutex::unlock()
102{
103 unlockImpl();
104}
105
106
107} // namespace Poco
108
109
110#endif // Foundation_NamedMutex_INCLUDED
111