Octave-Forge - Extra packages for GNU Octave | |
Home · Packages · Developers · Documentation · FAQ · Bugs · Mailing Lists · Links · Code |
00001 /* 00002 00003 Copyright (C) 2008 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 class octave_mutex; 00027 00028 class 00029 octave_base_mutex 00030 { 00031 public: 00032 friend class octave_mutex; 00033 00034 octave_base_mutex (void) : count (1) { } 00035 00036 virtual ~octave_base_mutex (void) { } 00037 00038 virtual void lock (void); 00039 00040 virtual void unlock (void); 00041 00042 private: 00043 int count; 00044 }; 00045 00046 class 00047 OCTAVE_API 00048 octave_mutex 00049 { 00050 public: 00051 octave_mutex (void); 00052 00053 octave_mutex (const octave_mutex& m) 00054 { 00055 rep = m.rep; 00056 rep->count++; 00057 } 00058 00059 ~octave_mutex (void) 00060 { 00061 if (--rep->count == 0) 00062 delete rep; 00063 } 00064 00065 octave_mutex& operator = (const octave_mutex& m) 00066 { 00067 if (rep != m.rep) 00068 { 00069 if (--rep->count == 0) 00070 delete rep; 00071 00072 rep = m.rep; 00073 rep->count++; 00074 } 00075 00076 return *this; 00077 } 00078 00079 void lock (void) 00080 { 00081 rep->lock (); 00082 } 00083 00084 void unlock (void) 00085 { 00086 rep->unlock (); 00087 } 00088 00089 protected: 00090 octave_base_mutex *rep; 00091 }; 00092 00093 class 00094 octave_autolock 00095 { 00096 public: 00097 octave_autolock (const octave_mutex& m) 00098 : mutex (m) 00099 { 00100 mutex.lock (); 00101 } 00102 00103 ~octave_autolock (void) 00104 { 00105 mutex.unlock (); 00106 } 00107 00108 private: 00109 00110 // No copying or default constructor! 00111 octave_autolock (void); 00112 octave_autolock (const octave_autolock&); 00113 octave_autolock& operator = (const octave_autolock&); 00114 00115 private: 00116 octave_mutex mutex; 00117 }; 00118 00119 #endif