mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-poco.git
synced 2025-11-01 19:17:59 +00:00
added AtomicCounter class
This commit is contained in:
@@ -249,6 +249,9 @@
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="">
|
||||
<File
|
||||
RelativePath=".\src\AtomicCounter.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\Bugcheck.cpp">
|
||||
</File>
|
||||
@@ -550,6 +553,9 @@
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="">
|
||||
<File
|
||||
RelativePath=".\include\Poco\AtomicCounter.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\Poco\Any.h">
|
||||
</File>
|
||||
|
||||
@@ -357,6 +357,10 @@
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\AtomicCounter.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\Bugcheck.cpp"
|
||||
>
|
||||
@@ -761,6 +765,10 @@
|
||||
RelativePath=".\include\Poco\Any.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\Poco\AtomicCounter.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\Poco\AutoPtr.h"
|
||||
>
|
||||
|
||||
@@ -352,6 +352,10 @@
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\AtomicCounter.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\Bugcheck.cpp"
|
||||
>
|
||||
@@ -752,6 +756,10 @@
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\include\Poco\AtomicCounter.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\Poco\Any.h"
|
||||
>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#
|
||||
# Makefile
|
||||
#
|
||||
# $Id: //poco/1.3/Foundation/Makefile#6 $
|
||||
# $Id: //poco/1.3/Foundation/Makefile#7 $
|
||||
#
|
||||
# Makefile for Poco Foundation
|
||||
#
|
||||
@@ -31,7 +31,7 @@ objects = ArchiveStrategy ASCIIEncoding AsyncChannel Base64Decoder Base64Encoder
|
||||
FileStreamFactory URIStreamFactory URIStreamOpener UTF16Encoding Windows1252Encoding \
|
||||
UTF8Encoding UnicodeConverter UUID UUIDGenerator Void Format \
|
||||
Pipe PipeImpl PipeStream DynamicAny DynamicAnyHolder SharedMemory \
|
||||
FileStream Unicode UTF8String \
|
||||
FileStream Unicode UTF8String AtomicCounter \
|
||||
adler32 compress crc32 deflate gzio infback inffast inflate inftrees \
|
||||
trees zutil \
|
||||
pcre_chartables pcre_compile pcre_globals pcre_maketables pcre_study \
|
||||
|
||||
327
Foundation/include/Poco/AtomicCounter.h
Normal file
327
Foundation/include/Poco/AtomicCounter.h
Normal file
@@ -0,0 +1,327 @@
|
||||
//
|
||||
// AtomicCounter.h
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/include/Poco/AtomicCounter.h#5 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Core
|
||||
// Module: AtomicCounter
|
||||
//
|
||||
// Definition of the AtomicCounter class.
|
||||
//
|
||||
// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef Foundation_AtomicCounter_INCLUDED
|
||||
#define Foundation_AtomicCounter_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#if POCO_OS == POCO_OS_WINDOWS_NT
|
||||
#include "Poco/UnWindows.h"
|
||||
#elif POCO_OS == POCO_OS_MAC_OS_X
|
||||
#include <libkern/OSAtomic.h>
|
||||
#else
|
||||
#include "Poco/Mutex.h"
|
||||
#endif // POCO_OS
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
class Foundation_API AtomicCounter
|
||||
/// This class implements a simple counter, which
|
||||
/// provides atomic operations that are safe to
|
||||
/// use in a multithreaded environment.
|
||||
///
|
||||
/// Typical usage of AtomicCounter is for implementing
|
||||
/// reference counting and similar things.
|
||||
///
|
||||
/// On some platforms, the implementation of AtomicCounter
|
||||
/// is based on atomic primitives specific to the platform
|
||||
/// (such as InterlockedIncrement, etc. on Windows), and
|
||||
/// thus very efficient. On platforms that do not support
|
||||
/// atomic primitives, operations are guarded by a FastMutex.
|
||||
///
|
||||
/// The following platforms currently have atomic
|
||||
/// primitives:
|
||||
/// - Windows
|
||||
/// - Mac OS X
|
||||
{
|
||||
public:
|
||||
typedef int ValueType; /// The underlying integer type.
|
||||
|
||||
AtomicCounter();
|
||||
/// Creates a new AtomicCounter and initializes it to zero.
|
||||
|
||||
explicit AtomicCounter(ValueType initialValue);
|
||||
/// Creates a new AtomicCounter and initializes it with
|
||||
/// the given value.
|
||||
|
||||
AtomicCounter(const AtomicCounter& counter);
|
||||
/// Creates the counter by copying another one.
|
||||
|
||||
~AtomicCounter();
|
||||
/// Destroys the AtomicCounter.
|
||||
|
||||
AtomicCounter& operator = (const AtomicCounter& counter);
|
||||
/// Assigns the value of another AtomicCounter.
|
||||
|
||||
AtomicCounter& operator = (ValueType value);
|
||||
/// Assigns a value to the counter.
|
||||
|
||||
operator ValueType () const;
|
||||
/// Returns the value of the counter.
|
||||
|
||||
ValueType value() const;
|
||||
/// Returns the value of the counter.
|
||||
|
||||
ValueType operator ++ (); // prefix
|
||||
/// Increments the counter and returns the result.
|
||||
|
||||
ValueType operator ++ (int); // postfix
|
||||
/// Increments the counter and returns the previous value.
|
||||
|
||||
ValueType operator -- (); // prefix
|
||||
/// Decrements the counter and returns the result.
|
||||
|
||||
ValueType operator -- (int); // postfix
|
||||
/// Decrements the counter and returns the previous value.
|
||||
|
||||
bool operator ! () const;
|
||||
/// Returns true if the counter is zero, false otherwise.
|
||||
|
||||
private:
|
||||
#if POCO_OS == POCO_OS_WINDOWS_NT
|
||||
typedef volatile LONG ImplType;
|
||||
#elif POCO_OS == POCO_OS_MAC_OS_X
|
||||
typedef volatile int32_t ImplType;
|
||||
#else // generic implementation based on FastMutex
|
||||
struct ImplType
|
||||
{
|
||||
mutable FastMutex mutex;
|
||||
volatile int value;
|
||||
};
|
||||
#endif // POCO_OS
|
||||
|
||||
ImplType _counter;
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// inlines
|
||||
//
|
||||
|
||||
|
||||
#if POCO_OS == POCO_OS_WINDOWS_NT
|
||||
//
|
||||
// Windows
|
||||
//
|
||||
inline AtomicCounter::operator AtomicCounter::ValueType () const
|
||||
{
|
||||
return _counter;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::value() const
|
||||
{
|
||||
return _counter;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
|
||||
{
|
||||
return InterlockedIncrement(&_counter);
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
|
||||
{
|
||||
ValueType result(_counter);
|
||||
InterlockedIncrement(&_counter);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
|
||||
{
|
||||
return InterlockedDecrement(&_counter);
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
|
||||
{
|
||||
ValueType result(_counter);
|
||||
InterlockedDecrement(&_counter);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline bool AtomicCounter::operator ! () const
|
||||
{
|
||||
return _counter == 0;
|
||||
}
|
||||
|
||||
|
||||
#elif POCO_OS == POCO_OS_MAC_OS_X
|
||||
//
|
||||
// Mac OS X
|
||||
//
|
||||
inline AtomicCounter::operator AtomicCounter::ValueType () const
|
||||
{
|
||||
return _counter;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::value() const
|
||||
{
|
||||
return _counter;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
|
||||
{
|
||||
return OSAtomicIncrement32(&_counter);
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
|
||||
{
|
||||
ValueType result(_counter);
|
||||
OSAtomicIncrement32(&_counter);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
|
||||
{
|
||||
return OSAtomicDecrement32(&_counter);
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
|
||||
{
|
||||
ValueType result(_counter);
|
||||
OSAtomicDecrement32(&_counter);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline bool AtomicCounter::operator ! () const
|
||||
{
|
||||
return _counter == 0;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
//
|
||||
// Generic implementation based on FastMutex
|
||||
//
|
||||
inline AtomicCounter::operator AtomicCounter::ValueType () const
|
||||
{
|
||||
ValueType result;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
result = _counter.value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::value() const
|
||||
{
|
||||
ValueType result;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
result = _counter.value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
|
||||
{
|
||||
ValueType result;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
result = ++_counter.value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
|
||||
{
|
||||
ValueType result;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
result = _counter.value++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
|
||||
{
|
||||
ValueType result;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
result = --_counter.value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
|
||||
{
|
||||
ValueType result;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
result = _counter.value--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
inline bool AtomicCounter::operator ! () const
|
||||
{
|
||||
bool result;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
result = _counter.value == 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#endif // POCO_OS
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#endif // Foundation_AtomicCounter_INCLUDED
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// RefCountedObject.h
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/include/Poco/RefCountedObject.h#1 $
|
||||
// $Id: //poco/1.3/Foundation/include/Poco/RefCountedObject.h#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Core
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
// Definition of the RefCountedObject class.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
@@ -41,7 +41,7 @@
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Mutex.h"
|
||||
#include "Poco/AtomicCounter.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@@ -78,8 +78,7 @@ private:
|
||||
RefCountedObject(const RefCountedObject&);
|
||||
RefCountedObject& operator = (const RefCountedObject&);
|
||||
|
||||
mutable int _rc;
|
||||
mutable FastMutex _rcMutex;
|
||||
mutable AtomicCounter _counter;
|
||||
};
|
||||
|
||||
|
||||
@@ -88,7 +87,19 @@ private:
|
||||
//
|
||||
inline int RefCountedObject::referenceCount() const
|
||||
{
|
||||
return _rc;
|
||||
return _counter.value();
|
||||
}
|
||||
|
||||
|
||||
inline void RefCountedObject::duplicate() const
|
||||
{
|
||||
++_counter;
|
||||
}
|
||||
|
||||
|
||||
inline void RefCountedObject::release() const
|
||||
{
|
||||
if (--_counter == 0) delete this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// SharedPtr.h
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/include/Poco/SharedPtr.h#6 $
|
||||
// $Id: //poco/1.3/Foundation/include/Poco/SharedPtr.h#7 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Core
|
||||
@@ -42,7 +42,7 @@
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Mutex.h"
|
||||
#include "Poco/AtomicCounter.h"
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
@@ -59,24 +59,21 @@ public:
|
||||
|
||||
void duplicate()
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
++_cnt;
|
||||
}
|
||||
|
||||
int release()
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
return --_cnt;
|
||||
}
|
||||
|
||||
int referenceCount() const
|
||||
{
|
||||
return _cnt;
|
||||
return _cnt.value();
|
||||
}
|
||||
|
||||
private:
|
||||
FastMutex _mutex;
|
||||
int _cnt;
|
||||
AtomicCounter _cnt;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//
|
||||
// UnWindows.h
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/include/Poco/UnWindows.h#1 $
|
||||
// $Id: //poco/1.3/Foundation/include/Poco/UnWindows.h#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Core
|
||||
// Module: UnWindows
|
||||
//
|
||||
// A wrapper around the "Poco/UnWindows.h" header file that #undef's some
|
||||
// of the macros for function names defined by "Poco/UnWindows.h" that
|
||||
// A wrapper around the <windows.h> header file that #undef's some
|
||||
// of the macros for function names defined by <windows.h> that
|
||||
// are a frequent source of conflicts (e.g., GetUserName).
|
||||
//
|
||||
// Remember, that most of the WIN32 API functions come in two variants,
|
||||
|
||||
171
Foundation/src/AtomicCounter.cpp
Normal file
171
Foundation/src/AtomicCounter.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
//
|
||||
// AtomicCounter.cpp
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/src/AtomicCounter.cpp#3 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Core
|
||||
// Module: AtomicCounter
|
||||
//
|
||||
// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/AtomicCounter.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
#if POCO_OS == POCO_OS_WINDOWS_NT
|
||||
//
|
||||
// Windows
|
||||
//
|
||||
AtomicCounter::AtomicCounter():
|
||||
_counter(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue):
|
||||
_counter(initialValue)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(const AtomicCounter& counter):
|
||||
_counter(counter.value())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::~AtomicCounter()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter)
|
||||
{
|
||||
InterlockedExchange(&_counter, counter.value());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value)
|
||||
{
|
||||
InterlockedExchange(&_counter, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
#elif POCO_OS == POCO_OS_MAC_OS_X
|
||||
//
|
||||
// Mac OS X
|
||||
//
|
||||
AtomicCounter::AtomicCounter():
|
||||
_counter(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue):
|
||||
_counter(initialValue)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(const AtomicCounter& counter):
|
||||
_counter(counter.value())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::~AtomicCounter()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter)
|
||||
{
|
||||
_counter = counter.value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value)
|
||||
{
|
||||
_counter = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
//
|
||||
// Generic implementation based on FastMutex
|
||||
//
|
||||
AtomicCounter::AtomicCounter()
|
||||
{
|
||||
_counter.value = 0;
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue)
|
||||
{
|
||||
_counter.value = initialValue;
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(const AtomicCounter& counter)
|
||||
{
|
||||
_counter.value = counter.value();
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::~AtomicCounter()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter)
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
_counter.value = counter.value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value)
|
||||
{
|
||||
FastMutex::ScopedLock lock(_counter.mutex);
|
||||
_counter.value = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
#endif // POCO_OS
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// RefCountedObject.cpp
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/src/RefCountedObject.cpp#1 $
|
||||
// $Id: //poco/1.3/Foundation/src/RefCountedObject.cpp#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Core
|
||||
@@ -40,7 +40,7 @@
|
||||
namespace Poco {
|
||||
|
||||
|
||||
RefCountedObject::RefCountedObject(): _rc(1)
|
||||
RefCountedObject::RefCountedObject(): _counter(1)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -50,21 +50,4 @@ RefCountedObject::~RefCountedObject()
|
||||
}
|
||||
|
||||
|
||||
void RefCountedObject::duplicate() const
|
||||
{
|
||||
_rcMutex.lock();
|
||||
++_rc;
|
||||
_rcMutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void RefCountedObject::release() const
|
||||
{
|
||||
_rcMutex.lock();
|
||||
int rc = --_rc;
|
||||
_rcMutex.unlock();
|
||||
if (rc == 0) delete this;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// CoreTest.cpp
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/testsuite/src/CoreTest.cpp#4 $
|
||||
// $Id: //poco/1.3/Foundation/testsuite/src/CoreTest.cpp#5 $
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
@@ -37,7 +37,9 @@
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Environment.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/Runnable.h"
|
||||
#include "Poco/Buffer.h"
|
||||
#include "Poco/AtomicCounter.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
@@ -47,7 +49,36 @@ using Poco::Bugcheck;
|
||||
using Poco::Exception;
|
||||
using Poco::Environment;
|
||||
using Poco::Thread;
|
||||
using Poco::Runnable;
|
||||
using Poco::Buffer;
|
||||
using Poco::AtomicCounter;
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
class ACTRunnable: public Poco::Runnable
|
||||
{
|
||||
public:
|
||||
ACTRunnable(AtomicCounter& counter):
|
||||
_counter(counter)
|
||||
{
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
for (int i = 0; i < 100000; ++i)
|
||||
{
|
||||
_counter++;
|
||||
_counter--;
|
||||
++_counter;
|
||||
--_counter;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
AtomicCounter& _counter;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
@@ -168,6 +199,48 @@ void CoreTest::testBuffer()
|
||||
}
|
||||
|
||||
|
||||
void CoreTest::testAtomicCounter()
|
||||
{
|
||||
AtomicCounter ac;
|
||||
|
||||
assert (ac.value() == 0);
|
||||
assert (ac++ == 0);
|
||||
assert (ac-- == 1);
|
||||
assert (++ac == 1);
|
||||
assert (--ac == 0);
|
||||
|
||||
ac = 2;
|
||||
assert (ac.value() == 2);
|
||||
|
||||
ac = 0;
|
||||
assert (ac.value() == 0);
|
||||
|
||||
AtomicCounter ac2(2);
|
||||
assert (ac2.value() == 2);
|
||||
|
||||
ACTRunnable act(ac);
|
||||
Thread t1;
|
||||
Thread t2;
|
||||
Thread t3;
|
||||
Thread t4;
|
||||
Thread t5;
|
||||
|
||||
t1.start(act);
|
||||
t2.start(act);
|
||||
t3.start(act);
|
||||
t4.start(act);
|
||||
t5.start(act);
|
||||
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
t4.join();
|
||||
t5.join();
|
||||
|
||||
assert (ac.value() == 0);
|
||||
}
|
||||
|
||||
|
||||
void CoreTest::setUp()
|
||||
{
|
||||
}
|
||||
@@ -187,6 +260,7 @@ CppUnit::Test* CoreTest::suite()
|
||||
CppUnit_addTest(pSuite, CoreTest, testBugcheck);
|
||||
CppUnit_addTest(pSuite, CoreTest, testEnvironment);
|
||||
CppUnit_addTest(pSuite, CoreTest, testBuffer);
|
||||
CppUnit_addTest(pSuite, CoreTest, testAtomicCounter);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// CoreTest.h
|
||||
//
|
||||
// $Id: //poco/1.3/Foundation/testsuite/src/CoreTest.h#3 $
|
||||
// $Id: //poco/1.3/Foundation/testsuite/src/CoreTest.h#4 $
|
||||
//
|
||||
// Definition of the CoreTest class.
|
||||
//
|
||||
@@ -52,6 +52,7 @@ public:
|
||||
void testFPE();
|
||||
void testEnvironment();
|
||||
void testBuffer();
|
||||
void testAtomicCounter();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
Reference in New Issue
Block a user