Apollo 10.0
自动驾驶开放平台
pthread_rw_lock.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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#ifndef CYBER_BASE_PTHREAD_RW_LOCK_H_
17#define CYBER_BASE_PTHREAD_RW_LOCK_H_
18
19#include <thread>
20
22
23namespace apollo {
24namespace cyber {
25namespace base {
26
28 friend class ReadLockGuard<PthreadRWLock>;
29 friend class WriteLockGuard<PthreadRWLock>;
30
31 public:
32 explicit PthreadRWLock(bool writer) {
33 pthread_rwlockattr_init(&rwlock_attr_);
34 if (writer) {
35 pthread_rwlockattr_setkind_np(
36 &rwlock_attr_, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
37 }
38 pthread_rwlockattr_setpshared(&rwlock_attr_, PTHREAD_PROCESS_SHARED);
39 pthread_rwlock_init(&rwlock_, &rwlock_attr_);
40 }
42
44 pthread_rwlock_destroy(&rwlock_);
45 pthread_rwlockattr_destroy(&rwlock_attr_);
46 }
47
48 void ReadLock();
49 void ReadUnlock();
50
51 void WriteLock();
52 void WriteUnlock();
53
54 private:
55 PthreadRWLock(const PthreadRWLock& other) = delete;
56 PthreadRWLock& operator=(const PthreadRWLock& other) = delete;
57 pthread_rwlock_t rwlock_;
58 pthread_rwlockattr_t rwlock_attr_;
59};
60
61inline void PthreadRWLock::ReadLock() { pthread_rwlock_rdlock(&rwlock_); }
62
63inline void PthreadRWLock::ReadUnlock() { pthread_rwlock_unlock(&rwlock_); }
64
65inline void PthreadRWLock::WriteLock() { pthread_rwlock_wrlock(&rwlock_); }
66
67inline void PthreadRWLock::WriteUnlock() { pthread_rwlock_unlock(&rwlock_); }
68
69} // namespace base
70} // namespace cyber
71} // namespace apollo
72
73#endif
class register implement
Definition arena_queue.h:37