Mutex.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #ifndef MUTEX_INCLUDEDEF_H
00019 #define MUTEX_INCLUDEDEF_H
00020 //-----------------------------------------------
00021 #include <pthread.h>
00022 
00023 const unsigned int INFINITE = 0;
00024 
00025 
00026 class Mutex
00027 {
00028 private:
00029         pthread_mutex_t m_hMutex;
00030 
00031 public:
00032         Mutex()
00033         {
00034                 pthread_mutex_init(&m_hMutex, 0);
00035         }
00036 
00037         Mutex( std::string sName)
00038         {
00039 // no named Mutexes for POSIX
00040                 pthread_mutex_init(&m_hMutex, 0);
00041         }
00042 
00043         ~Mutex()
00044         {
00045                 pthread_mutex_destroy(&m_hMutex);
00046         }
00047 
00050         bool lock( unsigned int uiTimeOut = INFINITE )
00051         {
00052                 int ret;
00053 
00054                 if (uiTimeOut == INFINITE)
00055                 {
00056                         ret = pthread_mutex_lock(&m_hMutex);
00057                 }
00058                 else
00059                 {
00060                         timespec abstime = { time(0) + uiTimeOut, 0 };
00061                         ret = pthread_mutex_timedlock(&m_hMutex, &abstime);
00062                 }
00063 
00064                 return ! ret;
00065         }
00066 
00067         void unlock()
00068         {
00069                 pthread_mutex_unlock(&m_hMutex);
00070         }
00071 };
00072 //-----------------------------------------------
00073 #endif
00074 


cob_relayboard
Author(s): Christian Connette
autogenerated on Sat Jun 8 2019 21:02:18