Synchronize.hh

00001 //********************************************************************
00002 //*** Thread/Synchronize.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: Synchronize.hh,v 1.10 2009-03-02 14:16:46 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of 
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 #ifndef INCLUDED_THREAD_SYNCHRONIZE_HH
00024 #define INCLUDED_THREAD_SYNCHRONIZE_HH
00025 
00026 namespace fatalmind {
00027 
00028 template<class Lock>
00029 class Synchronize {
00030     public:
00031         inline Synchronize(Lock& lock): _lock(lock) {
00032             _lock.lock();   
00033         }
00034 
00035         inline Synchronize(Synchronize& rho)
00036         : _lock(rho._lock)
00037         {
00038             _lock.recursive_ok();
00039         }
00040 
00041         inline ~Synchronize() {
00042             _lock.unlock();
00043         }
00044     private:
00045         Lock& _lock;
00046 };
00047 
00048 // NopLock specialization
00049 template<> class Synchronize<NopLock<> > {
00050     public:
00051         inline Synchronize(NopLock<> lock) {
00052         }
00053         inline ~Synchronize() {
00054         }
00055 };
00056 
00057 /*
00058  * 2 way synchro
00059  */
00060 
00061 template<class Lock, class Lock2>
00062 class Synchronize2 {
00063     public:
00064         inline Synchronize2(Lock& lock, Lock2& lock2)
00065         : _lock(lock) 
00066         , _lock2(lock2)
00067         {
00068             _lock.lock();   
00069             // TODO:: order locking for deadlock prevention
00070             if (&_lock != &_lock) {
00071                 _lock2.lock();
00072             }
00073         }
00074 
00075         inline ~Synchronize2() {
00076             if (&_lock != &_lock) {
00077                 _lock2.unlock();
00078             }
00079             _lock.unlock();
00080         }
00081     private:
00082         Lock& _lock;
00083         Lock2& _lock2;
00084 };
00085 
00086 template<> class Synchronize2<NopLock<> , NopLock<> > {
00087     public:
00088         inline Synchronize2(NopLock<>& lock, NopLock<>& lock2) {
00089         }
00090         inline ~Synchronize2() {
00091         }
00092 };
00093 
00094 } //namespace fatalmind
00095 
00096 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3