/Users/deen/code/yugabyte-db/src/yb/util/rw_semaphore-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <functional> |
34 | | #include <thread> |
35 | | |
36 | | #include <gtest/gtest.h> |
37 | | |
38 | | #include "yb/util/monotime.h" |
39 | | #include "yb/util/rw_semaphore.h" |
40 | | #include "yb/util/shared_lock.h" |
41 | | |
42 | | using std::vector; |
43 | | |
44 | | namespace yb { |
45 | | struct SharedState { |
46 | 1 | SharedState() : done(false), int_var(0) {} |
47 | | |
48 | | bool done; |
49 | | int64_t int_var; |
50 | | rw_semaphore sem; |
51 | | }; |
52 | | |
53 | | // Thread which increases the value in the shared state under the write lock. |
54 | 5 | void Writer(SharedState* state) { |
55 | 5 | int i = 0; |
56 | 234k | while (true) { |
57 | 234k | std::lock_guard<rw_semaphore> l(state->sem); |
58 | 234k | state->int_var += (i++); |
59 | 234k | if (state->done) { |
60 | 5 | break; |
61 | 5 | } |
62 | 234k | } |
63 | 5 | } |
64 | | |
65 | | // Thread which verifies that the value in the shared state only increases. |
66 | 5 | void Reader(SharedState* state) { |
67 | 5 | int64_t prev_val = 0; |
68 | 102k | while (true) { |
69 | 102k | SharedLock<rw_semaphore> l(state->sem); |
70 | | // The int var should only be seen to increase. |
71 | 102k | CHECK_GE(state->int_var, prev_val); |
72 | 102k | prev_val = state->int_var; |
73 | 102k | if (state->done) { |
74 | 5 | break; |
75 | 5 | } |
76 | 102k | } |
77 | 5 | } |
78 | | |
79 | | // Test which verifies basic functionality of the semaphore. |
80 | | // When run under TSAN this also verifies the barriers. |
81 | 1 | TEST(RWSemaphoreTest, TestBasicOperation) { |
82 | 1 | SharedState s; |
83 | 1 | vector<std::thread> threads; |
84 | | // Start 5 readers and writers. |
85 | 6 | for (int i = 0; i < 5; i++) { |
86 | 5 | threads.emplace_back(std::bind(Reader, &s)); |
87 | 5 | threads.emplace_back(std::bind(Writer, &s)); |
88 | 5 | } |
89 | | |
90 | | // Let them contend for a short amount of time. |
91 | 1 | SleepFor(MonoDelta::FromMilliseconds(50)); |
92 | | |
93 | | // Signal them to stop. |
94 | 1 | { |
95 | 1 | std::lock_guard<rw_semaphore> l(s.sem); |
96 | 1 | s.done = true; |
97 | 1 | } |
98 | | |
99 | 10 | for (auto& t : threads) { |
100 | 10 | t.join(); |
101 | 10 | } |
102 | 1 | } |
103 | | |
104 | | } // namespace yb |