Permalink
Browse files

Use NonZeroUsize in remutex

  • Loading branch information...
SimonSapin committed Oct 11, 2017
1 parent 03eda6d commit b846d20fd6074f07495af036fbe9f68be311df43
Showing with 17 additions and 8 deletions.
  1. +1 −0 Cargo.lock
  2. +3 −0 components/constellation/Cargo.toml
  3. +4 −0 components/remutex/Cargo.toml
  4. +7 −7 components/remutex/lib.rs
  5. +2 −1 components/servo/Cargo.toml
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -9,6 +9,9 @@ publish = false
name = "constellation"
path = "lib.rs"
[features]
unstable = ["servo_remutex/unstable"]
[dependencies]
backtrace = "0.3"
bluetooth_traits = { path = "../bluetooth_traits" }
@@ -9,6 +9,10 @@ publish = false
name = "servo_remutex"
path = "lib.rs"
[features]
unstable = ["nonzero/unstable"]
[dependencies]
lazy_static = "0.2"
log = "0.3.5"
nonzero = {path = "../nonzero"}
View
@@ -10,13 +10,13 @@
//! 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.
#![feature(nonzero)]
#![cfg_attr(feature = "unstable", feature(nonzero))]
extern crate core;
extern crate nonzero;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
use core::nonzero::NonZero;
use nonzero::NonZeroUsize;
use std::cell::{Cell, UnsafeCell};
use std::ops::Deref;
use std::sync::{LockResult, Mutex, MutexGuard, PoisonError, TryLockError, TryLockResult};
@@ -27,15 +27,15 @@ use std::sync::atomic::{AtomicUsize, Ordering};
// TODO: can we use the thread-id crate for this?
#[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); }
impl ThreadId {
#[allow(unsafe_code)]
fn new() -> ThreadId {
let number = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
ThreadId(NonZero::new(number).unwrap())
ThreadId(NonZeroUsize::new(number).unwrap())
}
pub fn current() -> ThreadId {
THREAD_ID.with(|tls| tls.clone())
@@ -59,13 +59,13 @@ impl AtomicOptThreadId {
#[allow(unsafe_code)]
pub fn load(&self, ordering: Ordering) -> Option<ThreadId> {
let number = self.0.load(ordering);
NonZero::new(number).map(ThreadId)
NonZeroUsize::new(number).map(ThreadId)
}
#[allow(unsafe_code)]
pub fn swap(&self, value: Option<ThreadId>, ordering: Ordering) -> Option<ThreadId> {
let number = value.map(|id| id.0.get()).unwrap_or(0);
let number = self.0.swap(number, ordering);
NonZero::new(number).map(ThreadId)
NonZeroUsize::new(number).map(ThreadId)
}
}
@@ -22,7 +22,8 @@ unstable = [
"euclid/unstable",
"layout_thread/unstable",
"msg/unstable",
"compositing/unstable"
"compositing/unstable",
"constellation/unstable",
]
[dependencies]

0 comments on commit b846d20

Please sign in to comment.