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