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"
12
13#if V8_OS_STARBOARD
14#include "starboard/common/condition_variable.h"
15#endif
16
17namespace v8 {
18namespace base {
19
20// Forward declarations.
21class ConditionVariableEvent;
22class 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
38 public:
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// }
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_
#define V8_BASE_EXPORT
Definition base-export.h:26
ConditionVariable & operator=(const ConditionVariable &)=delete
ConditionVariable(const ConditionVariable &)=delete
base::Mutex mutex
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671
wasm::ValueType type