Mutex.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef MUTEX_INCLUDEDEF_H
19 #define MUTEX_INCLUDEDEF_H
20 //-----------------------------------------------
21 #include <pthread.h>
22 
23 const unsigned int INFINITE = 0;
24 
25 
26 class Mutex
27 {
28 private:
29  pthread_mutex_t m_hMutex;
30 
31 public:
33  {
34  pthread_mutex_init(&m_hMutex, 0);
35  }
36 
37  Mutex( std::string sName)
38  {
39 // no named Mutexes for POSIX
40  pthread_mutex_init(&m_hMutex, 0);
41  }
42 
44  {
45  pthread_mutex_destroy(&m_hMutex);
46  }
47 
50  bool lock( unsigned int uiTimeOut = INFINITE )
51  {
52  int ret;
53 
54  if (uiTimeOut == INFINITE)
55  {
56  ret = pthread_mutex_lock(&m_hMutex);
57  }
58  else
59  {
60  timespec abstime = { time(0) + uiTimeOut, 0 };
61  ret = pthread_mutex_timedlock(&m_hMutex, &abstime);
62  }
63 
64  return ! ret;
65  }
66 
67  void unlock()
68  {
69  pthread_mutex_unlock(&m_hMutex);
70  }
71 };
72 //-----------------------------------------------
73 #endif
74 
Mutex()
Definition: Mutex.h:32
const unsigned int INFINITE
Definition: Mutex.h:23
Definition: Mutex.h:26
~Mutex()
Definition: Mutex.h:43
bool lock(unsigned int uiTimeOut=INFINITE)
Definition: Mutex.h:50
pthread_mutex_t m_hMutex
Definition: Mutex.h:29
Mutex(std::string sName)
Definition: Mutex.h:37
void unlock()
Definition: Mutex.h:67


cob_utilities
Author(s): Christian Connette
autogenerated on Wed Apr 7 2021 02:11:52