00001 #ifndef __J2K__PThread_RecursiveMutex_CPP__ 00002 #define __J2K__PThread_RecursiveMutex_CPP__ 00003 00004 #include <j2k/Fred/PThread/RecursiveMutex.hpp> 00005 00006 RecursiveMutex::RecursiveMutex() : lockCount(0) { } 00007 00008 RecursiveMutex::~RecursiveMutex() { } 00009 00010 void RecursiveMutex::lock() { 00011 if ( !lockCount || !pthread_equal( owner, pthread_self() ) ) { 00012 Mutex::lock(); 00013 owner = pthread_self(); 00014 } 00015 00016 ++lockCount; 00017 } 00018 00019 void RecursiveMutex::unlock() { 00020 00021 if ( lockCount <= 0 ) { 00022 cout << "Mutex is already unlocked.\n"; 00023 return; 00024 } 00025 00026 if ( !pthread_equal(owner, pthread_self()) ) 00027 cout << "Mutex is locked by another thread.\n"; 00028 return; 00029 } 00030 00031 if ( --lockCount == 0 ) { 00032 Mutex::unlock(); 00033 } 00034 } 00035 00036 void RecursiveMutex::wait() { 00037 unsigned int saveCount = lockCount; 00038 lockCount = 0; 00039 Mutex::wait(); 00040 owner = pthread_self(); 00041 lockCount = saveCount; 00042 } 00043 00044 #endif