2014-01-15 12:24:42 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-12-30 02:55:27 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-03-05 12:35:30 -06:00
|
|
|
//! Task bookkeeping
|
2013-12-30 02:55:27 -06:00
|
|
|
//!
|
2014-03-05 12:35:30 -06:00
|
|
|
//! This module keeps track of the number of running tasks so that entry points
|
|
|
|
//! with libnative know when it's possible to exit the program (once all tasks
|
|
|
|
//! have exited).
|
2013-12-30 02:55:27 -06:00
|
|
|
//!
|
2014-03-05 12:35:30 -06:00
|
|
|
//! The green counterpart for this is bookkeeping on sched pools, and it's up to
|
|
|
|
//! each respective runtime to make sure that they call increment() and
|
|
|
|
//! decrement() manually.
|
2013-12-30 02:55:27 -06:00
|
|
|
|
2014-03-05 12:35:30 -06:00
|
|
|
#[experimental]; // this is a massive code smell
|
|
|
|
#[doc(hidden)];
|
|
|
|
|
|
|
|
use sync::atomics;
|
|
|
|
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
|
2013-12-30 02:55:27 -06:00
|
|
|
|
|
|
|
static mut TASK_COUNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
|
2014-02-14 18:18:49 -06:00
|
|
|
static mut TASK_LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2013-12-30 02:55:27 -06:00
|
|
|
|
|
|
|
pub fn increment() {
|
2014-01-30 16:28:28 -06:00
|
|
|
let _ = unsafe { TASK_COUNT.fetch_add(1, atomics::SeqCst) };
|
2013-12-30 02:55:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decrement() {
|
|
|
|
unsafe {
|
|
|
|
if TASK_COUNT.fetch_sub(1, atomics::SeqCst) == 1 {
|
2014-02-13 00:17:50 -06:00
|
|
|
let mut guard = TASK_LOCK.lock();
|
|
|
|
guard.signal();
|
2013-12-30 02:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for all other native tasks in the system to exit. This is only used by
|
|
|
|
/// the entry points of native programs
|
|
|
|
pub fn wait_for_other_tasks() {
|
|
|
|
unsafe {
|
2014-03-04 20:20:46 -06:00
|
|
|
let mut guard = TASK_LOCK.lock();
|
|
|
|
while TASK_COUNT.load(atomics::SeqCst) > 0 {
|
|
|
|
guard.wait();
|
2013-12-30 02:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|