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 <decaf/internal/util/concurrent/Atomics.h> 00024 00025 namespace decaf { 00026 namespace util { 00027 namespace concurrent { 00028 namespace atomic { 00029 00033 template< typename T > 00034 class AtomicReference { 00035 private: 00036 00037 volatile void* value; 00038 00039 private: 00040 00041 AtomicReference(const AtomicReference&); 00042 AtomicReference& operator= (const AtomicReference&); 00043 00044 public: 00045 00046 AtomicReference() : value( NULL ) {} 00047 AtomicReference(T* value) : value((void*)value) {} 00048 00049 virtual ~AtomicReference() {} 00050 00055 T* get() const { 00056 return (T*)value; 00057 } 00058 00065 void set( T* newValue ) { 00066 internal::util::concurrent::Atomics::getAndSet(&this->value, (void*)newValue); 00067 } 00068 00079 bool compareAndSet( T* expect, T* update ) { 00080 return internal::util::concurrent::Atomics::compareAndSet(&this->value, (void*)expect, (void*)update); 00081 } 00082 00090 T* getAndSet( T* newValue ) { 00091 return (T*)internal::util::concurrent::Atomics::getAndSet(&this->value, (void*)newValue); 00092 } 00093 00098 std::string toString() const { 00099 return decaf::lang::Long::toString( (long long)this->value ); 00100 } 00101 00102 }; 00103 00104 }}}} 00105 00106 #endif /*_DECAF_UTIL_CONCURRENT_ATOMIC_ATOMICREFERENCE_H_*/
1.6.1