Skip to content

Commit

Permalink
std: Second pass stabilization of sync
Browse files Browse the repository at this point in the history
This pass performs a second pass of stabilization through the `std::sync`
module, avoiding modules/types that are being handled in other PRs (e.g.
mutexes, rwlocks, condvars, and channels).

The following items are now stable

* `sync::atomic`
* `sync::atomic::ATOMIC_BOOL_INIT` (was `INIT_ATOMIC_BOOL`)
* `sync::atomic::ATOMIC_INT_INIT` (was `INIT_ATOMIC_INT`)
* `sync::atomic::ATOMIC_UINT_INIT` (was `INIT_ATOMIC_UINT`)
* `sync::Once`
* `sync::ONCE_INIT`
* `sync::Once::call_once` (was `doit`)
  * C == `pthread_once(..)`
  * Boost == `call_once(..)`
  * Windows == `InitOnceExecuteOnce`
* `sync::Barrier`
* `sync::Barrier::new`
* `sync::Barrier::wait` (now returns a `bool`)
* `sync::Semaphore::new`
* `sync::Semaphore::acquire`
* `sync::Semaphore::release`

The following items remain unstable

* `sync::SemaphoreGuard`
* `sync::Semaphore::access` - it's unclear how this relates to the poisoning
                              story of mutexes.
* `sync::TaskPool` - the semantics of a failing task and whether a thread is
                     re-attached to a thread pool are somewhat unclear, and the
                     utility of this type in `sync` is question with respect to
                     the jobs of other primitives. This type will likely become
                     stable or move out of the standard library over time.
* `sync::Future` - futures as-is have yet to be deeply re-evaluated with the
                   recent core changes to Rust's synchronization story, and will
                   likely become stable in the future but are unstable until
                   that time comes.

[breaking-change]
  • Loading branch information
alexcrichton committed Jan 2, 2015
1 parent cd61416 commit f3a7ec7
Show file tree
Hide file tree
Showing 44 changed files with 168 additions and 237 deletions.
2 changes: 2 additions & 0 deletions src/doc/guide-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ getting the result later.
The basic example below illustrates this.

```{rust,ignore}
# #![allow(deprecated)]
use std::sync::Future;
# fn main() {
Expand Down Expand Up @@ -233,6 +234,7 @@ Here is another example showing how futures allow you to background
computations. The workload will be distributed on the available cores.

```{rust,ignore}
# #![allow(deprecated)]
# use std::num::Float;
# use std::sync::Future;
fn partial_sum(start: uint) -> f64 {
Expand Down
1 change: 1 addition & 0 deletions src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -5312,6 +5312,7 @@ example, if you wish to compute some value in the background, `Future` is
a useful thing to use:
```{rust}
# #![allow(deprecated)]
use std::sync::Future;
let mut delayed_value = Future::spawn(move || {
Expand Down
4 changes: 2 additions & 2 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1480,9 +1480,9 @@ data are being stored, or single-address and mutability properties are required.
```
use std::sync::atomic;
// Note that INIT_ATOMIC_UINT is a *const*, but it may be used to initialize a
// Note that ATOMIC_UINT_INIT is a *const*, but it may be used to initialize a
// static. This static can be modified, so it is not placed in read-only memory.
static COUNTER: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
static COUNTER: atomic::AtomicUint = atomic::ATOMIC_UINT_INIT;
// This table is a candidate to be placed in read-only memory.
static TABLE: &'static [uint] = &[1, 2, 3, /* ... */];
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ mod tests {
}
}
const NUM_ELEMENTS: uint = 2;
static DROP_COUNTER: AtomicUint = atomic::INIT_ATOMIC_UINT;
static DROP_COUNTER: AtomicUint = atomic::ATOMIC_UINT_INIT;

let v = Vec::from_elem(NUM_ELEMENTS, Nothing);

Expand Down
16 changes: 13 additions & 3 deletions src/libcore/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,27 @@ pub enum Ordering {

/// An `AtomicBool` initialized to `false`.
#[unstable = "may be renamed, pending conventions for static initalizers"]
pub const INIT_ATOMIC_BOOL: AtomicBool =
pub const ATOMIC_BOOL_INIT: AtomicBool =
AtomicBool { v: UnsafeCell { value: 0 } };
/// An `AtomicInt` initialized to `0`.
#[unstable = "may be renamed, pending conventions for static initalizers"]
pub const INIT_ATOMIC_INT: AtomicInt =
pub const ATOMIC_INT_INIT: AtomicInt =
AtomicInt { v: UnsafeCell { value: 0 } };
/// An `AtomicUint` initialized to `0`.
#[unstable = "may be renamed, pending conventions for static initalizers"]
pub const INIT_ATOMIC_UINT: AtomicUint =
pub const ATOMIC_UINT_INIT: AtomicUint =
AtomicUint { v: UnsafeCell { value: 0, } };

/// Deprecated
#[deprecated = "renamed to ATOMIC_BOOL_INIT"]
pub const INIT_ATOMIC_BOOL: AtomicBool = ATOMIC_BOOL_INIT;
/// Deprecated
#[deprecated = "renamed to ATOMIC_INT_INIT"]
pub const INIT_ATOMIC_INT: AtomicInt = ATOMIC_INT_INIT;
/// Deprecated
#[deprecated = "renamed to ATOMIC_UINT_INIT"]
pub const INIT_ATOMIC_UINT: AtomicUint = ATOMIC_UINT_INIT;

// NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
const UINT_TRUE: uint = -1;

Expand Down
2 changes: 2 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@
// FIXME: Can't be shared between threads. Dynamic borrows
// FIXME: Relationship to Atomic types and RWLock

#![stable]

use clone::Clone;
use cmp::PartialEq;
use default::Default;
Expand Down
6 changes: 3 additions & 3 deletions src/libcoretest/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ fn int_xor() {
assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
}

static S_BOOL : AtomicBool = INIT_ATOMIC_BOOL;
static S_INT : AtomicInt = INIT_ATOMIC_INT;
static S_UINT : AtomicUint = INIT_ATOMIC_UINT;
static S_BOOL : AtomicBool = ATOMIC_BOOL_INIT;
static S_INT : AtomicInt = ATOMIC_INT_INIT;
static S_UINT : AtomicUint = ATOMIC_UINT_INIT;

#[test]
fn static_init() {
Expand Down
2 changes: 1 addition & 1 deletion src/liblog/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ pub struct LogLocation {
#[doc(hidden)]
pub fn mod_enabled(level: u32, module: &str) -> bool {
static INIT: Once = ONCE_INIT;
INIT.doit(init);
INIT.call_once(init);

// It's possible for many threads are in this function, only one of them
// will perform the global initialization, but all of them will need to check
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/infer/region_inference/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a,
let output_path = {
let output_template = match requested_output {
Some(ref s) if s.as_slice() == "help" => {
static PRINTED_YET : atomic::AtomicBool = atomic::INIT_ATOMIC_BOOL;
static PRINTED_YET : atomic::AtomicBool = atomic::ATOMIC_BOOL_INIT;
if !PRINTED_YET.load(atomic::SeqCst) {
print_help_message();
PRINTED_YET.store(true, atomic::SeqCst);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ unsafe fn configure_llvm(sess: &Session) {
}
}

INIT.doit(|| {
INIT.call_once(|| {
llvm::LLVMInitializePasses();

// Only initialize the platforms supported by Rust here, because
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3097,7 +3097,7 @@ pub fn trans_crate<'tcx>(analysis: ty::CrateAnalysis<'tcx>)
use std::sync::{Once, ONCE_INIT};
static INIT: Once = ONCE_INIT;
static mut POISONED: bool = false;
INIT.doit(|| {
INIT.call_once(|| {
if llvm::LLVMStartMultithreaded() != 1 {
// use an extra bool to make sure that all future usage of LLVM
// cannot proceed despite the Once not running more than once.
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/comm/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! Generic support for building blocking abstractions.
use thread::Thread;
use sync::atomic::{AtomicBool, INIT_ATOMIC_BOOL, Ordering};
use sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use sync::Arc;
use kinds::{Sync, Send};
use kinds::marker::{NoSend, NoSync};
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct WaitToken {
pub fn tokens() -> (WaitToken, SignalToken) {
let inner = Arc::new(Inner {
thread: Thread::current(),
woken: INIT_ATOMIC_BOOL,
woken: ATOMIC_BOOL_INIT,
});
let wait_token = WaitToken {
inner: inner.clone(),
Expand Down
6 changes: 0 additions & 6 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use result::Result::{Ok, Err};
use slice::{SliceExt};
use slice;
use vec::Vec;
use kinds::{Send,Sync};

/// Wraps a Reader and buffers input from it
///
Expand Down Expand Up @@ -52,11 +51,6 @@ pub struct BufferedReader<R> {
cap: uint,
}


unsafe impl<R: Send> Send for BufferedReader<R> {}
unsafe impl<R: Send+Sync> Sync for BufferedReader<R> {}


impl<R: Reader> BufferedReader<R> {
/// Creates a new `BufferedReader` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub fn stdin() -> StdinReader {
static ONCE: Once = ONCE_INIT;

unsafe {
ONCE.doit(|| {
ONCE.call_once(|| {
// The default buffer capacity is 64k, but apparently windows doesn't like
// 64k reads on stdin. See #13304 for details, but the idea is that on
// windows we use a slightly smaller buffer that's been seen to be
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl TempDir {
return TempDir::new_in(&abs_tmpdir, suffix);
}

static CNT: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
static CNT: atomic::AtomicUint = atomic::ATOMIC_UINT_INIT;

let mut attempts = 0u;
loop {
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/io/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use libc;
use os;
use prelude::*;
use std::io::net::ip::*;
use sync::atomic::{AtomicUint, INIT_ATOMIC_UINT, Relaxed};
use sync::atomic::{AtomicUint, ATOMIC_UINT_INIT, Relaxed};

/// Get a port number, starting at 9600, for use in tests
pub fn next_test_port() -> u16 {
static NEXT_OFFSET: AtomicUint = INIT_ATOMIC_UINT;
static NEXT_OFFSET: AtomicUint = ATOMIC_UINT_INIT;
base_port() + NEXT_OFFSET.fetch_add(1, Relaxed) as u16
}

/// Get a temporary path which could be the location of a unix socket
pub fn next_test_unix() -> Path {
static COUNT: AtomicUint = INIT_ATOMIC_UINT;
static COUNT: AtomicUint = ATOMIC_UINT_INIT;
// base port and pid are an attempt to be unique between multiple
// test-runners of different configurations running on one
// buildbot, the count is to be unique within this executable.
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use slice::{AsSlice, SliceExt};
use slice::CloneSliceExt;
use str::{Str, StrExt};
use string::{String, ToString};
use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
use sync::atomic::{AtomicInt, ATOMIC_INT_INIT, SeqCst};
use vec::Vec;

#[cfg(unix)] use c_str::ToCStr;
Expand Down Expand Up @@ -596,7 +596,7 @@ pub fn last_os_error() -> String {
error_string(errno() as uint)
}

static EXIT_STATUS: AtomicInt = INIT_ATOMIC_INT;
static EXIT_STATUS: AtomicInt = ATOMIC_INT_INIT;

/// Sets the process exit code
///
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/rand/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ mod imp {
#[cfg(all(target_os = "linux",
any(target_arch = "x86_64", target_arch = "x86", target_arch = "arm")))]
fn is_getrandom_available() -> bool {
use sync::atomic::{AtomicBool, INIT_ATOMIC_BOOL, Relaxed};
use sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Relaxed};

static GETRANDOM_CHECKED: AtomicBool = INIT_ATOMIC_BOOL;
static GETRANDOM_AVAILABLE: AtomicBool = INIT_ATOMIC_BOOL;
static GETRANDOM_CHECKED: AtomicBool = ATOMIC_BOOL_INIT;
static GETRANDOM_AVAILABLE: AtomicBool = ATOMIC_BOOL_INIT;

if !GETRANDOM_CHECKED.load(Relaxed) {
let mut buf: [u8; 0] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/rt/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use sys::backtrace::write;
// For now logging is turned off by default, and this function checks to see
// whether the magical environment variable is present to see if it's turned on.
pub fn log_enabled() -> bool {
static ENABLED: atomic::AtomicInt = atomic::INIT_ATOMIC_INT;
static ENABLED: atomic::AtomicInt = atomic::ATOMIC_INT_INIT;
match ENABLED.load(atomic::SeqCst) {
1 => return false,
2 => return true,
Expand Down
119 changes: 0 additions & 119 deletions src/libstd/rt/exclusive.rs

This file was deleted.

Loading

1 comment on commit f3a7ec7

@alexcrichton
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=aturon

Please sign in to comment.