Commit Graph

528 Commits

Author SHA1 Message Date
kennytm
e253224541 Rollup merge of #47970 - vlovich:condvar_wait_until, r=dtolnay
Add Condvar APIs not susceptible to spurious wake

Provide wait_until and wait_timeout_until helper wrappers that aren't susceptible to spurious wake.
Additionally wait_timeout_until makes it possible to more easily write code that waits for a fixed amount of time in face of spurious wakes since otherwise each user would have to do math on adjusting the duration.

Implements #47960.
2018-02-25 15:54:39 +08:00
Vitali Lovich
14b403c91a Fix doc compile error 2018-02-20 13:07:21 -08:00
Guillaume Gomez
af140ecdbc Rollup merge of #48275 - matthiaskrgr:codespell, r=kennytm,varkor
fix more typos found by codespell.
2018-02-18 13:21:01 +01:00
Vitali Lovich
d549db8031 Fix tidy violation 2018-02-17 09:17:58 -08:00
Matthias Krüger
4452446292 fix more typos found by codespell. 2018-02-17 17:38:49 +01:00
Vitali Lovich
b1f04a3a2e Fix unit test compilation
Also fix some code snippets in documentation.
2018-02-16 20:00:18 -08:00
Guillaume Gomez
ba6a6d0f0a Fix condvar example 2018-02-15 19:00:51 +01:00
Vitali Lovich
6fe2d1d765 Misc fixes
Switch feature guards to unstable
Add missing semicolon
Remove mut that's no longer necessary
2018-02-13 11:32:04 -08:00
Vitali Lovich
97df227d19 Fix wait_timeout value 2018-02-12 21:08:14 -08:00
Vitali Lovich
95e4dc2ad1 Simplify wait_timeout_until & fix condition typo 2018-02-05 15:11:00 -08:00
Vitali Lovich
e72bd6df53 Review response
Make condition closure accept mut T&.
Clarify spurious wakeup documentation.
Cleanup doc example code.
2018-02-02 12:13:48 -08:00
Vitali Lovich
404e1a6700 Fix typo 2018-02-02 11:52:16 -08:00
Vitali Lovich
7d296c4843 Add Condvar APIs not susceptible to spurious wake
Provide wait_until and wait_timeout_until helper wrappers that aren't
susceptible to spurious wake.
2018-02-02 11:29:58 -08:00
Bulat Musin
d0d5db65c7 fix typo rwlock.rs
Hi. Fixed typo: contained -> content
2018-01-10 08:03:10 +03:00
Oliver Middleton
a8d107be25 Correct a few stability attributes 2017-12-27 14:11:05 +00:00
kennytm
30f1853649 Rollup merge of #46323 - ia0:fix_mpsc_error_conv, r=kennytm
Fix since for mpsc_error_conversions

This is a followup of #45506.
2017-11-29 18:37:51 +08:00
kennytm
0e78c29bea Rollup merge of #46287 - SimonSapin:stable-constness, r=aturon
Stabilize const-calling existing const-fns in std

Fixes #46038
2017-11-29 18:37:47 +08:00
kennytm
0ec3aee569 Rollup merge of #45969 - ia0:mpsc_recv_deadline, r=alexcrichton
Add std::sync::mpsc::Receiver::recv_deadline()

Essentially renames recv_max_until to recv_deadline (mostly copying recv_timeout
documentation). This function is useful to avoid the often unnecessary call to
Instant::now in recv_timeout (e.g. when the user already has a deadline). A
concrete example would be something along those lines:

```rust
use std::sync::mpsc::Receiver;
use std::time::{Duration, Instant};

/// Reads a batch of elements
///
/// Returns as soon as `max_size` elements have been received or `timeout` expires.
fn recv_batch_timeout<T>(receiver: &Receiver<T>, timeout: Duration, max_size: usize) -> Vec<T> {
    recv_batch_deadline(receiver, Instant::now() + timeout, max_size)
}

/// Reads a batch of elements
///
/// Returns as soon as `max_size` elements have been received or `deadline` is reached.
fn recv_batch_deadline<T>(receiver: &Receiver<T>, deadline: Instant, max_size: usize) -> Vec<T> {
    let mut result = Vec::new();
    while let Ok(x) = receiver.recv_deadline(deadline) {
        result.push(x);
        if result.len() == max_size {
            break;
        }
    }
    result
}
```
2017-11-29 18:37:43 +08:00
Julien Cretin
8e025d8009 Fix doc test of previous commit 2017-11-28 21:22:30 +01:00
Julien Cretin
43323b3194 Fix since for mpsc_error_conversions 2017-11-28 15:58:36 +01:00
Julien Cretin
8424cacff8 Use an unstable feature linked to #46316 2017-11-27 22:31:00 +01:00
kennytm
2f012e4405 Rollup merge of #45506 - ia0:mpsc_recv_error_from, r=alexcrichton
Implement From<RecvError> for TryRecvError and RecvTimeoutError

According to the documentation, it looks to me that `TryRecvError` and `RecvTimeoutError` are strict extensions of `RecvError`. As such, it makes sense to allow conversion from the latter type to the two former types without constraining future developments.

This permits to write `input.recv()?` and `input.recv_timeout(timeout)?` in the same function for example.
2017-11-28 03:16:41 +08:00
Simon Sapin
6c5f53e65e Stabilize const-calling existing const-fns in std
Fixes #46038
2017-11-26 23:43:44 +01:00
Eduardo Pinho
0855ea1832 impl From<T> for RwLock<T> 2017-11-18 21:05:06 +00:00
Eduardo Pinho
1bfc6c1296 impl From<T> for Mutex<T> 2017-11-18 16:52:46 +00:00
Julien Cretin
428c875ac3 Add std::sync::mpsc::Receiver::recv_deadline()
Essentially renames recv_max_until to recv_deadline (mostly copying recv_timeout
documentation). This function is useful to avoid the often unnecessary call to
Instant::now in recv_timeout (e.g. when the user already has a deadline). A
concrete example would be something along those lines:

```rust
use std::sync::mpsc::Receiver;
use std::time::{Duration, Instant};

/// Reads a batch of elements
///
/// Returns as soon as `max_size` elements have been received or `timeout` expires.
fn recv_batch_timeout<T>(receiver: &Receiver<T>, timeout: Duration, max_size: usize) -> Vec<T> {
    recv_batch_deadline(receiver, Instant::now() + timeout, max_size)
}

/// Reads a batch of elements
///
/// Returns as soon as `max_size` elements have been received or `deadline` is reached.
fn recv_batch_deadline<T>(receiver: &Receiver<T>, deadline: Instant, max_size: usize) -> Vec<T> {
    let mut result = Vec::new();
    while let Ok(x) = receiver.recv_deadline(deadline) {
        result.push(x);
        if result.len() == max_size {
            break;
        }
    }
    result
}
```
2017-11-13 22:50:22 +01:00
Alex Crichton
6bc8f164b0 std: Remove rand crate and module
This commit removes the `rand` crate from the standard library facade as
well as the `__rand` module in the standard library. Neither of these
were used in any meaningful way in the standard library itself. The only
need for randomness in libstd is to initialize the thread-local keys of
a `HashMap`, and that unconditionally used `OsRng` defined in the
standard library anyway.

The cruft of the `rand` crate and the extra `rand` support in the
standard library makes libstd slightly more difficult to port to new
platforms, namely WebAssembly which doesn't have any randomness at all
(without interfacing with JS). The purpose of this commit is to clarify
and streamline randomness in libstd, focusing on how it's only required
in one location, hashmap seeds.

Note that the `rand` crate out of tree has almost always been a drop-in
replacement for the `rand` crate in-tree, so any usage (accidental or
purposeful) of the crate in-tree should switch to the `rand` crate on
crates.io. This then also has the further benefit of avoiding
duplication (mostly) between the two crates!
2017-11-08 20:41:17 -08:00
Ralf Jung
71534c45cc RwLock guards are Sync if T is
Currently, the compiler requires `T` to also be `Send`.  There is no reason for
that.  `&Rw{Read,Write}LockGuard` only provides a shared referenced to `T`, sending
that across threads is safe if `T` is `Sync`.
2017-11-01 13:39:44 +01:00
bors
31bbe57c79 Auto merge of #45267 - oconnor663:rwlock_send, r=alexcrichton
remove the `T: Sync` requirement for `RwLock<T>: Send`

That requirement makes sense for containers like `Arc` that don't
uniquely own their contents, but `RwLock` is not one of those.

This restriction was added in 380d23b5d4, but it's not clear why. @hniksic
and I [were discussing this on reddit](https://www.reddit.com/r/rust/comments/763o7r/blog_posts_introducing_lockfree_rust_comparing/dobcvbm/). I might be totally wrong about this change being sound, but I'm super curious to find out :)
2017-11-01 04:32:15 +00:00
Julien Cretin
448215d226 Implement From<SendError<T>> for TrySendError<T> 2017-10-31 00:18:53 +01:00
Alex Crichton
ca18537197 Bump to 1.23 and update bootstrap
This commit updates the bootstrap compiler, bumps the version to 1.23, updates
Cargo, updates books, and updates crates.io dependencies
2017-10-26 13:59:18 -07:00
Julien Cretin
73ed6cf7fb Implement From<RecvError> for TryRecvError and RecvTimeoutError 2017-10-25 00:06:28 +02:00
Corey Farwell
aae94c7368 Improve docs around Once::call_once_force and OnceState. 2017-10-22 10:25:31 -04:00
Jack O'Connor
fbf6885fd3 remove the T: Sync requirement for RwLock<T>: Send
That requirement makes sense for containers like `Arc` that don't
uniquely own their contents, but `RwLock` is not one of those.

This restriction was added in
380d23b5d4,
but it's not clear why.
2017-10-13 18:54:49 -04:00
bors
a47c9f870f Auto merge of #44963 - JLockerman:fix_spsc, r=alexcrichton
Improve performance of spsc_queue and stream.

This PR makes two main changes:

1. It switches the `spsc_queue` node caching strategy from keeping a shared
counter of the number of nodes in the cache to keeping a consumer only counter
of the number of node eligible to be cached.
2. It separates the consumer and producers fields of `spsc_queue` and `stream` into
a producer cache line and consumer cache line.

Overall, it speeds up `mpsc` in `spsc` mode by 2-10x.
Variance is higher than I'd like (that 2-10x speedup is on one benchmark), I believe this is due to the drop check in `send` (`fn stream::Queue::send:107`). I think this check can be combined with the sleep detection code into a version which only uses 1 shared variable, and only one atomic access per `send`, but I haven't looked through the select implementation enough to be sure.

The code currently assumes a cache line size of 64 bytes. I added a CacheAligned newtype in `mpsc` which I expect to reuse for `shared`. It doesn't really belong there, it would probably be best put in `core::sync::atomic`, but putting it in `core` would involve making it public, which I thought would require an RFC.

Benchmark runner is [here](3eca46279c/shootout), benchmarks [here](3eca46279c/queue_bench/src/lib.rs (L170-L293)).

Fixes #44512.
2017-10-11 19:32:19 +00:00
kennytm
23a99f4e0e Rollup merge of #44775 - MaloJaffre:debug-struct, r=sfackler
Refactor to use `debug_struct` in several Debug impls

Also use `pad` and derive `Debug` for `Edge`.

Fixes #44771.
2017-10-10 22:43:57 +08:00
Malo Jaffré
679457ad2a Refactor to use debug_struct in several Debug impls
Fixes #44771.
2017-10-09 20:09:08 +02:00
Joshua Lockerman
bb7945e2fe Remove Queue::new. 2017-10-08 20:20:32 -04:00
JLockerman
41320fa52e cfg out Queue::new for emscripten
Queue::new is only used is tests atm, which causes warnings on emscripten which does not run queue tests.
2017-10-08 18:40:45 -04:00
Guillaume Gomez
e1df72fdab Add missing urls for Mutex 2017-10-04 14:03:23 +02:00
Joshua Lockerman
68341a91ee Improve performance of spsc_queue and stream.
This commit makes two main changes.
1. It switches the spsc_queue node caching strategy from keeping a shared
counter of the number of nodes in the cache to keeping a consumer only counter
of the number of node eligible to be cached.
2. It separate the consumer and producers fields of spsc_queue and stream into
a producer cache line and consumer cache line.
2017-10-01 12:15:35 -04:00
Lucas Morales
f283875a78 std::sync::RwLock docs improvement 2017-09-22 22:12:21 -04:00
Alex Burka
681e5da61e change #![feature(const_fn)] to specific gates 2017-09-16 15:53:02 +00:00
Corey Farwell
a4387d54dc Rollup merge of #43891 - Fourchaux:master, r=steveklabnik
Fix typos & us spellings

Fixing some typos and non en-US spellings.

(Update of PR https://github.com/rust-lang/rust/pull/42812 )
2017-08-17 10:44:07 -04:00
Zack M. Davis
1b6c9605e4 use field init shorthand EVERYWHERE
Like #43008 (f668999), but _much more aggressive_.
2017-08-15 15:29:17 -07:00
Fourchaux
c7104be1a3 Fix typos & us spellings 2017-08-15 21:56:30 +02:00
bors
d21ec9b4ef Auto merge of #43582 - ivanbakel:unused_mut_ref, r=arielb1
Fixed mutable vars being marked used when they weren't

#### NB : bootstrapping is slow on my machine, even with `keep-stage` - fixes for occurances in the current codebase are <s>in the pipeline</s> done. This PR is being put up for review of the fix of the issue.

Fixes #43526, Fixes #30280, Fixes #25049

### Issue
Whenever the compiler detected a mutable deref being used mutably, it marked an associated value as being used mutably as well. In the case of derefencing local variables which were mutable references, this incorrectly marked the reference itself being used mutably, instead of its contents - with the consequence of making the following code emit no warnings
```
fn do_thing<T>(mut arg : &mut T) {
    ... // don't touch arg - just deref it to access the T
}
```

### Fix
Make dereferences not be counted as a mutable use, but only when they're on borrows on local variables.
#### Why not on things other than local variables?
  * Whenever you capture a variable in a closure, it gets turned into a hidden reference - when you use it in the closure, it gets dereferenced. If the closure uses the variable mutably, that is actually a mutable use of the thing being dereffed to, so it has to be counted.
  * If you deref a mutable `Box` to access the contents mutably, you are using the `Box` mutably - so it has to be counted.
2017-08-10 08:53:22 +00:00
Stefan Schindler
702750c538 Use explicit wrapping_add to prevent potential unexpected behavior on debug builds 2017-08-08 22:58:09 +02:00
Isaac van Bakel
400075d9d9 Fixed all unnecessary muts in language core 2017-08-01 23:01:24 +01:00
Oliver Middleton
f2566bbaeb Correct some stability attributes
These show up in rustdoc so need to be correct.
2017-07-10 02:07:29 +01:00