mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Use NonZeroUsize in remutex
This commit is contained in:
parent
8cdf2ad0ed
commit
ecf3b05153
5 changed files with 17 additions and 8 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2992,6 +2992,7 @@ version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"nonzero 0.0.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -9,6 +9,9 @@ publish = false
|
||||||
name = "constellation"
|
name = "constellation"
|
||||||
path = "lib.rs"
|
path = "lib.rs"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
unstable = ["servo_remutex/unstable"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
backtrace = "0.3"
|
backtrace = "0.3"
|
||||||
bluetooth_traits = { path = "../bluetooth_traits" }
|
bluetooth_traits = { path = "../bluetooth_traits" }
|
||||||
|
|
|
@ -9,6 +9,10 @@ publish = false
|
||||||
name = "servo_remutex"
|
name = "servo_remutex"
|
||||||
path = "lib.rs"
|
path = "lib.rs"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
unstable = ["nonzero/unstable"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
lazy_static = "0.2"
|
lazy_static = "0.2"
|
||||||
log = "0.3.5"
|
log = "0.3.5"
|
||||||
|
nonzero = {path = "../nonzero"}
|
||||||
|
|
|
@ -10,13 +10,13 @@
|
||||||
//! It provides the same interface as https://github.com/rust-lang/rust/blob/master/src/libstd/sys/common/remutex.rs
|
//! It provides the same interface as https://github.com/rust-lang/rust/blob/master/src/libstd/sys/common/remutex.rs
|
||||||
//! so if those types are ever exported, we should be able to replace this implemtation.
|
//! so if those types are ever exported, we should be able to replace this implemtation.
|
||||||
|
|
||||||
#![feature(nonzero)]
|
#![cfg_attr(feature = "unstable", feature(nonzero))]
|
||||||
|
|
||||||
extern crate core;
|
extern crate nonzero;
|
||||||
#[macro_use] extern crate lazy_static;
|
#[macro_use] extern crate lazy_static;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
|
|
||||||
use core::nonzero::NonZero;
|
use nonzero::NonZeroUsize;
|
||||||
use std::cell::{Cell, UnsafeCell};
|
use std::cell::{Cell, UnsafeCell};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::{LockResult, Mutex, MutexGuard, PoisonError, TryLockError, TryLockResult};
|
use std::sync::{LockResult, Mutex, MutexGuard, PoisonError, TryLockError, TryLockResult};
|
||||||
|
@ -27,7 +27,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
// TODO: can we use the thread-id crate for this?
|
// TODO: can we use the thread-id crate for this?
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
pub struct ThreadId(NonZero<usize>);
|
pub struct ThreadId(NonZeroUsize);
|
||||||
|
|
||||||
lazy_static!{ static ref THREAD_COUNT: AtomicUsize = AtomicUsize::new(1); }
|
lazy_static!{ static ref THREAD_COUNT: AtomicUsize = AtomicUsize::new(1); }
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ impl ThreadId {
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
fn new() -> ThreadId {
|
fn new() -> ThreadId {
|
||||||
let number = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
|
let number = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
|
||||||
ThreadId(NonZero::new(number).unwrap())
|
ThreadId(NonZeroUsize::new(number).unwrap())
|
||||||
}
|
}
|
||||||
pub fn current() -> ThreadId {
|
pub fn current() -> ThreadId {
|
||||||
THREAD_ID.with(|tls| tls.clone())
|
THREAD_ID.with(|tls| tls.clone())
|
||||||
|
@ -59,13 +59,13 @@ impl AtomicOptThreadId {
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub fn load(&self, ordering: Ordering) -> Option<ThreadId> {
|
pub fn load(&self, ordering: Ordering) -> Option<ThreadId> {
|
||||||
let number = self.0.load(ordering);
|
let number = self.0.load(ordering);
|
||||||
NonZero::new(number).map(ThreadId)
|
NonZeroUsize::new(number).map(ThreadId)
|
||||||
}
|
}
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub fn swap(&self, value: Option<ThreadId>, ordering: Ordering) -> Option<ThreadId> {
|
pub fn swap(&self, value: Option<ThreadId>, ordering: Ordering) -> Option<ThreadId> {
|
||||||
let number = value.map(|id| id.0.get()).unwrap_or(0);
|
let number = value.map(|id| id.0.get()).unwrap_or(0);
|
||||||
let number = self.0.swap(number, ordering);
|
let number = self.0.swap(number, ordering);
|
||||||
NonZero::new(number).map(ThreadId)
|
NonZeroUsize::new(number).map(ThreadId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,8 @@ unstable = [
|
||||||
"euclid/unstable",
|
"euclid/unstable",
|
||||||
"layout_thread/unstable",
|
"layout_thread/unstable",
|
||||||
"msg/unstable",
|
"msg/unstable",
|
||||||
"compositing/unstable"
|
"compositing/unstable",
|
||||||
|
"constellation/unstable",
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue