00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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&);
00101 PosixMutex<PosixMutexSpecialization>& operator=(PosixMutex&);
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 }
00118
00119 #endif