-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathplatform.rs
More file actions
362 lines (338 loc) · 12.6 KB
/
platform.rs
File metadata and controls
362 lines (338 loc) · 12.6 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use crate::Isolate;
use crate::isolate::RealIsolate;
use crate::support::int;
use crate::support::Opaque;
use crate::support::Shared;
use crate::support::SharedPtrBase;
use crate::support::SharedRef;
use crate::support::UniquePtr;
use crate::support::UniqueRef;
use crate::support::long;
unsafe extern "C" {
fn v8__Platform__NewDefaultPlatform(
thread_pool_size: int,
idle_task_support: bool,
) -> *mut Platform;
fn v8__Platform__NewUnprotectedDefaultPlatform(
thread_pool_size: int,
idle_task_support: bool,
) -> *mut Platform;
fn v8__Platform__NewSingleThreadedDefaultPlatform(
idle_task_support: bool,
) -> *mut Platform;
fn v8__Platform__NewNotifyingPlatform(
thread_pool_size: int,
idle_task_support: bool,
context: *mut std::ffi::c_void,
) -> *mut Platform;
fn v8__Platform__DELETE(this: *mut Platform);
fn v8__Platform__PumpMessageLoop(
platform: *mut Platform,
isolate: *mut RealIsolate,
wait_for_work: bool,
) -> bool;
fn v8__Platform__RunIdleTasks(
platform: *mut Platform,
isolate: *mut RealIsolate,
idle_time_in_seconds: f64,
);
fn v8__Platform__NotifyIsolateShutdown(
platform: *mut Platform,
isolate: *mut RealIsolate,
);
fn std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(
unique_ptr: UniquePtr<Platform>,
) -> SharedPtrBase<Platform>;
fn std__shared_ptr__v8__Platform__get(
ptr: *const SharedPtrBase<Platform>,
) -> *mut Platform;
fn std__shared_ptr__v8__Platform__COPY(
ptr: *const SharedPtrBase<Platform>,
) -> SharedPtrBase<Platform>;
fn std__shared_ptr__v8__Platform__reset(ptr: *mut SharedPtrBase<Platform>);
fn std__shared_ptr__v8__Platform__use_count(
ptr: *const SharedPtrBase<Platform>,
) -> long;
}
#[repr(C)]
#[derive(Debug)]
pub struct Platform(Opaque);
/// Trait for receiving notifications when foreground tasks are posted to an
/// isolate's task runner. Implementations must be thread-safe as callbacks
/// can fire from any V8 background thread.
///
/// This follows the trait-based pattern used by the inspector API
/// (`V8InspectorClientImpl`, `ChannelImpl`).
pub trait ForegroundTaskCallback: Send + Sync {
/// Called when a foreground task has been posted for the given isolate.
///
/// `isolate_ptr` is the raw `v8::Isolate*` pointer of the target isolate.
/// `delay_in_seconds` is 0.0 for immediate tasks, or the delay before the
/// task should be executed. For delayed tasks, the embedder should schedule
/// a wake-up after the given delay (e.g. via a timer in tokio).
///
/// This may be called from ANY thread (V8 background threads, etc.).
fn on_foreground_task_posted(
&self,
isolate_ptr: *mut std::ffi::c_void,
delay_in_seconds: f64,
);
}
// FFI callbacks called from C++ NotifyingPlatform/NotifyingTaskRunner.
// `context` is a raw pointer to a `Box<dyn ForegroundTaskCallback>`.
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__NotifyingPlatform__onForegroundTaskPosted(
context: *mut std::ffi::c_void,
isolate: *mut std::ffi::c_void,
delay_in_seconds: f64,
) {
let callback =
unsafe { &*(context as *const Box<dyn ForegroundTaskCallback>) };
callback.on_foreground_task_posted(isolate, delay_in_seconds);
}
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__NotifyingPlatform__dropContext(
context: *mut std::ffi::c_void,
) {
unsafe {
let _ = Box::from_raw(context as *mut Box<dyn ForegroundTaskCallback>);
}
}
/// Returns a new instance of the default v8::Platform implementation.
///
/// |thread_pool_size| is the number of worker threads to allocate for
/// background jobs. If a value of zero is passed, a suitable default
/// based on the current number of processors online will be chosen.
/// If |idle_task_support| is enabled then the platform will accept idle
/// tasks (IdleTasksEnabled will return true) and will rely on the embedder
/// calling v8::platform::RunIdleTasks to process the idle tasks.
///
/// The default platform for v8 may include restrictions and caveats on thread
/// creation and initialization. This platform should only be used in cases
/// where v8 can be reliably initialized on the application's main thread, or
/// the parent thread to all threads in the system that will use v8.
///
/// One example of a restriction is the use of Memory Protection Keys (pkeys) on
/// modern Linux systems using modern Intel/AMD processors. This particular
/// technology requires that all threads using v8 are created as descendent
/// threads of the thread that called `v8::Initialize`.
#[inline(always)]
pub fn new_default_platform(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Platform> {
Platform::new(thread_pool_size, idle_task_support)
}
/// Creates a platform that is identical to the default platform, but does not
/// enforce thread-isolated allocations. This may reduce security in some cases,
/// so this method should be used with caution in cases where the threading
/// guarantees of `new_default_platform` cannot be upheld (generally for tests).
#[inline(always)]
pub fn new_unprotected_default_platform(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Platform> {
Platform::new_unprotected(thread_pool_size, idle_task_support)
}
/// The same as new_default_platform() but disables the worker thread pool.
/// It must be used with the --single-threaded V8 flag.
///
/// If |idle_task_support| is enabled then the platform will accept idle
/// tasks (IdleTasksEnabled will return true) and will rely on the embedder
/// calling v8::platform::RunIdleTasks to process the idle tasks.
#[inline(always)]
pub fn new_single_threaded_default_platform(
idle_task_support: bool,
) -> UniqueRef<Platform> {
Platform::new_single_threaded(idle_task_support)
}
/// Creates a NotifyingPlatform that wraps DefaultPlatform and calls the
/// provided [`ForegroundTaskCallback`] whenever a foreground task is posted
/// for any isolate.
///
/// This allows embedders to wake their event loop when V8 background threads
/// complete work and post foreground continuations (e.g. background compilation
/// finishing, Atomics.waitAsync resolving). For delayed tasks, the embedder
/// should schedule a wake-up after `delay_in_seconds` (e.g. via a timer).
///
/// The callback may be invoked from ANY thread (V8 background threads, etc.)
/// and must be safe to call concurrently.
///
/// Thread-isolated allocations are disabled (same as `new_unprotected_default_platform`).
#[inline(always)]
pub fn new_notifying_platform(
thread_pool_size: u32,
idle_task_support: bool,
callback: impl ForegroundTaskCallback + 'static,
) -> UniqueRef<Platform> {
Platform::new_notifying(thread_pool_size, idle_task_support, callback)
}
impl Platform {
/// Returns a new instance of the default v8::Platform implementation.
///
/// |thread_pool_size| is the number of worker threads to allocate for
/// background jobs. If a value of zero is passed, a suitable default
/// based on the current number of processors online will be chosen.
/// If |idle_task_support| is enabled then the platform will accept idle
/// tasks (IdleTasksEnabled will return true) and will rely on the embedder
/// calling v8::platform::RunIdleTasks to process the idle tasks.
///
/// The default platform for v8 may include restrictions and caveats on thread
/// creation and initialization. This platform should only be used in cases
/// where v8 can be reliably initialized on the application's main thread, or
/// the parent thread to all threads in the system that will use v8.
///
/// One example of a restriction is the use of Memory Protection Keys (pkeys)
/// on modern Linux systems using modern Intel/AMD processors. This particular
/// technology requires that all threads using v8 are created as descendent
/// threads of the thread that called `v8::Initialize`.
#[inline(always)]
pub fn new(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Self> {
unsafe {
UniqueRef::from_raw(v8__Platform__NewDefaultPlatform(
thread_pool_size.min(16) as i32,
idle_task_support,
))
}
}
/// Creates a platform that is identical to the default platform, but does not
/// enforce thread-isolated allocations. This may reduce security in some
/// cases, so this method should be used with caution in cases where the
/// threading guarantees of `new_default_platform` cannot be upheld (generally
/// for tests).
#[inline(always)]
pub fn new_unprotected(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Self> {
unsafe {
UniqueRef::from_raw(v8__Platform__NewUnprotectedDefaultPlatform(
thread_pool_size.min(16) as i32,
idle_task_support,
))
}
}
/// The same as new() but disables the worker thread pool.
/// It must be used with the --single-threaded V8 flag.
///
/// If |idle_task_support| is enabled then the platform will accept idle
/// tasks (IdleTasksEnabled will return true) and will rely on the embedder
/// calling v8::platform::RunIdleTasks to process the idle tasks.
#[inline(always)]
pub fn new_single_threaded(idle_task_support: bool) -> UniqueRef<Self> {
unsafe {
UniqueRef::from_raw(v8__Platform__NewSingleThreadedDefaultPlatform(
idle_task_support,
))
}
}
/// Creates a NotifyingPlatform (subclass of DefaultPlatform) that dispatches
/// to the provided [`ForegroundTaskCallback`] whenever a foreground task is
/// posted for an isolate.
///
/// The callback trait object is owned by the platform and will be dropped
/// when the platform is destroyed.
#[inline(always)]
pub fn new_notifying(
thread_pool_size: u32,
idle_task_support: bool,
callback: impl ForegroundTaskCallback + 'static,
) -> UniqueRef<Self> {
// Double-box: inner Box<dyn> is a fat pointer, outer Box gives us a
// thin pointer we can pass through C++ void*.
let boxed: Box<dyn ForegroundTaskCallback> = Box::new(callback);
let context = Box::into_raw(Box::new(boxed)) as *mut std::ffi::c_void;
unsafe {
UniqueRef::from_raw(v8__Platform__NewNotifyingPlatform(
thread_pool_size.min(16) as i32,
idle_task_support,
context,
))
}
}
}
impl Platform {
/// Pumps the message loop for the given isolate.
///
/// The caller has to make sure that this is called from the right thread.
/// Returns true if a task was executed, and false otherwise. If the call to
/// PumpMessageLoop is nested within another call to PumpMessageLoop, only
/// nestable tasks may run. Otherwise, any task may run. Unless requested through
/// the |wait_for_work| parameter, this call does not block if no task is pending.
#[inline(always)]
pub fn pump_message_loop(
platform: &SharedRef<Self>,
isolate: &Isolate,
wait_for_work: bool,
) -> bool {
unsafe {
v8__Platform__PumpMessageLoop(
&**platform as *const Self as *mut _,
isolate.as_real_ptr(),
wait_for_work,
)
}
}
/// Runs pending idle tasks for at most |idle_time_in_seconds| seconds.
///
/// The caller has to make sure that this is called from the right thread.
/// This call does not block if no task is pending.
#[inline(always)]
pub fn run_idle_tasks(
platform: &SharedRef<Self>,
isolate: &Isolate,
idle_time_in_seconds: f64,
) {
unsafe {
v8__Platform__RunIdleTasks(
&**platform as *const Self as *mut _,
isolate.as_real_ptr(),
idle_time_in_seconds,
);
}
}
/// Notifies the given platform about the Isolate getting deleted soon. Has to
/// be called for all Isolates which are deleted - unless we're shutting down
/// the platform.
///
/// The |platform| has to be created using |NewDefaultPlatform|.
#[inline(always)]
pub(crate) unsafe fn notify_isolate_shutdown(
platform: &SharedRef<Self>,
isolate: &Isolate,
) {
unsafe {
v8__Platform__NotifyIsolateShutdown(
&**platform as *const Self as *mut _,
isolate.as_real_ptr(),
);
}
}
}
impl Shared for Platform {
fn from_unique_ptr(unique_ptr: UniquePtr<Self>) -> SharedPtrBase<Self> {
unsafe {
std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(unique_ptr)
}
}
fn get(ptr: &SharedPtrBase<Self>) -> *const Self {
unsafe { std__shared_ptr__v8__Platform__get(ptr) }
}
fn clone(ptr: &SharedPtrBase<Self>) -> SharedPtrBase<Self> {
unsafe { std__shared_ptr__v8__Platform__COPY(ptr) }
}
fn reset(ptr: &mut SharedPtrBase<Self>) {
unsafe { std__shared_ptr__v8__Platform__reset(ptr) }
}
fn use_count(ptr: &SharedPtrBase<Self>) -> long {
unsafe { std__shared_ptr__v8__Platform__use_count(ptr) }
}
}
impl Drop for Platform {
fn drop(&mut self) {
unsafe { v8__Platform__DELETE(self) };
}
}