Reformatted atomic32 files.
BUG= Review URL: https://webrtc-codereview.appspot.com/937016 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3067 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
fa65c851fe
commit
9cb9fc17b1
@ -27,39 +27,39 @@ namespace webrtc {
|
|||||||
// align the 32 bit value correctly (on a 32 bit boundary), so as long as you're
|
// align the 32 bit value correctly (on a 32 bit boundary), so as long as you're
|
||||||
// not doing things like reinterpret_cast over some custom allocated memory
|
// not doing things like reinterpret_cast over some custom allocated memory
|
||||||
// without being careful with alignment, you should be fine.
|
// without being careful with alignment, you should be fine.
|
||||||
class Atomic32
|
class Atomic32 {
|
||||||
{
|
public:
|
||||||
public:
|
Atomic32(WebRtc_Word32 initial_value = 0);
|
||||||
Atomic32(WebRtc_Word32 initialValue = 0);
|
~Atomic32();
|
||||||
~Atomic32();
|
|
||||||
|
|
||||||
// Prefix operator!
|
// Prefix operator!
|
||||||
WebRtc_Word32 operator++();
|
WebRtc_Word32 operator++();
|
||||||
WebRtc_Word32 operator--();
|
WebRtc_Word32 operator--();
|
||||||
|
|
||||||
WebRtc_Word32 operator+=(WebRtc_Word32 value);
|
WebRtc_Word32 operator+=(WebRtc_Word32 value);
|
||||||
WebRtc_Word32 operator-=(WebRtc_Word32 value);
|
WebRtc_Word32 operator-=(WebRtc_Word32 value);
|
||||||
|
|
||||||
// Sets the value atomically to newValue if the value equals compare value.
|
// Sets the value atomically to new_value if the value equals compare value.
|
||||||
// The function returns true if the exchange happened.
|
// The function returns true if the exchange happened.
|
||||||
bool CompareExchange(WebRtc_Word32 newValue, WebRtc_Word32 compareValue);
|
bool CompareExchange(WebRtc_Word32 new_value, WebRtc_Word32 compare_value);
|
||||||
WebRtc_Word32 Value() const;
|
WebRtc_Word32 Value() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Disable the + and - operator since it's unclear what these operations
|
// Disable the + and - operator since it's unclear what these operations
|
||||||
// should do.
|
// should do.
|
||||||
Atomic32 operator+(const Atomic32& other);
|
Atomic32 operator+(const Atomic32& other);
|
||||||
Atomic32 operator-(const Atomic32& other);
|
Atomic32 operator-(const Atomic32& other);
|
||||||
|
|
||||||
// Checks if |_value| is 32bit aligned.
|
// Checks if |_value| is 32bit aligned.
|
||||||
inline bool Is32bitAligned() const {
|
inline bool Is32bitAligned() const {
|
||||||
return (reinterpret_cast<ptrdiff_t>(&_value) & 3) == 0;
|
return (reinterpret_cast<ptrdiff_t>(&value_) & 3) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Atomic32);
|
DISALLOW_COPY_AND_ASSIGN(Atomic32);
|
||||||
|
|
||||||
WebRtc_Word32 _value;
|
WebRtc_Word32 value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
|
||||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_
|
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_
|
||||||
|
@ -18,43 +18,37 @@
|
|||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue)
|
Atomic32::Atomic32(WebRtc_Word32 initial_value)
|
||||||
{
|
: value_(initial_value) {
|
||||||
assert(Is32bitAligned());
|
assert(Is32bitAligned());
|
||||||
}
|
}
|
||||||
|
|
||||||
Atomic32::~Atomic32()
|
Atomic32::~Atomic32() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator++()
|
WebRtc_Word32 Atomic32::operator++() {
|
||||||
{
|
return OSAtomicIncrement32Barrier(&value_);
|
||||||
return OSAtomicIncrement32Barrier(&_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator--()
|
WebRtc_Word32 Atomic32::operator--() {
|
||||||
{
|
return OSAtomicDecrement32Barrier(&value_);
|
||||||
return OSAtomicDecrement32Barrier(&_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value)
|
WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) {
|
||||||
{
|
return OSAtomicAdd32Barrier(value, &value_);
|
||||||
return OSAtomicAdd32Barrier(value, &_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value)
|
WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) {
|
||||||
{
|
return OSAtomicAdd32Barrier(-value, &value_);
|
||||||
return OSAtomicAdd32Barrier(-value, &_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Atomic32::CompareExchange(WebRtc_Word32 newValue,
|
bool Atomic32::CompareExchange(WebRtc_Word32 new_value,
|
||||||
WebRtc_Word32 compareValue)
|
WebRtc_Word32 compare_value) {
|
||||||
{
|
return OSAtomicCompareAndSwap32Barrier(compare_value, new_value, &value_);
|
||||||
return OSAtomicCompareAndSwap32Barrier(compareValue, newValue, &_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::Value() const
|
WebRtc_Word32 Atomic32::Value() const {
|
||||||
{
|
return value_;
|
||||||
return _value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
@ -18,47 +18,41 @@
|
|||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue)
|
Atomic32::Atomic32(WebRtc_Word32 initial_value)
|
||||||
{
|
: value_(initial_value) {
|
||||||
assert(Is32bitAligned());
|
assert(Is32bitAligned());
|
||||||
}
|
}
|
||||||
|
|
||||||
Atomic32::~Atomic32()
|
Atomic32::~Atomic32() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator++()
|
WebRtc_Word32 Atomic32::operator++() {
|
||||||
{
|
return __sync_fetch_and_add(&value_, 1) + 1;
|
||||||
return __sync_fetch_and_add(&_value, 1) + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator--()
|
WebRtc_Word32 Atomic32::operator--() {
|
||||||
{
|
return __sync_fetch_and_sub(&value_, 1) - 1;
|
||||||
return __sync_fetch_and_sub(&_value, 1) - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value)
|
WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) {
|
||||||
{
|
WebRtc_Word32 return_value = __sync_fetch_and_add(&value_, value);
|
||||||
WebRtc_Word32 returnValue = __sync_fetch_and_add(&_value, value);
|
return_value += value;
|
||||||
returnValue += value;
|
return return_value;
|
||||||
return returnValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value)
|
WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) {
|
||||||
{
|
WebRtc_Word32 return_value = __sync_fetch_and_sub(&value_, value);
|
||||||
WebRtc_Word32 returnValue = __sync_fetch_and_sub(&_value, value);
|
return_value -= value;
|
||||||
returnValue -= value;
|
return return_value;
|
||||||
return returnValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Atomic32::CompareExchange(WebRtc_Word32 newValue,
|
bool Atomic32::CompareExchange(WebRtc_Word32 new_value,
|
||||||
WebRtc_Word32 compareValue)
|
WebRtc_Word32 compare_value) {
|
||||||
{
|
return __sync_bool_compare_and_swap(&value_, compare_value, new_value);
|
||||||
return __sync_bool_compare_and_swap(&_value, compareValue, newValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::Value() const
|
WebRtc_Word32 Atomic32::Value() const {
|
||||||
{
|
return value_;
|
||||||
return _value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
@ -18,55 +18,50 @@
|
|||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue)
|
Atomic32::Atomic32(WebRtc_Word32 initial_value)
|
||||||
{
|
: value_(initial_value) {
|
||||||
// Make sure that the counter variable we're using is of the same size
|
// Make sure that the counter variable we're using is of the same size
|
||||||
// as what the API expects.
|
// as what the API expects.
|
||||||
COMPILE_ASSERT(sizeof(_value) == sizeof(LONG));
|
COMPILE_ASSERT(sizeof(value_) == sizeof(LONG));
|
||||||
assert(Is32bitAligned());
|
assert(Is32bitAligned());
|
||||||
}
|
}
|
||||||
|
|
||||||
Atomic32::~Atomic32()
|
Atomic32::~Atomic32() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator++()
|
WebRtc_Word32 Atomic32::operator++() {
|
||||||
{
|
return static_cast<WebRtc_Word32>(InterlockedIncrement(
|
||||||
return static_cast<WebRtc_Word32>(InterlockedIncrement(
|
reinterpret_cast<volatile LONG*>(&value_)));
|
||||||
reinterpret_cast<volatile LONG*>(&_value)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator--()
|
WebRtc_Word32 Atomic32::operator--() {
|
||||||
{
|
return static_cast<WebRtc_Word32>(InterlockedDecrement(
|
||||||
return static_cast<WebRtc_Word32>(InterlockedDecrement(
|
reinterpret_cast<volatile LONG*>(&value_)));
|
||||||
reinterpret_cast<volatile LONG*>(&_value)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value)
|
WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) {
|
||||||
{
|
return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_),
|
||||||
return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value),
|
value);
|
||||||
value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value)
|
WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) {
|
||||||
{
|
return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_),
|
||||||
return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value),
|
-value);
|
||||||
-value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Atomic32::CompareExchange(WebRtc_Word32 newValue,
|
bool Atomic32::CompareExchange(WebRtc_Word32 new_value,
|
||||||
WebRtc_Word32 compareValue)
|
WebRtc_Word32 compare_value) {
|
||||||
{
|
const LONG old_value = InterlockedCompareExchange(
|
||||||
const LONG oldValue = InterlockedCompareExchange(
|
reinterpret_cast<volatile LONG*>(&value_),
|
||||||
reinterpret_cast<volatile LONG*>(&_value),
|
new_value,
|
||||||
newValue,
|
compare_value);
|
||||||
compareValue);
|
|
||||||
// If the old value and the compare value is the same an exchange happened.
|
// If the old value and the compare value is the same an exchange happened.
|
||||||
return (oldValue == compareValue);
|
return (old_value == compare_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtc_Word32 Atomic32::Value() const
|
WebRtc_Word32 Atomic32::Value() const {
|
||||||
{
|
return value_;
|
||||||
return _value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
Loading…
x
Reference in New Issue
Block a user