2013-02-03 18:15:43 -08:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
2013-10-17 01:40:33 -07:00
|
|
|
use cast;
|
2013-02-03 18:15:43 -08:00
|
|
|
use libc;
|
|
|
|
use ops::Drop;
|
2013-10-17 01:40:33 -07:00
|
|
|
use unstable::raw;
|
|
|
|
use uint;
|
2013-02-03 18:15:43 -08:00
|
|
|
|
|
|
|
#[allow(non_camel_case_types)] // runtime type
|
|
|
|
type raw_thread = libc::c_void;
|
|
|
|
|
2013-03-15 18:07:36 -07:00
|
|
|
pub struct Thread {
|
2013-10-20 06:03:09 +05:30
|
|
|
priv main: ~fn(),
|
|
|
|
priv raw_thread: *raw_thread,
|
|
|
|
priv joined: bool
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
impl Thread {
|
2013-10-17 01:40:33 -07:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn start(main: ~fn()) -> Thread {
|
2013-10-17 01:40:33 -07:00
|
|
|
// This is the starting point of rust os threads. The first thing we do
|
|
|
|
// is make sure that we don't trigger __morestack (also why this has a
|
|
|
|
// no_split_stack annotation), and then we re-build the main function
|
|
|
|
// and invoke it from there.
|
|
|
|
#[no_split_stack]
|
|
|
|
extern "C" fn thread_start(code: *(), env: *()) {
|
|
|
|
use rt::context;
|
|
|
|
unsafe {
|
|
|
|
context::record_stack_bounds(0, uint::max_value);
|
|
|
|
let f: &fn() = cast::transmute(raw::Closure {
|
|
|
|
code: code,
|
|
|
|
env: env,
|
|
|
|
});
|
|
|
|
f();
|
|
|
|
}
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
2013-10-17 01:40:33 -07:00
|
|
|
|
|
|
|
let raw_thread = unsafe {
|
|
|
|
let c: raw::Closure = cast::transmute_copy(&main);
|
|
|
|
let raw::Closure { code, env } = c;
|
|
|
|
rust_raw_thread_start(thread_start, code, env)
|
|
|
|
};
|
2013-02-03 18:15:43 -08:00
|
|
|
Thread {
|
|
|
|
main: main,
|
2013-10-17 01:40:33 -07:00
|
|
|
raw_thread: raw_thread,
|
|
|
|
joined: false,
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 12:05:15 -07:00
|
|
|
|
|
|
|
pub fn join(self) {
|
2013-08-14 21:41:40 -04:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
|
2013-07-27 12:05:15 -07:00
|
|
|
assert!(!self.joined);
|
|
|
|
let mut this = self;
|
|
|
|
unsafe { rust_raw_thread_join(this.raw_thread); }
|
|
|
|
this.joined = true;
|
|
|
|
}
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Thread {
|
2013-09-16 21:18:07 -04:00
|
|
|
fn drop(&mut self) {
|
2013-08-14 21:41:40 -04:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
|
2013-07-27 12:05:15 -07:00
|
|
|
assert!(self.joined);
|
|
|
|
unsafe { rust_raw_thread_delete(self.raw_thread) }
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
2013-10-17 01:40:33 -07:00
|
|
|
fn rust_raw_thread_start(f: extern "C" fn(*(), *()),
|
|
|
|
code: *(), env: *()) -> *raw_thread;
|
|
|
|
fn rust_raw_thread_join(thread: *raw_thread);
|
|
|
|
fn rust_raw_thread_delete(thread: *raw_thread);
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|