00001 /* 00002 * AtomicCounter.hpp 00003 * 00004 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 00005 * 00006 * Oracle is a registered trademarks of Oracle Corporation and/or its 00007 * affiliates. 00008 * 00009 * This software is the confidential and proprietary information of Oracle 00010 * Corporation. You shall not disclose such confidential and proprietary 00011 * information and shall use it only in accordance with the terms of the 00012 * license agreement you entered into with Oracle. 00013 * 00014 * This notice may not be removed or altered. 00015 */ 00016 #ifndef COH_ATOMIC_COUNTER_HPP 00017 #define COH_ATOMIC_COUNTER_HPP 00018 00019 #include "coherence/lang.ns" 00020 00021 #include "coherence/native/NativeAtomic64.hpp" 00022 00023 COH_OPEN_NAMESPACE2(coherence,util) 00024 00025 using coherence::native::NativeAtomic64; 00026 00027 00028 /** 00029 * AtomicCounter allows for atomic updates to a "int64_t" value where possible 00030 * without requiring synchronization. 00031 * 00032 * @author nsa 12.28.2007 00033 */ 00034 class COH_EXPORT AtomicCounter 00035 : public cloneable_spec<AtomicCounter> 00036 { 00037 friend class factory<AtomicCounter>; 00038 00039 // ----- constructor ---------------------------------------------------- 00040 00041 protected: 00042 /** 00043 * Create an AtomicCounter initialized with a specified value. 00044 * 00045 * @param cValue the initial value of the Atomic64 00046 */ 00047 AtomicCounter(int64_t cValue = 0); 00048 00049 /** 00050 * Copy constructor. 00051 */ 00052 AtomicCounter(const AtomicCounter& that); 00053 00054 00055 // ----- AtomicCounter interface ---------------------------------------- 00056 00057 public: 00058 /** 00059 * Incremenet the value by c, and return the new value. 00060 * 00061 * @param c the amount to increment the counter by 00062 * 00063 * @return the new value 00064 */ 00065 virtual int64_t increment(int64_t c = 1); 00066 00067 /** 00068 * Incremenet the value by c, and return the original value. 00069 * 00070 * @param c the amount to increment the counter by 00071 * 00072 * @return the original value 00073 */ 00074 virtual int64_t postIncrement(int64_t c = 1); 00075 00076 /** 00077 * Decrement the value by c, and return the new value. 00078 * 00079 * @param c the amount to decrement the counter by 00080 * 00081 * @return the new value 00082 */ 00083 virtual int64_t decrement(int64_t c = 1); 00084 00085 /** 00086 * Decrement the value by c, and return the original value. 00087 * 00088 * @param c the amount to decrement the counter by 00089 * 00090 * @return the original value 00091 */ 00092 virtual int64_t postDecrement(int64_t c = 1); 00093 00094 /** 00095 * Return the current value of the counter. 00096 * 00097 * @return the current value 00098 */ 00099 virtual int64_t getCount() const; 00100 00101 /** 00102 * Update the current value, only if it is equal to the assumed value. 00103 * 00104 * @param cAssume the assumed old value 00105 * @param cNew the new value 00106 * 00107 * @return the prior actual value, if the returned value does is not 00108 * equal to the supplied assumed value then update did not take 00109 * place 00110 */ 00111 virtual int64_t update(int64_t cAssume, int64_t cNew); 00112 00113 /** 00114 * Update the current value, and return the previous value. 00115 * 00116 * @param cNew the new value 00117 * 00118 * @return the previous value just before the update went through 00119 */ 00120 virtual int64_t setCount(int64_t cNew); 00121 00122 00123 // ----- Object interface ----------------------------------------------- 00124 00125 public: 00126 /** 00127 * {@inheritDoc} 00128 */ 00129 virtual void toStream(std::ostream& out) const; 00130 00131 00132 // ----- data members --------------------------------------------------- 00133 00134 protected: 00135 /** 00136 * The underlying native implementation. 00137 */ 00138 NativeAtomic64 m_atomic; 00139 }; 00140 00141 COH_CLOSE_NAMESPACE2 00142 00143 #endif // COH_ATOMIC_COUNTER_HPP