Skip to content

std: Return Result from RWLock/Mutex methods #19661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ fn main() {
for i in range(0u, 3u) {
let number = numbers.clone();
Thread::spawn(move || {
let mut array = number.lock();
let mut array = number.lock().unwrap();

(*array)[i] += 1;

Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
//! let five = five.clone();
//!
//! Thread::spawn(move || {
//! let mut number = five.lock();
//! let mut number = five.lock().unwrap();
//!
//! *number += 1;
//!
Expand Down Expand Up @@ -722,7 +722,7 @@ mod tests {

let a = Arc::new(Cycle { x: Mutex::new(None) });
let b = a.clone().downgrade();
*a.x.lock() = Some(b);
*a.x.lock().unwrap() = Some(b);

// hopefully we don't double-free (or leak)...
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl SharedEmitter {
}

fn dump(&mut self, handler: &Handler) {
let mut buffer = self.buffer.lock();
let mut buffer = self.buffer.lock().unwrap();
for diag in buffer.iter() {
match diag.code {
Some(ref code) => {
Expand All @@ -123,7 +123,7 @@ impl Emitter for SharedEmitter {
msg: &str, code: Option<&str>, lvl: Level) {
assert!(cmsp.is_none(), "SharedEmitter doesn't support spans");

self.buffer.lock().push(Diagnostic {
self.buffer.lock().unwrap().push(Diagnostic {
msg: msg.to_string(),
code: code.map(|s| s.to_string()),
lvl: lvl,
Expand Down Expand Up @@ -915,7 +915,7 @@ fn run_work_multithreaded(sess: &Session,

loop {
// Avoid holding the lock for the entire duration of the match.
let maybe_work = work_items_arc.lock().pop();
let maybe_work = work_items_arc.lock().unwrap().pop();
match maybe_work {
Some(work) => {
execute_work_item(&cgcx, work);
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/comm/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<T: Send> Packet<T> {
// and that could cause problems on platforms where it is
// represented by opaque data structure
pub fn postinit_lock(&self) -> MutexGuard<()> {
self.select_lock.lock()
self.select_lock.lock().unwrap()
}

// This function is used at the creation of a shared packet to inherit a
Expand Down Expand Up @@ -435,7 +435,7 @@ impl<T: Send> Packet<T> {
// about looking at and dealing with to_wake. Once we have acquired the
// lock, we are guaranteed that inherit_blocker is done.
{
let _guard = self.select_lock.lock();
let _guard = self.select_lock.lock().unwrap();
}

// Like the stream implementation, we want to make sure that the count
Expand Down
26 changes: 13 additions & 13 deletions src/libstd/comm/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ fn wait<'a, 'b, T: Send>(lock: &'a Mutex<State<T>>,
NoneBlocked => {}
_ => unreachable!(),
}
drop(guard); // unlock
wait_token.wait(); // block
lock.lock() // relock
drop(guard); // unlock
wait_token.wait(); // block
lock.lock().unwrap() // relock
}

/// Wakes up a thread, dropping the lock at the correct time
Expand Down Expand Up @@ -161,7 +161,7 @@ impl<T: Send> Packet<T> {
fn acquire_send_slot(&self) -> MutexGuard<State<T>> {
let mut node = Node { token: None, next: 0 as *mut Node };
loop {
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();
// are we ready to go?
if guard.disconnected || guard.buf.size() < guard.buf.cap() {
return guard;
Expand Down Expand Up @@ -202,7 +202,7 @@ impl<T: Send> Packet<T> {
}

pub fn try_send(&self, t: T) -> Result<(), super::TrySendError<T>> {
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();
if guard.disconnected {
Err(super::RecvDisconnected(t))
} else if guard.buf.size() == guard.buf.cap() {
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<T: Send> Packet<T> {
// When reading this, remember that there can only ever be one receiver at
// time.
pub fn recv(&self) -> Result<T, ()> {
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();

// Wait for the buffer to have something in it. No need for a while loop
// because we're the only receiver.
Expand All @@ -258,7 +258,7 @@ impl<T: Send> Packet<T> {
}

pub fn try_recv(&self) -> Result<T, Failure> {
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();

// Easy cases first
if guard.disconnected { return Err(Disconnected) }
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<T: Send> Packet<T> {
}

// Not much to do other than wake up a receiver if one's there
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();
if guard.disconnected { return }
guard.disconnected = true;
match mem::replace(&mut guard.blocker, NoneBlocked) {
Expand All @@ -326,7 +326,7 @@ impl<T: Send> Packet<T> {
}

pub fn drop_port(&self) {
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();

if guard.disconnected { return }
guard.disconnected = true;
Expand Down Expand Up @@ -372,14 +372,14 @@ impl<T: Send> Packet<T> {
// If Ok, the value is whether this port has data, if Err, then the upgraded
// port needs to be checked instead of this one.
pub fn can_recv(&self) -> bool {
let guard = self.lock.lock();
let guard = self.lock.lock().unwrap();
guard.disconnected || guard.buf.size() > 0
}

// Attempts to start selection on this port. This can either succeed or fail
// because there is data waiting.
pub fn start_selection(&self, token: SignalToken) -> StartResult {
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();
if guard.disconnected || guard.buf.size() > 0 {
Abort
} else {
Expand All @@ -397,7 +397,7 @@ impl<T: Send> Packet<T> {
//
// The return value indicates whether there's data on this port.
pub fn abort_selection(&self) -> bool {
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();
match mem::replace(&mut guard.blocker, NoneBlocked) {
NoneBlocked => true,
BlockedSender(token) => {
Expand All @@ -413,7 +413,7 @@ impl<T: Send> Packet<T> {
impl<T: Send> Drop for Packet<T> {
fn drop(&mut self) {
assert_eq!(self.channels.load(atomic::SeqCst), 0);
let mut guard = self.lock.lock();
let mut guard = self.lock.lock().unwrap();
assert!(guard.queue.dequeue().is_none());
assert!(guard.canceled.is_none());
}
Expand Down
20 changes: 10 additions & 10 deletions src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl StdinReader {
/// ```
pub fn lock<'a>(&'a mut self) -> StdinReaderGuard<'a> {
StdinReaderGuard {
inner: self.inner.lock()
inner: self.inner.lock().unwrap()
}
}

Expand All @@ -155,53 +155,53 @@ impl StdinReader {
/// The read is performed atomically - concurrent read calls in other
/// threads will not interleave with this one.
pub fn read_line(&mut self) -> IoResult<String> {
self.inner.lock().0.read_line()
self.inner.lock().unwrap().0.read_line()
}

/// Like `Buffer::read_until`.
///
/// The read is performed atomically - concurrent read calls in other
/// threads will not interleave with this one.
pub fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> {
self.inner.lock().0.read_until(byte)
self.inner.lock().unwrap().0.read_until(byte)
}

/// Like `Buffer::read_char`.
///
/// The read is performed atomically - concurrent read calls in other
/// threads will not interleave with this one.
pub fn read_char(&mut self) -> IoResult<char> {
self.inner.lock().0.read_char()
self.inner.lock().unwrap().0.read_char()
}
}

impl Reader for StdinReader {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
self.inner.lock().0.read(buf)
self.inner.lock().unwrap().0.read(buf)
}

// We have to manually delegate all of these because the default impls call
// read more than once and we don't want those calls to interleave (or
// incur the costs of repeated locking).

fn read_at_least(&mut self, min: uint, buf: &mut [u8]) -> IoResult<uint> {
self.inner.lock().0.read_at_least(min, buf)
self.inner.lock().unwrap().0.read_at_least(min, buf)
}

fn push_at_least(&mut self, min: uint, len: uint, buf: &mut Vec<u8>) -> IoResult<uint> {
self.inner.lock().0.push_at_least(min, len, buf)
self.inner.lock().unwrap().0.push_at_least(min, len, buf)
}

fn read_to_end(&mut self) -> IoResult<Vec<u8>> {
self.inner.lock().0.read_to_end()
self.inner.lock().unwrap().0.read_to_end()
}

fn read_le_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
self.inner.lock().0.read_le_uint_n(nbytes)
self.inner.lock().unwrap().0.read_le_uint_n(nbytes)
}

fn read_be_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
self.inner.lock().0.read_be_uint_n(nbytes)
self.inner.lock().unwrap().0.read_be_uint_n(nbytes)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ impl Barrier {
/// Barriers are re-usable after all threads have rendezvoused once, and can
/// be used continuously.
pub fn wait(&self) {
let mut lock = self.lock.lock();
let mut lock = self.lock.lock().unwrap();
let local_gen = lock.generation_id;
lock.count += 1;
if lock.count < self.num_threads {
// We need a while loop to guard against spurious wakeups.
// http://en.wikipedia.org/wiki/Spurious_wakeup
while local_gen == lock.generation_id &&
lock.count < self.num_threads {
self.cvar.wait(&lock);
lock = self.cvar.wait(lock).unwrap();
}
} else {
lock.count = 0;
Expand Down
Loading