Apollo 10.0
自动驾驶开放平台
rw_lock_guard.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
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
30namespace apollo {
31namespace cyber {
32namespace base {
33
34template <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
47template <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_
class register implement
Definition arena_queue.h:37