/Users/deen/code/yugabyte-db/src/yb/util/mutex.cc
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 | | // Portions (c) 2011 The Chromium Authors. |
33 | | |
34 | | #include "yb/util/mutex.h" |
35 | | |
36 | | #include <glog/logging.h> |
37 | | |
38 | | #include "yb/util/stack_trace.h" |
39 | | #include "yb/util/env.h" |
40 | | |
41 | | namespace yb { |
42 | | |
43 | | Mutex::Mutex() |
44 | | #ifndef NDEBUG |
45 | | : owning_tid_(0), |
46 | | stack_trace_(new StackTrace()) |
47 | | #endif |
48 | 19.4M | { |
49 | 19.4M | #ifndef NDEBUG |
50 | | // In debug, setup attributes for lock error checking. |
51 | 19.4M | pthread_mutexattr_t mta; |
52 | 19.4M | int rv = pthread_mutexattr_init(&mta); |
53 | 19.4M | DCHECK_EQ(0, rv) << ". " << strerror(rv)0 ; |
54 | 19.4M | rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
55 | 19.4M | DCHECK_EQ(0, rv) << ". " << strerror(rv)0 ; |
56 | 19.4M | rv = pthread_mutex_init(&native_handle_, &mta); |
57 | 19.4M | DCHECK_EQ(0, rv) << ". " << strerror(rv)0 ; |
58 | 19.4M | rv = pthread_mutexattr_destroy(&mta); |
59 | 19.4M | DCHECK_EQ(0, rv) << ". " << strerror(rv)0 ; |
60 | | #else |
61 | | // In release, go with the default lock attributes. |
62 | | pthread_mutex_init(&native_handle_, NULL); |
63 | | #endif |
64 | 19.4M | } |
65 | | |
66 | 17.0M | Mutex::~Mutex() { |
67 | 17.0M | int rv = pthread_mutex_destroy(&native_handle_); |
68 | 17.0M | DCHECK_EQ(0, rv) << ". " << strerror(rv)0 ; |
69 | 17.0M | } |
70 | | |
71 | 0 | bool Mutex::TryAcquire() { |
72 | 0 | int rv = pthread_mutex_trylock(&native_handle_); |
73 | 0 | #ifndef NDEBUG |
74 | 0 | DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv) |
75 | 0 | << ". Owner tid: " << owning_tid_ << "; Self tid: " << Env::Default()->gettid() |
76 | 0 | << "; Owner stack: " << std::endl << stack_trace_->Symbolize();; |
77 | 0 | if (rv == 0) { |
78 | 0 | CheckUnheldAndMark(); |
79 | 0 | } |
80 | 0 | #endif |
81 | 0 | return rv == 0; |
82 | 0 | } |
83 | | |
84 | 3.39G | void Mutex::Acquire() { |
85 | 3.39G | int rv = pthread_mutex_lock(&native_handle_); |
86 | 3.39G | #ifndef NDEBUG |
87 | 3.39G | DCHECK_EQ(0, rv) << ". " << strerror(rv) |
88 | 1 | << ". Owner tid: " << owning_tid_ << "; Self tid: " << Env::Default()->gettid() |
89 | 1 | << "; Owner stack: " << std::endl << stack_trace_->Symbolize();; |
90 | 3.39G | CheckUnheldAndMark(); |
91 | | #else |
92 | | DCHECK(!rv); |
93 | | #endif |
94 | 3.39G | } |
95 | | |
96 | 3.39G | void Mutex::Release() { |
97 | 3.39G | #ifndef NDEBUG |
98 | 3.39G | CheckHeldAndUnmark(); |
99 | 3.39G | #endif |
100 | 3.39G | int rv = pthread_mutex_unlock(&native_handle_); |
101 | 3.39G | DCHECK_EQ(0, rv) << ". " << strerror(rv)0 ; |
102 | 3.39G | } |
103 | | |
104 | | #ifndef NDEBUG |
105 | 6.93G | void Mutex::AssertAcquired() const { |
106 | 6.93G | DCHECK_EQ(Env::Default()->gettid(), owning_tid_); |
107 | 6.93G | } |
108 | | |
109 | 3.53G | void Mutex::CheckHeldAndUnmark() { |
110 | 3.53G | AssertAcquired(); |
111 | 3.53G | owning_tid_ = 0; |
112 | 3.53G | stack_trace_->Reset(); |
113 | 3.53G | } |
114 | | |
115 | 3.53G | void Mutex::CheckUnheldAndMark() { |
116 | 3.53G | DCHECK_EQ(0, owning_tid_); |
117 | 3.53G | owning_tid_ = Env::Default()->gettid(); |
118 | 3.53G | stack_trace_->Collect(); |
119 | 3.53G | } |
120 | | |
121 | | #endif |
122 | | |
123 | | } // namespace yb |