00001 extern "C" {
00002
00003 }
00004
00005 #include "guard.h"
00006 #include "exceptions.h"
00007
00008 Guard::Guard(const pthread_mutex_t *the_mutex)
00009 : mutex(the_mutex)
00010 {
00011 MUTEX_LOG("Guard locking mutex " << mutex);
00012 int ret = pthread_mutex_lock(const_cast<pthread_mutex_t*>(mutex));
00013 #if 0
00014 PTHREAD_CHECK_AND_THROW(ret, "mutex_lock(mutex)");
00015 #else
00016 if (ret != 0) {
00017 clog << "Guard: Failed to lock mutex " << mutex << ": " << strerror(ret) << endl;
00018 throw pthread_exception(__FILE__, __LINE__-3, ret, "");
00019 }
00020 #endif
00021
00022
00023
00024
00025
00026 }
00027
00028 Guard::~Guard()
00029 {
00030
00031 MUTEX_LOG("Guard unlocking mutex " << mutex);
00032 int ret = pthread_mutex_unlock(const_cast<pthread_mutex_t*>(mutex));
00033 PTHREAD_CHECK_AND_THROW(ret, "mutex_unlock(mutex)");
00034 }
00035
00036 void
00037 Guard::initialize_mutex(pthread_mutex_t &the_mutex)
00038 {
00039 clog << "Initialize_mutex " << &the_mutex << endl;
00040 int ret = pthread_mutex_init(&the_mutex, NULL);
00041 PTHREAD_CHECK_AND_THROW(ret, "mutex_init(mutex)");
00042 }
00043
00044 void
00045 Guard::destroy_mutex(pthread_mutex_t &the_mutex)
00046 {
00047 int ret = pthread_mutex_destroy(&the_mutex);
00048 if (ret != 0)
00049 cerr << "Guard::destroy_mutex: Unable to destroy mutex ("
00050 << &the_mutex << "): " << strerror(ret) << endl;
00051 }