diff options
author | Alan Jeffrey <ajeffrey@mozilla.com> | 2018-11-02 10:57:26 -0500 |
---|---|---|
committer | Alan Jeffrey <ajeffrey@mozilla.com> | 2018-11-02 10:57:26 -0500 |
commit | 57085eea6a396f07904f6e1b68134cb1d4534eb0 (patch) | |
tree | 25a0b2e5ca864a293ae5505a45e14e1519e3e331 | |
parent | 95bfaa0a770479fb3bf6bf0b1f85c9ae343e66ff (diff) | |
download | servo-57085eea6a396f07904f6e1b68134cb1d4534eb0.tar.gz servo-57085eea6a396f07904f6e1b68134cb1d4534eb0.zip |
Shut down servo when the ML app quits
-rw-r--r-- | ports/libmlservo/src/lib.rs | 23 | ||||
-rw-r--r-- | support/magicleap/Servo2D/code/src/Servo2D.cpp | 4 |
2 files changed, 22 insertions, 5 deletions
diff --git a/ports/libmlservo/src/lib.rs b/ports/libmlservo/src/lib.rs index 6b5bb2e6d00..8f4118d959c 100644 --- a/ports/libmlservo/src/lib.rs +++ b/ports/libmlservo/src/lib.rs @@ -43,6 +43,9 @@ use std::os::raw::c_char; use std::os::raw::c_void; use std::path::PathBuf; use std::rc::Rc; +use std::thread; +use std::time::Duration; +use std::time::Instant; #[repr(u32)] pub enum MLLogLevel { @@ -221,11 +224,25 @@ pub unsafe extern "C" fn navigate_servo(servo: *mut ServoInstance, text: *const } } +// Some magic numbers for shutdown +const SHUTDOWN_DURATION: Duration = Duration::from_secs(10); +const SHUTDOWN_POLL_INTERVAL: Duration = Duration::from_millis(100); + #[no_mangle] pub unsafe extern "C" fn discard_servo(servo: *mut ServoInstance) { - // Servo drop goes here! - if !servo.is_null() { - Box::from_raw(servo); + if let Some(servo) = servo.as_mut() { + let mut servo = Box::from_raw(servo); + let finish = Instant::now() + SHUTDOWN_DURATION; + 'outer: while Instant::now() < finish { + servo.servo.handle_events(vec![WindowEvent::Quit]); + for (_, msg) in servo.servo.get_events() { + if let EmbedderMsg::Shutdown = msg { + break 'outer; + } + } + thread::sleep(SHUTDOWN_POLL_INTERVAL); + } + servo.servo.deinit(); } } diff --git a/support/magicleap/Servo2D/code/src/Servo2D.cpp b/support/magicleap/Servo2D/code/src/Servo2D.cpp index d24316490b6..2c1ba02a038 100644 --- a/support/magicleap/Servo2D/code/src/Servo2D.cpp +++ b/support/magicleap/Servo2D/code/src/Servo2D.cpp @@ -58,8 +58,6 @@ Servo2D::Servo2D() { // Destroy a Servo 2D instance Servo2D::~Servo2D() { ML_LOG(Debug, "Servo2D Destructor."); - discard_servo(servo_); - servo_ = nullptr; } // The prism dimensions @@ -167,6 +165,8 @@ int Servo2D::init() { int Servo2D::deInit() { ML_LOG(Debug, "Servo2D Deinitializing."); + discard_servo(servo_); + servo_ = nullptr; return 0; } |