Skip to content

Commit

Permalink
Merge pull request #122 from faern/upgrade-to-rust2018
Browse files Browse the repository at this point in the history
Upgrade to rust2018
  • Loading branch information
Amanieu authored Mar 4, 2019
2 parents 950fee9 + d88854d commit 66d67bb
Show file tree
Hide file tree
Showing 30 changed files with 229 additions and 274 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ language: rust
sudo: false

rust:
- 1.24.0
- 1.26.2
- 1.31.0
- stable
- beta
- nightly
Expand Down
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
[package]
name = "parking_lot"
version = "0.7.1"
version = "0.8.0"
authors = ["Amanieu d'Antras <[email protected]>"]
description = "More compact and efficient implementations of the standard synchronization primitives."
license = "Apache-2.0/MIT"
repository = "https://github.com/Amanieu/parking_lot"
readme = "README.md"
keywords = ["mutex", "condvar", "rwlock", "once", "thread"]
categories = ["concurrency"]
edition = "2018"

[dependencies]
parking_lot_core = { path = "core", version = "0.4" }
lock_api = { path = "lock_api", version = "0.1" }
parking_lot_core = { path = "core", version = "0.5" }
lock_api = { path = "lock_api", version = "0.2" }

[dev-dependencies]
rand = "0.6"
lazy_static = "1.0"

[features]
default = ["owning_ref"]
default = []
owning_ref = ["lock_api/owning_ref"]
nightly = ["parking_lot_core/nightly", "lock_api/nightly"]
deadlock_detection = ["parking_lot_core/deadlock_detection"]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
parking_lot = "0.6"
parking_lot = "0.8"
```

and this to your crate root:
Expand All @@ -112,7 +112,7 @@ To enable nightly-only features, add this to your `Cargo.toml` instead:

```toml
[dependencies]
parking_lot = {version = "0.6", features = ["nightly"]}
parking_lot = {version = "0.8", features = ["nightly"]}
```

The experimental deadlock detector can be enabled with the
Expand All @@ -124,7 +124,7 @@ changes to the core API do not cause breaking changes for users of `parking_lot`

## Minimum Rust version

The current minimum required Rust version is 1.24. Any change to this is
The current minimum required Rust version is 1.31. Any change to this is
considered a breaking change and will require a major version bump.

## License
Expand Down
8 changes: 4 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ environment:
- TARGET: nightly-i686-pc-windows-msvc
- TARGET: nightly-x86_64-pc-windows-gnu
- TARGET: nightly-i686-pc-windows-gnu
- TARGET: 1.24.0-x86_64-pc-windows-msvc
- TARGET: 1.24.0-i686-pc-windows-msvc
- TARGET: 1.24.0-x86_64-pc-windows-gnu
- TARGET: 1.24.0-i686-pc-windows-gnu
- TARGET: 1.31.0-x86_64-pc-windows-msvc
- TARGET: 1.31.0-i686-pc-windows-msvc
- TARGET: 1.31.0-x86_64-pc-windows-gnu
- TARGET: 1.31.0-i686-pc-windows-gnu

install:
- SET PATH=C:\Python27;C:\Python27\Scripts;%PATH%;%APPDATA%\Python\Scripts
Expand Down
6 changes: 2 additions & 4 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "parking_lot_core"
version = "0.4.0"
version = "0.5.0"
authors = ["Amanieu d'Antras <[email protected]>"]
description = "An advanced API for creating custom synchronization primitives."
license = "Apache-2.0/MIT"
repository = "https://github.com/Amanieu/parking_lot"
keywords = ["mutex", "condvar", "rwlock", "once", "thread"]
categories = ["concurrency"]
edition = "2018"

[dependencies]
smallvec = "0.6"
Expand All @@ -25,6 +26,3 @@ winapi = { version = "0.3", features = ["winnt", "ntstatus", "minwindef",
[features]
nightly = []
deadlock_detection = ["petgraph", "thread-id", "backtrace"]

[build-dependencies]
rustc_version = "0.2"
11 changes: 0 additions & 11 deletions core/build.rs

This file was deleted.

33 changes: 8 additions & 25 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,7 @@
//! reference count and the two mutex bits in the same atomic word.

#![warn(missing_docs)]
#![cfg_attr(
all(feature = "nightly", target_os = "linux"),
feature(integer_atomics)
)]

extern crate rand;
extern crate smallvec;

#[cfg(feature = "deadlock_detection")]
extern crate backtrace;
#[cfg(feature = "deadlock_detection")]
extern crate petgraph;
#[cfg(feature = "deadlock_detection")]
extern crate thread_id;

#[cfg(unix)]
extern crate libc;

#[cfg(windows)]
extern crate winapi;
#![warn(rust_2018_idioms)]

#[cfg(all(feature = "nightly", target_os = "linux"))]
#[path = "thread_parker/linux.rs"]
Expand All @@ -77,8 +58,10 @@ mod spinwait;
mod util;
mod word_lock;

pub use parking_lot::deadlock;
pub use parking_lot::{park, unpark_all, unpark_filter, unpark_one, unpark_requeue};
pub use parking_lot::{FilterOp, ParkResult, ParkToken, RequeueOp, UnparkResult, UnparkToken};
pub use parking_lot::{DEFAULT_PARK_TOKEN, DEFAULT_UNPARK_TOKEN};
pub use spinwait::SpinWait;
pub use self::parking_lot::deadlock;
pub use self::parking_lot::{park, unpark_all, unpark_filter, unpark_one, unpark_requeue};
pub use self::parking_lot::{
FilterOp, ParkResult, ParkToken, RequeueOp, UnparkResult, UnparkToken,
};
pub use self::parking_lot::{DEFAULT_PARK_TOKEN, DEFAULT_UNPARK_TOKEN};
pub use self::spinwait::SpinWait;
48 changes: 14 additions & 34 deletions core/src/parking_lot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use rand::rngs::SmallRng;
use rand::{FromEntropy, Rng};
use crate::thread_parker::ThreadParker;
use crate::util::UncheckedOptionExt;
use crate::word_lock::WordLock;
use core::{
cell::{Cell, UnsafeCell},
ptr,
sync::atomic::{AtomicPtr, AtomicUsize, Ordering},
};
use rand::{rngs::SmallRng, FromEntropy, Rng};
use smallvec::SmallVec;
use std::cell::{Cell, UnsafeCell};
#[cfg(not(has_localkey_try_with))]
use std::panic;
use std::ptr;
use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use std::thread::LocalKey;
use std::time::{Duration, Instant};
use thread_parker::ThreadParker;
use util::UncheckedOptionExt;
use word_lock::WordLock;

static NUM_THREADS: AtomicUsize = AtomicUsize::new(0);
static HASHTABLE: AtomicPtr<HashTable> = AtomicPtr::new(ptr::null_mut());
Expand Down Expand Up @@ -50,7 +48,7 @@ impl HashTable {
}
}

#[cfg_attr(has_repr_align, repr(align(64)))]
#[repr(align(64))]
struct Bucket {
// Lock protecting the queue
mutex: WordLock,
Expand All @@ -61,12 +59,6 @@ struct Bucket {

// Next time at which point be_fair should be set
fair_timeout: UnsafeCell<FairTimeout>,

// Padding to avoid false sharing between buckets. Ideally we would just
// align the bucket structure to 64 bytes, but Rust doesn't support that
// yet. Remove this field when dropping support for Rust < 1.25
#[cfg(not(has_repr_align))]
_padding: [u8; 64],
}

impl Bucket {
Expand All @@ -77,8 +69,6 @@ impl Bucket {
queue_head: Cell::new(ptr::null()),
queue_tail: Cell::new(ptr::null()),
fair_timeout: UnsafeCell::new(FairTimeout::new()),
#[cfg(not(has_repr_align))]
_padding: unsafe { ::std::mem::uninitialized() },
}
}
}
Expand Down Expand Up @@ -173,24 +163,14 @@ fn with_thread_data<F, T>(f: F) -> T
where
F: FnOnce(&ThreadData) -> T,
{
// Try to read from thread-local storage, but return None if the TLS has
// already been destroyed.
#[cfg(has_localkey_try_with)]
fn try_get_tls(key: &'static LocalKey<ThreadData>) -> Option<*const ThreadData> {
key.try_with(|x| x as *const ThreadData).ok()
}
#[cfg(not(has_localkey_try_with))]
fn try_get_tls(key: &'static LocalKey<ThreadData>) -> Option<*const ThreadData> {
panic::catch_unwind(|| key.with(|x| x as *const ThreadData)).ok()
}

// Unlike word_lock::ThreadData, parking_lot::ThreadData is always expensive
// to construct. Try to use a thread-local version if possible. Otherwise just
// create a ThreadData on the stack
let mut thread_data_storage = None;
thread_local!(static THREAD_DATA: ThreadData = ThreadData::new());
let thread_data_ptr = try_get_tls(&THREAD_DATA)
.unwrap_or_else(|| thread_data_storage.get_or_insert_with(ThreadData::new));
let thread_data_ptr = THREAD_DATA
.try_with(|x| x as *const ThreadData)
.unwrap_or_else(|_| thread_data_storage.get_or_insert_with(ThreadData::new));

f(unsafe { &*thread_data_ptr })
}
Expand Down Expand Up @@ -1106,6 +1086,7 @@ pub mod deadlock {
#[cfg(feature = "deadlock_detection")]
mod deadlock_impl {
use super::{get_hashtable, lock_bucket, with_thread_data, ThreadData, NUM_THREADS};
use crate::word_lock::WordLock;
use backtrace::Backtrace;
use petgraph;
use petgraph::graphmap::DiGraphMap;
Expand All @@ -1114,7 +1095,6 @@ mod deadlock_impl {
use std::sync::atomic::Ordering;
use std::sync::mpsc;
use thread_id;
use word_lock::WordLock;

/// Representation of a deadlocked thread
pub struct DeadlockedThread {
Expand Down
2 changes: 1 addition & 1 deletion core/src/spinwait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::thread_parker;
use std::sync::atomic::spin_loop_hint;
use thread_parker;

// Wastes some CPU time for the given number of iterations,
// using a hint to indicate to the CPU that we are spinning.
Expand Down
5 changes: 2 additions & 3 deletions core/src/thread_parker/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
//! A simple spin lock based thread parker. Used on platforms without better
//! parking facilities available.

use std::sync::atomic::{spin_loop_hint, AtomicBool, Ordering};
use std::thread;
use std::time::Instant;
use core::sync::atomic::{spin_loop_hint, AtomicBool, Ordering};
use std::{thread, time::Instant};

// Helper type for putting a thread to sleep until some other thread wakes it up
pub struct ThreadParker {
Expand Down
9 changes: 5 additions & 4 deletions core/src/thread_parker/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use core::{
ptr,
sync::atomic::{AtomicI32, Ordering},
};
use libc;
use std::ptr;
use std::sync::atomic::{AtomicI32, Ordering};
use std::thread;
use std::time::Instant;
use std::{thread, time::Instant};

const FUTEX_WAIT: i32 = 0;
const FUTEX_WAKE: i32 = 1;
Expand Down
16 changes: 10 additions & 6 deletions core/src/thread_parker/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use libc;
use std::cell::{Cell, UnsafeCell};
use std::mem;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use std::ptr;
use std::thread;
use std::time::{Duration, Instant};
use core::ptr;
use core::{
cell::{Cell, UnsafeCell},
mem,
};
use libc;
use std::{
thread,
time::{Duration, Instant},
};

// x32 Linux uses a non-standard type for tv_nsec in timespec.
// See https://sourceware.org/bugzilla/show_bug.cgi?id=16437
Expand Down
32 changes: 20 additions & 12 deletions core/src/thread_parker/windows/keyed_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use std::mem;
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Instant;

use winapi::shared::minwindef::{TRUE, ULONG};
use winapi::shared::ntdef::NTSTATUS;
use winapi::shared::ntstatus::{STATUS_SUCCESS, STATUS_TIMEOUT};
use winapi::um::handleapi::CloseHandle;
use winapi::um::libloaderapi::{GetModuleHandleA, GetProcAddress};
use winapi::um::winnt::{ACCESS_MASK, GENERIC_READ, GENERIC_WRITE, LPCSTR};
use winapi::um::winnt::{BOOLEAN, HANDLE, LARGE_INTEGER, PHANDLE, PLARGE_INTEGER, PVOID};
use core::{mem, ptr};
use std::{
sync::atomic::{AtomicUsize, Ordering},
time::Instant,
};
use winapi::{
shared::{
minwindef::{TRUE, ULONG},
ntdef::NTSTATUS,
ntstatus::{STATUS_SUCCESS, STATUS_TIMEOUT},
},
um::{
handleapi::CloseHandle,
libloaderapi::{GetModuleHandleA, GetProcAddress},
winnt::{
ACCESS_MASK, BOOLEAN, GENERIC_READ, GENERIC_WRITE, HANDLE, LARGE_INTEGER, LPCSTR,
PHANDLE, PLARGE_INTEGER, PVOID,
},
},
};

const STATE_UNPARKED: usize = 0;
const STATE_PARKED: usize = 1;
Expand Down
7 changes: 4 additions & 3 deletions core/src/thread_parker/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use std::ptr;
use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use core::{
ptr,
sync::atomic::{AtomicPtr, AtomicUsize, Ordering},
};
use std::time::Instant;
use winapi;

mod keyed_event;
mod waitaddress;
Expand Down
Loading

0 comments on commit 66d67bb

Please sign in to comment.