oct-mutex.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2008-2012 Michael Goffioul
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_octave_mutex_h)
00024 #define octave_octave_mutex_h 1
00025 
00026 #include "oct-refcount.h"
00027 
00028 class octave_mutex;
00029 
00030 class
00031 octave_base_mutex
00032 {
00033 public:
00034   friend class octave_mutex;
00035 
00036   octave_base_mutex (void) : count (1) { }
00037 
00038   virtual ~octave_base_mutex (void) { }
00039 
00040   virtual void lock (void);
00041 
00042   virtual void unlock (void);
00043 
00044   virtual bool try_lock (void);
00045 
00046 private:
00047   octave_refcount<int> count;
00048 };
00049 
00050 class
00051 OCTAVE_API
00052 octave_mutex
00053 {
00054 public:
00055   octave_mutex (void);
00056 
00057   octave_mutex (const octave_mutex& m)
00058     : rep (m.rep)
00059   {
00060     rep->count++;
00061   }
00062 
00063   ~octave_mutex (void)
00064   {
00065     if (--rep->count == 0)
00066       delete rep;
00067   }
00068 
00069   octave_mutex& operator = (const octave_mutex& m)
00070   {
00071     if (rep != m.rep)
00072       {
00073         if (--rep->count == 0)
00074           delete rep;
00075 
00076         rep = m.rep;
00077         rep->count++;
00078       }
00079 
00080     return *this;
00081   }
00082 
00083   void lock (void)
00084   {
00085     rep->lock ();
00086   }
00087 
00088   void unlock (void)
00089   {
00090     rep->unlock ();
00091   }
00092 
00093   bool try_lock (void)
00094   {
00095     return rep->try_lock ();
00096   }
00097 
00098 protected:
00099   octave_base_mutex *rep;
00100 };
00101 
00102 class
00103 octave_autolock
00104 {
00105 public:
00106   octave_autolock (const octave_mutex& m, bool block = true)
00107     : mutex (m), lock_result (false)
00108   {
00109     if (block)
00110       {
00111         mutex.lock ();
00112         lock_result = true;
00113       }
00114     else
00115       lock_result = mutex.try_lock ();
00116   }
00117 
00118   ~octave_autolock (void)
00119   {
00120     if (lock_result)
00121       mutex.unlock ();
00122   }
00123 
00124   bool ok (void) const { return lock_result; }
00125 
00126   operator bool (void) const { return ok (); }
00127 
00128 private:
00129 
00130   // No copying or default constructor!
00131   octave_autolock (void);
00132   octave_autolock (const octave_autolock&);
00133   octave_autolock& operator = (const octave_autolock&);
00134 
00135 private:
00136   octave_mutex mutex;
00137   bool lock_result;
00138 };
00139 
00140 class
00141 OCTAVE_API
00142 octave_thread
00143 {
00144 public:
00145   static void init (void);
00146 
00147   static bool is_octave_thread (void);
00148 };
00149 
00150 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines