Skip to content

Commit 37bfef0

Browse files
committed
Implement thread
1 parent ea6f5aa commit 37bfef0

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/libstd/sys/redox/thread.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use io;
1515
use libc;
1616
use mem;
1717
use sys_common::thread::start_thread;
18+
use sys::cvt;
1819
use time::Duration;
1920

2021
pub struct Thread {
@@ -30,11 +31,15 @@ impl Thread {
3031
pub unsafe fn new<'a>(_stack: usize, p: Box<FnBox() + 'a>) -> io::Result<Thread> {
3132
let p = box p;
3233

33-
start_thread(&*p as *const _ as *mut _);
34-
35-
::sys_common::util::dumb_print(format_args!("thread\n"));
36-
37-
unimplemented!();
34+
let id = cvt(libc::clone(libc::CLONE_VM | libc::CLONE_FS | libc::CLONE_FILES))?;
35+
if id == 0 {
36+
start_thread(&*p as *const _ as *mut _);
37+
let _ = libc::exit(0);
38+
panic!("thread failed to exit");
39+
} else {
40+
mem::forget(p);
41+
Ok(Thread { id: id })
42+
}
3843
}
3944

4045
pub fn yield_now() {
@@ -69,8 +74,8 @@ impl Thread {
6974
}
7075

7176
pub fn join(self) {
72-
::sys_common::util::dumb_print(format_args!("Thread::join"));
73-
unimplemented!();
77+
let mut status = 0;
78+
libc::waitpid(self.id, &mut status, 0).unwrap();
7479
}
7580

7681
pub fn id(&self) -> libc::pid_t { self.id }
@@ -82,13 +87,6 @@ impl Thread {
8287
}
8388
}
8489

85-
impl Drop for Thread {
86-
fn drop(&mut self) {
87-
::sys_common::util::dumb_print(format_args!("Thread::drop"));
88-
unimplemented!();
89-
}
90-
}
91-
9290
pub mod guard {
9391
pub unsafe fn current() -> Option<usize> { None }
9492
pub unsafe fn init() -> Option<usize> { None }

0 commit comments

Comments
 (0)