00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef _MPCL_SYSTEM_POSIX_1_MUTEX__
00027 #define _MPCL_SYSTEM_POSIX_1_MUTEX__
00028 
00029 #include <pthread.h>
00030 #include "../defs.hh"
00031 #include "../exceptions.hh"
00032 #include "../mutex.hh"
00033 
00034 
00036 namespace mpcl
00037 {
00038 
00040   namespace system
00041   {
00042 
00047     class TMutex : public IMutex
00048     {
00049 
00051         friend class TCondition;
00052 
00053 
00054       private:
00055 
00057         pthread_mutex_t   tPosixId;
00058 
00059 
00060       public:
00061 
00062         
00063         
00064         
00065 
00067         TMutex (void)
00068           throw (TErrorException) :
00069           IMutex   ()             ,
00070           tPosixId () 
00071         {
00072           if ( pthread_mutex_init (&tPosixId, NULL) )
00073           {
00074             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00075           }
00076         }
00077 
00079         ~TMutex()
00080           throw (TErrorException)
00081         {
00082           if ( pthread_mutex_destroy (&tPosixId) )
00083           {
00084             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00085           }
00086         }
00087 
00089         void lock (void)
00090           throw (TErrorException)
00091         {
00092           if ( pthread_mutex_lock (&tPosixId) )
00093           {
00094             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00095           }
00096         }
00097 
00099         void unlock (void)
00100           throw (TErrorException)
00101         {
00102           if ( pthread_mutex_unlock (&tPosixId) )
00103           {
00104             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00105           }
00106         }
00107 
00112         bool tryLock (void)
00113         {
00114           return ( pthread_mutex_trylock (&tPosixId) != 0 );
00115         }
00116 
00117     };  
00118 
00119   }  
00120 
00121 }  
00122 
00123 
00124 #endif  // not _MPCL_SYSTEM_POSIX_1_MUTEX__