source: XMLIO_V2/external/src/POCO/Foundation.save/Poco/AtomicCounter.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: 7.3 KB
Line 
1//
2// AtomicCounter.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/AtomicCounter.h#6 $
5//
6// Library: Foundation
7// Package: Core
8// Module:  AtomicCounter
9//
10// Definition of the AtomicCounter class.
11//
12// Copyright (c) 2009, 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_AtomicCounter_INCLUDED
40#define Foundation_AtomicCounter_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#if POCO_OS == POCO_OS_WINDOWS_NT
45#include "Poco/UnWindows.h"
46#elif POCO_OS == POCO_OS_MAC_OS_X
47#include <libkern/OSAtomic.h>
48#else
49#include "Poco/Mutex.h"
50#endif // POCO_OS
51
52
53namespace Poco {
54
55
56class Foundation_API AtomicCounter
57        /// This class implements a simple counter, which
58        /// provides atomic operations that are safe to
59        /// use in a multithreaded environment.
60        ///
61        /// Typical usage of AtomicCounter is for implementing
62        /// reference counting and similar things.
63        ///
64        /// On some platforms, the implementation of AtomicCounter
65        /// is based on atomic primitives specific to the platform
66        /// (such as InterlockedIncrement, etc. on Windows), and
67        /// thus very efficient. On platforms that do not support
68        /// atomic primitives, operations are guarded by a FastMutex.
69        ///
70        /// The following platforms currently have atomic
71        /// primitives:
72        ///   - Windows
73        ///   - Mac OS X
74{
75public:
76        typedef int ValueType; /// The underlying integer type.
77       
78        AtomicCounter();
79                /// Creates a new AtomicCounter and initializes it to zero.
80               
81        explicit AtomicCounter(ValueType initialValue);
82                /// Creates a new AtomicCounter and initializes it with
83                /// the given value.
84       
85        AtomicCounter(const AtomicCounter& counter);
86                /// Creates the counter by copying another one.
87       
88        ~AtomicCounter();
89                /// Destroys the AtomicCounter.
90
91        AtomicCounter& operator = (const AtomicCounter& counter);
92                /// Assigns the value of another AtomicCounter.
93               
94        AtomicCounter& operator = (ValueType value);
95                /// Assigns a value to the counter.
96
97        operator ValueType () const;
98                /// Returns the value of the counter.
99               
100        ValueType value() const;
101                /// Returns the value of the counter.
102               
103        ValueType operator ++ (); // prefix
104                /// Increments the counter and returns the result.
105               
106        ValueType operator ++ (int); // postfix
107                /// Increments the counter and returns the previous value.
108               
109        ValueType operator -- (); // prefix
110                /// Decrements the counter and returns the result.
111               
112        ValueType operator -- (int); // postfix
113                /// Decrements the counter and returns the previous value.
114               
115        bool operator ! () const;
116                /// Returns true if the counter is zero, false otherwise.
117
118private:
119#if POCO_OS == POCO_OS_WINDOWS_NT
120        typedef volatile LONG ImplType;
121#elif POCO_OS == POCO_OS_MAC_OS_X
122        typedef int32_t ImplType;
123#else // generic implementation based on FastMutex
124        struct ImplType
125        {
126                mutable FastMutex mutex;
127                volatile int      value;
128        };
129#endif // POCO_OS
130
131        ImplType _counter;
132};
133
134
135//
136// inlines
137//
138
139
140#if POCO_OS == POCO_OS_WINDOWS_NT
141//
142// Windows
143//
144inline AtomicCounter::operator AtomicCounter::ValueType () const
145{
146        return _counter;
147}
148
149       
150inline AtomicCounter::ValueType AtomicCounter::value() const
151{
152        return _counter;
153}
154
155
156inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
157{
158        return InterlockedIncrement(&_counter);
159}
160
161       
162inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
163{
164        ValueType result(_counter);
165        InterlockedIncrement(&_counter);
166        return result;
167}
168
169
170inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
171{
172        return InterlockedDecrement(&_counter);
173}
174
175       
176inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
177{
178        ValueType result(_counter);
179        InterlockedDecrement(&_counter);
180        return result;
181}
182
183       
184inline bool AtomicCounter::operator ! () const
185{
186        return _counter == 0;
187}
188
189
190#elif POCO_OS == POCO_OS_MAC_OS_X
191//
192// Mac OS X
193//
194inline AtomicCounter::operator AtomicCounter::ValueType () const
195{
196        return _counter;
197}
198
199       
200inline AtomicCounter::ValueType AtomicCounter::value() const
201{
202        return _counter;
203}
204
205
206inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
207{
208        return OSAtomicIncrement32(&_counter);
209}
210
211       
212inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
213{
214        ValueType result(_counter);
215        OSAtomicIncrement32(&_counter);
216        return result;
217}
218
219
220inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
221{
222        return OSAtomicDecrement32(&_counter);
223}
224
225       
226inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
227{
228        ValueType result(_counter);
229        OSAtomicDecrement32(&_counter);
230        return result;
231}
232
233       
234inline bool AtomicCounter::operator ! () const
235{
236        return _counter == 0;
237}
238
239
240#else
241//
242// Generic implementation based on FastMutex
243//
244inline AtomicCounter::operator AtomicCounter::ValueType () const
245{
246        ValueType result;
247        {
248                FastMutex::ScopedLock lock(_counter.mutex);
249                result = _counter.value;
250        }
251        return result;
252}
253
254       
255inline AtomicCounter::ValueType AtomicCounter::value() const
256{
257        ValueType result;
258        {
259                FastMutex::ScopedLock lock(_counter.mutex);
260                result = _counter.value;
261        }
262        return result;
263}
264
265
266inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
267{
268        ValueType result;
269        {
270                FastMutex::ScopedLock lock(_counter.mutex);
271                result = ++_counter.value;
272        }
273        return result;
274}
275
276       
277inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
278{
279        ValueType result;
280        {
281                FastMutex::ScopedLock lock(_counter.mutex);
282                result = _counter.value++;
283        }
284        return result;
285}
286
287
288inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
289{
290        ValueType result;
291        {
292                FastMutex::ScopedLock lock(_counter.mutex);
293                result = --_counter.value;
294        }
295        return result;
296}
297
298       
299inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
300{
301        ValueType result;
302        {
303                FastMutex::ScopedLock lock(_counter.mutex);
304                result = _counter.value--;
305        }
306        return result;
307}
308
309       
310inline bool AtomicCounter::operator ! () const
311{
312        bool result;
313        {
314                FastMutex::ScopedLock lock(_counter.mutex);
315                result = _counter.value == 0;
316        }
317        return result;
318}
319
320
321#endif // POCO_OS
322
323
324} // namespace Poco
325
326
327#endif // Foundation_AtomicCounter_INCLUDED
Note: See TracBrowser for help on using the repository browser.