v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
condition-variable.h
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
5
#ifndef V8_BASE_PLATFORM_CONDITION_VARIABLE_H_
6
#define V8_BASE_PLATFORM_CONDITION_VARIABLE_H_
7
8
#include "absl/synchronization/mutex.h"
9
#include "
src/base/base-export.h
"
10
#include "
src/base/lazy-instance.h
"
11
#include "
src/base/platform/mutex.h
"
12
13
#if V8_OS_STARBOARD
14
#include "starboard/common/condition_variable.h"
15
#endif
16
17
namespace
v8
{
18
namespace
base
{
19
20
// Forward declarations.
21
class
ConditionVariableEvent;
22
class
TimeDelta;
23
24
// -----------------------------------------------------------------------------
25
// ConditionVariable
26
//
27
// This class is a synchronization primitive that can be used to block a thread,
28
// or multiple threads at the same time, until:
29
// - a notification is received from another thread,
30
// - a timeout expires, or
31
// - a spurious wakeup occurs
32
// Any thread that intends to wait on a ConditionVariable has to acquire a lock
33
// on a Mutex first. The |Wait()| and |WaitFor()| operations atomically release
34
// the mutex and suspend the execution of the calling thread. When the condition
35
// variable is notified, the thread is awakened, and the mutex is reacquired.
36
37
class
V8_BASE_EXPORT
ConditionVariable
{
38
public
:
39
ConditionVariable
();
40
ConditionVariable
(
const
ConditionVariable
&) =
delete
;
41
ConditionVariable
&
operator=
(
const
ConditionVariable
&) =
delete
;
42
~ConditionVariable
();
43
44
// If any threads are waiting on this condition variable, calling
45
// |NotifyOne()| unblocks one of the waiting threads.
46
void
NotifyOne();
47
48
// Unblocks all threads currently waiting for this condition variable.
49
void
NotifyAll();
50
51
// |Wait()| causes the calling thread to block until the condition variable is
52
// notified or a spurious wakeup occurs. Atomically releases the mutex, blocks
53
// the current executing thread, and adds it to the list of threads waiting on
54
// this condition variable. The thread will be unblocked when |NotifyAll()| or
55
// |NotifyOne()| is executed. It may also be unblocked spuriously. When
56
// unblocked, regardless of the reason, the lock on the mutex is reacquired
57
// and |Wait()| exits.
58
void
Wait(
Mutex
*
mutex
);
59
60
// Atomically releases the mutex, blocks the current executing thread, and
61
// adds it to the list of threads waiting on this condition variable. The
62
// thread will be unblocked when |NotifyAll()| or |NotifyOne()| is executed,
63
// or when the relative timeout |rel_time| expires. It may also be unblocked
64
// spuriously. When unblocked, regardless of the reason, the lock on the mutex
65
// is reacquired and |WaitFor()| exits. Returns true if the condition variable
66
// was notified prior to the timeout.
67
bool
WaitFor(
Mutex
*
mutex
,
const
TimeDelta
& rel_time)
V8_WARN_UNUSED_RESULT
;
68
69
private
:
70
absl::CondVar
native_handle_
;
71
};
72
73
// POD ConditionVariable initialized lazily (i.e. the first time Pointer() is
74
// called).
75
// Usage:
76
// static LazyConditionVariable my_condvar =
77
// LAZY_CONDITION_VARIABLE_INITIALIZER;
78
//
79
// void my_function() {
80
// MutexGuard lock_guard(&my_mutex);
81
// my_condvar.Pointer()->Wait(&my_mutex);
82
// }
83
using
LazyConditionVariable
=
84
LazyStaticInstance
<
ConditionVariable
,
85
DefaultConstructTrait<ConditionVariable>
,
86
ThreadSafeInitOnceTrait
>
::type
;
87
88
#define LAZY_CONDITION_VARIABLE_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER
89
90
}
// namespace base
91
}
// namespace v8
92
93
#endif
// V8_BASE_PLATFORM_CONDITION_VARIABLE_H_
base-export.h
V8_BASE_EXPORT
#define V8_BASE_EXPORT
Definition
base-export.h:26
v8::base::ConditionVariable
Definition
condition-variable.h:37
v8::base::ConditionVariable::native_handle_
absl::CondVar native_handle_
Definition
condition-variable.h:70
v8::base::ConditionVariable::operator=
ConditionVariable & operator=(const ConditionVariable &)=delete
v8::base::ConditionVariable::ConditionVariable
ConditionVariable(const ConditionVariable &)=delete
v8::base::ConditionVariable::~ConditionVariable
~ConditionVariable()
v8::base::ConditionVariable::ConditionVariable
ConditionVariable()
v8::base::Mutex
Definition
mutex.h:36
v8::base::TimeDelta
Definition
time.h:67
base
OpIndex base
Definition
instruction-selector-ia32.cc:65
lazy-instance.h
mutex
base::Mutex mutex
Definition
module-compiler.cc:314
mutex.h
v8
Definition
api-arguments-inl.h:19
v8::base::DefaultConstructTrait
Definition
lazy-instance.h:128
v8::base::LazyInstanceImpl
Definition
lazy-instance.h:165
v8::base::LazyStaticInstance
Definition
lazy-instance.h:200
v8::base::ThreadSafeInitOnceTrait
Definition
lazy-instance.h:142
V8_WARN_UNUSED_RESULT
#define V8_WARN_UNUSED_RESULT
Definition
v8config.h:671
type
wasm::ValueType type
Definition
wasm-inlining-into-js.cc:30
src
base
platform
condition-variable.h
Generated on Sun Apr 6 2025 21:08:50 for v8 by
1.12.0