Lock.hh

00001 //********************************************************************
00002 //*** Thread/Lock.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: Lock.hh,v 1.25 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 
00024 #ifndef INCLUDED_THREAD_LOCK_HH
00025 #define INCLUDED_THREAD_LOCK_HH
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include "../config.h"
00029 #endif
00030 
00031 #ifndef INCLUDED_THREAD_LOCKFWD_HH
00032 #include "LockFwd.hh"
00033 #endif
00034 
00035 #ifndef INCLUDED_CONDVARFWD_H
00036 #include "CondVarFwd.hh"
00037 #endif
00038 
00039 #ifndef POSIX_TIMER_HH
00040 #include "POSIX/timer.hh"
00041 #endif
00042 
00043 #ifndef INCLUDED_INT2TYPE_HH
00044 #include "stdtools/Int2Type.hh"
00045 #endif
00046 
00047 #include <iosfwd>
00048 
00049 // TODO: configure via configure ;)
00050 
00051 #include "PosixMutex.hh"
00052 #include "LinuxRecursivePosixMutexSpecialization.hh"
00053 
00054 namespace fatalmind {
00055 
00056 #ifdef HAVE_PTHREAD_MUTEXATTR_SETTYPE
00057 #ifdef HAVE_PTHREAD_MUTEX_RECURSIVE_NP
00058 #define HAVE_RECURSIVE_LOCK
00059 template<class Statistics>
00060 class RecursiveLock
00061 : public PosixMutex<LinuxRecursivePosixMutexSpecialization, Statistics> {
00062 };
00063 #endif
00064 #endif
00065 
00066 #ifndef HAVE_RECURSIVE_LOCK
00067 template <class Statistics = DefaultLockStatistics >
00068 class RecursiveLock: public FastLock<Statistics> {
00069 };
00070 #endif
00071 
00072 template<class Statistics>
00073 class FastLock
00074 : public PosixMutex<DefaultPosixMutexSpecialization, Statistics> {
00075 };
00076 
00077 template<class Statistics>
00078 class NopLock {
00079     public:
00080         inline NopLock() {};
00081         inline void lock() {};
00082         inline void unlock() {};
00083         inline void singleThreaded();
00084 };
00085 
00086 template<class MutexType>
00087 class NopCondVar: public MutexType {
00088     public:
00089         NopCondVar() {
00090         };
00091         inline void lock() {};
00092         inline void unlock() {};
00093         inline void wait() {};
00094         template<class TM>
00095         inline bool wait(const struct POSIX::timespec<TM>* timeout) {
00096             return false;
00097         }
00098         inline void signal() {};
00099         inline void broadcast() {};
00100 };
00101 
00102 template<class MutexType>
00103 std::ostream& operator<<(std::ostream& os, const NopCondVar<MutexType>&) {
00104     return os;
00105 }
00106 template<class UsagePattern, class TimingPattern>
00107 class LockStatistics: public UsagePattern, public TimingPattern {
00108 };
00109 
00110 class NopLockUsageStatistics {
00111     public:
00112         inline NopLockUsageStatistics() {};
00113         inline void stat_lock() {};
00114         inline void stat_unlock() {};
00115     friend 
00116         std::ostream& operator<<(std::ostream& o, const NopLockUsageStatistics& s);
00117 };
00118 
00119 class NopLockTimingStatistics {
00120     public:
00121         inline NopLockTimingStatistics() {};
00122         typedef nopTimer _timer_t;
00123         int _waittime;
00124 
00125     friend
00126         std::ostream& operator<<(std::ostream& o, const NopLockTimingStatistics& s);
00127 };
00128 
00129 } // namespace fatalmind
00130 
00131 
00132 namespace fatalmind {
00133 
00140 template<class Statistics>
00141 struct MultiThreadedModel {
00142 #ifdef HAVE_RECURSIVE_LOCK
00143     typedef PosixMutex<LinuxRecursivePosixMutexSpecialization, Statistics> RecursiveLock;
00144 #endif
00145     typedef PosixMutex<DefaultPosixMutexSpecialization, Statistics> FastLock;
00146 #ifdef HAVE_RECURSIVE_LOCK
00147     typedef CondVar<RecursiveLock> RecursiveCondVar;
00148 #endif
00149     typedef CondVar<FastLock>      FastCondVar;
00150     static const bool isMultiThreaded = true;
00151     typedef Int2Type<true> Type;
00152 };
00153 
00160 template<class Statistics>
00161 struct SingleThreadedModel {
00162     typedef NopLock<Statistics> RecursiveLock;
00163     typedef NopLock<Statistics> FastLock;
00164     typedef NopCondVar<RecursiveLock> RecursiveCondVar;
00165     typedef NopCondVar<FastLock>      FastCondVar;
00166     static const bool isMultiThreaded = false;
00167     typedef Int2Type<false> Type;
00168 };
00169 
00170 
00171 
00172 } // namespace fatalmind
00173 #ifndef INCLUDED_CONDVAR_H
00174 #include "CondVar.hh"
00175 #endif
00176 
00177 #endif

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