source: XMLIO_V2/external/include/Poco/Mutex.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1//
2// Mutex.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/Mutex.h#2 $
5//
6// Library: Foundation
7// Package: Threading
8// Module:  Mutex
9//
10// Definition of the Mutex and FastMutex classes.
11//
12// Copyright (c) 2004-2008, 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_Mutex_INCLUDED
40#define Foundation_Mutex_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#include "Poco/Exception.h"
45#include "Poco/ScopedLock.h"
46
47
48#if defined(POCO_OS_FAMILY_WINDOWS)
49#include "Poco/Mutex_WIN32.h"
50#else
51#include "Poco/Mutex_POSIX.h"
52#endif
53
54
55namespace Poco {
56
57
58class Foundation_API Mutex: private MutexImpl
59        /// A Mutex (mutual exclusion) is a synchronization
60        /// mechanism used to control access to a shared resource
61        /// in a concurrent (multithreaded) scenario.
62        /// Mutexes are recursive, that is, the same mutex can be
63        /// locked multiple times by the same thread (but, of course,
64        /// not by other threads).
65        /// Using the ScopedLock class is the preferred way to automatically
66        /// lock and unlock a mutex.
67{
68public:
69        typedef Poco::ScopedLock<Mutex> ScopedLock;
70       
71        Mutex();
72                /// creates the Mutex.
73               
74        ~Mutex();
75                /// destroys the Mutex.
76
77        void lock();
78                /// Locks the mutex. Blocks if the mutex
79                /// is held by another thread.
80               
81        void lock(long milliseconds);
82                /// Locks the mutex. Blocks up to the given number of milliseconds
83                /// if the mutex is held by another thread. Throws a TimeoutException
84                /// if the mutex can not be locked within the given timeout.
85                ///
86                /// Performance Note: On most platforms (including Windows), this member function is
87                /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
88                /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
89
90        bool tryLock();
91                /// Tries to lock the mutex. Returns false immediately
92                /// if the mutex is already held by another thread.
93                /// Returns true if the mutex was successfully locked.
94
95        bool tryLock(long milliseconds);
96                /// Locks the mutex. Blocks up to the given number of milliseconds
97                /// if the mutex is held by another thread.
98                /// Returns true if the mutex was successfully locked.
99                ///
100                /// Performance Note: On most platforms (including Windows), this member function is
101                /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
102                /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
103
104        void unlock();
105                /// Unlocks the mutex so that it can be acquired by
106                /// other threads.
107       
108private:
109        Mutex(const Mutex&);
110        Mutex& operator = (const Mutex&);
111};
112
113
114class Foundation_API FastMutex: private FastMutexImpl
115        /// A FastMutex (mutual exclusion) is similar to a Mutex.
116        /// Unlike a Mutex, however, a FastMutex is not recursive,
117        /// which means that a deadlock will occur if the same
118        /// thread tries to lock a mutex it has already locked again.
119        /// Locking a FastMutex is faster than locking a recursive Mutex.
120        /// Using the ScopedLock class is the preferred way to automatically
121        /// lock and unlock a mutex.
122{
123public:
124        typedef Poco::ScopedLock<FastMutex> ScopedLock;
125
126        FastMutex();
127                /// creates the Mutex.
128               
129        ~FastMutex();
130                /// destroys the Mutex.
131
132        void lock();
133                /// Locks the mutex. Blocks if the mutex
134                /// is held by another thread.
135
136        void lock(long milliseconds);
137                /// Locks the mutex. Blocks up to the given number of milliseconds
138                /// if the mutex is held by another thread. Throws a TimeoutException
139                /// if the mutex can not be locked within the given timeout.
140                ///
141                /// Performance Note: On most platforms (including Windows), this member function is
142                /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
143                /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
144
145        bool tryLock();
146                /// Tries to lock the mutex. Returns false immediately
147                /// if the mutex is already held by another thread.
148                /// Returns true if the mutex was successfully locked.
149
150        bool tryLock(long milliseconds);
151                /// Locks the mutex. Blocks up to the given number of milliseconds
152                /// if the mutex is held by another thread.
153                /// Returns true if the mutex was successfully locked.
154                ///
155                /// Performance Note: On most platforms (including Windows), this member function is
156                /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
157                /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
158
159        void unlock();
160                /// Unlocks the mutex so that it can be acquired by
161                /// other threads.
162       
163private:
164        FastMutex(const FastMutex&);
165        FastMutex& operator = (const FastMutex&);
166};
167
168
169//
170// inlines
171//
172inline void Mutex::lock()
173{
174        lockImpl();
175}
176
177
178inline void Mutex::lock(long milliseconds)
179{
180        if (!tryLockImpl(milliseconds))
181                throw TimeoutException();
182}
183
184
185inline bool Mutex::tryLock()
186{
187        return tryLockImpl();
188}
189
190
191inline bool Mutex::tryLock(long milliseconds)
192{
193        return tryLockImpl(milliseconds);
194}
195
196
197inline void Mutex::unlock()
198{
199        unlockImpl();
200}
201
202
203inline void FastMutex::lock()
204{
205        lockImpl();
206}
207
208
209inline void FastMutex::lock(long milliseconds)
210{
211        if (!tryLockImpl(milliseconds))
212                throw TimeoutException();
213}
214
215
216inline bool FastMutex::tryLock()
217{
218        return tryLockImpl();
219}
220
221
222inline bool FastMutex::tryLock(long milliseconds)
223{
224        return tryLockImpl(milliseconds);
225}
226
227
228inline void FastMutex::unlock()
229{
230        unlockImpl();
231}
232
233
234} // namespace Poco
235
236
237#endif // Foundation_Mutex_INCLUDED
Note: See TracBrowser for help on using the repository browser.