Skip to content

Commit 895a501

Browse files
authored
Rollup merge of #148343 - connortsui20:guard-ref-condvar, r=Amanieu
`nonpoison::Condvar` should take `MutexGuard` by reference Feature: `#![feature(nonpoison_condvar)]` Tracking Issue: #134645 Specific comment: #134645 (comment) Changes the `nonpoison::Condvar` API to take `MutexGuard` by reference instead of ownership. I'm actually not entirely sure why the current poison variant of `Condvar` takes by ownership, is it just legacy reasons? Additionally, the `nonpoison_and_poison_unwrap_test` macro doesn't really make sense anymore now that the APIs are completely different, so this reverts that change from a few months ago. r? `@Amanieu`
2 parents 33dc838 + c1153b0 commit 895a501

File tree

4 files changed

+550
-302
lines changed

4 files changed

+550
-302
lines changed

library/std/src/sync/barrier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Barrier {
125125
let local_gen = lock.generation_id;
126126
lock.count += 1;
127127
if lock.count < self.num_threads {
128-
let _guard = self.cvar.wait_while(lock, |state| local_gen == state.generation_id);
128+
self.cvar.wait_while(&mut lock, |state| local_gen == state.generation_id);
129129
BarrierWaitResult(false)
130130
} else {
131131
lock.count = 0;

library/std/src/sync/nonpoison/condvar.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::fmt;
2+
use crate::ops::DerefMut;
23
use crate::sync::WaitTimeoutResult;
34
use crate::sync::nonpoison::{MutexGuard, mutex};
45
use crate::sys::sync as sys;
@@ -38,7 +39,7 @@ use crate::time::{Duration, Instant};
3839
/// let (lock, cvar) = &*pair;
3940
/// let mut started = lock.lock();
4041
/// while !*started {
41-
/// started = cvar.wait(started);
42+
/// cvar.wait(&mut started);
4243
/// }
4344
/// ```
4445
///
@@ -115,16 +116,15 @@ impl Condvar {
115116
/// let mut started = lock.lock();
116117
/// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
117118
/// while !*started {
118-
/// started = cvar.wait(started);
119+
/// cvar.wait(&mut started);
119120
/// }
120121
/// ```
121122
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
122-
pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
123+
pub fn wait<T>(&self, guard: &mut MutexGuard<'_, T>) {
123124
unsafe {
124-
let lock = mutex::guard_lock(&guard);
125+
let lock = mutex::guard_lock(guard);
125126
self.inner.wait(lock);
126127
}
127-
guard
128128
}
129129

130130
/// Blocks the current thread until the provided condition becomes false.
@@ -167,21 +167,17 @@ impl Condvar {
167167
/// // Wait for the thread to start up.
168168
/// let (lock, cvar) = &*pair;
169169
/// // As long as the value inside the `Mutex<bool>` is `true`, we wait.
170-
/// let _guard = cvar.wait_while(lock.lock(), |pending| { *pending });
170+
/// let mut guard = lock.lock();
171+
/// cvar.wait_while(&mut guard, |pending| { *pending });
171172
/// ```
172173
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
173-
pub fn wait_while<'a, T, F>(
174-
&self,
175-
mut guard: MutexGuard<'a, T>,
176-
mut condition: F,
177-
) -> MutexGuard<'a, T>
174+
pub fn wait_while<T, F>(&self, guard: &mut MutexGuard<'_, T>, mut condition: F)
178175
where
179176
F: FnMut(&mut T) -> bool,
180177
{
181-
while condition(&mut *guard) {
182-
guard = self.wait(guard);
178+
while condition(guard.deref_mut()) {
179+
self.wait(guard);
183180
}
184-
guard
185181
}
186182

187183
/// Waits on this condition variable for a notification, timing out after a
@@ -206,7 +202,7 @@ impl Condvar {
206202
/// The returned [`WaitTimeoutResult`] value indicates if the timeout is
207203
/// known to have elapsed.
208204
///
209-
/// Like [`wait`], the lock specified will be re-acquired when this function
205+
/// Like [`wait`], the lock specified will have been re-acquired when this function
210206
/// returns, regardless of whether the timeout elapsed or not.
211207
///
212208
/// [`wait`]: Self::wait
@@ -239,26 +235,25 @@ impl Condvar {
239235
/// let mut started = lock.lock();
240236
/// // as long as the value inside the `Mutex<bool>` is `false`, we wait
241237
/// loop {
242-
/// let result = cvar.wait_timeout(started, Duration::from_millis(10));
238+
/// let result = cvar.wait_timeout(&mut started, Duration::from_millis(10));
243239
/// // 10 milliseconds have passed, or maybe the value changed!
244-
/// started = result.0;
245240
/// if *started == true {
246241
/// // We received the notification and the value has been updated, we can leave.
247242
/// break
248243
/// }
249244
/// }
250245
/// ```
251246
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
252-
pub fn wait_timeout<'a, T>(
247+
pub fn wait_timeout<T>(
253248
&self,
254-
guard: MutexGuard<'a, T>,
249+
guard: &mut MutexGuard<'_, T>,
255250
dur: Duration,
256-
) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
251+
) -> WaitTimeoutResult {
257252
let success = unsafe {
258-
let lock = mutex::guard_lock(&guard);
253+
let lock = mutex::guard_lock(guard);
259254
self.inner.wait_timeout(lock, dur)
260255
};
261-
(guard, WaitTimeoutResult(!success))
256+
WaitTimeoutResult(!success)
262257
}
263258

264259
/// Waits on this condition variable for a notification, timing out after a
@@ -277,7 +272,7 @@ impl Condvar {
277272
/// The returned [`WaitTimeoutResult`] value indicates if the timeout is
278273
/// known to have elapsed without the condition being met.
279274
///
280-
/// Like [`wait_while`], the lock specified will be re-acquired when this
275+
/// Like [`wait_while`], the lock specified will have been re-acquired when this
281276
/// function returns, regardless of whether the timeout elapsed or not.
282277
///
283278
/// [`wait_while`]: Self::wait_while
@@ -307,37 +302,39 @@ impl Condvar {
307302
///
308303
/// // wait for the thread to start up
309304
/// let (lock, cvar) = &*pair;
305+
/// let mut guard = lock.lock();
310306
/// let result = cvar.wait_timeout_while(
311-
/// lock.lock(),
307+
/// &mut guard,
312308
/// Duration::from_millis(100),
313309
/// |&mut pending| pending,
314310
/// );
315-
/// if result.1.timed_out() {
311+
/// if result.timed_out() {
316312
/// // timed-out without the condition ever evaluating to false.
317313
/// }
318-
/// // access the locked mutex via result.0
314+
/// // access the locked mutex via guard
319315
/// ```
320316
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
321-
pub fn wait_timeout_while<'a, T, F>(
317+
pub fn wait_timeout_while<T, F>(
322318
&self,
323-
mut guard: MutexGuard<'a, T>,
319+
guard: &mut MutexGuard<'_, T>,
324320
dur: Duration,
325321
mut condition: F,
326-
) -> (MutexGuard<'a, T>, WaitTimeoutResult)
322+
) -> WaitTimeoutResult
327323
where
328324
F: FnMut(&mut T) -> bool,
329325
{
330326
let start = Instant::now();
331-
loop {
332-
if !condition(&mut *guard) {
333-
return (guard, WaitTimeoutResult(false));
334-
}
327+
328+
while condition(guard.deref_mut()) {
335329
let timeout = match dur.checked_sub(start.elapsed()) {
336330
Some(timeout) => timeout,
337-
None => return (guard, WaitTimeoutResult(true)),
331+
None => return WaitTimeoutResult(true),
338332
};
339-
guard = self.wait_timeout(guard, timeout).0;
333+
334+
self.wait_timeout(guard, timeout);
340335
}
336+
337+
WaitTimeoutResult(false)
341338
}
342339

343340
/// Wakes up one blocked thread on this condvar.
@@ -378,7 +375,7 @@ impl Condvar {
378375
/// let mut started = lock.lock();
379376
/// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
380377
/// while !*started {
381-
/// started = cvar.wait(started);
378+
/// cvar.wait(&mut started);
382379
/// }
383380
/// ```
384381
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
@@ -422,7 +419,7 @@ impl Condvar {
422419
/// let mut started = lock.lock();
423420
/// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
424421
/// while !*started {
425-
/// started = cvar.wait(started);
422+
/// cvar.wait(&mut started);
426423
/// }
427424
/// ```
428425
#[unstable(feature = "nonpoison_condvar", issue = "134645")]

0 commit comments

Comments
 (0)