mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Auto merge of #12955 - nox:try-borrow, r=emilio
Use try_borrow instead of borrow_state <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12955) <!-- Reviewable:end -->
This commit is contained in:
commit
530c36a99a
4 changed files with 44 additions and 63 deletions
|
@ -6,7 +6,7 @@
|
|||
|
||||
use dom::bindings::trace::JSTraceable;
|
||||
use js::jsapi::JSTracer;
|
||||
use std::cell::{BorrowState, Ref, RefCell, RefMut};
|
||||
use std::cell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut};
|
||||
use util::thread_state;
|
||||
use util::thread_state::SCRIPT;
|
||||
|
||||
|
@ -52,49 +52,6 @@ impl<T> DOMRefCell<T> {
|
|||
&mut *self.value.as_unsafe_cell().get()
|
||||
}
|
||||
|
||||
/// Is the cell mutably borrowed?
|
||||
///
|
||||
/// For safety checks in debug builds only.
|
||||
pub fn is_mutably_borrowed(&self) -> bool {
|
||||
self.value.borrow_state() == BorrowState::Writing
|
||||
}
|
||||
|
||||
/// Attempts to immutably borrow the wrapped value.
|
||||
///
|
||||
/// The borrow lasts until the returned `Ref` exits scope. Multiple
|
||||
/// immutable borrows can be taken out at the same time.
|
||||
///
|
||||
/// Returns `None` if the value is currently mutably borrowed.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
pub fn try_borrow(&self) -> Option<Ref<T>> {
|
||||
debug_assert!(thread_state::get().is_script());
|
||||
match self.value.borrow_state() {
|
||||
BorrowState::Writing => None,
|
||||
_ => Some(self.value.borrow()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Mutably borrows the wrapped value.
|
||||
///
|
||||
/// The borrow lasts until the returned `RefMut` exits scope. The value
|
||||
/// cannot be borrowed while this borrow is active.
|
||||
///
|
||||
/// Returns `None` if the value is currently borrowed.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
pub fn try_borrow_mut(&self) -> Option<RefMut<T>> {
|
||||
debug_assert!(thread_state::get().is_script());
|
||||
match self.value.borrow_state() {
|
||||
BorrowState::Unused => Some(self.value.borrow_mut()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Version of the above that we use during restyle while the script thread
|
||||
/// is blocked.
|
||||
pub fn borrow_mut_for_layout(&self) -> RefMut<T> {
|
||||
|
@ -149,4 +106,34 @@ impl<T> DOMRefCell<T> {
|
|||
pub fn borrow_mut(&self) -> RefMut<T> {
|
||||
self.try_borrow_mut().expect("DOMRefCell<T> already borrowed")
|
||||
}
|
||||
|
||||
/// Attempts to immutably borrow the wrapped value.
|
||||
///
|
||||
/// The borrow lasts until the returned `Ref` exits scope. Multiple
|
||||
/// immutable borrows can be taken out at the same time.
|
||||
///
|
||||
/// Returns `None` if the value is currently mutably borrowed.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
pub fn try_borrow(&self) -> Result<Ref<T>, BorrowError<T>> {
|
||||
debug_assert!(thread_state::get().is_script());
|
||||
self.value.try_borrow()
|
||||
}
|
||||
|
||||
/// Mutably borrows the wrapped value.
|
||||
///
|
||||
/// The borrow lasts until the returned `RefMut` exits scope. The value
|
||||
/// cannot be borrowed while this borrow is active.
|
||||
///
|
||||
/// Returns `None` if the value is currently borrowed.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
pub fn try_borrow_mut(&self) -> Result<RefMut<T>, BorrowMutError<T>> {
|
||||
debug_assert!(thread_state::get().is_script());
|
||||
self.value.try_borrow_mut()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#![feature(as_unsafe_cell)]
|
||||
#![feature(borrow_state)]
|
||||
#![feature(box_syntax)]
|
||||
#![feature(conservative_impl_trait)]
|
||||
#![feature(const_fn)]
|
||||
|
@ -19,6 +18,7 @@
|
|||
#![feature(slice_patterns)]
|
||||
#![feature(stmt_expr_attributes)]
|
||||
#![feature(question_mark)]
|
||||
#![feature(try_borrow)]
|
||||
#![feature(try_from)]
|
||||
|
||||
#![deny(unsafe_code)]
|
||||
|
|
|
@ -17,7 +17,7 @@ use wrappers::CefWrap;
|
|||
use compositing::windowing::{WindowNavigateMsg, WindowEvent};
|
||||
use glutin_app;
|
||||
use libc::c_int;
|
||||
use std::cell::{Cell, RefCell, BorrowState};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicIsize, Ordering};
|
||||
|
@ -183,24 +183,18 @@ impl ServoCefBrowserExtensions for CefBrowser {
|
|||
}
|
||||
|
||||
fn send_window_event(&self, event: WindowEvent) {
|
||||
self.downcast().message_queue.borrow_mut().push(event);
|
||||
let browser = self.downcast();
|
||||
|
||||
loop {
|
||||
match self.downcast().servo_browser.borrow_state() {
|
||||
BorrowState::Unused => {
|
||||
let event = match self.downcast().message_queue.borrow_mut().pop() {
|
||||
None => return,
|
||||
Some(event) => event,
|
||||
};
|
||||
self.downcast().servo_browser.borrow_mut().handle_event(event);
|
||||
}
|
||||
_ => {
|
||||
// We're trying to send an event while processing another one. This will
|
||||
// cause general badness, so queue up that event instead of immediately
|
||||
// processing it.
|
||||
break
|
||||
}
|
||||
if let Ok(mut servo_browser) = browser.servo_browser.try_borrow_mut() {
|
||||
servo_browser.handle_event(event);
|
||||
while let Some(event) = browser.message_queue.borrow_mut().pop() {
|
||||
servo_browser.handle_event(event);
|
||||
}
|
||||
} else {
|
||||
// If we fail to borrow mutably, this means we're trying to send an
|
||||
// event while processing another one. This will cause general badness,
|
||||
// we just queue up that event instead of immediately processing it.
|
||||
browser.message_queue.borrow_mut().push(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#![feature(borrow_state)]
|
||||
#![feature(box_syntax)]
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(filling_drop)]
|
||||
#![feature(link_args)]
|
||||
#![feature(plugin)]
|
||||
#![feature(try_borrow)]
|
||||
#![feature(unicode)]
|
||||
#![feature(unsafe_no_drop_flag)]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue