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 POCO_OS == POCO_OS_ANDROID
28#include "Poco/NamedMutex_Android.h"
29#elif defined(POCO_OS_FAMILY_UNIX)
30#include "Poco/NamedMutex_UNIX.h"
31#endif
32
33
34namespace Poco {
35
36
37class Foundation_API NamedMutex: private NamedMutexImpl
38 /// A NamedMutex (mutual exclusion) is a global synchronization
39 /// mechanism used to control access to a shared resource
40 /// in a concurrent (multi process) scenario.
41 /// Using the ScopedLock class is the preferred way to automatically
42 /// lock and unlock a mutex.
43 ///
44 /// Unlike a Mutex or a FastMutex, which itself is the unit of synchronization,
45 /// a NamedMutex refers to a named operating system resource being the
46 /// unit of synchronization.
47 /// In other words, there can be multiple instances of NamedMutex referring
48 /// to the same actual synchronization object.
49 ///
50 ///
51 /// There should not be more than one instance of NamedMutex for
52 /// a given name in a process. Otherwise, the instances may
53 /// interfere with each other.
54{
55public:
56 typedef Poco::ScopedLock<NamedMutex> ScopedLock;
57
58 NamedMutex(const std::string& name);
59 /// creates the Mutex.
60
61 ~NamedMutex();
62 /// destroys the Mutex.
63
64 void lock();
65 /// Locks the mutex. Blocks if the mutex
66 /// is held by another process or thread.
67
68 bool tryLock();
69 /// Tries to lock the mutex. Returns false immediately
70 /// if the mutex is already held by another process or thread.
71 /// Returns true if the mutex was successfully locked.
72
73 void unlock();
74 /// Unlocks the mutex so that it can be acquired by
75 /// other threads.
76
77private:
78 NamedMutex();
79 NamedMutex(const NamedMutex&);
80 NamedMutex& operator = (const NamedMutex&);
81};
82
83
84//
85// inlines
86//
87inline void NamedMutex::lock()
88{
89 lockImpl();
90}
91
92
93inline bool NamedMutex::tryLock()
94{
95 return tryLockImpl();
96}
97
98
99inline void NamedMutex::unlock()
100{
101 unlockImpl();
102}
103
104
105} // namespace Poco
106
107
108#endif // Foundation_NamedMutex_INCLUDED
109