diff options
author | Gregory Terzian <gterzian@users.noreply.github.com> | 2020-06-24 15:07:48 +0800 |
---|---|---|
committer | Gregory Terzian <gterzian@users.noreply.github.com> | 2020-06-30 13:22:38 +0800 |
commit | 44ebca72da45575df4e5970e25920c46c14aa0cb (patch) | |
tree | 88a9ed7d5529b9b83425d7b7cdd9b86f18ec12c2 /components/script/script_runtime.rs | |
parent | 0b61cfc3ae803ac0f9deef937f890f83b24c9a35 (diff) | |
download | servo-44ebca72da45575df4e5970e25920c46c14aa0cb.tar.gz servo-44ebca72da45575df4e5970e25920c46c14aa0cb.zip |
ensure clean shutdown of all threads running JS
Diffstat (limited to 'components/script/script_runtime.rs')
-rw-r--r-- | components/script/script_runtime.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs index 4016f3b9858..f2d8bb73947 100644 --- a/components/script/script_runtime.rs +++ b/components/script/script_runtime.rs @@ -52,7 +52,10 @@ use js::jsapi::{BuildIdCharVector, DisableIncrementalGC, GCDescription, GCProgre use js::jsapi::{Dispatchable as JSRunnable, Dispatchable_MaybeShuttingDown}; use js::jsapi::{HandleObject, Heap, JobQueue}; use js::jsapi::{JSContext as RawJSContext, JSTracer, SetDOMCallbacks, SetGCSliceCallback}; -use js::jsapi::{JSGCInvocationKind, JSGCStatus, JS_AddExtraGCRootsTracer, JS_SetGCCallback}; +use js::jsapi::{ + JSGCInvocationKind, JSGCStatus, JS_AddExtraGCRootsTracer, JS_RequestInterruptCallback, + JS_SetGCCallback, +}; use js::jsapi::{JSGCMode, JSGCParamKey, JS_SetGCParameter, JS_SetGlobalJitCompilerOption}; use js::jsapi::{ JSJitCompilerOption, JS_SetOffthreadIonCompilationEnabled, JS_SetParallelParsingEnabled, @@ -845,6 +848,34 @@ unsafe fn set_gc_zeal_options(cx: *mut RawJSContext) { #[cfg(not(feature = "debugmozjs"))] unsafe fn set_gc_zeal_options(_: *mut RawJSContext) {} +#[repr(transparent)] +/// A wrapper around a JSContext that is Send, +/// enabling an interrupt to be requested +/// from a thread other than the one running JS using that context. +pub struct ContextForRequestInterrupt(*mut RawJSContext); + +impl ContextForRequestInterrupt { + pub fn new(context: *mut RawJSContext) -> ContextForRequestInterrupt { + ContextForRequestInterrupt(context) + } + + #[allow(unsafe_code)] + /// Can be called from any thread, to request the callback set by + /// JS_AddInterruptCallback to be called + /// on the thread where that context is running. + pub fn request_interrupt(&self) { + unsafe { + JS_RequestInterruptCallback(self.0); + } + } +} + +#[allow(unsafe_code)] +/// It is safe to call `JS_RequestInterruptCallback(cx)` from any thread. +/// See the docs for the corresponding `requestInterrupt` method, +/// at `mozjs/js/src/vm/JSContext.h`. +unsafe impl Send for ContextForRequestInterrupt {} + #[derive(Clone, Copy)] #[repr(transparent)] pub struct JSContext(*mut RawJSContext); |