oct-refcount.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2012 Jaroslav Hajek
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
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 // Encapsulates a reference counter.
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   // Increment/Decrement. int is postfix.
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
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines