Apollo  6.0
Open source self driving car software
mutex.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 #pragma once
17 
18 #include <pthread.h>
19 
20 namespace apollo {
21 namespace perception {
22 namespace lib {
23 
24 class Mutex {
25  public:
26  Mutex() { pthread_mutex_init(&mu_, nullptr); }
27 
28  ~Mutex() { pthread_mutex_destroy(&mu_); }
29 
30  inline void Lock() { pthread_mutex_lock(&mu_); }
31 
32  inline void Unlock() { pthread_mutex_unlock(&mu_); }
33 
34  inline bool TryLock() { return pthread_mutex_trylock(&mu_) == 0; }
35 
36  Mutex(const Mutex &) = delete;
37  Mutex &operator=(const Mutex &) = delete;
38 
39  private:
40  friend class CondVar;
41  pthread_mutex_t mu_;
42 };
43 
44 class MutexLock {
45  public:
46  explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
47  ~MutexLock() { mu_->Unlock(); }
48 
49  MutexLock(const MutexLock &) = delete;
50  MutexLock &operator=(const MutexLock &) = delete;
51 
52  private:
53  Mutex *const mu_;
54 };
55 
56 // Wrapper for pthread_cond_t
57 class CondVar {
58  public:
59  CondVar() { pthread_cond_init(&cv_, nullptr); }
60  ~CondVar() { pthread_cond_destroy(&cv_); }
61 
62  void Wait(Mutex *mu) { pthread_cond_wait(&cv_, &mu->mu_); }
63 
64  void Signal() { pthread_cond_signal(&cv_); }
65 
66  void Signalall() { pthread_cond_broadcast(&cv_); }
67 
68  CondVar(const CondVar &) = delete;
69  CondVar &operator=(const CondVar &) = delete;
70 
71  private:
72  pthread_cond_t cv_;
73 };
74 
76  public:
77  explicit BlockingCounter(size_t cnt) : counter_(cnt) {}
78 
79  bool Decrement() {
80  MutexLock lock(&mutex_);
81  --counter_;
82 
83  if (counter_ == 0u) {
84  cond_.Signalall();
85  }
86 
87  return counter_ == 0u;
88  }
89 
90  void Reset(size_t cnt) {
91  MutexLock lock(&mutex_);
92  counter_ = cnt;
93  }
94 
95  void Wait() {
96  MutexLock lock(&mutex_);
97 
98  while (counter_ != 0u) {
99  cond_.Wait(&mutex_);
100  }
101  }
102 
103  BlockingCounter(const BlockingCounter &) = delete;
104  BlockingCounter &operator=(const BlockingCounter &) = delete;
105 
106  private:
107  Mutex mutex_;
108  CondVar cond_;
109  size_t counter_;
110 };
111 
112 class RwMutex {
113  public:
114  RwMutex() { pthread_rwlock_init(&mu_, nullptr); }
115  ~RwMutex() { pthread_rwlock_destroy(&mu_); }
116 
117  inline void ReaderLock() { pthread_rwlock_rdlock(&mu_); }
118  inline void WriterLock() { pthread_rwlock_wrlock(&mu_); }
119 
120  inline void Unlock() { pthread_rwlock_unlock(&mu_); }
121 
122  RwMutex(const RwMutex &) = delete;
123  RwMutex &operator=(const RwMutex &) = delete;
124 
125  private:
126  pthread_rwlock_t mu_;
127 };
128 
130  public:
131  explicit ReaderMutexLock(RwMutex *mu) : mu_(mu) { mu_->ReaderLock(); }
132  ~ReaderMutexLock() { mu_->Unlock(); }
133 
134  ReaderMutexLock(const ReaderMutexLock &) = delete;
135  ReaderMutexLock &operator=(const ReaderMutexLock &) = delete;
136 
137  private:
138  RwMutex *mu_ = nullptr;
139 };
140 
142  public:
143  explicit WriterMutexLock(RwMutex *mu) : mu_(mu) { mu_->WriterLock(); }
144  ~WriterMutexLock() { mu_->Unlock(); }
145 
146  WriterMutexLock(const WriterMutexLock &) = delete;
147  WriterMutexLock &operator=(const WriterMutexLock &) = delete;
148 
149  private:
150  RwMutex *mu_ = nullptr;
151 };
152 
153 } // namespace lib
154 } // namespace perception
155 } // namespace apollo
Mutex()
Definition: mutex.h:26
void Lock()
Definition: mutex.h:30
CondVar()
Definition: mutex.h:59
friend class CondVar
Definition: mutex.h:40
void WriterLock()
Definition: mutex.h:118
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
MutexLock(Mutex *mu)
Definition: mutex.h:46
RwMutex()
Definition: mutex.h:114
bool TryLock()
Definition: mutex.h:34
void Signalall()
Definition: mutex.h:66
~CondVar()
Definition: mutex.h:60
Definition: mutex.h:24
void Signal()
Definition: mutex.h:64
WriterMutexLock(RwMutex *mu)
Definition: mutex.h:143
void Unlock()
Definition: mutex.h:32
void Wait()
Definition: mutex.h:95
BlockingCounter(size_t cnt)
Definition: mutex.h:77
void Unlock()
Definition: mutex.h:120
~MutexLock()
Definition: mutex.h:47
ReaderMutexLock(RwMutex *mu)
Definition: mutex.h:131
void ReaderLock()
Definition: mutex.h:117
~RwMutex()
Definition: mutex.h:115
void Reset(size_t cnt)
Definition: mutex.h:90
void Wait(Mutex *mu)
Definition: mutex.h:62
~WriterMutexLock()
Definition: mutex.h:144
Definition: mutex.h:57
~Mutex()
Definition: mutex.h:28
Mutex & operator=(const Mutex &)=delete
~ReaderMutexLock()
Definition: mutex.h:132
bool Decrement()
Definition: mutex.h:79
Definition: mutex.h:112