Apollo 10.0
自动驾驶开放平台
mutex.h
浏览该文件的文档.
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
20namespace apollo {
21namespace perception {
22namespace lib {
23
24class 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
44class 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
57class 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
105
106 private:
107 Mutex mutex_;
108 CondVar cond_;
109 size_t counter_;
110};
111
112class 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(); }
133
136
137 private:
138 RwMutex *mu_ = nullptr;
139};
140
142 public:
143 explicit WriterMutexLock(RwMutex *mu) : mu_(mu) { mu_->WriterLock(); }
145
148
149 private:
150 RwMutex *mu_ = nullptr;
151};
152
153} // namespace lib
154} // namespace perception
155} // namespace apollo
BlockingCounter & operator=(const BlockingCounter &)=delete
BlockingCounter(const BlockingCounter &)=delete
CondVar(const CondVar &)=delete
CondVar & operator=(const CondVar &)=delete
MutexLock & operator=(const MutexLock &)=delete
MutexLock(const MutexLock &)=delete
Mutex(const Mutex &)=delete
Mutex & operator=(const Mutex &)=delete
ReaderMutexLock(const ReaderMutexLock &)=delete
ReaderMutexLock & operator=(const ReaderMutexLock &)=delete
RwMutex & operator=(const RwMutex &)=delete
RwMutex(const RwMutex &)=delete
WriterMutexLock & operator=(const WriterMutexLock &)=delete
WriterMutexLock(const WriterMutexLock &)=delete
class register implement
Definition arena_queue.h:37