• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrcommon/ibrcommon/thread/Mutex.cpp

Go to the documentation of this file.
00001 #include "ibrcommon/config.h"
00002 #include "ibrcommon/thread/Mutex.h"
00003 #include <errno.h>
00004 
00005 namespace ibrcommon
00006 {
00007         Mutex::Mutex(MUTEX_TYPE type)
00008         {
00009                 pthread_mutexattr_t attr;
00010                 pthread_mutexattr_init(&attr);
00011                 pthread_mutexattr_settype(&attr, type);
00012                 pthread_mutex_init(&m_mutex, &attr);
00013                 pthread_mutexattr_destroy(&attr);
00014         }
00015 
00016         Mutex::~Mutex()
00017         {
00018                 pthread_mutex_destroy( &m_mutex );
00019         }
00020 
00021         void Mutex::trylock() throw (MutexException)
00022         {
00023                 int ret = pthread_mutex_trylock( &m_mutex );
00024 
00025                 switch (ret)
00026                 {
00027                 case 0:
00028                         break;
00029 
00030                 case EBUSY:
00031                         throw MutexException("The mutex could not be acquired because it was already locked.");
00032                         break;
00033                 }
00034         }
00035 
00036         void Mutex::enter() throw (MutexException)
00037         {
00038                 switch (pthread_mutex_lock( &m_mutex ))
00039                 {
00040                 case 0:
00041                         return;
00042 
00043 //              case EINVAL:
00044 //                      throw MutexException("The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling.");
00045 
00046                 // The pthread_mutex_trylock() function will fail if:
00047 
00048                 case EBUSY:
00049                         throw MutexException("The mutex could not be acquired because it was already locked.");
00050 
00051                 // The pthread_mutex_lock(), pthread_mutex_trylock() and pthread_mutex_unlock() functions may fail if:
00052 
00053                 case EINVAL:
00054                         throw MutexException("The value specified by mutex does not refer to an initialised mutex object.");
00055 
00056                 case EAGAIN:
00057                         throw MutexException("The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.");
00058 
00059                 // The pthread_mutex_lock() function may fail if:
00060 
00061                 case EDEADLK:
00062                         throw MutexException("The current thread already owns the mutex.");
00063 
00064                 // The pthread_mutex_unlock() function may fail if:
00065 
00066                 case EPERM:
00067                         throw MutexException("The current thread does not own the mutex.");
00068 
00069                 default:
00070                         throw MutexException("can not lock the mutex");
00071                 }
00072         }
00073 
00074         void Mutex::leave() throw (MutexException)
00075         {
00076                 if (0 != pthread_mutex_unlock( &m_mutex ))
00077                 {
00078                         throw MutexException("can not unlock mutex");
00079                 }
00080         }
00081 }

Generated on Thu Nov 11 2010 09:49:47 for IBR-DTNSuite by  doxygen 1.7.1