1 /*
2  * Copyright 2022 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 <shared_mutex>
20 
21 namespace android::ftl {
22 
23 // Wrapper around std::shared_mutex to provide capabilities for thread-safety
24 // annotations.
25 // TODO(b/257958323): This class is no longer needed once b/135688034 is fixed (currently blocked on
26 // b/175635923).
27 class [[clang::capability("shared_mutex")]] SharedMutex final {
28  public:
lock()29   [[clang::acquire_capability()]] void lock() {
30     mutex_.lock();
31   }
unlock()32   [[clang::release_capability()]] void unlock() {
33     mutex_.unlock();
34   }
35 
lock_shared()36   [[clang::acquire_shared_capability()]] void lock_shared() {
37     mutex_.lock_shared();
38   }
unlock_shared()39   [[clang::release_shared_capability()]] void unlock_shared() {
40     mutex_.unlock_shared();
41   }
42 
43  private:
44   std::shared_mutex mutex_;
45 };
46 
47 }  // namespace android::ftl
48