1 /* 2 * Copyright (C) 2020 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/LICENSE2.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 #include <android-base/macros.h> 22 #include <android-base/thread_annotations.h> 23 24 // As of the end of May 2020, std::shared_mutex is *not* simply a pthread_rwlock, but rather a 25 // combination of std::mutex and std::condition variable, which is obviously less efficient. This 26 // immitates what std::shared_mutex should be doing and is compatible with RAII thread wrappers. 27 28 class SHARED_CAPABILITY("mutex") RwLock { 29 public: RwLock()30 RwLock() {} ~RwLock()31 ~RwLock() {} 32 lock()33 void lock() ACQUIRE() { pthread_rwlock_wrlock(&rwlock_); } lock_shared()34 void lock_shared() ACQUIRE_SHARED() { pthread_rwlock_rdlock(&rwlock_); } 35 unlock()36 void unlock() RELEASE() { pthread_rwlock_unlock(&rwlock_); } 37 38 private: 39 pthread_rwlock_t rwlock_ = PTHREAD_RWLOCK_INITIALIZER; 40 }; 41 42 // std::shared_lock does not have thread annotations, so we need our own. 43 44 class SCOPED_CAPABILITY SharedLock { 45 public: SharedLock(RwLock & lock)46 explicit SharedLock(RwLock& lock) ACQUIRE_SHARED(lock) : lock_(lock) { lock_.lock_shared(); } RELEASE()47 ~SharedLock() RELEASE() { lock_.unlock(); } 48 lock_shared()49 void lock_shared() ACQUIRE_SHARED() { lock_.lock_shared(); } unlock()50 void unlock() RELEASE() { lock_.unlock(); } 51 52 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedLock); 53 54 private: 55 RwLock& lock_; 56 }; 57