forked from boostorg/smart_ptr
Make ++a, where a is an atomic_count, return the new value.
[SVN r52456]
This commit is contained in:
@ -31,13 +31,12 @@
|
||||
// ++a;
|
||||
//
|
||||
// Effects: Atomically increments the value of a
|
||||
// Returns: nothing
|
||||
// Returns: (long) the new value of a
|
||||
//
|
||||
// --a;
|
||||
//
|
||||
// Effects: Atomically decrements the value of a
|
||||
// Returns: (long) zero if the new value of a is zero,
|
||||
// unspecified non-zero value otherwise (usually the new value)
|
||||
// Returns: (long) the new value of a
|
||||
//
|
||||
// Important note: when --a returns zero, it must act as a
|
||||
// read memory barrier (RMB); i.e. the calling thread must
|
||||
|
@ -42,9 +42,9 @@ public:
|
||||
|
||||
explicit atomic_count( long v ) : value_( v ) {}
|
||||
|
||||
void operator++()
|
||||
long operator++()
|
||||
{
|
||||
__atomic_add(&value_, 1);
|
||||
return __exchange_and_add( &value_, +1 ) + 1;
|
||||
}
|
||||
|
||||
long operator--()
|
||||
|
@ -25,16 +25,9 @@ public:
|
||||
|
||||
explicit atomic_count( long v ) : value_( static_cast< int >( v ) ) {}
|
||||
|
||||
void operator++()
|
||||
long operator++()
|
||||
{
|
||||
__asm__
|
||||
(
|
||||
"lock\n\t"
|
||||
"incl %0":
|
||||
"+m"( value_ ): // output (%0)
|
||||
: // inputs
|
||||
"cc" // clobbers
|
||||
);
|
||||
return atomic_exchange_and_add( &value_, +1 ) + 1;
|
||||
}
|
||||
|
||||
long operator--()
|
||||
|
@ -62,10 +62,10 @@ public:
|
||||
pthread_mutex_destroy(&mutex_);
|
||||
}
|
||||
|
||||
void operator++()
|
||||
long operator++()
|
||||
{
|
||||
scoped_lock lock(mutex_);
|
||||
++value_;
|
||||
return ++value_;
|
||||
}
|
||||
|
||||
long operator--()
|
||||
|
@ -31,9 +31,9 @@ public:
|
||||
|
||||
explicit atomic_count( long v ) : value_( v ) {}
|
||||
|
||||
void operator++()
|
||||
long operator++()
|
||||
{
|
||||
__sync_add_and_fetch( &value_, 1 );
|
||||
return __sync_add_and_fetch( &value_, 1 );
|
||||
}
|
||||
|
||||
long operator--()
|
||||
|
@ -58,5 +58,6 @@ import testing ;
|
||||
[ run sp_recursive_assign2_rv_test.cpp ]
|
||||
[ run esft_constructor_test.cpp ]
|
||||
[ compile-fail auto_ptr_lv_fail.cpp ]
|
||||
[ run atomic_count_test2.cpp ]
|
||||
;
|
||||
}
|
||||
|
55
test/atomic_count_test2.cpp
Normal file
55
test/atomic_count_test2.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
//
|
||||
// atomic_count_test2.cpp
|
||||
//
|
||||
// Copyright 2009 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/detail/atomic_count.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::detail::atomic_count n( 4 );
|
||||
|
||||
BOOST_TEST( n == 4 );
|
||||
|
||||
BOOST_TEST( ++n == 5 );
|
||||
BOOST_TEST( ++n == 6 );
|
||||
|
||||
BOOST_TEST( n == 6 );
|
||||
|
||||
BOOST_TEST( --n == 5 );
|
||||
BOOST_TEST( --n == 4 );
|
||||
|
||||
BOOST_TEST( n == 4 );
|
||||
|
||||
boost::detail::atomic_count m( 0 );
|
||||
|
||||
BOOST_TEST( m == 0 );
|
||||
|
||||
BOOST_TEST( ++m == 1 );
|
||||
BOOST_TEST( ++m == 2 );
|
||||
|
||||
BOOST_TEST( m == 2 );
|
||||
|
||||
BOOST_TEST( --m == 1 );
|
||||
BOOST_TEST( --m == 0 );
|
||||
|
||||
BOOST_TEST( m == 0 );
|
||||
|
||||
BOOST_TEST( --m == -1 );
|
||||
BOOST_TEST( --m == -2 );
|
||||
|
||||
BOOST_TEST( m == -2 );
|
||||
|
||||
BOOST_TEST( ++m == -1 );
|
||||
BOOST_TEST( ++m == 0 );
|
||||
|
||||
BOOST_TEST( m == 0 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user