aha-libv8-node 16.0.0.0-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ext/libv8-node/.location.yml +1 -0
- data/ext/libv8-node/location.rb +76 -0
- data/ext/libv8-node/paths.rb +30 -0
- data/lib/libv8-node.rb +1 -0
- data/lib/libv8/node.rb +11 -0
- data/lib/libv8/node/version.rb +7 -0
- data/vendor/v8/include/cppgc/allocation.h +229 -0
- data/vendor/v8/include/cppgc/common.h +29 -0
- data/vendor/v8/include/cppgc/cross-thread-persistent.h +345 -0
- data/vendor/v8/include/cppgc/custom-space.h +97 -0
- data/vendor/v8/include/cppgc/default-platform.h +75 -0
- data/vendor/v8/include/cppgc/ephemeron-pair.h +30 -0
- data/vendor/v8/include/cppgc/garbage-collected.h +116 -0
- data/vendor/v8/include/cppgc/heap-consistency.h +236 -0
- data/vendor/v8/include/cppgc/heap-state.h +59 -0
- data/vendor/v8/include/cppgc/heap-statistics.h +110 -0
- data/vendor/v8/include/cppgc/heap.h +199 -0
- data/vendor/v8/include/cppgc/internal/api-constants.h +47 -0
- data/vendor/v8/include/cppgc/internal/atomic-entry-flag.h +48 -0
- data/vendor/v8/include/cppgc/internal/caged-heap-local-data.h +68 -0
- data/vendor/v8/include/cppgc/internal/compiler-specific.h +38 -0
- data/vendor/v8/include/cppgc/internal/finalizer-trait.h +90 -0
- data/vendor/v8/include/cppgc/internal/gc-info.h +47 -0
- data/vendor/v8/include/cppgc/internal/logging.h +50 -0
- data/vendor/v8/include/cppgc/internal/name-trait.h +111 -0
- data/vendor/v8/include/cppgc/internal/persistent-node.h +132 -0
- data/vendor/v8/include/cppgc/internal/pointer-policies.h +143 -0
- data/vendor/v8/include/cppgc/internal/prefinalizer-handler.h +30 -0
- data/vendor/v8/include/cppgc/internal/write-barrier.h +390 -0
- data/vendor/v8/include/cppgc/liveness-broker.h +74 -0
- data/vendor/v8/include/cppgc/macros.h +26 -0
- data/vendor/v8/include/cppgc/member.h +271 -0
- data/vendor/v8/include/cppgc/name-provider.h +65 -0
- data/vendor/v8/include/cppgc/object-size-trait.h +58 -0
- data/vendor/v8/include/cppgc/persistent.h +365 -0
- data/vendor/v8/include/cppgc/platform.h +151 -0
- data/vendor/v8/include/cppgc/prefinalizer.h +52 -0
- data/vendor/v8/include/cppgc/process-heap-statistics.h +36 -0
- data/vendor/v8/include/cppgc/sentinel-pointer.h +32 -0
- data/vendor/v8/include/cppgc/source-location.h +91 -0
- data/vendor/v8/include/cppgc/testing.h +50 -0
- data/vendor/v8/include/cppgc/trace-trait.h +116 -0
- data/vendor/v8/include/cppgc/type-traits.h +228 -0
- data/vendor/v8/include/cppgc/visitor.h +340 -0
- data/vendor/v8/include/libplatform/libplatform-export.h +29 -0
- data/vendor/v8/include/libplatform/libplatform.h +117 -0
- data/vendor/v8/include/libplatform/v8-tracing.h +334 -0
- data/vendor/v8/include/v8-cppgc.h +278 -0
- data/vendor/v8/include/v8-fast-api-calls.h +419 -0
- data/vendor/v8/include/v8-inspector-protocol.h +13 -0
- data/vendor/v8/include/v8-inspector.h +336 -0
- data/vendor/v8/include/v8-internal.h +462 -0
- data/vendor/v8/include/v8-metrics.h +189 -0
- data/vendor/v8/include/v8-platform.h +710 -0
- data/vendor/v8/include/v8-profiler.h +1116 -0
- data/vendor/v8/include/v8-unwinder-state.h +30 -0
- data/vendor/v8/include/v8-util.h +652 -0
- data/vendor/v8/include/v8-value-serializer-version.h +24 -0
- data/vendor/v8/include/v8-version-string.h +38 -0
- data/vendor/v8/include/v8-version.h +20 -0
- data/vendor/v8/include/v8-wasm-trap-handler-posix.h +31 -0
- data/vendor/v8/include/v8-wasm-trap-handler-win.h +28 -0
- data/vendor/v8/include/v8.h +12479 -0
- data/vendor/v8/include/v8config.h +521 -0
- data/vendor/v8/out.gn/libv8/obj/libv8_monolith.a +0 -0
- metadata +137 -0
@@ -0,0 +1,59 @@
|
|
1
|
+
// Copyright 2021 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 INCLUDE_CPPGC_HEAP_STATE_H_
|
6
|
+
#define INCLUDE_CPPGC_HEAP_STATE_H_
|
7
|
+
|
8
|
+
#include "v8config.h" // NOLINT(build/include_directory)
|
9
|
+
|
10
|
+
namespace cppgc {
|
11
|
+
|
12
|
+
class HeapHandle;
|
13
|
+
|
14
|
+
namespace subtle {
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Helpers to peek into heap-internal state.
|
18
|
+
*/
|
19
|
+
class V8_EXPORT HeapState final {
|
20
|
+
public:
|
21
|
+
/**
|
22
|
+
* Returns whether the garbage collector is marking. This API is experimental
|
23
|
+
* and is expected to be removed in future.
|
24
|
+
*
|
25
|
+
* \param heap_handle The corresponding heap.
|
26
|
+
* \returns true if the garbage collector is currently marking, and false
|
27
|
+
* otherwise.
|
28
|
+
*/
|
29
|
+
static bool IsMarking(const HeapHandle& heap_handle);
|
30
|
+
|
31
|
+
/*
|
32
|
+
* Returns whether the garbage collector is sweeping. This API is experimental
|
33
|
+
* and is expected to be removed in future.
|
34
|
+
*
|
35
|
+
* \param heap_handle The corresponding heap.
|
36
|
+
* \returns true if the garbage collector is currently sweeping, and false
|
37
|
+
* otherwise.
|
38
|
+
*/
|
39
|
+
static bool IsSweeping(const HeapHandle& heap_handle);
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Returns whether the garbage collector is in the atomic pause, i.e., the
|
43
|
+
* mutator is stopped from running. This API is experimental and is expected
|
44
|
+
* to be removed in future.
|
45
|
+
*
|
46
|
+
* \param heap_handle The corresponding heap.
|
47
|
+
* \returns true if the garbage collector is currently in the atomic pause,
|
48
|
+
* and false otherwise.
|
49
|
+
*/
|
50
|
+
static bool IsInAtomicPause(const HeapHandle& heap_handle);
|
51
|
+
|
52
|
+
private:
|
53
|
+
HeapState() = delete;
|
54
|
+
};
|
55
|
+
|
56
|
+
} // namespace subtle
|
57
|
+
} // namespace cppgc
|
58
|
+
|
59
|
+
#endif // INCLUDE_CPPGC_HEAP_STATE_H_
|
@@ -0,0 +1,110 @@
|
|
1
|
+
// Copyright 2021 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 INCLUDE_CPPGC_HEAP_STATISTICS_H_
|
6
|
+
#define INCLUDE_CPPGC_HEAP_STATISTICS_H_
|
7
|
+
|
8
|
+
#include <memory>
|
9
|
+
#include <string>
|
10
|
+
#include <vector>
|
11
|
+
|
12
|
+
namespace cppgc {
|
13
|
+
|
14
|
+
/**
|
15
|
+
* `HeapStatistics` contains memory consumption and utilization statistics for a
|
16
|
+
* cppgc heap.
|
17
|
+
*/
|
18
|
+
struct HeapStatistics final {
|
19
|
+
/**
|
20
|
+
* Specifies the detail level of the heap statistics. Brief statistics contain
|
21
|
+
* only the top-level allocated and used memory statistics for the entire
|
22
|
+
* heap. Detailed statistics also contain a break down per space and page, as
|
23
|
+
* well as freelist statistics and object type histograms. Note that used
|
24
|
+
* memory reported by brief statistics and detailed statistics might differ
|
25
|
+
* slightly.
|
26
|
+
*/
|
27
|
+
enum DetailLevel : uint8_t {
|
28
|
+
kBrief,
|
29
|
+
kDetailed,
|
30
|
+
};
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Statistics of object types. For each type the statistics record its name,
|
34
|
+
* how many objects of that type were allocated, and the overall size used by
|
35
|
+
* these objects.
|
36
|
+
*/
|
37
|
+
struct ObjectStatistics {
|
38
|
+
/** Number of distinct types in the heap. */
|
39
|
+
size_t num_types = 0;
|
40
|
+
/** Name of each type in the heap. */
|
41
|
+
std::vector<std::string> type_name;
|
42
|
+
/** Number of allocated objects per each type. */
|
43
|
+
std::vector<size_t> type_count;
|
44
|
+
/** Overall size of allocated objects per each type. */
|
45
|
+
std::vector<size_t> type_bytes;
|
46
|
+
};
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Page granularity statistics. For each page the statistics record the
|
50
|
+
* allocated memory size and overall used memory size for the page.
|
51
|
+
*/
|
52
|
+
struct PageStatistics {
|
53
|
+
/** Overall amount of memory allocated for the page. */
|
54
|
+
size_t physical_size_bytes = 0;
|
55
|
+
/** Amount of memory actually used on the page. */
|
56
|
+
size_t used_size_bytes = 0;
|
57
|
+
};
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Stastistics of the freelist (used only in non-large object spaces). For
|
61
|
+
* each bucket in the freelist the statistics record the bucket size, the
|
62
|
+
* number of freelist entries in the bucket, and the overall allocated memory
|
63
|
+
* consumed by these freelist entries.
|
64
|
+
*/
|
65
|
+
struct FreeListStatistics {
|
66
|
+
/** bucket sizes in the freelist. */
|
67
|
+
std::vector<size_t> bucket_size;
|
68
|
+
/** number of freelist entries per bucket. */
|
69
|
+
std::vector<size_t> free_count;
|
70
|
+
/** memory size concumed by freelist entries per size. */
|
71
|
+
std::vector<size_t> free_size;
|
72
|
+
};
|
73
|
+
|
74
|
+
/**
|
75
|
+
* Space granularity statistics. For each space the statistics record the
|
76
|
+
* space name, the amount of allocated memory and overall used memory for the
|
77
|
+
* space. The statistics also contain statistics for each of the space's
|
78
|
+
* pages, its freelist and the objects allocated on the space.
|
79
|
+
*/
|
80
|
+
struct SpaceStatistics {
|
81
|
+
/** The space name */
|
82
|
+
std::string name;
|
83
|
+
/** Overall amount of memory allocated for the space. */
|
84
|
+
size_t physical_size_bytes = 0;
|
85
|
+
/** Amount of memory actually used on the space. */
|
86
|
+
size_t used_size_bytes = 0;
|
87
|
+
/** Statistics for each of the pages in the space. */
|
88
|
+
std::vector<PageStatistics> page_stats;
|
89
|
+
/** Statistics for the freelist of the space. */
|
90
|
+
FreeListStatistics free_list_stats;
|
91
|
+
/** Statistics for object allocated on the space. Filled only when
|
92
|
+
* NameProvider::HideInternalNames() is false. */
|
93
|
+
ObjectStatistics object_stats;
|
94
|
+
};
|
95
|
+
|
96
|
+
/** Overall amount of memory allocated for the heap. */
|
97
|
+
size_t physical_size_bytes = 0;
|
98
|
+
/** Amount of memory actually used on the heap. */
|
99
|
+
size_t used_size_bytes = 0;
|
100
|
+
/** Detail level of this HeapStatistics. */
|
101
|
+
DetailLevel detail_level;
|
102
|
+
|
103
|
+
/** Statistics for each of the spaces in the heap. Filled only when
|
104
|
+
* detail_level is kDetailed. */
|
105
|
+
std::vector<SpaceStatistics> space_stats;
|
106
|
+
};
|
107
|
+
|
108
|
+
} // namespace cppgc
|
109
|
+
|
110
|
+
#endif // INCLUDE_CPPGC_HEAP_STATISTICS_H_
|
@@ -0,0 +1,199 @@
|
|
1
|
+
// Copyright 2020 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 INCLUDE_CPPGC_HEAP_H_
|
6
|
+
#define INCLUDE_CPPGC_HEAP_H_
|
7
|
+
|
8
|
+
#include <memory>
|
9
|
+
#include <vector>
|
10
|
+
|
11
|
+
#include "cppgc/common.h"
|
12
|
+
#include "cppgc/custom-space.h"
|
13
|
+
#include "cppgc/platform.h"
|
14
|
+
#include "v8config.h" // NOLINT(build/include_directory)
|
15
|
+
|
16
|
+
/**
|
17
|
+
* cppgc - A C++ garbage collection library.
|
18
|
+
*/
|
19
|
+
namespace cppgc {
|
20
|
+
|
21
|
+
class AllocationHandle;
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Implementation details of cppgc. Those details are considered internal and
|
25
|
+
* may change at any point in time without notice. Users should never rely on
|
26
|
+
* the contents of this namespace.
|
27
|
+
*/
|
28
|
+
namespace internal {
|
29
|
+
class Heap;
|
30
|
+
} // namespace internal
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Used for additional heap APIs.
|
34
|
+
*/
|
35
|
+
class HeapHandle;
|
36
|
+
|
37
|
+
class V8_EXPORT Heap {
|
38
|
+
public:
|
39
|
+
/**
|
40
|
+
* Specifies the stack state the embedder is in.
|
41
|
+
*/
|
42
|
+
using StackState = EmbedderStackState;
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Specifies whether conservative stack scanning is supported.
|
46
|
+
*/
|
47
|
+
enum class StackSupport : uint8_t {
|
48
|
+
/**
|
49
|
+
* Conservative stack scan is supported.
|
50
|
+
*/
|
51
|
+
kSupportsConservativeStackScan,
|
52
|
+
/**
|
53
|
+
* Conservative stack scan is not supported. Embedders may use this option
|
54
|
+
* when using custom infrastructure that is unsupported by the library.
|
55
|
+
*/
|
56
|
+
kNoConservativeStackScan,
|
57
|
+
};
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Specifies supported marking types
|
61
|
+
*/
|
62
|
+
enum class MarkingType : uint8_t {
|
63
|
+
/**
|
64
|
+
* Atomic stop-the-world marking. This option does not require any write
|
65
|
+
* barriers but is the most intrusive in terms of jank.
|
66
|
+
*/
|
67
|
+
kAtomic,
|
68
|
+
/**
|
69
|
+
* Incremental marking, i.e. interleave marking is the rest of the
|
70
|
+
* application on the same thread.
|
71
|
+
*/
|
72
|
+
kIncremental,
|
73
|
+
/**
|
74
|
+
* Incremental and concurrent marking.
|
75
|
+
*/
|
76
|
+
kIncrementalAndConcurrent
|
77
|
+
};
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Specifies supported sweeping types
|
81
|
+
*/
|
82
|
+
enum class SweepingType : uint8_t {
|
83
|
+
/**
|
84
|
+
* Atomic stop-the-world sweeping. All of sweeping is performed at once.
|
85
|
+
*/
|
86
|
+
kAtomic,
|
87
|
+
/**
|
88
|
+
* Incremental and concurrent sweeping. Sweeping is split and interleaved
|
89
|
+
* with the rest of the application.
|
90
|
+
*/
|
91
|
+
kIncrementalAndConcurrent
|
92
|
+
};
|
93
|
+
|
94
|
+
/**
|
95
|
+
* Constraints for a Heap setup.
|
96
|
+
*/
|
97
|
+
struct ResourceConstraints {
|
98
|
+
/**
|
99
|
+
* Allows the heap to grow to some initial size in bytes before triggering
|
100
|
+
* garbage collections. This is useful when it is known that applications
|
101
|
+
* need a certain minimum heap to run to avoid repeatedly invoking the
|
102
|
+
* garbage collector when growing the heap.
|
103
|
+
*/
|
104
|
+
size_t initial_heap_size_bytes = 0;
|
105
|
+
};
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Options specifying Heap properties (e.g. custom spaces) when initializing a
|
109
|
+
* heap through `Heap::Create()`.
|
110
|
+
*/
|
111
|
+
struct HeapOptions {
|
112
|
+
/**
|
113
|
+
* Creates reasonable defaults for instantiating a Heap.
|
114
|
+
*
|
115
|
+
* \returns the HeapOptions that can be passed to `Heap::Create()`.
|
116
|
+
*/
|
117
|
+
static HeapOptions Default() { return {}; }
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Custom spaces added to heap are required to have indices forming a
|
121
|
+
* numbered sequence starting at 0, i.e., their `kSpaceIndex` must
|
122
|
+
* correspond to the index they reside in the vector.
|
123
|
+
*/
|
124
|
+
std::vector<std::unique_ptr<CustomSpaceBase>> custom_spaces;
|
125
|
+
|
126
|
+
/**
|
127
|
+
* Specifies whether conservative stack scan is supported. When conservative
|
128
|
+
* stack scan is not supported, the collector may try to invoke
|
129
|
+
* garbage collections using non-nestable task, which are guaranteed to have
|
130
|
+
* no interesting stack, through the provided Platform. If such tasks are
|
131
|
+
* not supported by the Platform, the embedder must take care of invoking
|
132
|
+
* the GC through `ForceGarbageCollectionSlow()`.
|
133
|
+
*/
|
134
|
+
StackSupport stack_support = StackSupport::kSupportsConservativeStackScan;
|
135
|
+
|
136
|
+
/**
|
137
|
+
* Specifies which types of marking are supported by the heap.
|
138
|
+
*/
|
139
|
+
MarkingType marking_support = MarkingType::kIncrementalAndConcurrent;
|
140
|
+
|
141
|
+
/**
|
142
|
+
* Specifies which types of sweeping are supported by the heap.
|
143
|
+
*/
|
144
|
+
SweepingType sweeping_support = SweepingType::kIncrementalAndConcurrent;
|
145
|
+
|
146
|
+
/**
|
147
|
+
* Resource constraints specifying various properties that the internal
|
148
|
+
* GC scheduler follows.
|
149
|
+
*/
|
150
|
+
ResourceConstraints resource_constraints;
|
151
|
+
};
|
152
|
+
|
153
|
+
/**
|
154
|
+
* Creates a new heap that can be used for object allocation.
|
155
|
+
*
|
156
|
+
* \param platform implemented and provided by the embedder.
|
157
|
+
* \param options HeapOptions specifying various properties for the Heap.
|
158
|
+
* \returns a new Heap instance.
|
159
|
+
*/
|
160
|
+
static std::unique_ptr<Heap> Create(
|
161
|
+
std::shared_ptr<Platform> platform,
|
162
|
+
HeapOptions options = HeapOptions::Default());
|
163
|
+
|
164
|
+
virtual ~Heap() = default;
|
165
|
+
|
166
|
+
/**
|
167
|
+
* Forces garbage collection.
|
168
|
+
*
|
169
|
+
* \param source String specifying the source (or caller) triggering a
|
170
|
+
* forced garbage collection.
|
171
|
+
* \param reason String specifying the reason for the forced garbage
|
172
|
+
* collection.
|
173
|
+
* \param stack_state The embedder stack state, see StackState.
|
174
|
+
*/
|
175
|
+
void ForceGarbageCollectionSlow(
|
176
|
+
const char* source, const char* reason,
|
177
|
+
StackState stack_state = StackState::kMayContainHeapPointers);
|
178
|
+
|
179
|
+
/**
|
180
|
+
* \returns the opaque handle for allocating objects using
|
181
|
+
* `MakeGarbageCollected()`.
|
182
|
+
*/
|
183
|
+
AllocationHandle& GetAllocationHandle();
|
184
|
+
|
185
|
+
/**
|
186
|
+
* \returns the opaque heap handle which may be used to refer to this heap in
|
187
|
+
* other APIs. Valid as long as the underlying `Heap` is alive.
|
188
|
+
*/
|
189
|
+
HeapHandle& GetHeapHandle();
|
190
|
+
|
191
|
+
private:
|
192
|
+
Heap() = default;
|
193
|
+
|
194
|
+
friend class internal::Heap;
|
195
|
+
};
|
196
|
+
|
197
|
+
} // namespace cppgc
|
198
|
+
|
199
|
+
#endif // INCLUDE_CPPGC_HEAP_H_
|
@@ -0,0 +1,47 @@
|
|
1
|
+
// Copyright 2020 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 INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_
|
6
|
+
#define INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_
|
7
|
+
|
8
|
+
#include <stddef.h>
|
9
|
+
#include <stdint.h>
|
10
|
+
|
11
|
+
#include "v8config.h" // NOLINT(build/include_directory)
|
12
|
+
|
13
|
+
namespace cppgc {
|
14
|
+
namespace internal {
|
15
|
+
|
16
|
+
// Embedders should not rely on this code!
|
17
|
+
|
18
|
+
// Internal constants to avoid exposing internal types on the API surface.
|
19
|
+
namespace api_constants {
|
20
|
+
|
21
|
+
constexpr size_t kKB = 1024;
|
22
|
+
constexpr size_t kMB = kKB * 1024;
|
23
|
+
constexpr size_t kGB = kMB * 1024;
|
24
|
+
|
25
|
+
// Offset of the uint16_t bitfield from the payload contaning the
|
26
|
+
// in-construction bit. This is subtracted from the payload pointer to get
|
27
|
+
// to the right bitfield.
|
28
|
+
static constexpr size_t kFullyConstructedBitFieldOffsetFromPayload =
|
29
|
+
2 * sizeof(uint16_t);
|
30
|
+
// Mask for in-construction bit.
|
31
|
+
static constexpr uint16_t kFullyConstructedBitMask = uint16_t{1};
|
32
|
+
|
33
|
+
static constexpr size_t kPageSize = size_t{1} << 17;
|
34
|
+
|
35
|
+
static constexpr size_t kLargeObjectSizeThreshold = kPageSize / 2;
|
36
|
+
|
37
|
+
#if defined(CPPGC_CAGED_HEAP)
|
38
|
+
constexpr size_t kCagedHeapReservationSize = static_cast<size_t>(4) * kGB;
|
39
|
+
constexpr size_t kCagedHeapReservationAlignment = kCagedHeapReservationSize;
|
40
|
+
#endif
|
41
|
+
|
42
|
+
} // namespace api_constants
|
43
|
+
|
44
|
+
} // namespace internal
|
45
|
+
} // namespace cppgc
|
46
|
+
|
47
|
+
#endif // INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_
|
@@ -0,0 +1,48 @@
|
|
1
|
+
// Copyright 2020 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 INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_
|
6
|
+
#define INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_
|
7
|
+
|
8
|
+
#include <atomic>
|
9
|
+
|
10
|
+
namespace cppgc {
|
11
|
+
namespace internal {
|
12
|
+
|
13
|
+
// A flag which provides a fast check whether a scope may be entered on the
|
14
|
+
// current thread, without needing to access thread-local storage or mutex. Can
|
15
|
+
// have false positives (i.e., spuriously report that it might be entered), so
|
16
|
+
// it is expected that this will be used in tandem with a precise check that the
|
17
|
+
// scope is in fact entered on that thread.
|
18
|
+
//
|
19
|
+
// Example:
|
20
|
+
// g_frobnicating_flag.MightBeEntered() &&
|
21
|
+
// ThreadLocalFrobnicator().IsFrobnicating()
|
22
|
+
//
|
23
|
+
// Relaxed atomic operations are sufficient, since:
|
24
|
+
// - all accesses remain atomic
|
25
|
+
// - each thread must observe its own operations in order
|
26
|
+
// - no thread ever exits the flag more times than it enters (if used correctly)
|
27
|
+
// And so if a thread observes zero, it must be because it has observed an equal
|
28
|
+
// number of exits as entries.
|
29
|
+
class AtomicEntryFlag final {
|
30
|
+
public:
|
31
|
+
void Enter() { entries_.fetch_add(1, std::memory_order_relaxed); }
|
32
|
+
void Exit() { entries_.fetch_sub(1, std::memory_order_relaxed); }
|
33
|
+
|
34
|
+
// Returns false only if the current thread is not between a call to Enter
|
35
|
+
// and a call to Exit. Returns true if this thread or another thread may
|
36
|
+
// currently be in the scope guarded by this flag.
|
37
|
+
bool MightBeEntered() const {
|
38
|
+
return entries_.load(std::memory_order_relaxed) != 0;
|
39
|
+
}
|
40
|
+
|
41
|
+
private:
|
42
|
+
std::atomic_int entries_{0};
|
43
|
+
};
|
44
|
+
|
45
|
+
} // namespace internal
|
46
|
+
} // namespace cppgc
|
47
|
+
|
48
|
+
#endif // INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_
|