Author: tabish
Date: Thu Aug 28 04:29:43 2008
New Revision: 689787
URL: http://svn.apache.org/viewvc?rev=689787&view=rev
Log:
Add Class AtomicBoolean and tests.
Added:
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.cpp
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.h
Modified:
activemq/activemq-cpp/trunk/src/main/Makefile.am
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.cpp
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.h
activemq/activemq-cpp/trunk/src/test/Makefile.am
activemq/activemq-cpp/trunk/src/test/testRegistry.cpp
Modified: activemq/activemq-cpp/trunk/src/main/Makefile.am
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/Makefile.am?rev=689787&r1=689786&r2=689787&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/main/Makefile.am Thu Aug 28 04:29:43 2008
@@ -130,6 +130,7 @@
decaf/util/concurrent/CountDownLatch.cpp \
decaf/util/concurrent/PooledThread.cpp \
decaf/util/concurrent/ThreadPool.cpp \
+ decaf/util/concurrent/atomic/AtomicBoolean.cpp \
decaf/util/Date.cpp \
decaf/util/UUID.cpp \
decaf/util/StringTokenizer.cpp \
@@ -386,6 +387,7 @@
decaf/util/concurrent/PooledThreadListener.h \
decaf/util/concurrent/TaskListener.h \
decaf/util/concurrent/ThreadPool.h \
+ decaf/util/concurrent/atomic/AtomicBoolean.h \
decaf/util/Config.h \
decaf/util/Date.h \
decaf/util/UUID.h \
Modified:
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.cpp?rev=689787&r1=689786&r2=689787&view=diff
==============================================================================
---
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.cpp
(original)
+++
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.cpp
Thu Aug 28 04:29:43 2008
@@ -18,6 +18,9 @@
#include "AtomicBoolean.h"
#include <decaf/lang/Boolean.h>
+#include <apr_atomic.h>
+
+#include <iostream>
using namespace decaf;
using namespace decaf::lang;
@@ -26,12 +29,19 @@
using namespace decaf::util::concurrent::atomic;
////////////////////////////////////////////////////////////////////////////////
-AtomicBoolean::AtomicBoolean() {
+AtomicBoolean::AtomicBoolean() : value(0) {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+AtomicBoolean::AtomicBoolean( bool initialValue ) {
+ this->value = initialValue ? 1 : 0;
}
////////////////////////////////////////////////////////////////////////////////
bool AtomicBoolean::compareAndSet( bool expect, bool update ) {
- // TODO
+ int upd = update ? 1 : 0;
+ int exp = expect ? 1 : 0;
+ return apr_atomic_cas32( &this->value, upd, exp ) == exp;
}
////////////////////////////////////////////////////////////////////////////////
Modified:
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.h?rev=689787&r1=689786&r2=689787&view=diff
==============================================================================
---
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.h
(original)
+++
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/atomic/AtomicBoolean.h
Thu Aug 28 04:29:43 2008
@@ -34,7 +34,7 @@
class DECAF_API AtomicBoolean {
private:
- volatile int value;
+ volatile unsigned int value;
public:
Modified: activemq/activemq-cpp/trunk/src/test/Makefile.am
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/Makefile.am?rev=689787&r1=689786&r2=689787&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/test/Makefile.am Thu Aug 28 04:29:43 2008
@@ -125,6 +125,7 @@
decaf/util/concurrent/CountDownLatchTest.cpp \
decaf/util/concurrent/MutexTest.cpp \
decaf/util/concurrent/ThreadPoolTest.cpp \
+ decaf/util/concurrent/atomic/AtomicBooleanTest.cpp \
decaf/nio/BufferTest.cpp \
testRegistry.cpp \
main.cpp
@@ -239,11 +240,12 @@
decaf/util/concurrent/CountDownLatchTest.h \
decaf/util/concurrent/MutexTest.h \
decaf/util/concurrent/ThreadPoolTest.h \
+ decaf/util/concurrent/atomic/AtomicBooleanTest.h \
decaf/nio/BufferTest.h
## include activemq/connector/openwire/marshal/v1/srcmakefile.mk
## include activemq/connector/openwire/marshal/v2/srcmakefile.mk
-include activemq/connector/openwire/marshal/v3/srcmakefile.mk
+## include activemq/connector/openwire/marshal/v3/srcmakefile.mk
## Compile this as part of make check
check_PROGRAMS = activemq-test
Added:
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.cpp?rev=689787&view=auto
==============================================================================
---
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.cpp
(added)
+++
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.cpp
Thu Aug 28 04:29:43 2008
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "AtomicBooleanTest.h"
+
+#include <decaf/util/concurrent/atomic/AtomicBoolean.h>
+#include <decaf/lang/Boolean.h>
+#include <decaf/lang/Thread.h>
+
+using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace decaf::util::concurrent::atomic;
+
+////////////////////////////////////////////////////////////////////////////////
+AtomicBooleanTest::AtomicBooleanTest() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void AtomicBooleanTest::testConstructor() {
+ AtomicBoolean aboolean;
+ CPPUNIT_ASSERT( aboolean.get() == false );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void AtomicBooleanTest::testConstructor2() {
+ AtomicBoolean aboolean( true );
+ CPPUNIT_ASSERT( aboolean.get() == true );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void AtomicBooleanTest::testGetSet() {
+ AtomicBoolean ai( true );
+ CPPUNIT_ASSERT( true == ai.get() );
+ ai.set( false );
+ CPPUNIT_ASSERT( false == ai.get() );
+ ai.set( true );
+ CPPUNIT_ASSERT( true == ai.get() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void AtomicBooleanTest::testCompareAndSet() {
+ AtomicBoolean ai( true );
+ CPPUNIT_ASSERT( ai.compareAndSet( true, false ) );
+ CPPUNIT_ASSERT( false == ai.get() );
+ CPPUNIT_ASSERT( ai.compareAndSet( false, false ) );
+ CPPUNIT_ASSERT( false == ai.get() );
+ CPPUNIT_ASSERT( !ai.compareAndSet( true, false ) );
+ CPPUNIT_ASSERT( !ai.get() );
+ CPPUNIT_ASSERT( ai.compareAndSet( false, true ) );
+ CPPUNIT_ASSERT( true == ai.get() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+class MyRunnable : public Runnable {
+private:
+
+ AtomicBoolean* aip;
+
+public:
+
+ MyRunnable( AtomicBoolean* ai ) : aip( ai ) {}
+
+ virtual void run() {
+ while( !aip->compareAndSet( false, true ) ) Thread::yield();
+ }
+
+};
+
+////////////////////////////////////////////////////////////////////////////////
+void AtomicBooleanTest::testCompareAndSetInMultipleThreads() {
+ AtomicBoolean ai(true);
+
+ MyRunnable runnable( &ai );
+ Thread t( &runnable );
+
+ try {
+
+ t.start();
+ CPPUNIT_ASSERT( ai.compareAndSet( true, false ) );
+ t.join();
+
+ } catch(Exception& e) {
+ CPPUNIT_FAIL( "Should Not Throw" );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void AtomicBooleanTest::testGetAndSet() {
+ AtomicBoolean ai( true );
+ CPPUNIT_ASSERT( true == ai.getAndSet( false ) );
+ CPPUNIT_ASSERT( false == ai.getAndSet( false ) );
+ CPPUNIT_ASSERT( false == ai.getAndSet( true ) );
+ CPPUNIT_ASSERT( true == ai.get() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void AtomicBooleanTest::testToString() {
+ AtomicBoolean ai;
+ CPPUNIT_ASSERT( ai.toString() == Boolean::toString( false ) );
+ ai.set( true );
+ CPPUNIT_ASSERT( ai.toString() == Boolean::toString( true ) );
+}
Added:
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.h?rev=689787&view=auto
==============================================================================
---
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.h
(added)
+++
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/atomic/AtomicBooleanTest.h
Thu Aug 28 04:29:43 2008
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_UTIL_CONCURRENT_ATOMIC_ATOMICBOOLEANTEST_H_
+#define _DECAF_UTIL_CONCURRENT_ATOMIC_ATOMICBOOLEANTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace decaf {
+namespace util {
+namespace concurrent {
+namespace atomic {
+
+ class AtomicBooleanTest : public CppUnit::TestFixture {
+
+ CPPUNIT_TEST_SUITE( AtomicBooleanTest );
+ CPPUNIT_TEST( testConstructor );
+ CPPUNIT_TEST( testConstructor2 );
+ CPPUNIT_TEST( testGetSet );
+ CPPUNIT_TEST( testCompareAndSet );
+ CPPUNIT_TEST( testCompareAndSetInMultipleThreads );
+ CPPUNIT_TEST( testGetAndSet );
+ CPPUNIT_TEST( testToString );
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+
+ AtomicBooleanTest();
+ virtual ~AtomicBooleanTest() {}
+
+ void testConstructor();
+ void testConstructor2();
+ void testGetSet();
+ void testCompareAndSet();
+ void testCompareAndSetInMultipleThreads();
+ void testGetAndSet();
+ void testToString();
+
+ };
+
+}}}}
+
+#endif /*_DECAF_UTIL_CONCURRENT_ATOMIC_ATOMICBOOLEANTEST_H_*/
Modified: activemq/activemq-cpp/trunk/src/test/testRegistry.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/testRegistry.cpp?rev=689787&r1=689786&r2=689787&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/testRegistry.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/testRegistry.cpp Thu Aug 28 04:29:43
2008
@@ -241,6 +241,9 @@
#include <decaf/util/concurrent/ThreadPoolTest.h>
CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::concurrent::ThreadPoolTest );
+#include <decaf/util/concurrent/atomic/AtomicBooleanTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION(
decaf::util::concurrent::atomic::AtomicBooleanTest );
+
#include <decaf/util/DateTest.h>
CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::DateTest );
#include <decaf/util/UUIDTest.h>