root / Community Core Vision / Windows / libs / poco / include / Poco / NamedMutex.h @ 8

View | Annotate | Download (3.7 KB)

1 8 amit
//
2 8 amit
// NamedMutex.h
3 8 amit
//
4 8 amit
// $Id: //poco/1.3/Foundation/include/Poco/NamedMutex.h#1 $
5 8 amit
//
6 8 amit
// Library: Foundation
7 8 amit
// Package: Processes
8 8 amit
// Module:  NamedMutex
9 8 amit
//
10 8 amit
// Definition of the NamedMutex class.
11 8 amit
//
12 8 amit
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13 8 amit
// and Contributors.
14 8 amit
//
15 8 amit
// Permission is hereby granted, free of charge, to any person or organization
16 8 amit
// obtaining a copy of the software and accompanying documentation covered by
17 8 amit
// this license (the "Software") to use, reproduce, display, distribute,
18 8 amit
// execute, and transmit the Software, and to prepare derivative works of the
19 8 amit
// Software, and to permit third-parties to whom the Software is furnished to
20 8 amit
// do so, all subject to the following:
21 8 amit
//
22 8 amit
// The copyright notices in the Software and this entire statement, including
23 8 amit
// the above license grant, this restriction and the following disclaimer,
24 8 amit
// must be included in all copies of the Software, in whole or in part, and
25 8 amit
// all derivative works of the Software, unless such copies or derivative
26 8 amit
// works are solely in the form of machine-executable object code generated by
27 8 amit
// a source language processor.
28 8 amit
//
29 8 amit
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 8 amit
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 8 amit
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32 8 amit
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33 8 amit
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34 8 amit
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35 8 amit
// DEALINGS IN THE SOFTWARE.
36 8 amit
//
37 8 amit
38 8 amit
39 8 amit
#ifndef Foundation_NamedMutex_INCLUDED
40 8 amit
#define Foundation_NamedMutex_INCLUDED
41 8 amit
42 8 amit
43 8 amit
#include "Poco/Foundation.h"
44 8 amit
#include "Poco/ScopedLock.h"
45 8 amit
46 8 amit
47 8 amit
#if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
48 8 amit
#include "Poco/NamedMutex_WIN32U.h"
49 8 amit
#elif defined(POCO_OS_FAMILY_WINDOWS)
50 8 amit
#include "Poco/NamedMutex_WIN32.h"
51 8 amit
#elif defined(POCO_OS_FAMILY_UNIX)
52 8 amit
#include "Poco/NamedMutex_UNIX.h"
53 8 amit
#else
54 8 amit
#include "Poco/NamedMutex_VMS.h"
55 8 amit
#endif
56 8 amit
57 8 amit
58 8 amit
namespace Poco {
59 8 amit
60 8 amit
61 8 amit
class Foundation_API NamedMutex: private NamedMutexImpl
62 8 amit
        /// A NamedMutex (mutual exclusion) is a global synchronization
63 8 amit
        /// mechanism used to control access to a shared resource
64 8 amit
        /// in a concurrent (multi process) scenario.
65 8 amit
        /// Using the ScopedLock class is the preferred way to automatically
66 8 amit
        /// lock and unlock a mutex.
67 8 amit
        ///
68 8 amit
        /// Unlike a Mutex or a FastMutex, which itself is the unit of synchronization,
69 8 amit
        /// a NamedMutex refers to a named operating system resource being the
70 8 amit
        /// unit of synchronization.
71 8 amit
        /// In other words, there can be multiple instances of NamedMutex referring
72 8 amit
        /// to the same actual synchronization object.
73 8 amit
        ///
74 8 amit
        ///
75 8 amit
        /// There should not be more than one instance of NamedMutex for
76 8 amit
        /// a given name in a process. Otherwise, the instances may
77 8 amit
        /// interfere with each other.
78 8 amit
{
79 8 amit
public:
80 8 amit
        typedef Poco::ScopedLock<NamedMutex> ScopedLock;
81 8 amit
82 8 amit
        NamedMutex(const std::string& name);
83 8 amit
                /// creates the Mutex.
84 8 amit
85 8 amit
        ~NamedMutex();
86 8 amit
                /// destroys the Mutex.
87 8 amit
88 8 amit
        void lock();
89 8 amit
                /// Locks the mutex. Blocks if the mutex
90 8 amit
                /// is held by another process or thread.
91 8 amit
92 8 amit
        bool tryLock();
93 8 amit
                /// Tries to lock the mutex. Returns false immediately
94 8 amit
                /// if the mutex is already held by another process or thread.
95 8 amit
                /// Returns true if the mutex was successfully locked.
96 8 amit
97 8 amit
        void unlock();
98 8 amit
                /// Unlocks the mutex so that it can be acquired by
99 8 amit
                /// other threads.
100 8 amit
101 8 amit
private:
102 8 amit
        NamedMutex();
103 8 amit
        NamedMutex(const NamedMutex&);
104 8 amit
        NamedMutex& operator = (const NamedMutex&);
105 8 amit
};
106 8 amit
107 8 amit
108 8 amit
//
109 8 amit
// inlines
110 8 amit
//
111 8 amit
inline void NamedMutex::lock()
112 8 amit
{
113 8 amit
        lockImpl();
114 8 amit
}
115 8 amit
116 8 amit
117 8 amit
inline bool NamedMutex::tryLock()
118 8 amit
{
119 8 amit
        return tryLockImpl();
120 8 amit
}
121 8 amit
122 8 amit
123 8 amit
inline void NamedMutex::unlock()
124 8 amit
{
125 8 amit
        unlockImpl();
126 8 amit
}
127 8 amit
128 8 amit
129 8 amit
} // namespace Poco
130 8 amit
131 8 amit
132 8 amit
#endif // Foundation_NamedMutex_INCLUDED