1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
// 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.
#[allow(non_camel_case_types)];
use cast;
use libc;
use ops::Drop;
use uint;
use ptr;
#[cfg(windows)]
use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T,
LPVOID, DWORD, LPDWORD, HANDLE};
#[cfg(windows)] type rust_thread = HANDLE;
#[cfg(unix)] type rust_thread = libc::pthread_t;
pub struct Thread {
priv native: rust_thread,
priv joined: bool
}
static DEFAULT_STACK_SIZE: libc::size_t = 1024*1024;
#[cfg(windows)] type rust_thread_return = DWORD;
#[cfg(unix)] type rust_thread_return = *libc::c_void;
impl Thread {
pub fn start(main: proc()) -> Thread {
// 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 extract the main function
// and invoke it.
#[no_split_stack]
extern "C" fn thread_start(trampoline: *libc::c_void) -> rust_thread_return {
use rt::context;
unsafe {
context::record_stack_bounds(0, uint::max_value);
let f: ~proc() = cast::transmute(trampoline);
(*f)();
}
unsafe { cast::transmute(0) }
}
let native = native_thread_create(thread_start, ~main);
Thread {
native: native,
joined: false,
}
}
pub fn join(mut self) {
assert!(!self.joined);
native_thread_join(self.native);
self.joined = true;
}
}
#[cfg(windows)]
fn native_thread_create(thread_start: extern "C" fn(*libc::c_void) -> rust_thread_return,
tramp: ~proc()) -> rust_thread {
unsafe {
let ptr: *mut libc::c_void = cast::transmute(tramp);
CreateThread(ptr::mut_null(), DEFAULT_STACK_SIZE, thread_start, ptr, 0, ptr::mut_null())
}
}
#[cfg(windows)]
fn native_thread_join(native: rust_thread) {
use libc::consts::os::extra::INFINITE;
unsafe { WaitForSingleObject(native, INFINITE); }
}
#[cfg(unix)]
fn native_thread_create(thread_start: extern "C" fn(*libc::c_void) -> rust_thread_return,
tramp: ~proc()) -> rust_thread {
use unstable::intrinsics;
let mut native: libc::pthread_t = unsafe { intrinsics::uninit() };
unsafe {
use libc::consts::os::posix01::PTHREAD_CREATE_JOINABLE;
let mut attr: libc::pthread_attr_t = intrinsics::uninit();
assert!(pthread_attr_init(&mut attr) == 0);
assert!(pthread_attr_setstacksize(&mut attr, DEFAULT_STACK_SIZE) == 0);
assert!(pthread_attr_setdetachstate(&mut attr, PTHREAD_CREATE_JOINABLE) == 0);
let ptr: *libc::c_void = cast::transmute(tramp);
assert!(pthread_create(&mut native, &attr, thread_start, ptr) == 0);
}
native
}
#[cfg(unix)]
fn native_thread_join(native: rust_thread) {
unsafe { assert!(pthread_join(native, ptr::null()) == 0) }
}
impl Drop for Thread {
fn drop(&mut self) {
assert!(self.joined);
}
}
#[cfg(windows)]
extern "system" {
fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T,
lpStartAddress: extern "C" fn(*libc::c_void) -> rust_thread_return,
lpParameter: LPVOID, dwCreationFlags: DWORD, lpThreadId: LPDWORD) -> HANDLE;
fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
}
#[cfg(unix)]
extern {
fn pthread_create(native: *mut libc::pthread_t, attr: *libc::pthread_attr_t,
f: extern "C" fn(*libc::c_void) -> rust_thread_return,
value: *libc::c_void) -> libc::c_int;
fn pthread_join(native: libc::pthread_t, value: **libc::c_void) -> libc::c_int;
fn pthread_attr_init(attr: *mut libc::pthread_attr_t) -> libc::c_int;
fn pthread_attr_setstacksize(attr: *mut libc::pthread_attr_t,
stack_size: libc::size_t) -> libc::c_int;
fn pthread_attr_setdetachstate(attr: *mut libc::pthread_attr_t,
state: libc::c_int) -> libc::c_int;
}
|