Apollo  6.0
Open source self driving car software
rw_lock_guard.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 
17 #ifndef CYBER_BASE_RW_LOCK_GUARD_H_
18 #define CYBER_BASE_RW_LOCK_GUARD_H_
19 
20 #include <unistd.h>
21 
22 #include <atomic>
23 #include <condition_variable>
24 #include <cstdint>
25 #include <cstdlib>
26 #include <iostream>
27 #include <mutex>
28 #include <thread>
29 
30 namespace apollo {
31 namespace cyber {
32 namespace base {
33 
34 template <typename RWLock>
36  public:
37  explicit ReadLockGuard(RWLock& lock) : rw_lock_(lock) { rw_lock_.ReadLock(); }
38 
39  ~ReadLockGuard() { rw_lock_.ReadUnlock(); }
40 
41  private:
42  ReadLockGuard(const ReadLockGuard& other) = delete;
43  ReadLockGuard& operator=(const ReadLockGuard& other) = delete;
44  RWLock& rw_lock_;
45 };
46 
47 template <typename RWLock>
49  public:
50  explicit WriteLockGuard(RWLock& lock) : rw_lock_(lock) {
51  rw_lock_.WriteLock();
52  }
53 
54  ~WriteLockGuard() { rw_lock_.WriteUnlock(); }
55 
56  private:
57  WriteLockGuard(const WriteLockGuard& other) = delete;
58  WriteLockGuard& operator=(const WriteLockGuard& other) = delete;
59  RWLock& rw_lock_;
60 };
61 
62 } // namespace base
63 } // namespace cyber
64 } // namespace apollo
65 
66 #endif // CYBER_BASE_RW_LOCK_GUARD_H_
WriteLockGuard(RWLock &lock)
Definition: rw_lock_guard.h:50
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
~ReadLockGuard()
Definition: rw_lock_guard.h:39
Definition: rw_lock_guard.h:48
ReadLockGuard(RWLock &lock)
Definition: rw_lock_guard.h:37
~WriteLockGuard()
Definition: rw_lock_guard.h:54
Definition: rw_lock_guard.h:35