activemq-cpp-3.3.0

src/main/decaf/util/concurrent/atomic/AtomicReference.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef _DECAF_UTIL_CONCURRENT_ATOMIC_ATOMICREFERENCE_H_
00019 #define _DECAF_UTIL_CONCURRENT_ATOMIC_ATOMICREFERENCE_H_
00020 
00021 #include <decaf/util/Config.h>
00022 #include <decaf/lang/Long.h>
00023 #include <apr_atomic.h>
00024 
00025 namespace decaf {
00026 namespace util {
00027 namespace concurrent {
00028 namespace atomic {
00029 
00033     template< typename T >
00034     class AtomicReference {
00035 
00036         volatile void* value;
00037 
00038     public:
00039 
00040         AtomicReference() : value( NULL ) {}
00041         AtomicReference( T* value ) : value( (void*)value ) {}
00042 
00043         virtual ~AtomicReference() {}
00044 
00049         T* get() const {
00050             return (T*)value;
00051         }
00052 
00059         void set( T* newValue ) {
00060             apr_atomic_xchgptr( &this->value, (void*)newValue );
00061         }
00062 
00073         bool compareAndSet( T* expect, T* update ) {
00074             return apr_atomic_casptr( &this->value, (void*)update, (void*)expect ) == (void*)expect;
00075         }
00076 
00084         T* getAndSet( T* newValue ) {
00085             return (T*)apr_atomic_xchgptr( &this->value, (void*)newValue );
00086         }
00087 
00092         std::string toString() const {
00093             return decaf::lang::Long::toString( (long long)this->value );
00094         }
00095 
00096     };
00097 
00098 }}}}
00099 
00100 #endif /*_DECAF_UTIL_CONCURRENT_ATOMIC_ATOMICREFERENCE_H_*/