YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
5.29M
{
49
5.29M
#ifndef NDEBUG
50
  // In debug, setup attributes for lock error checking.
51
5.29M
  pthread_mutexattr_t mta;
52
5.29M
  int rv = pthread_mutexattr_init(&mta);
53
0
  DCHECK_EQ(0, rv) << ". " << strerror(rv);
54
5.29M
  rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
55
0
  DCHECK_EQ(0, rv) << ". " << strerror(rv);
56
5.29M
  rv = pthread_mutex_init(&native_handle_, &mta);
57
0
  DCHECK_EQ(0, rv) << ". " << strerror(rv);
58
5.29M
  rv = pthread_mutexattr_destroy(&mta);
59
0
  DCHECK_EQ(0, rv) << ". " << strerror(rv);
60
#else
61
  // In release, go with the default lock attributes.
62
  pthread_mutex_init(&native_handle_, NULL);
63
#endif
64
5.29M
}
65
66
3.85M
Mutex::~Mutex() {
67
3.85M
  int rv = pthread_mutex_destroy(&native_handle_);
68
0
  DCHECK_EQ(0, rv) << ". " << strerror(rv);
69
3.85M
}
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
533M
void Mutex::Acquire() {
85
533M
  int rv = pthread_mutex_lock(&native_handle_);
86
533M
#ifndef NDEBUG
87
1
  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
533M
  CheckUnheldAndMark();
91
#else
92
  DCHECK(!rv);
93
#endif
94
533M
}
95
96
533M
void Mutex::Release() {
97
533M
#ifndef NDEBUG
98
533M
  CheckHeldAndUnmark();
99
533M
#endif
100
533M
  int rv = pthread_mutex_unlock(&native_handle_);
101
0
  DCHECK_EQ(0, rv) << ". " << strerror(rv);
102
533M
}
103
104
#ifndef NDEBUG
105
1.12G
void Mutex::AssertAcquired() const {
106
1.12G
  DCHECK_EQ(Env::Default()->gettid(), owning_tid_);
107
1.12G
}
108
109
589M
void Mutex::CheckHeldAndUnmark() {
110
589M
  AssertAcquired();
111
589M
  owning_tid_ = 0;
112
589M
  stack_trace_->Reset();
113
589M
}
114
115
589M
void Mutex::CheckUnheldAndMark() {
116
589M
  DCHECK_EQ(0, owning_tid_);
117
589M
  owning_tid_ = Env::Default()->gettid();
118
589M
  stack_trace_->Collect();
119
589M
}
120
121
#endif
122
123
} // namespace yb