00001 /** @file rutz/atomic_darwin.h rutz/atomic_darwin.H Atomic integer operations implemented using Apple Darwin's OSAtomicAdd32Barrier() */ 00002 00003 /////////////////////////////////////////////////////////////////////// 00004 // 00005 // Copyright (c) 2006-2007 University of Southern California 00006 // Rob Peters <rjpeters at usc dot edu> 00007 // 00008 // created: Thu Oct 5 10:55:50 2006 00009 // commit: $Id: atomic_darwin.h 8249 2007-04-12 06:03:40Z rjpeters $ 00010 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/atomic_darwin.h $ 00011 // 00012 // -------------------------------------------------------------------- 00013 // 00014 // This file is part of GroovX. 00015 // [http://www.klab.caltech.edu/rjpeters/groovx/] 00016 // 00017 // GroovX is free software; you can redistribute it and/or modify it 00018 // under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation; either version 2 of the License, or 00020 // (at your option) any later version. 00021 // 00022 // GroovX is distributed in the hope that it will be useful, but 00023 // WITHOUT ANY WARRANTY; without even the implied warranty of 00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00025 // General Public License for more details. 00026 // 00027 // You should have received a copy of the GNU General Public License 00028 // along with GroovX; if not, write to the Free Software Foundation, 00029 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00030 // 00031 /////////////////////////////////////////////////////////////////////// 00032 00033 #ifndef GROOVX_RUTZ_ATOMIC_DARWIN_H_UTC20070412044456_DEFINED 00034 #define GROOVX_RUTZ_ATOMIC_DARWIN_H_UTC20070412044456_DEFINED 00035 00036 #include <libkern/OSAtomic.h> 00037 #include <limits> 00038 00039 namespace rutz 00040 { 00041 00042 /// Atomic integer operations implemented using Apple Darwin's OSAtomicAdd32Barrier(). 00043 class darwin_atomic_int 00044 { 00045 private: 00046 int32_t x; 00047 00048 darwin_atomic_int(const darwin_atomic_int&); 00049 darwin_atomic_int& operator=(const darwin_atomic_int&); 00050 00051 public: 00052 //! Construct with an initial value of 0. 00053 darwin_atomic_int() : x(0) {} 00054 00055 //! Get the maximum representable value 00056 static int max_value() { return std::numeric_limits<int32_t>::max(); } 00057 00058 //! Get the current value. 00059 int atomic_get() const 00060 { return x; } 00061 00062 //! Set value to the given value \a v. 00063 void atomic_set(int v) 00064 { x = v; } 00065 00066 //! Add \a v to the value. 00067 void atomic_add(int i) 00068 { OSAtomicAdd32Barrier(i, &x); } 00069 00070 //! Subtract \a v from the value. 00071 void atomic_sub(int i) 00072 { OSAtomicAdd32Barrier(-i, &x); } 00073 00074 //! Subtract \a v from the value; return true if the new value is zero. 00075 bool atomic_sub_test_zero(int i) 00076 { return (OSAtomicAdd32Barrier(-i, &x) == 0); } 00077 00078 //! Increment the value by one. 00079 void atomic_incr() 00080 { OSAtomicAdd32Barrier(1, &x); } 00081 00082 //! Decrement the value by one. 00083 void atomic_decr() 00084 { OSAtomicAdd32Barrier(-1, &x); } 00085 00086 //! Decrement the value by one; return true if the new value is zero. 00087 bool atomic_decr_test_zero() 00088 { return (OSAtomicAdd32Barrier(-1, &x) == 0); } 00089 00090 //! Increment the value by one; return true if the new value is zero. 00091 bool atomic_incr_test_zero() 00092 { return (OSAtomicAdd32Barrier(1, &x) == 0); } 00093 00094 //! Add \a v to the value and return the new value 00095 int atomic_add_return(int i) 00096 { return OSAtomicAdd32Barrier(i, &x); } 00097 00098 //! Subtract \a v from the value and return the new value 00099 int atomic_sub_return(int i) 00100 { return OSAtomicAdd32Barrier(-i, &x); } 00101 00102 //! Increment the value by one and return the new value. 00103 int atomic_incr_return() 00104 { return OSAtomicAdd32Barrier(1, &x); } 00105 00106 //! Decrement the value by one and return the new value. 00107 int atomic_decr_return() 00108 { return OSAtomicAdd32Barrier(-1, &x); } 00109 }; 00110 00111 } // end namespace rutz 00112 00113 // ###################################################################### 00114 /* So things look consistent in everyone's emacs... */ 00115 /* Local Variables: */ 00116 /* mode: c++ */ 00117 /* indent-tabs-mode: nil */ 00118 /* End: */ 00119 00120 static const char __attribute__((used)) vcid_groovx_rutz_atomic_darwin_h_utc20070412044456[] = "$Id: atomic_darwin.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/atomic_darwin.h $"; 00121 #endif // !GROOVX_RUTZ_ATOMIC_DARWIN_H_UTC20070412044456DEFINED