atomic_mutex.h

Go to the documentation of this file.
00001 
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_mutex.h 10065 2007-04-12 05:54:56Z rjpeters $
00010 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/rutz/atomic_mutex.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 //
00032 
00033 #ifndef GROOVX_RUTZ_ATOMIC_MUTEX_H_UTC20070412044614_DEFINED
00034 #define GROOVX_RUTZ_ATOMIC_MUTEX_H_UTC20070412044614_DEFINED
00035 
00036 #include <limits>
00037 #include <pthread.h>
00038 
00039 namespace rutz
00040 {
00041 
00043 class mutex_atomic_int
00044 {
00045 private:
00046   int x;
00047   pthread_mutex_t mut;
00048 
00049   mutex_atomic_int(const mutex_atomic_int&);
00050   mutex_atomic_int& operator=(const mutex_atomic_int&);
00051 
00052 public:
00054   mutex_atomic_int() : x(0)
00055   { pthread_mutex_init(&mut, NULL); }
00056 
00058   static int max_value() { return std::numeric_limits<int>::max(); }
00059 
00061   int atomic_get() const
00062   { return x; }
00063 
00065   void atomic_set(int v)
00066   { pthread_mutex_lock(&mut); x = v; pthread_mutex_unlock(&mut); }
00067 
00069   void atomic_add(int i)
00070   { pthread_mutex_lock(&mut); x += i; pthread_mutex_unlock(&mut); }
00071 
00073   void atomic_sub(int i)
00074   { pthread_mutex_lock(&mut); x -= i; pthread_mutex_unlock(&mut); }
00075 
00077   bool atomic_sub_test_zero(int i)
00078   {
00079     bool ret;
00080     pthread_mutex_lock(&mut);
00081     ret = bool((x -= i) == 0);
00082     pthread_mutex_unlock(&mut);
00083     return ret;
00084   }
00085 
00087   void atomic_incr()
00088   { pthread_mutex_lock(&mut); ++x; pthread_mutex_unlock(&mut); }
00089 
00091   void atomic_decr()
00092   { pthread_mutex_lock(&mut); --x; pthread_mutex_unlock(&mut); }
00093 
00095   bool atomic_decr_test_zero()
00096   {
00097     bool ret;
00098     pthread_mutex_lock(&mut);
00099     ret = bool(--x == 0);
00100     pthread_mutex_unlock(&mut);
00101     return ret;
00102   }
00103 
00105   bool atomic_incr_test_zero()
00106   {
00107     bool ret;
00108     pthread_mutex_lock(&mut);
00109     ret = bool(++x == 0);
00110     pthread_mutex_unlock(&mut);
00111     return ret;
00112   }
00113 
00115   int atomic_add_return(int i)
00116   {
00117     int ret;
00118     pthread_mutex_lock(&mut);
00119     ret = (x += i);
00120     pthread_mutex_unlock(&mut);
00121     return ret;
00122   }
00123 
00125   int atomic_sub_return(int i)
00126   {
00127     int ret;
00128     pthread_mutex_lock(&mut);
00129     ret = (x -= i);
00130     pthread_mutex_unlock(&mut);
00131     return ret;
00132   }
00133 
00135   int atomic_incr_return()
00136   {
00137     int ret;
00138     pthread_mutex_lock(&mut);
00139     ret = ++x;
00140     pthread_mutex_unlock(&mut);
00141     return ret;
00142   }
00143 
00145   int atomic_decr_return()
00146   {
00147     int ret;
00148     pthread_mutex_lock(&mut);
00149     ret = --x;
00150     pthread_mutex_unlock(&mut);
00151     return ret;
00152   }
00153 };
00154 
00155 } // end namespace rutz
00156 
00157 // ######################################################################
00158 /* So things look consistent in everyone's emacs... */
00159 /* Local Variables: */
00160 /* mode: c++ */
00161 /* indent-tabs-mode: nil */
00162 /* End: */
00163 
00164 static const char __attribute__((used)) vcid_groovx_rutz_atomic_mutex_h_utc20070412044614[] = "$Id: atomic_mutex.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00165 #endif // !GROOVX_RUTZ_ATOMIC_MUTEX_H_UTC20070412044614DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.