Mutex.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #ifndef COIL_MUTEX_H
00021 #define COIL_MUTEX_H
00022 
00023 #include <windows.h>
00024 
00025 namespace coil
00026 {
00027   typedef HANDLE pthread_mutex_t;
00028   
00042   class Mutex
00043   {
00044   public:
00064     Mutex(const char * const name = 0)
00065     {
00066         SECURITY_DESCRIPTOR sd_buffer;
00067         ::InitializeSecurityDescriptor(&sd_buffer, 
00068                                        SECURITY_DESCRIPTOR_REVISION);
00069         ::SetSecurityDescriptorDacl (&sd_buffer, TRUE, 0, FALSE);
00070                 m_Security_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
00071                 m_Security_attr.lpSecurityDescriptor = &sd_buffer;
00072                 m_Security_attr.bInheritHandle = TRUE;
00073                 mutex_ = ::CreateMutexA( &m_Security_attr,
00074                                          FALSE,
00075                                          name );
00076 
00077 
00078     }
00079 
00095     ~Mutex()
00096     {
00097                 ::CloseHandle(mutex_);
00098                 
00099     }
00100 
00116     inline void lock()
00117     {
00118                 ::WaitForSingleObject(mutex_,INFINITE);
00119     }
00120 
00136     inline bool trylock()
00137     {
00138         unsigned long dwret;
00139                 dwret = ::WaitForSingleObject(mutex_,0);
00140         switch(dwret)
00141                 {
00142                   case WAIT_ABANDONED:
00143                           return true;
00144                           break;
00145                   case WAIT_OBJECT_0:
00146                           return false;
00147                   case WAIT_TIMEOUT:
00148                           return true;
00149                   default:
00150                           return true;
00151                 }
00152     }
00153 
00169     inline void unlock()
00170     {
00171                 ::ReleaseMutex(mutex_);
00172     }
00173     HANDLE mutex_;
00174     
00175   private:
00176     SECURITY_ATTRIBUTES m_Security_attr;
00177 
00178         Mutex(const Mutex&);
00179     Mutex& operator=(const Mutex &);
00180   };
00181 };
00182 #endif // COIL_MUTEX_H


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:38