forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex.cc
More file actions
79 lines (66 loc) Β· 1.51 KB
/
mutex.cc
File metadata and controls
79 lines (66 loc) Β· 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
namespace v8 {
namespace base {
RecursiveMutex::~RecursiveMutex() {
DCHECK_EQ(0, level_);
}
void RecursiveMutex::Lock() {
int own_id = v8::base::OS::GetCurrentThreadId();
if (thread_id_ == own_id) {
level_++;
return;
}
mutex_.Lock();
DCHECK_EQ(0, level_);
thread_id_ = own_id;
level_ = 1;
}
void RecursiveMutex::Unlock() {
#ifdef DEBUG
int own_id = v8::base::OS::GetCurrentThreadId();
CHECK_EQ(thread_id_, own_id);
#endif
if ((--level_) == 0) {
thread_id_ = 0;
mutex_.Unlock();
}
}
bool RecursiveMutex::TryLock() {
int own_id = v8::base::OS::GetCurrentThreadId();
if (thread_id_ == own_id) {
level_++;
return true;
}
if (mutex_.TryLock()) {
DCHECK_EQ(0, level_);
thread_id_ = own_id;
level_ = 1;
return true;
}
return false;
}
Mutex::Mutex() {
#ifdef DEBUG
level_ = 0;
#endif
}
Mutex::~Mutex() { DCHECK_EQ(0, level_); }
void Mutex::Lock() ABSL_NO_THREAD_SAFETY_ANALYSIS {
native_handle_.Lock();
AssertUnheldAndMark();
}
void Mutex::Unlock() ABSL_NO_THREAD_SAFETY_ANALYSIS {
AssertHeldAndUnmark();
native_handle_.Unlock();
}
bool Mutex::TryLock() ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (!native_handle_.TryLock()) return false;
AssertUnheldAndMark();
return true;
}
} // namespace base
} // namespace v8