mutex.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
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 #ifndef CARTOGRAPHER_COMMON_MUTEX_H_
18 #define CARTOGRAPHER_COMMON_MUTEX_H_
19 
20 #include <condition_variable>
21 #include <mutex>
22 
24 
25 namespace cartographer {
26 namespace common {
27 
28 // Enable thread safety attributes only with clang.
29 // The attributes can be safely erased when compiling with other compilers.
30 #if defined(__SUPPORT_TS_ANNOTATION__) || defined(__clang__)
31 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
32 #else
33 #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
34 #endif
35 
36 #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
37 
38 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
39 
40 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
41 
42 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
43 
44 #define REQUIRES(...) \
45  THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
46 
47 #define ACQUIRE(...) \
48  THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
49 
50 #define RELEASE(...) \
51  THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
52 
53 #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
54 
55 #define NO_THREAD_SAFETY_ANALYSIS \
56  THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
57 
58 // Defines an annotated mutex that can only be locked through its scoped locker
59 // implementation.
60 class CAPABILITY("mutex") Mutex {
61  public:
62  // A RAII class that acquires a mutex in its constructor, and
63  // releases it in its destructor. It also implements waiting functionality on
64  // conditions that get checked whenever the mutex is released.
65  class SCOPED_CAPABILITY Locker {
66  public:
67  Locker(Mutex* mutex) ACQUIRE(mutex) : mutex_(mutex), lock_(mutex->mutex_) {}
68 
69  ~Locker() RELEASE() {
70  lock_.unlock();
71  mutex_->condition_.notify_all();
72  }
73 
74  template <typename Predicate>
75  void Await(Predicate predicate) REQUIRES(this) {
76  mutex_->condition_.wait(lock_, predicate);
77  }
78 
79  template <typename Predicate>
80  bool AwaitWithTimeout(Predicate predicate, common::Duration timeout)
81  REQUIRES(this) {
82  return mutex_->condition_.wait_for(lock_, timeout, predicate);
83  }
84 
85  private:
86  Mutex* mutex_;
87  std::unique_lock<std::mutex> lock_;
88  };
89 
90  private:
91  std::condition_variable condition_;
92  std::mutex mutex_;
93 };
94 
95 using MutexLocker = Mutex::Locker;
96 
97 } // namespace common
98 } // namespace cartographer
99 
100 #endif // CARTOGRAPHER_COMMON_MUTEX_H_
#define SCOPED_CAPABILITY
Definition: mutex.h:38
#define RELEASE(...)
Definition: mutex.h:50
UniversalTimeScaleClock::duration Duration
Definition: time.h:43
#define REQUIRES(...)
Definition: mutex.h:44
class CAPABILITY("mutex") Mutex
Definition: mutex.h:60
#define ACQUIRE(...)
Definition: mutex.h:47
Mutex::Locker MutexLocker
Definition: mutex.h:95


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:39