mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Use NonZeroUsize in remutex
This commit is contained in:
parent
8cdf2ad0ed
commit
ecf3b05153
5 changed files with 17 additions and 8 deletions
|
@ -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"}
|
||||
|
|
|
@ -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,7 +27,7 @@ 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); }
|
||||
|
||||
|
@ -35,7 +35,7 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue