C++ Client API Reference for Oracle Coherence
14c (14.1.2.0.0)

F79659-03

coherence/util/AtomicCounter.hpp

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