PosixMutex.hh

00001 //********************************************************************
00002 //*** Thread/PosixMutex.hh
00003 //*** Copyright (c) 2002-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: PosixMutex.hh,v 1.22 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_POSIXMUTEX_H
00025 #define INCLUDED_POSIXMUTEX_H
00026 
00027 #ifndef INCLUDED_THREAD_LOCKSTATISTICSFWD_HH
00028 #include "LockStatisticsFwd.hh"
00029 #endif
00030 
00031 namespace fatalmind {
00032     class DefaultPosixMutexSpecialization;
00033 
00034     template<class PosixMutexSpecialization = DefaultPosixMutexSpecialization
00035         , class Statistics = DefaultLockStatistics>
00036     class PosixMutex;
00037 }
00038 
00039 #ifndef INCLUDED_ERRNO_H
00040 #include "Exceptions/Errno.hh"
00041 #endif
00042 
00043 // TODO, split so that this is not public -> make instantiations file
00044 #include <pthread.h>
00045 
00046 namespace fatalmind {
00047 
00048 class DefaultPosixMutexSpecialization {
00049     protected:
00050         inline pthread_mutexattr_t* getAttr() {
00051             return NULL;
00052         }
00053 };
00054 
00055 template<class PosixMutexSpecialization, class Statistics>
00056 std::ostream& operator<<(std::ostream& o
00057     , const PosixMutex<PosixMutexSpecialization, Statistics>& mutex
00058 );
00059 
00060 template<class PosixMutexSpecialization, class Statistics>
00061 class PosixMutex : public PosixMutexSpecialization, public Statistics {
00062     public:
00063         PosixMutex() {
00064             Errno::ThrowOnError(
00065                 pthread_mutex_init(&d_mutex, PosixMutexSpecialization::getAttr())
00066             );
00067         };
00068 
00069 
00070         void lock() {
00071             typename Statistics::_timer_t timingstat(Statistics::_waittime, true);
00072             Errno::ThrowOnError(
00073                 pthread_mutex_lock(&d_mutex)
00074             );
00075             Statistics::stat_lock();
00076         };
00077 
00078         void unlock() {
00079             Statistics::stat_unlock();
00080             Errno::ThrowOnError(
00081                 pthread_mutex_unlock(&d_mutex)
00082             );
00083         };
00084 
00085         ~PosixMutex() {
00086             int status = pthread_mutex_destroy(&d_mutex);
00087             while (status != 0) {
00088                 lock();
00089                 unlock();
00090                 status = pthread_mutex_destroy(&d_mutex);
00091             }
00092         };
00093 
00094         void multiThreaded() {
00095         };
00096     friend 
00097         std::ostream& operator<< <>(std::ostream& o, const PosixMutex<PosixMutexSpecialization, Statistics>& mutex);
00098 
00099     private:
00100         PosixMutex(PosixMutex&); // copy is not allowed
00101         PosixMutex<PosixMutexSpecialization>& operator=(PosixMutex&); // assignment is not allowed
00102 
00103     protected:
00104         using Statistics::stat_lock;
00105         using Statistics::stat_unlock;
00106 
00107         pthread_mutex_t     d_mutex;
00108 };
00109 
00110 template<class PosixMutexSpecialization, class Statistics>
00111 std::ostream& operator<<(std::ostream& o
00112     , const PosixMutex<PosixMutexSpecialization, Statistics>& mutex
00113 ) {
00114     return o << static_cast<const Statistics&>(mutex);
00115 }
00116 
00117 } // namespace fatalmind
00118 
00119 #endif

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