Skip to content

Commit 9668ab5

Browse files
committed
std: Move libnative task count bookkeeping to std
When using tasks in Rust, the expectation is that the runtime does not exit before all tasks have exited. This is enforced in libgreen through the `SchedPool` type, and it is enforced in libnative through a `bookkeeping` module and a global count/mutex pair. Unfortunately, this means that a process which originates with libgreen will not wait for spawned native tasks. In order to fix this problem, the bookkeeping module was moved from libnative to libstd so the runtime itself can wait for native tasks to exit. Green tasks do not manage themselves through this bookkeeping module, but native tasks will continue to manage themselves through this module. Closes #12684
1 parent e6acff8 commit 9668ab5

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

src/libnative/io/timer_helper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
//! time.
2222
2323
use std::cast;
24+
use std::rt::bookkeeping;
2425
use std::rt;
2526
use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
2627

27-
use bookkeeping;
2828
use io::timer::{Req, Shutdown};
2929
use task;
3030

src/libnative/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
use std::os;
5959
use std::rt;
6060

61-
mod bookkeeping;
6261
pub mod io;
6362
pub mod task;
6463

@@ -105,6 +104,5 @@ pub fn start(argc: int, argv: **u8, main: proc()) -> int {
105104
/// number of arguments.
106105
pub fn run(main: proc()) -> int {
107106
main();
108-
bookkeeping::wait_for_other_tasks();
109107
os::get_exit_status()
110108
}

src/libnative/task.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
1717
use std::any::Any;
1818
use std::cast;
19+
use std::rt::bookkeeping;
1920
use std::rt::env;
2021
use std::rt::local::Local;
2122
use std::rt::rtio;
23+
use std::rt::stack;
2224
use std::rt::task::{Task, BlockedTask, SendMessage};
2325
use std::rt::thread::Thread;
2426
use std::rt;
2527
use std::task::TaskOpts;
2628
use std::unstable::mutex::NativeMutex;
27-
use std::rt::stack;
2829

2930
use io;
3031
use task;
31-
use bookkeeping;
3232

3333
/// Creates a new Task which is ready to execute as a 1:1 task.
3434
pub fn new(stack_bounds: (uint, uint)) -> ~Task {

src/libnative/bookkeeping.rs src/libstd/rt/bookkeeping.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! 1:1 Task bookkeeping
11+
//! Task bookkeeping
1212
//!
13-
//! This module keeps track of the number of running 1:1 tasks so that entry
14-
//! points with libnative know when it's possible to exit the program (once all
15-
//! tasks have exited).
13+
//! This module keeps track of the number of running tasks so that entry points
14+
//! with libnative know when it's possible to exit the program (once all tasks
15+
//! have exited).
1616
//!
17-
//! The green counterpart for this is bookkeeping on sched pools.
17+
//! The green counterpart for this is bookkeeping on sched pools, and it's up to
18+
//! each respective runtime to make sure that they call increment() and
19+
//! decrement() manually.
1820
19-
use std::sync::atomics;
20-
use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
21+
#[experimental]; // this is a massive code smell
22+
#[doc(hidden)];
23+
24+
use sync::atomics;
25+
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
2126

2227
static mut TASK_COUNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
2328
static mut TASK_LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;

src/libstd/rt/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ pub mod args;
128128
// Support for running procedures when a program has exited.
129129
mod at_exit_imp;
130130

131+
// Bookkeeping for task counts
132+
pub mod bookkeeping;
133+
131134
// Stack overflow protection
132135
pub mod stack;
133136

@@ -207,6 +210,7 @@ pub fn at_exit(f: proc()) {
207210
/// Invoking cleanup while portions of the runtime are still in use may cause
208211
/// undefined behavior.
209212
pub unsafe fn cleanup() {
213+
bookkeeping::wait_for_other_tasks();
210214
at_exit_imp::run();
211215
args::cleanup();
212216
local_ptr::cleanup();

src/test/run-pass/issue-12684.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-fast
12+
13+
extern crate native;
14+
extern crate green;
15+
extern crate rustuv;
16+
17+
#[start]
18+
fn start(argc: int, argv: **u8) -> int { green::start(argc, argv, main) }
19+
20+
fn main() {
21+
native::task::spawn(proc() customtask());
22+
}
23+
24+
fn customtask() {
25+
let mut timer = std::io::timer::Timer::new().unwrap();
26+
let periodic = timer.periodic(10);
27+
periodic.recv();
28+
}

src/test/run-pass/issue-12699.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// ignore-fast
12+
1113
extern crate native;
1214

1315
use std::io::timer;

0 commit comments

Comments
 (0)