-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsynchronization_absl.cc
83 lines (65 loc) · 1.88 KB
/
synchronization_absl.cc
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
80
81
82
83
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h" // NOLINT
#if defined(DART_USE_ABSL)
#include "platform/synchronization.h"
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
Mutex::Mutex() {}
Mutex::~Mutex() {}
ABSL_NO_THREAD_SAFETY_ANALYSIS
void Mutex::Lock() {
mutex_.Lock();
owner_.Acquire();
}
ABSL_NO_THREAD_SAFETY_ANALYSIS
bool Mutex::TryLock() {
if (!mutex_.TryLock()) {
return false;
}
owner_.Acquire();
return true;
}
ABSL_NO_THREAD_SAFETY_ANALYSIS
void Mutex::Unlock() {
owner_.Release();
mutex_.Unlock();
}
ConditionVariable::ConditionVariable() {}
ConditionVariable::~ConditionVariable() {}
ABSL_NO_THREAD_SAFETY_ANALYSIS
ConditionVariable::WaitResult ConditionVariable::Wait(Mutex* mutex,
int64_t timeout_millis) {
static_assert(kNoTimeout * kMicrosecondsPerMillisecond == kNoTimeout);
return WaitMicros(mutex, timeout_millis * kMicrosecondsPerMillisecond);
}
ABSL_NO_THREAD_SAFETY_ANALYSIS
ConditionVariable::WaitResult ConditionVariable::WaitMicros(
Mutex* mutex,
int64_t timeout_micros) {
mutex->owner_.Release();
Monitor::WaitResult retval = kNotified;
if (timeout_micros == kNoTimeout) {
// Wait forever.
cv_.Wait(&mutex->mutex_);
} else {
if (cv_.WaitWithTimeout(&mutex->mutex_,
absl::Microseconds(timeout_micros))) {
retval = kTimedOut;
}
}
mutex->owner_.Acquire();
return retval;
}
ABSL_NO_THREAD_SAFETY_ANALYSIS
void ConditionVariable::Notify() {
cv_.Signal();
}
ABSL_NO_THREAD_SAFETY_ANALYSIS
void ConditionVariable::NotifyAll() {
cv_.SignalAll();
}
} // namespace dart
#endif // defined(DART_USE_ABSL)