/Users/deen/code/yugabyte-db/src/yb/util/mutex.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef YB_UTIL_MUTEX_H |
33 | | #define YB_UTIL_MUTEX_H |
34 | | |
35 | | #include <pthread.h> |
36 | | #include <sys/types.h> |
37 | | |
38 | | #include <memory> |
39 | | |
40 | | #include <glog/logging.h> |
41 | | |
42 | | #include "yb/gutil/macros.h" |
43 | | |
44 | | namespace yb { |
45 | | |
46 | | class StackTrace; |
47 | | |
48 | | // A lock built around pthread_mutex_t. Does not allow recursion. |
49 | | // |
50 | | // The following checks will be performed in DEBUG mode: |
51 | | // Acquire(), TryAcquire() - the lock isn't already held. |
52 | | // Release() - the lock is already held by this thread. |
53 | | // |
54 | | class Mutex { |
55 | | public: |
56 | | Mutex(); |
57 | | ~Mutex(); |
58 | | |
59 | | void Acquire(); |
60 | | void Release(); |
61 | | bool TryAcquire(); |
62 | | |
63 | 1.31M | void lock() { Acquire(); } |
64 | 1.31M | void unlock() { Release(); } |
65 | 0 | bool try_lock() { return TryAcquire(); } |
66 | | |
67 | | #ifndef NDEBUG |
68 | | void AssertAcquired() const; |
69 | | #else |
70 | | void AssertAcquired() const {} |
71 | | #endif |
72 | | |
73 | | private: |
74 | | friend class ConditionVariable; |
75 | | |
76 | | pthread_mutex_t native_handle_; |
77 | | |
78 | | #ifndef NDEBUG |
79 | | // Members and routines taking care of locks assertions. |
80 | | void CheckHeldAndUnmark(); |
81 | | void CheckUnheldAndMark(); |
82 | | |
83 | | // All private data is implicitly protected by native_handle_. |
84 | | // Be VERY careful to only access members under that lock. |
85 | | uint64_t owning_tid_; |
86 | | std::unique_ptr<StackTrace> stack_trace_; |
87 | | #endif |
88 | | |
89 | | DISALLOW_COPY_AND_ASSIGN(Mutex); |
90 | | }; |
91 | | |
92 | | // A helper class that acquires the given Lock while the MutexLock is in scope. |
93 | | class MutexLock { |
94 | | public: |
95 | | struct AlreadyAcquired {}; |
96 | | |
97 | | // Acquires 'lock' (must be unheld) and wraps around it. |
98 | | // |
99 | | // Sample usage: |
100 | | // { |
101 | | // MutexLock l(lock_); // acquired |
102 | | // ... |
103 | | // } // released |
104 | | explicit MutexLock(Mutex& lock) // NOLINT |
105 | | : lock_(&lock), |
106 | 3.30G | owned_(true) { |
107 | 3.30G | lock_->Acquire(); |
108 | 3.30G | } |
109 | | |
110 | | // Wraps around 'lock' (must already be held by this thread). |
111 | | // |
112 | | // Sample usage: |
113 | | // { |
114 | | // lock_.Acquire(); // acquired |
115 | | // ... |
116 | | // MutexLock l(lock_, AlreadyAcquired()); |
117 | | // ... |
118 | | // } // released |
119 | | MutexLock(Mutex& lock, const AlreadyAcquired&) // NOLINT |
120 | | : lock_(&lock), |
121 | 0 | owned_(true) { |
122 | 0 | lock_->AssertAcquired(); |
123 | 0 | } |
124 | | |
125 | 94.8M | void Lock() { |
126 | 94.8M | DCHECK(!owned_); |
127 | 94.8M | lock_->Acquire(); |
128 | 94.8M | owned_ = true; |
129 | 94.8M | } |
130 | | |
131 | 3.39G | void Unlock() { |
132 | 3.39G | DCHECK(owned_); |
133 | 3.39G | lock_->AssertAcquired(); |
134 | 3.39G | lock_->Release(); |
135 | 3.39G | owned_ = false; |
136 | 3.39G | } |
137 | | |
138 | 3.30G | ~MutexLock() { |
139 | 3.30G | if (owned_) { |
140 | 3.18G | Unlock(); |
141 | 3.18G | } |
142 | 3.30G | } |
143 | | |
144 | 837k | bool OwnsLock() const { |
145 | 837k | return owned_; |
146 | 837k | } |
147 | | |
148 | | private: |
149 | | Mutex* lock_; |
150 | | bool owned_; |
151 | | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
152 | | }; |
153 | | |
154 | | } // namespace yb |
155 | | #endif /* YB_UTIL_MUTEX_H */ |