|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <mutex> |
| 6 | +#include <shared_mutex> |
| 7 | +#include <utility> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +struct lockable_with_counters { |
| 12 | + void lock() { |
| 13 | + ++lock_count; |
| 14 | + } |
| 15 | + |
| 16 | + void unlock() { |
| 17 | + ++unlock_count; |
| 18 | + } |
| 19 | + |
| 20 | + void lock_shared() { |
| 21 | + ++shared_lock_count; |
| 22 | + } |
| 23 | + |
| 24 | + void unlock_shared() { |
| 25 | + ++shared_unlock_count; |
| 26 | + } |
| 27 | + |
| 28 | + int lock_count = 0; |
| 29 | + int unlock_count = 0; |
| 30 | + int shared_lock_count = 0; |
| 31 | + int shared_unlock_count = 0; |
| 32 | +}; |
| 33 | + |
| 34 | +#ifdef __clang__ |
| 35 | +#pragma clang diagnostic push |
| 36 | +#pragma clang diagnostic ignored "-Wself-move" |
| 37 | +#endif // __clang__ |
| 38 | +#pragma warning(push) |
| 39 | +#pragma warning(disable : 26800) // use a moved-from object |
| 40 | +int main() { |
| 41 | + lockable_with_counters lockable1; |
| 42 | + lockable_with_counters lockable2; |
| 43 | + |
| 44 | + { |
| 45 | + unique_lock<lockable_with_counters> lock_a(lockable1); |
| 46 | + assert(lockable1.lock_count == 1); |
| 47 | + assert(lockable1.unlock_count == 0); |
| 48 | + |
| 49 | + lock_a = move(lock_a); |
| 50 | + assert(lockable1.lock_count == 1); |
| 51 | + assert(lockable1.unlock_count == 0); |
| 52 | + { |
| 53 | + unique_lock<lockable_with_counters> lock_b(lockable2); |
| 54 | + assert(lockable2.lock_count == 1); |
| 55 | + assert(lockable2.unlock_count == 0); |
| 56 | + |
| 57 | + lock_a = move(lock_b); |
| 58 | + |
| 59 | + assert(lockable1.lock_count == 1); |
| 60 | + assert(lockable1.unlock_count == 1); |
| 61 | + assert(lockable2.lock_count == 1); |
| 62 | + assert(lockable2.unlock_count == 0); |
| 63 | + } |
| 64 | + |
| 65 | + assert(lockable1.lock_count == 1); |
| 66 | + assert(lockable1.unlock_count == 1); |
| 67 | + assert(lockable2.lock_count == 1); |
| 68 | + assert(lockable2.unlock_count == 0); |
| 69 | + } |
| 70 | + |
| 71 | + assert(lockable1.lock_count == 1); |
| 72 | + assert(lockable1.unlock_count == 1); |
| 73 | + assert(lockable2.lock_count == 1); |
| 74 | + assert(lockable2.unlock_count == 1); |
| 75 | + |
| 76 | + { |
| 77 | + shared_lock<lockable_with_counters> lock_a(lockable1); |
| 78 | + assert(lockable1.shared_lock_count == 1); |
| 79 | + assert(lockable1.shared_unlock_count == 0); |
| 80 | + |
| 81 | + lock_a = move(lock_a); |
| 82 | + assert(lockable1.shared_lock_count == 1); |
| 83 | + assert(lockable1.shared_unlock_count == 0); |
| 84 | + { |
| 85 | + shared_lock<lockable_with_counters> lock_b(lockable2); |
| 86 | + assert(lockable2.shared_lock_count == 1); |
| 87 | + assert(lockable2.shared_unlock_count == 0); |
| 88 | + |
| 89 | + lock_a = move(lock_b); |
| 90 | + |
| 91 | + assert(lockable1.shared_lock_count == 1); |
| 92 | + assert(lockable1.shared_unlock_count == 1); |
| 93 | + assert(lockable2.shared_lock_count == 1); |
| 94 | + assert(lockable2.shared_unlock_count == 0); |
| 95 | + } |
| 96 | + |
| 97 | + assert(lockable1.shared_lock_count == 1); |
| 98 | + assert(lockable1.shared_unlock_count == 1); |
| 99 | + assert(lockable2.shared_lock_count == 1); |
| 100 | + assert(lockable2.shared_unlock_count == 0); |
| 101 | + } |
| 102 | + |
| 103 | + assert(lockable1.shared_lock_count == 1); |
| 104 | + assert(lockable1.shared_unlock_count == 1); |
| 105 | + assert(lockable2.shared_lock_count == 1); |
| 106 | + assert(lockable2.shared_unlock_count == 1); |
| 107 | +} |
| 108 | +#pragma warning(pop) |
| 109 | +#ifdef __clang__ |
| 110 | +#pragma clang diagnostic pop |
| 111 | +#endif // __clang__ |
0 commit comments