2012-08-14 18:25:42 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
2012-10-03 16:52:09 -05:00
|
|
|
// tjc: Re-forbid deprecated modes once a snapshot fixes the
|
|
|
|
// function problem
|
2012-08-14 18:25:42 -05:00
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-04-04 23:17:50 -05:00
|
|
|
#[doc(hidden)];
|
|
|
|
|
2012-09-04 13:12:17 -05:00
|
|
|
use compare_and_swap = rustrt::rust_compare_and_swap_ptr;
|
|
|
|
use task::TaskBuilder;
|
2012-09-18 16:51:40 -05:00
|
|
|
use task::atomically;
|
2012-04-06 19:03:00 -05:00
|
|
|
|
2012-07-03 18:11:00 -05:00
|
|
|
extern mod rustrt {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-04-06 19:03:00 -05:00
|
|
|
fn rust_task_weaken(ch: rust_port_id);
|
|
|
|
fn rust_task_unweaken(ch: rust_port_id);
|
2012-09-18 16:51:40 -05:00
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_atomic_increment(p: &mut libc::intptr_t)
|
|
|
|
-> libc::intptr_t;
|
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_atomic_decrement(p: &mut libc::intptr_t)
|
|
|
|
-> libc::intptr_t;
|
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_compare_and_swap_ptr(address: &mut libc::uintptr_t,
|
|
|
|
oldval: libc::uintptr_t,
|
|
|
|
newval: libc::uintptr_t) -> bool;
|
|
|
|
|
|
|
|
fn rust_create_little_lock() -> rust_little_lock;
|
|
|
|
fn rust_destroy_little_lock(lock: rust_little_lock);
|
|
|
|
fn rust_lock_little_lock(lock: rust_little_lock);
|
|
|
|
fn rust_unlock_little_lock(lock: rust_little_lock);
|
2012-04-04 23:17:50 -05:00
|
|
|
}
|
|
|
|
|
2012-09-18 16:51:40 -05:00
|
|
|
#[allow(non_camel_case_types)] // runtime type
|
|
|
|
type rust_port_id = uint;
|
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
type GlobalPtr = *libc::uintptr_t;
|
2012-04-04 23:17:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Atomically gets a channel from a pointer to a pointer-sized memory location
|
|
|
|
* or, if no channel exists creates and installs a new channel and sets up a
|
|
|
|
* new task to receive from it.
|
|
|
|
*/
|
2012-09-26 19:20:14 -05:00
|
|
|
pub unsafe fn chan_from_global_ptr<T: Send>(
|
2012-08-13 18:20:27 -05:00
|
|
|
global: GlobalPtr,
|
2012-08-15 16:10:46 -05:00
|
|
|
task_fn: fn() -> task::TaskBuilder,
|
2012-10-04 21:58:31 -05:00
|
|
|
f: fn~(comm::Port<T>)
|
2012-08-15 16:10:46 -05:00
|
|
|
) -> comm::Chan<T> {
|
2012-04-04 23:17:50 -05:00
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
enum Msg {
|
|
|
|
Proceed,
|
|
|
|
Abort
|
2012-04-04 23:17:50 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug,~"ENTERING chan_from_global_ptr, before is_prob_zero check");
|
2012-04-04 23:17:50 -05:00
|
|
|
let is_probably_zero = *global == 0u;
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug,~"after is_prob_zero check");
|
2012-04-04 23:17:50 -05:00
|
|
|
if is_probably_zero {
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug,~"is probably zero...");
|
2012-04-04 23:17:50 -05:00
|
|
|
// There's no global channel. We must make it
|
|
|
|
|
2012-08-08 15:38:26 -05:00
|
|
|
let (setup_po, setup_ch) = do task_fn().spawn_conversation
|
2012-09-10 18:31:00 -05:00
|
|
|
|move f, setup_po, setup_ch| {
|
2012-08-27 16:22:25 -05:00
|
|
|
let po = comm::Port::<T>();
|
2012-10-03 16:38:01 -05:00
|
|
|
let ch = comm::Chan(&po);
|
2012-04-04 23:17:50 -05:00
|
|
|
comm::send(setup_ch, ch);
|
|
|
|
|
|
|
|
// Wait to hear if we are the official instance of
|
|
|
|
// this global task
|
2012-08-13 18:20:27 -05:00
|
|
|
match comm::recv::<Msg>(setup_po) {
|
2012-09-10 18:31:00 -05:00
|
|
|
Proceed => f(move po),
|
2012-08-13 18:20:27 -05:00
|
|
|
Abort => ()
|
2012-04-04 23:17:50 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug,~"before setup recv..");
|
2012-04-04 23:17:50 -05:00
|
|
|
// This is the proposed global channel
|
|
|
|
let ch = comm::recv(setup_po);
|
|
|
|
// 0 is our sentinal value. It is not a valid channel
|
2012-09-12 17:39:28 -05:00
|
|
|
assert *ch != 0;
|
2012-04-04 23:17:50 -05:00
|
|
|
|
|
|
|
// Install the channel
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug,~"BEFORE COMPARE AND SWAP");
|
2012-04-04 23:17:50 -05:00
|
|
|
let swapped = compare_and_swap(
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::reinterpret_cast(&global),
|
|
|
|
0u, cast::reinterpret_cast(&ch));
|
2012-08-22 19:24:52 -05:00
|
|
|
log(debug,fmt!("AFTER .. swapped? %?", swapped));
|
2012-04-04 23:17:50 -05:00
|
|
|
|
|
|
|
if swapped {
|
|
|
|
// Success!
|
2012-08-13 18:20:27 -05:00
|
|
|
comm::send(setup_ch, Proceed);
|
2012-04-04 23:17:50 -05:00
|
|
|
ch
|
|
|
|
} else {
|
|
|
|
// Somebody else got in before we did
|
2012-08-13 18:20:27 -05:00
|
|
|
comm::send(setup_ch, Abort);
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::reinterpret_cast(&*global)
|
2012-04-04 23:17:50 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug, ~"global != 0");
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::reinterpret_cast(&*global)
|
2012-04-04 23:17:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn test_from_global_chan1() {
|
2012-04-04 23:17:50 -05:00
|
|
|
|
|
|
|
// This is unreadable, right?
|
|
|
|
|
|
|
|
// The global channel
|
2012-09-28 23:51:14 -05:00
|
|
|
let globchan = 0;
|
2012-10-03 16:38:01 -05:00
|
|
|
let globchanp = ptr::addr_of(&globchan);
|
2012-04-04 23:17:50 -05:00
|
|
|
|
|
|
|
// Create the global channel, attached to a new task
|
2012-06-24 22:18:18 -05:00
|
|
|
let ch = unsafe {
|
2012-07-23 18:53:22 -05:00
|
|
|
do chan_from_global_ptr(globchanp, task::task) |po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
let ch = comm::recv(po);
|
|
|
|
comm::send(ch, true);
|
|
|
|
let ch = comm::recv(po);
|
|
|
|
comm::send(ch, true);
|
|
|
|
}
|
2012-04-04 23:17:50 -05:00
|
|
|
};
|
|
|
|
// Talk to it
|
2012-08-27 16:22:25 -05:00
|
|
|
let po = comm::Port();
|
2012-10-03 16:38:01 -05:00
|
|
|
comm::send(ch, comm::Chan(&po));
|
2012-04-04 23:17:50 -05:00
|
|
|
assert comm::recv(po) == true;
|
|
|
|
|
|
|
|
// This one just reuses the previous channel
|
2012-06-24 22:18:18 -05:00
|
|
|
let ch = unsafe {
|
2012-07-23 18:53:22 -05:00
|
|
|
do chan_from_global_ptr(globchanp, task::task) |po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
let ch = comm::recv(po);
|
|
|
|
comm::send(ch, false);
|
|
|
|
}
|
2012-04-04 23:17:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Talk to the original global task
|
2012-08-27 16:22:25 -05:00
|
|
|
let po = comm::Port();
|
2012-10-03 16:38:01 -05:00
|
|
|
comm::send(ch, comm::Chan(&po));
|
2012-04-04 23:17:50 -05:00
|
|
|
assert comm::recv(po) == true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn test_from_global_chan2() {
|
2012-04-04 23:17:50 -05:00
|
|
|
|
2012-09-28 23:51:14 -05:00
|
|
|
for iter::repeat(100) {
|
2012-04-04 23:17:50 -05:00
|
|
|
// The global channel
|
2012-09-28 23:51:14 -05:00
|
|
|
let globchan = 0;
|
2012-10-03 16:38:01 -05:00
|
|
|
let globchanp = ptr::addr_of(&globchan);
|
2012-04-04 23:17:50 -05:00
|
|
|
|
2012-08-27 16:22:25 -05:00
|
|
|
let resultpo = comm::Port();
|
2012-10-03 16:38:01 -05:00
|
|
|
let resultch = comm::Chan(&resultpo);
|
2012-04-04 23:17:50 -05:00
|
|
|
|
|
|
|
// Spawn a bunch of tasks that all want to compete to
|
|
|
|
// create the global channel
|
2012-09-28 23:51:14 -05:00
|
|
|
for uint::range(0, 10) |i| {
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::spawn {
|
2012-06-24 22:18:18 -05:00
|
|
|
let ch = unsafe {
|
2012-06-26 15:55:56 -05:00
|
|
|
do chan_from_global_ptr(
|
2012-07-23 18:53:22 -05:00
|
|
|
globchanp, task::task) |po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
|
2012-09-28 23:51:14 -05:00
|
|
|
for uint::range(0, 10) |_j| {
|
2012-06-24 22:18:18 -05:00
|
|
|
let ch = comm::recv(po);
|
|
|
|
comm::send(ch, {i});
|
|
|
|
}
|
2012-04-04 23:17:50 -05:00
|
|
|
}
|
|
|
|
};
|
2012-08-27 16:22:25 -05:00
|
|
|
let po = comm::Port();
|
2012-10-03 16:38:01 -05:00
|
|
|
comm::send(ch, comm::Chan(&po));
|
2012-08-27 16:22:25 -05:00
|
|
|
// We are The winner if our version of the
|
2012-04-04 23:17:50 -05:00
|
|
|
// task was installed
|
|
|
|
let winner = comm::recv(po);
|
|
|
|
comm::send(resultch, winner == i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// There should be only one winner
|
|
|
|
let mut winners = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, 10u) |_i| {
|
2012-04-04 23:17:50 -05:00
|
|
|
let res = comm::recv(resultpo);
|
|
|
|
if res { winners += 1u };
|
|
|
|
}
|
|
|
|
assert winners == 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Convert the current task to a 'weak' task temporarily
|
|
|
|
*
|
|
|
|
* As a weak task it will not be counted towards the runtime's set
|
|
|
|
* of live tasks. When there are no more outstanding live (non-weak) tasks
|
|
|
|
* the runtime will send an exit message on the provided channel.
|
|
|
|
*
|
|
|
|
* This function is super-unsafe. Do not use.
|
|
|
|
*
|
|
|
|
* # Safety notes
|
|
|
|
*
|
|
|
|
* * Weak tasks must either die on their own or exit upon receipt of
|
|
|
|
* the exit message. Failure to do so will cause the runtime to never
|
|
|
|
* exit
|
|
|
|
* * Tasks must not call `weaken_task` multiple times. This will
|
|
|
|
* break the kernel's accounting of live tasks.
|
|
|
|
* * Weak tasks must not be supervised. A supervised task keeps
|
|
|
|
* a reference to its parent, so the parent will not die.
|
|
|
|
*/
|
2012-09-26 19:20:14 -05:00
|
|
|
pub unsafe fn weaken_task(f: fn(comm::Port<()>)) {
|
2012-08-27 16:22:25 -05:00
|
|
|
let po = comm::Port();
|
2012-10-03 16:38:01 -05:00
|
|
|
let ch = comm::Chan(&po);
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-09-18 19:34:08 -05:00
|
|
|
rustrt::rust_task_weaken(cast::reinterpret_cast(&ch));
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
let _unweaken = Unweaken(ch);
|
2012-04-06 19:03:00 -05:00
|
|
|
f(po);
|
|
|
|
|
2012-08-15 20:46:55 -05:00
|
|
|
struct Unweaken {
|
2012-09-06 21:40:15 -05:00
|
|
|
ch: comm::Chan<()>,
|
2012-06-21 23:46:43 -05:00
|
|
|
drop unsafe {
|
2012-09-18 19:34:08 -05:00
|
|
|
rustrt::rust_task_unweaken(cast::reinterpret_cast(&self.ch));
|
2012-06-21 23:46:43 -05:00
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
}
|
2012-09-04 17:23:28 -05:00
|
|
|
|
|
|
|
fn Unweaken(ch: comm::Chan<()>) -> Unweaken {
|
|
|
|
Unweaken {
|
|
|
|
ch: ch
|
|
|
|
}
|
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn test_weaken_task_then_unweaken() {
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::try {
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-06-30 18:19:07 -05:00
|
|
|
do weaken_task |_po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn test_weaken_task_wait() {
|
2012-07-23 14:53:18 -05:00
|
|
|
do task::spawn_unlinked {
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-06-30 18:19:07 -05:00
|
|
|
do weaken_task |po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
comm::recv(po);
|
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn test_weaken_task_stress() {
|
2012-04-06 19:03:00 -05:00
|
|
|
// Create a bunch of weak tasks
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat(100u) {
|
|
|
|
do task::spawn {
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-06-30 18:19:07 -05:00
|
|
|
do weaken_task |_po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 14:53:18 -05:00
|
|
|
do task::spawn_unlinked {
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-06-30 18:19:07 -05:00
|
|
|
do weaken_task |po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
// Wait for it to tell us to die
|
|
|
|
comm::recv(po);
|
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[ignore(cfg(windows))]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn test_weaken_task_fail() {
|
2012-07-04 14:04:28 -05:00
|
|
|
let res = do task::try {
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-06-30 18:19:07 -05:00
|
|
|
do weaken_task |_po| {
|
2012-06-24 22:18:18 -05:00
|
|
|
fail;
|
|
|
|
}
|
2012-04-06 19:03:00 -05:00
|
|
|
}
|
|
|
|
};
|
2012-09-25 18:23:04 -05:00
|
|
|
assert result::is_err(&res);
|
2012-05-24 16:49:39 -05:00
|
|
|
}
|
2012-09-18 16:51:40 -05:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Shared state & exclusive ARC
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
// An unwrapper uses this protocol to communicate with the "other" task that
|
|
|
|
// drops the last refcount on an arc. Unfortunately this can't be a proper
|
|
|
|
// pipe protocol because the unwrapper has to access both stages at once.
|
|
|
|
type UnwrapProto = ~mut Option<(pipes::ChanOne<()>, pipes::PortOne<bool>)>;
|
|
|
|
|
|
|
|
struct ArcData<T> {
|
|
|
|
mut count: libc::intptr_t,
|
|
|
|
mut unwrapper: libc::uintptr_t, // either a UnwrapProto or 0
|
|
|
|
// FIXME(#3224) should be able to make this non-option to save memory, and
|
|
|
|
// in unwrap() use "let ~ArcData { data: result, _ } = thing" to unwrap it
|
|
|
|
mut data: Option<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ArcDestruct<T> {
|
|
|
|
mut data: *libc::c_void,
|
|
|
|
drop unsafe {
|
|
|
|
if self.data.is_null() {
|
|
|
|
return; // Happens when destructing an unwrapper's handle.
|
|
|
|
}
|
|
|
|
do task::unkillable {
|
2012-09-18 19:34:08 -05:00
|
|
|
let data: ~ArcData<T> = cast::reinterpret_cast(&self.data);
|
2012-09-18 16:51:40 -05:00
|
|
|
let new_count = rustrt::rust_atomic_decrement(&mut data.count);
|
|
|
|
assert new_count >= 0;
|
|
|
|
if new_count == 0 {
|
|
|
|
// Were we really last, or should we hand off to an unwrapper?
|
|
|
|
// It's safe to not xchg because the unwrapper will set the
|
|
|
|
// unwrap lock *before* dropping his/her reference. In effect,
|
|
|
|
// being here means we're the only *awake* task with the data.
|
|
|
|
if data.unwrapper != 0 {
|
|
|
|
let p: UnwrapProto =
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::reinterpret_cast(&data.unwrapper);
|
2012-09-18 16:51:40 -05:00
|
|
|
let (message, response) = option::swap_unwrap(p);
|
|
|
|
// Send 'ready' and wait for a response.
|
|
|
|
pipes::send_one(move message, ());
|
|
|
|
// Unkillable wait. Message guaranteed to come.
|
|
|
|
if pipes::recv_one(move response) {
|
|
|
|
// Other task got the data.
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::forget(move data);
|
2012-09-18 16:51:40 -05:00
|
|
|
} else {
|
|
|
|
// Other task was killed. drop glue takes over.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// drop glue takes over.
|
|
|
|
}
|
|
|
|
} else {
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::forget(move data);
|
2012-09-18 16:51:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ArcDestruct<T>(data: *libc::c_void) -> ArcDestruct<T> {
|
|
|
|
ArcDestruct {
|
|
|
|
data: data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
pub unsafe fn unwrap_shared_mutable_state<T: Send>(rc: SharedMutableState<T>)
|
2012-09-18 16:51:40 -05:00
|
|
|
-> T {
|
|
|
|
struct DeathThroes<T> {
|
|
|
|
mut ptr: Option<~ArcData<T>>,
|
|
|
|
mut response: Option<pipes::ChanOne<bool>>,
|
|
|
|
drop unsafe {
|
|
|
|
let response = option::swap_unwrap(&mut self.response);
|
|
|
|
// In case we get killed early, we need to tell the person who
|
|
|
|
// tried to wake us whether they should hand-off the data to us.
|
|
|
|
if task::failing() {
|
|
|
|
pipes::send_one(move response, false);
|
|
|
|
// Either this swap_unwrap or the one below (at "Got here")
|
|
|
|
// ought to run.
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::forget(option::swap_unwrap(&mut self.ptr));
|
2012-09-18 16:51:40 -05:00
|
|
|
} else {
|
|
|
|
assert self.ptr.is_none();
|
|
|
|
pipes::send_one(move response, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do task::unkillable {
|
2012-09-18 19:34:08 -05:00
|
|
|
let ptr: ~ArcData<T> = cast::reinterpret_cast(&rc.data);
|
2012-09-18 16:51:40 -05:00
|
|
|
let (c1,p1) = pipes::oneshot(); // ()
|
|
|
|
let (c2,p2) = pipes::oneshot(); // bool
|
|
|
|
let server: UnwrapProto = ~mut Some((move c1,move p2));
|
2012-09-18 19:34:08 -05:00
|
|
|
let serverp: libc::uintptr_t = cast::transmute(move server);
|
2012-09-18 16:51:40 -05:00
|
|
|
// Try to put our server end in the unwrapper slot.
|
|
|
|
if rustrt::rust_compare_and_swap_ptr(&mut ptr.unwrapper, 0, serverp) {
|
|
|
|
// Got in. Step 0: Tell destructor not to run. We are now it.
|
|
|
|
rc.data = ptr::null();
|
|
|
|
// Step 1 - drop our own reference.
|
|
|
|
let new_count = rustrt::rust_atomic_decrement(&mut ptr.count);
|
|
|
|
assert new_count >= 0;
|
|
|
|
if new_count == 0 {
|
|
|
|
// We were the last owner. Can unwrap immediately.
|
|
|
|
// Also we have to free the server endpoints.
|
2012-09-18 19:34:08 -05:00
|
|
|
let _server: UnwrapProto = cast::transmute(move serverp);
|
2012-09-18 16:51:40 -05:00
|
|
|
option::swap_unwrap(&mut ptr.data)
|
|
|
|
// drop glue takes over.
|
|
|
|
} else {
|
|
|
|
// The *next* person who sees the refcount hit 0 will wake us.
|
|
|
|
let end_result =
|
|
|
|
DeathThroes { ptr: Some(move ptr),
|
|
|
|
response: Some(move c2) };
|
|
|
|
let mut p1 = Some(move p1); // argh
|
|
|
|
do task::rekillable {
|
|
|
|
pipes::recv_one(option::swap_unwrap(&mut p1));
|
|
|
|
}
|
|
|
|
// Got here. Back in the 'unkillable' without getting killed.
|
|
|
|
// Recover ownership of ptr, then take the data out.
|
|
|
|
let ptr = option::swap_unwrap(&mut end_result.ptr);
|
|
|
|
option::swap_unwrap(&mut ptr.data)
|
|
|
|
// drop glue takes over.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Somebody else was trying to unwrap. Avoid guaranteed deadlock.
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::forget(move ptr);
|
2012-09-18 16:51:40 -05:00
|
|
|
// Also we have to free the (rejected) server endpoints.
|
2012-09-18 19:34:08 -05:00
|
|
|
let _server: UnwrapProto = cast::transmute(move serverp);
|
2012-09-18 16:51:40 -05:00
|
|
|
fail ~"Another task is already unwrapping this ARC!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* COMPLETELY UNSAFE. Used as a primitive for the safe versions in std::arc.
|
|
|
|
*
|
|
|
|
* Data races between tasks can result in crashes and, with sufficient
|
|
|
|
* cleverness, arbitrary type coercion.
|
|
|
|
*/
|
2012-09-26 19:20:14 -05:00
|
|
|
pub type SharedMutableState<T: Send> = ArcDestruct<T>;
|
2012-09-18 16:51:40 -05:00
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
pub unsafe fn shared_mutable_state<T: Send>(data: T) ->
|
2012-09-26 19:20:14 -05:00
|
|
|
SharedMutableState<T> {
|
2012-09-18 16:51:40 -05:00
|
|
|
let data = ~ArcData { count: 1, unwrapper: 0, data: Some(move data) };
|
|
|
|
unsafe {
|
2012-09-18 19:34:08 -05:00
|
|
|
let ptr = cast::transmute(move data);
|
2012-09-18 16:51:40 -05:00
|
|
|
ArcDestruct(ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub unsafe fn get_shared_mutable_state<T: Send>(rc: &a/SharedMutableState<T>)
|
2012-09-18 16:51:40 -05:00
|
|
|
-> &a/mut T {
|
|
|
|
unsafe {
|
2012-09-18 19:34:08 -05:00
|
|
|
let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
|
2012-09-18 16:51:40 -05:00
|
|
|
assert ptr.count > 0;
|
|
|
|
// Cast us back into the correct region
|
2012-09-18 19:34:08 -05:00
|
|
|
let r = cast::transmute_region(option::get_ref(&ptr.data));
|
|
|
|
cast::forget(move ptr);
|
|
|
|
return cast::transmute_mut(r);
|
2012-09-18 16:51:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub unsafe fn get_shared_immutable_state<T: Send>(
|
|
|
|
rc: &a/SharedMutableState<T>) -> &a/T {
|
2012-09-18 16:51:40 -05:00
|
|
|
unsafe {
|
2012-09-18 19:34:08 -05:00
|
|
|
let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
|
2012-09-18 16:51:40 -05:00
|
|
|
assert ptr.count > 0;
|
|
|
|
// Cast us back into the correct region
|
2012-09-18 19:34:08 -05:00
|
|
|
let r = cast::transmute_region(option::get_ref(&ptr.data));
|
|
|
|
cast::forget(move ptr);
|
2012-09-18 16:51:40 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 19:20:14 -05:00
|
|
|
pub unsafe fn clone_shared_mutable_state<T: Send>(rc: &SharedMutableState<T>)
|
2012-09-18 16:51:40 -05:00
|
|
|
-> SharedMutableState<T> {
|
|
|
|
unsafe {
|
2012-09-18 19:34:08 -05:00
|
|
|
let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
|
2012-09-18 16:51:40 -05:00
|
|
|
let new_count = rustrt::rust_atomic_increment(&mut ptr.count);
|
|
|
|
assert new_count >= 2;
|
2012-09-18 19:34:08 -05:00
|
|
|
cast::forget(move ptr);
|
2012-09-18 16:51:40 -05:00
|
|
|
}
|
|
|
|
ArcDestruct((*rc).data)
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
|
|
#[allow(non_camel_case_types)] // runtime type
|
|
|
|
type rust_little_lock = *libc::c_void;
|
|
|
|
|
|
|
|
struct LittleLock {
|
|
|
|
l: rust_little_lock,
|
|
|
|
drop { rustrt::rust_destroy_little_lock(self.l); }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn LittleLock() -> LittleLock {
|
|
|
|
LittleLock {
|
|
|
|
l: rustrt::rust_create_little_lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LittleLock {
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn lock<T>(f: fn() -> T) -> T {
|
|
|
|
struct Unlock {
|
|
|
|
l: rust_little_lock,
|
|
|
|
drop { rustrt::rust_unlock_little_lock(self.l); }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn Unlock(l: rust_little_lock) -> Unlock {
|
|
|
|
Unlock {
|
|
|
|
l: l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do atomically {
|
|
|
|
rustrt::rust_lock_little_lock(self.l);
|
|
|
|
let _r = Unlock(self.l);
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ExData<T: Send> { lock: LittleLock, mut failed: bool, mut data: T, }
|
|
|
|
/**
|
|
|
|
* An arc over mutable data that is protected by a lock. For library use only.
|
|
|
|
*/
|
2012-09-26 19:20:14 -05:00
|
|
|
pub struct Exclusive<T: Send> { x: SharedMutableState<ExData<T>> }
|
2012-09-18 16:51:40 -05:00
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
pub fn exclusive<T:Send >(user_data: T) -> Exclusive<T> {
|
2012-09-18 16:51:40 -05:00
|
|
|
let data = ExData {
|
|
|
|
lock: LittleLock(), mut failed: false, mut data: user_data
|
|
|
|
};
|
|
|
|
Exclusive { x: unsafe { shared_mutable_state(move data) } }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Send> Exclusive<T> {
|
|
|
|
// Duplicate an exclusive ARC, as std::arc::clone.
|
|
|
|
fn clone() -> Exclusive<T> {
|
|
|
|
Exclusive { x: unsafe { clone_shared_mutable_state(&self.x) } }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exactly like std::arc::mutex_arc,access(), but with the little_lock
|
|
|
|
// instead of a proper mutex. Same reason for being unsafe.
|
|
|
|
//
|
|
|
|
// Currently, scheduling operations (i.e., yielding, receiving on a pipe,
|
|
|
|
// accessing the provided condition variable) are prohibited while inside
|
|
|
|
// the exclusive. Supporting that is a work in progress.
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn with<U>(f: fn(x: &mut T) -> U) -> U {
|
|
|
|
let rec = unsafe { get_shared_mutable_state(&self.x) };
|
|
|
|
do rec.lock.lock {
|
|
|
|
if rec.failed {
|
|
|
|
fail ~"Poisoned exclusive - another task failed inside!";
|
|
|
|
}
|
|
|
|
rec.failed = true;
|
|
|
|
let result = f(&mut rec.data);
|
|
|
|
rec.failed = false;
|
|
|
|
move result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn with_imm<U>(f: fn(x: &T) -> U) -> U {
|
|
|
|
do self.with |x| {
|
2012-09-18 19:34:08 -05:00
|
|
|
f(cast::transmute_immut(x))
|
2012-09-18 16:51:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(#2585) make this a by-move method on the exclusive
|
2012-10-02 13:37:37 -05:00
|
|
|
pub fn unwrap_exclusive<T: Send>(arc: Exclusive<T>) -> T {
|
2012-09-18 16:51:40 -05:00
|
|
|
let Exclusive { x: x } <- arc;
|
|
|
|
let inner = unsafe { unwrap_shared_mutable_state(move x) };
|
|
|
|
let ExData { data: data, _ } <- inner;
|
|
|
|
move data
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub mod tests {
|
2012-09-18 16:51:40 -05:00
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn exclusive_arc() {
|
2012-09-18 16:51:40 -05:00
|
|
|
let mut futures = ~[];
|
|
|
|
|
|
|
|
let num_tasks = 10u;
|
|
|
|
let count = 10u;
|
|
|
|
|
|
|
|
let total = exclusive(~mut 0u);
|
|
|
|
|
|
|
|
for uint::range(0u, num_tasks) |_i| {
|
|
|
|
let total = total.clone();
|
2012-09-26 19:33:34 -05:00
|
|
|
futures.push(future::spawn(|| {
|
2012-09-18 16:51:40 -05:00
|
|
|
for uint::range(0u, count) |_i| {
|
|
|
|
do total.with |count| {
|
|
|
|
**count += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
for futures.each |f| { f.get() }
|
|
|
|
|
|
|
|
do total.with |total| {
|
|
|
|
assert **total == num_tasks * count
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn exclusive_poison() {
|
2012-09-18 16:51:40 -05:00
|
|
|
// Tests that if one task fails inside of an exclusive, subsequent
|
|
|
|
// accesses will also fail.
|
|
|
|
let x = exclusive(1);
|
|
|
|
let x2 = x.clone();
|
|
|
|
do task::try {
|
|
|
|
do x2.with |one| {
|
|
|
|
assert *one == 2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
do x.with |one| {
|
|
|
|
assert *one == 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn exclusive_unwrap_basic() {
|
2012-09-18 16:51:40 -05:00
|
|
|
let x = exclusive(~~"hello");
|
|
|
|
assert unwrap_exclusive(x) == ~~"hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn exclusive_unwrap_contended() {
|
2012-09-18 16:51:40 -05:00
|
|
|
let x = exclusive(~~"hello");
|
|
|
|
let x2 = ~mut Some(x.clone());
|
|
|
|
do task::spawn {
|
|
|
|
let x2 = option::swap_unwrap(x2);
|
|
|
|
do x2.with |_hello| { }
|
|
|
|
task::yield();
|
|
|
|
}
|
|
|
|
assert unwrap_exclusive(x) == ~~"hello";
|
|
|
|
|
|
|
|
// Now try the same thing, but with the child task blocking.
|
|
|
|
let x = exclusive(~~"hello");
|
|
|
|
let x2 = ~mut Some(x.clone());
|
|
|
|
let mut res = None;
|
|
|
|
do task::task().future_result(|+r| res = Some(r)).spawn {
|
|
|
|
let x2 = option::swap_unwrap(x2);
|
|
|
|
assert unwrap_exclusive(x2) == ~~"hello";
|
|
|
|
}
|
|
|
|
// Have to get rid of our reference before blocking.
|
|
|
|
{ let _x = move x; } // FIXME(#3161) util::ignore doesn't work here
|
|
|
|
let res = option::swap_unwrap(&mut res);
|
|
|
|
future::get(&res);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn exclusive_unwrap_conflict() {
|
2012-09-18 16:51:40 -05:00
|
|
|
let x = exclusive(~~"hello");
|
|
|
|
let x2 = ~mut Some(x.clone());
|
|
|
|
let mut res = None;
|
|
|
|
do task::task().future_result(|+r| res = Some(r)).spawn {
|
|
|
|
let x2 = option::swap_unwrap(x2);
|
|
|
|
assert unwrap_exclusive(x2) == ~~"hello";
|
|
|
|
}
|
|
|
|
assert unwrap_exclusive(x) == ~~"hello";
|
|
|
|
let res = option::swap_unwrap(&mut res);
|
|
|
|
future::get(&res);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[ignore(cfg(windows))]
|
2012-09-26 19:20:14 -05:00
|
|
|
pub fn exclusive_unwrap_deadlock() {
|
2012-09-18 16:51:40 -05:00
|
|
|
// This is not guaranteed to get to the deadlock before being killed,
|
|
|
|
// but it will show up sometimes, and if the deadlock were not there,
|
|
|
|
// the test would nondeterministically fail.
|
|
|
|
let result = do task::try {
|
|
|
|
// a task that has two references to the same exclusive will
|
|
|
|
// deadlock when it unwraps. nothing to be done about that.
|
|
|
|
let x = exclusive(~~"hello");
|
|
|
|
let x2 = x.clone();
|
|
|
|
do task::spawn {
|
|
|
|
for 10.times { task::yield(); } // try to let the unwrapper go
|
|
|
|
fail; // punt it awake from its deadlock
|
|
|
|
}
|
|
|
|
let _z = unwrap_exclusive(x);
|
|
|
|
do x2.with |_hello| { }
|
|
|
|
};
|
|
|
|
assert result.is_err();
|
|
|
|
}
|
|
|
|
}
|