Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_refcount_h)
00024 #define octave_refcount_h 1
00025
00026 #ifndef OCTAVE_CONFIG_INCLUDED
00027 # error "The file <octave/config.h> must be included before oct-refcount.h."
00028 #endif
00029
00030 #if defined (USE_ATOMIC_REFCOUNT) && (defined (_MSC_VER) || defined (__GNUC__))
00031 # if defined (_MSC_VER)
00032 # include <intrin.h>
00033 # define OCTREFCOUNT_ATOMIC_INCREMENT(x) _InterlockedIncrement((long*)x)
00034 # define OCTREFCOUNT_ATOMIC_DECREMENT(x) _InterlockedDecrement((long*)x)
00035 # define OCTREFCOUNT_ATOMIC_INCREMENT_POST(x) _InterlockedExchangeAdd((long*)x, 1)
00036 # define OCTREFCOUNT_ATOMIC_DECREMENT_POST(x) _InterlockedExchangeAdd((long*)x, -1)
00037 # elif defined (__GNUC__)
00038 # define OCTREFCOUNT_ATOMIC_INCREMENT(x) __sync_add_and_fetch(x, 1)
00039 # define OCTREFCOUNT_ATOMIC_DECREMENT(x) __sync_add_and_fetch(x, -1)
00040 # define OCTREFCOUNT_ATOMIC_INCREMENT_POST(x) __sync_fetch_and_add(x, 1)
00041 # define OCTREFCOUNT_ATOMIC_DECREMENT_POST(x) __sync_fetch_and_add(x, -1)
00042 # endif
00043 #else // Generic non-locking versions
00044 # define OCTREFCOUNT_ATOMIC_INCREMENT(x) ++(*(x))
00045 # define OCTREFCOUNT_ATOMIC_DECREMENT(x) --(*(x))
00046 # define OCTREFCOUNT_ATOMIC_INCREMENT_POST(x) (*(x))++
00047 # define OCTREFCOUNT_ATOMIC_DECREMENT_POST(x) (*(x))--
00048 #endif
00049
00050
00051 template <class T>
00052 class octave_refcount
00053 {
00054 public:
00055 typedef T count_type;
00056
00057 octave_refcount(count_type initial_count) : count(initial_count) {}
00058
00059
00060 count_type operator++(void)
00061 {
00062 return OCTREFCOUNT_ATOMIC_INCREMENT (&count);
00063 }
00064
00065 count_type operator++(int)
00066 {
00067 return OCTREFCOUNT_ATOMIC_INCREMENT_POST (&count);
00068 }
00069
00070 count_type operator--(void)
00071 {
00072 return OCTREFCOUNT_ATOMIC_DECREMENT (&count);
00073 }
00074
00075 count_type operator--(int)
00076 {
00077 return OCTREFCOUNT_ATOMIC_DECREMENT_POST (&count);
00078 }
00079
00080 operator count_type (void) const
00081 {
00082 return static_cast<count_type const volatile&> (count);
00083 }
00084
00085 private:
00086 count_type count;
00087 };
00088
00089 #endif