v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
semaphore.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_SEMAPHORE_H_
6#define V8_BASE_PLATFORM_SEMAPHORE_H_
7
10#if V8_OS_WIN
12#endif
13
14#if V8_OS_DARWIN
15#include <dispatch/dispatch.h>
16#elif V8_OS_ZOS
17#include "zos-semaphore.h"
18#elif V8_OS_POSIX
19#include <semaphore.h>
20#endif
21
22#if V8_OS_STARBOARD
23#include "starboard/common/semaphore.h"
24#endif
25
26namespace v8 {
27namespace base {
28
29// Forward declarations.
30class TimeDelta;
31
32// ----------------------------------------------------------------------------
33// Semaphore
34//
35// A semaphore object is a synchronization object that maintains a count. The
36// count is decremented each time a thread completes a wait for the semaphore
37// object and incremented each time a thread signals the semaphore. When the
38// count reaches zero, threads waiting for the semaphore blocks until the
39// count becomes non-zero.
40
42 public:
43 explicit Semaphore(int count);
44 Semaphore(const Semaphore&) = delete;
45 Semaphore& operator=(const Semaphore&) = delete;
47
48 // Increments the semaphore counter.
49 void Signal();
50
51 // Decrements the semaphore counter if it is positive, or blocks until it
52 // becomes positive and then decrements the counter.
53 void Wait();
54
55 // Like Wait() but returns after rel_time time has passed. If the timeout
56 // happens the return value is false and the counter is unchanged. Otherwise
57 // the semaphore counter is decremented and true is returned.
59
60#if V8_OS_DARWIN
61 using NativeHandle = dispatch_semaphore_t;
62#elif V8_OS_POSIX
63 using NativeHandle = sem_t;
64#elif V8_OS_WIN
65 using NativeHandle = HANDLE;
66#elif V8_OS_STARBOARD
67 using NativeHandle = starboard::Semaphore;
68#endif
69
70 NativeHandle& native_handle() {
71 return native_handle_;
72 }
73 const NativeHandle& native_handle() const {
74 return native_handle_;
75 }
76
77 private:
78 NativeHandle native_handle_;
79};
80
81// POD Semaphore initialized lazily (i.e. the first time Pointer() is called).
82// Usage:
83// // The following semaphore starts at 0.
84// static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER;
85//
86// void my_function() {
87// // Do something with my_semaphore.Pointer().
88// }
89//
90
91template <int N>
93 static Semaphore* Create() {
94 return new Semaphore(N);
95 }
96};
97
98template <int N>
103
104#define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER
105
106} // namespace base
107} // namespace v8
108
109#endif // V8_BASE_PLATFORM_SEMAPHORE_H_
#define V8_BASE_EXPORT
Definition base-export.h:26
NativeHandle & native_handle()
Definition semaphore.h:70
Semaphore(int count)
NativeHandle native_handle_
Definition semaphore.h:78
bool WaitFor(const TimeDelta &rel_time) V8_WARN_UNUSED_RESULT
const NativeHandle & native_handle() const
Definition semaphore.h:73
Semaphore & operator=(const Semaphore &)=delete
Semaphore(const Semaphore &)=delete
static Semaphore * Create()
Definition semaphore.h:93
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671
wasm::ValueType type
void * HANDLE