diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-13 17:17:50 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-16 10:13:56 +1100 |
| commit | 76a59fd6e2d5c8c42193c047fd5eaba982d499f7 (patch) | |
| tree | a913c967de98b492f47fdd0bbd5a11cf0be96ed5 /src/libnative | |
| parent | fba32ea79f1828ef441d91abca3635fad57f323d (diff) | |
| download | rust-76a59fd6e2d5c8c42193c047fd5eaba982d499f7.tar.gz rust-76a59fd6e2d5c8c42193c047fd5eaba982d499f7.zip | |
std: add an RAII unlocker to Mutex.
This automatically unlocks its lock when it goes out of scope, and provides a safe(ish) method to call .wait.
Diffstat (limited to 'src/libnative')
| -rw-r--r-- | src/libnative/bookkeeping.rs | 14 | ||||
| -rw-r--r-- | src/libnative/io/net.rs | 3 | ||||
| -rw-r--r-- | src/libnative/io/timer_helper.rs | 3 | ||||
| -rw-r--r-- | src/libnative/task.rs | 15 |
4 files changed, 15 insertions, 20 deletions
diff --git a/src/libnative/bookkeeping.rs b/src/libnative/bookkeeping.rs index 868586b3691..e0aaf20e838 100644 --- a/src/libnative/bookkeeping.rs +++ b/src/libnative/bookkeeping.rs @@ -29,9 +29,8 @@ pub fn increment() { pub fn decrement() { unsafe { if TASK_COUNT.fetch_sub(1, atomics::SeqCst) == 1 { - TASK_LOCK.lock(); - TASK_LOCK.signal(); - TASK_LOCK.unlock(); + let mut guard = TASK_LOCK.lock(); + guard.signal(); } } } @@ -40,11 +39,12 @@ pub fn decrement() { /// the entry points of native programs pub fn wait_for_other_tasks() { unsafe { - TASK_LOCK.lock(); - while TASK_COUNT.load(atomics::SeqCst) > 0 { - TASK_LOCK.wait(); + { + let mut guard = TASK_LOCK.lock(); + while TASK_COUNT.load(atomics::SeqCst) > 0 { + guard.wait(); + } } - TASK_LOCK.unlock(); TASK_LOCK.destroy(); } } diff --git a/src/libnative/io/net.rs b/src/libnative/io/net.rs index ec6a5c5cb9b..1de729aee2e 100644 --- a/src/libnative/io/net.rs +++ b/src/libnative/io/net.rs @@ -222,7 +222,7 @@ pub fn init() { static mut INITIALIZED: bool = false; static mut LOCK: Mutex = MUTEX_INIT; - LOCK.lock(); + let _guard = LOCK.lock(); if !INITIALIZED { let mut data: WSADATA = mem::init(); let ret = WSAStartup(0x202, // version 2.2 @@ -230,7 +230,6 @@ pub fn init() { assert_eq!(ret, 0); INITIALIZED = true; } - LOCK.unlock(); } } diff --git a/src/libnative/io/timer_helper.rs b/src/libnative/io/timer_helper.rs index 2c976e67d25..8ddce2c3990 100644 --- a/src/libnative/io/timer_helper.rs +++ b/src/libnative/io/timer_helper.rs @@ -41,7 +41,7 @@ pub fn boot(helper: fn(imp::signal, Port<Req>)) { static mut INITIALIZED: bool = false; unsafe { - LOCK.lock(); + let mut _guard = LOCK.lock(); if !INITIALIZED { let (msgp, msgc) = Chan::new(); // promote this to a shared channel @@ -58,7 +58,6 @@ pub fn boot(helper: fn(imp::signal, Port<Req>)) { rt::at_exit(proc() { shutdown() }); INITIALIZED = true; } - LOCK.unlock(); } } diff --git a/src/libnative/task.rs b/src/libnative/task.rs index a9c3afbbb16..d940edcadc7 100644 --- a/src/libnative/task.rs +++ b/src/libnative/task.rs @@ -191,20 +191,19 @@ impl rt::Runtime for Ops { let task = BlockedTask::block(cur_task); if times == 1 { - (*me).lock.lock(); + let mut guard = (*me).lock.lock(); (*me).awoken = false; match f(task) { Ok(()) => { while !(*me).awoken { - (*me).lock.wait(); + guard.wait(); } } Err(task) => { cast::forget(task.wake()); } } - (*me).lock.unlock(); } else { let mut iter = task.make_selectable(times); - (*me).lock.lock(); + let mut guard = (*me).lock.lock(); (*me).awoken = false; let success = iter.all(|task| { match f(task) { @@ -216,9 +215,8 @@ impl rt::Runtime for Ops { } }); while success && !(*me).awoken { - (*me).lock.wait(); + guard.wait(); } - (*me).lock.unlock(); } // re-acquire ownership of the task cur_task = cast::transmute::<uint, ~Task>(cur_task_dupe); @@ -235,10 +233,9 @@ impl rt::Runtime for Ops { let me = &mut *self as *mut Ops; to_wake.put_runtime(self as ~rt::Runtime); cast::forget(to_wake); - (*me).lock.lock(); + let mut guard = (*me).lock.lock(); (*me).awoken = true; - (*me).lock.signal(); - (*me).lock.unlock(); + guard.signal(); } } |
