|
lock_guard.hhGo to the documentation of this file.00001 // 00002 // $Id: lock_guard.hh,v 1.5 2003/10/30 15:05:12 cholm Exp $ 00003 // 00004 // threadmm::lock_guard 00005 // Copyright (C) 2002 Christian Holm Christensen <cholm@nbi.dk> 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public License 00009 // as published by the Free Software Foundation; either version 2.1 00010 // of the License, or (at your option) any later version. 00011 // 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 // Lesser General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU Lesser General Public 00018 // License along with this library; if not, write to the Free 00019 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 00020 // 02111-1307 USA 00021 // 00022 #ifndef THREADMM_lock_guard 00023 #define THREADMM_lock_guard 00024 00030 namespace threadmm 00031 { 00072 template <typename Lock> 00073 class lock_guard 00074 { 00075 public: 00077 typedef Lock lock_type; 00078 protected: 00080 lock_type& _lock; 00083 bool _released; 00084 public: 00089 lock_guard(lock_type& l, bool spin=false) 00090 : _lock(l) { this->lock(spin); this->_released = false; } 00095 lock_guard(volatile lock_type& l, bool spin=false) 00096 : _lock(const_cast<Lock&>(l)) { 00097 this->lock(spin); this->_released = false; } 00100 virtual ~lock_guard() { if (!this->_released) this->unlock(); } 00105 void lock(bool spin=false); 00110 void lock(bool spin=false) volatile; 00112 void unlock() { this->_released = true; this->_lock.unlock(); } 00113 }; 00114 template <typename Lock> 00115 void lock_guard<Lock>::lock(bool spin) 00116 { 00117 if (!spin) 00118 this->_lock.lock(); 00119 else 00120 while (!this->_lock.try_lock()); 00121 } 00122 #if 0 00123 template <typename Lock> 00124 void lock_guard<Lock>::lock(bool spin) volatile 00125 { 00126 Lock* tmp = const_cast<Lock*>(&_lock); 00127 if (!spin) 00128 this->tmp->lock(); 00129 else 00130 while (!this->tmp.try_lock()); 00131 } 00132 #endif 00133 } 00134 00135 #endif 00136 //____________________________________________________________________ 00137 // 00138 // EOF 00139 // Last update Tue Nov 9 12:40:50 2004 Christian Holm Created by DoxyGen 1.3.9.1 |