Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Mutex.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2016 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * File: Mutex.h
21  *
22  */
23 #ifndef MUTEX_H
24 #define MUTEX_H
25 
26 #include <pthread.h>
27 #include <errno.h>
28 
29 #include <stdio.h>
30 
31 #include <system_error>
32 #include <Logging.h>
33 
34 
35 namespace AICommon
36 {
37 
38 
39 #if (AI_BUILD_TYPE == AI_RELEASE)
40  #define __MutexThrowOnError(err) \
41  (void)err
42 
43 #elif (AI_BUILD_TYPE == AI_DEBUG)
44  #define __MutexThrowOnError(err) \
45  do { \
46  if (__builtin_expect((err != 0), 0)) \
47  throw std::system_error(std::error_code(err, std::system_category())); \
48  } while(0)
49 
50 #else
51  #error Unknown AI build type
52 
53 #endif
54 
55 
56 
66 class Mutex
67 {
68 public:
69  Mutex()
70  {
71 #if (AI_BUILD_TYPE == AI_DEBUG)
72  pthread_mutexattr_t attr;
73  pthread_mutexattr_init(&attr);
74  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
75  pthread_mutex_init(&mLock, &attr);
76  pthread_mutexattr_destroy(&attr);
77 #elif (AI_BUILD_TYPE == AI_RELEASE)
78  pthread_mutex_init(&mLock, nullptr);
79 #else
80  #error Unknown AI build type
81 #endif
82  }
83 
84  Mutex(const Mutex&) = delete;
85  Mutex(const Mutex&&) = delete;
86 
87  ~Mutex()
88  {
89  int err = pthread_mutex_destroy(&mLock);
90  try
91  {
92  __MutexThrowOnError(err);
93  }
94  catch(const std::system_error& exception)
95  {
96  AI_LOG_FATAL("Mutex failed to be destroyed %s", exception.what());
97  }
98  }
99 
100 public:
101  void lock()
102  {
103  int err = pthread_mutex_lock(&mLock);
104  __MutexThrowOnError(err);
105  }
106 
107  void unlock()
108  {
109  int err = pthread_mutex_unlock(&mLock);
110  __MutexThrowOnError(err);
111  }
112 
113  bool try_lock()
114  {
115  int err = pthread_mutex_trylock(&mLock);
116  if (err == 0)
117  {
118  return true;
119  }
120  else if (err == EBUSY)
121  {
122  return false;
123  }
124  else
125  {
126  __MutexThrowOnError(err);
127  return false;
128  }
129  }
130 
131 public:
132  typedef pthread_mutex_t* native_handle_type;
133  native_handle_type native_handle()
134  {
135  return &mLock;
136  }
137 
138 private:
139  pthread_mutex_t mLock;
140 };
141 
142 } // namespace AICommon
143 
144 
145 #endif // !defined(MUTEX_H)
146 
Definition: Mutex.h:67