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 #include <ftl/shared_mutex.h>
18 #include <gtest/gtest.h>
19 #include <ftl/fake_guard.h>
20
21 namespace android::test {
22
TEST(SharedMutex,SharedLock)23 TEST(SharedMutex, SharedLock) {
24 ftl::SharedMutex mutex;
25 std::shared_lock shared_lock(mutex);
26
27 { std::shared_lock shared_lock2(mutex); }
28 }
29
TEST(SharedMutex,ExclusiveLock)30 TEST(SharedMutex, ExclusiveLock) {
31 ftl::SharedMutex mutex;
32 std::unique_lock unique_lock(mutex);
33 }
34
TEST(SharedMutex,Annotations)35 TEST(SharedMutex, Annotations) {
36 struct {
37 void foo() FTL_ATTRIBUTE(requires_shared_capability(mutex)) { num++; }
38 void bar() FTL_ATTRIBUTE(requires_capability(mutex)) { num++; }
39 void baz() {
40 std::shared_lock shared_lock(mutex);
41 num++;
42 }
43 ftl::SharedMutex mutex;
44 int num = 0;
45
46 } s;
47
48 {
49 // TODO(b/257958323): Use an RAII class instead of locking manually.
50 s.mutex.lock_shared();
51 s.foo();
52 s.baz();
53 s.mutex.unlock_shared();
54 }
55 s.mutex.lock();
56 s.bar();
57 s.mutex.unlock();
58 }
59
60 } // namespace android::test
61