mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-14 19:57:17 +01:00
4a6bfc086c
Switched to POCO library for unified platform/library interface. Deprecated the external module API. It was creating more problems than solving. Removed most built-in libraries in favor of system libraries for easier maintenance. Cleaned and secured code with help from static analyzers.
134 lines
2.7 KiB
C++
134 lines
2.7 KiB
C++
//
|
|
// AtomicCounter.h
|
|
//
|
|
// Library: Foundation
|
|
// Package: Core
|
|
// Module: AtomicCounter
|
|
//
|
|
// Definition of the AtomicCounter class.
|
|
//
|
|
// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
|
|
// and Contributors.
|
|
//
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
//
|
|
|
|
|
|
#ifndef Foundation_AtomicCounter_INCLUDED
|
|
#define Foundation_AtomicCounter_INCLUDED
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
|
#include <atomic>
|
|
|
|
|
|
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 functionality.
|
|
{
|
|
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;
|
|
/// Converts the AtomicCounter to ValueType.
|
|
|
|
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:
|
|
std::atomic<int> _counter;
|
|
};
|
|
|
|
|
|
//
|
|
// inlines
|
|
//
|
|
|
|
inline AtomicCounter::operator AtomicCounter::ValueType () const
|
|
{
|
|
return _counter.load();
|
|
}
|
|
|
|
|
|
inline AtomicCounter::ValueType AtomicCounter::value() const
|
|
{
|
|
return _counter.load();
|
|
}
|
|
|
|
|
|
inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
|
|
{
|
|
return ++_counter;
|
|
}
|
|
|
|
|
|
inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
|
|
{
|
|
return _counter++;
|
|
}
|
|
|
|
|
|
inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
|
|
{
|
|
return --_counter;
|
|
}
|
|
|
|
|
|
inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
|
|
{
|
|
return _counter--;
|
|
}
|
|
|
|
|
|
inline bool AtomicCounter::operator ! () const
|
|
{
|
|
return _counter.load() == 0;
|
|
}
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
#endif // Foundation_AtomicCounter_INCLUDED
|