v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
mutex.cc
Go to the documentation of this file.
1// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
8
9namespace v8 {
10namespace base {
11
15
18 if (thread_id_ == own_id) {
19 level_++;
20 return;
21 }
22 mutex_.Lock();
23 DCHECK_EQ(0, level_);
24 thread_id_ = own_id;
25 level_ = 1;
26}
27
29#ifdef DEBUG
31 CHECK_EQ(thread_id_, own_id);
32#endif
33 if ((--level_) == 0) {
34 thread_id_ = 0;
35 mutex_.Unlock();
36 }
37}
38
41 if (thread_id_ == own_id) {
42 level_++;
43 return true;
44 }
45 if (mutex_.TryLock()) {
46 DCHECK_EQ(0, level_);
47 thread_id_ = own_id;
48 level_ = 1;
49 return true;
50 }
51 return false;
52}
53
55#ifdef DEBUG
56 level_ = 0;
57#endif
58}
59
60Mutex::~Mutex() { DCHECK_EQ(0, level_); }
61
62void Mutex::Lock() ABSL_NO_THREAD_SAFETY_ANALYSIS {
63 native_handle_.Lock();
65}
66
67void Mutex::Unlock() ABSL_NO_THREAD_SAFETY_ANALYSIS {
69 native_handle_.Unlock();
70}
71
72bool Mutex::TryLock() ABSL_NO_THREAD_SAFETY_ANALYSIS {
73 if (!native_handle_.TryLock()) return false;
75 return true;
76}
77
78} // namespace base
79} // namespace v8
void Unlock()
Definition mutex.cc:67
bool TryLock() V8_WARN_UNUSED_RESULT
Definition mutex.cc:72
V8_INLINE void AssertUnheldAndMark()
Definition mutex.h:80
absl::Mutex native_handle_
Definition mutex.h:91
void Lock()
Definition mutex.cc:62
V8_INLINE void AssertHeldAndUnmark()
Definition mutex.h:71
static int GetCurrentThreadId()
Definition platform.cc:29
std::atomic< int > thread_id_
Definition mutex.h:160
bool TryLock() V8_WARN_UNUSED_RESULT
Definition mutex.cc:39
#define CHECK_EQ(lhs, rhs)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485