Move atomically to unstable::sync, and document what it actually does. Close #7872.

This commit is contained in:
Ben Blum 2013-07-30 21:38:44 -04:00
parent 2e6dc161b6
commit bc7cee7bbf
5 changed files with 57 additions and 62 deletions

View file

@ -105,7 +105,7 @@ mod dl {
use path;
use ptr;
use str;
use task;
use unstable::sync::atomically;
use result::*;
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
@ -120,7 +120,7 @@ mod dl {
pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
unsafe {
do task::atomically {
do atomically {
let _old_error = dlerror();
let result = f();
@ -164,7 +164,7 @@ mod dl {
use libc;
use path;
use ptr;
use task;
use unstable::sync::atomically;
use result::*;
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
@ -181,7 +181,7 @@ mod dl {
pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
unsafe {
do task::atomically {
do atomically {
SetLastError(0);
let result = f();

View file

@ -85,7 +85,7 @@ fn test_run_in_bare_thread_exchange() {
pub fn change_dir_locked(p: &Path, action: &fn()) -> bool {
use os;
use os::change_dir;
use task;
use unstable::sync::atomically;
use unstable::finally::Finally;
unsafe {
@ -93,7 +93,7 @@ pub fn change_dir_locked(p: &Path, action: &fn()) -> bool {
// in the `action` callback can cause deadlock. Doing it in
// `task::atomically` to try to avoid that, but ... I don't know
// this is all bogus.
return do task::atomically {
return do atomically {
rust_take_change_dir_lock();
do (||{

View file

@ -16,7 +16,6 @@ use ptr;
use option::*;
use either::{Either, Left, Right};
use task;
use task::atomically;
use unstable::atomics::{AtomicOption,AtomicUint,Acquire,Release,SeqCst};
use unstable::finally::Finally;
use ops::Drop;
@ -271,6 +270,48 @@ impl<T> Drop for UnsafeAtomicRcBox<T>{
/****************************************************************************/
/**
* Enables a runtime assertion that no operation in the argument closure shall
* use scheduler operations (yield, recv, spawn, etc). This is for use with
* pthread mutexes, which may block the entire scheduler thread, rather than
* just one task, and is hence prone to deadlocks if mixed with yielding.
*
* NOTE: THIS DOES NOT PROVIDE LOCKING, or any sort of critical-section
* synchronization whatsoever. It only makes sense to use for CPU-local issues.
*/
// FIXME(#8140) should not be pub
pub unsafe fn atomically<U>(f: &fn() -> U) -> U {
use rt::task::Task;
use task::rt;
use rt::local::Local;
use rt::{context, OldTaskContext, TaskContext};
match context() {
OldTaskContext => {
let t = rt::rust_get_task();
do (|| {
rt::rust_task_inhibit_kill(t);
rt::rust_task_inhibit_yield(t);
f()
}).finally {
rt::rust_task_allow_yield(t);
rt::rust_task_allow_kill(t);
}
}
TaskContext => {
let t = Local::unsafe_borrow::<Task>();
do (|| {
(*t).death.inhibit_yield();
f()
}).finally {
(*t).death.allow_yield();
}
}
// FIXME(#3095): As in unkillable().
_ => f()
}
}
#[allow(non_camel_case_types)] // runtime type
type rust_little_lock = *libc::c_void;
@ -395,11 +436,18 @@ mod tests {
use cell::Cell;
use comm;
use option::*;
use super::{Exclusive, UnsafeAtomicRcBox};
use super::{Exclusive, UnsafeAtomicRcBox, atomically};
use task;
use uint;
use util;
#[test]
fn test_atomically() {
// NB. The whole runtime will abort on an 'atomic-sleep' violation,
// so we can't really test for the converse behaviour.
unsafe { do atomically { } } task::yield(); // oughtn't fail
}
#[test]
fn exclusive_new_arc() {
unsafe {