Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 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 | #pragma once |
| 18 | |
| 19 | #include <pthread.h> |
| 20 | |
| 21 | // As of the end of Dec 2019, std::shared_mutex is *not* simply a pthread_rwlock, but rather a |
| 22 | // combination of std::mutex and std::condition variable, which is obviously less efficient. This |
| 23 | // immitates what std::shared_mutex should be doing and is compatible with std::shared_lock and |
| 24 | // std::unique_lock. |
| 25 | |
| 26 | class RwLock { |
| 27 | public: |
| 28 | RwLock() {} |
| 29 | ~RwLock() {} |
| 30 | |
| 31 | void lock() { pthread_rwlock_wrlock(&rwlock_); } |
| 32 | void unlock() { pthread_rwlock_unlock(&rwlock_); } |
| 33 | |
| 34 | void lock_shared() { pthread_rwlock_rdlock(&rwlock_); } |
| 35 | void unlock_shared() { pthread_rwlock_unlock(&rwlock_); } |
| 36 | |
| 37 | private: |
| 38 | pthread_rwlock_t rwlock_ = PTHREAD_RWLOCK_INITIALIZER; |
| 39 | }; |