aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys/unix/thread.rs
diff options
context:
space:
mode:
authorFenrir <[email protected]>2017-03-05 00:25:16 -0700
committerFenrir <[email protected]>2017-03-05 02:19:40 -0700
commit5299b505b79c48e788067d66a727636ff933de92 (patch)
treee882dbf4591a81bdefd7c721038eac3562eaacaf /ctr-std/src/sys/unix/thread.rs
parentMerge pull request #24 from FenrirWolf/unit_type (diff)
downloadarchived-ctru-rs-5299b505b79c48e788067d66a727636ff933de92.tar.xz
archived-ctru-rs-5299b505b79c48e788067d66a727636ff933de92.zip
Initial thread support
Diffstat (limited to 'ctr-std/src/sys/unix/thread.rs')
-rw-r--r--ctr-std/src/sys/unix/thread.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/ctr-std/src/sys/unix/thread.rs b/ctr-std/src/sys/unix/thread.rs
new file mode 100644
index 0000000..4eac345
--- /dev/null
+++ b/ctr-std/src/sys/unix/thread.rs
@@ -0,0 +1,90 @@
+// Copyright 2016 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.
+
+use alloc::boxed::FnBox;
+use libc;
+use cmp;
+use ffi::CStr;
+use io;
+use mem;
+use ptr;
+use sys_common::thread::start_thread;
+use time::Duration;
+
+use libctru::svc::svcSleepThread;
+use libctru::thread::{threadCreate, threadJoin, threadFree};
+use libctru::thread::Thread as ThreadHandle;
+
+pub struct Thread {
+ handle: ThreadHandle,
+}
+
+// Some platforms may have pthread_t as a pointer in which case we still want
+// a thread to be Send/Sync
+unsafe impl Send for Thread {}
+unsafe impl Sync for Thread {}
+
+impl Thread {
+ pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>) -> io::Result<Thread> {
+ let p = box p;
+ let stack_size = cmp::max(stack, 4 * 1024);
+
+ let handle = threadCreate(Some(thread_func), &*p as *const _ as *mut _,
+ stack_size, 0x29, -2, 0);
+
+ return if handle == ptr::null_mut() {
+ Err(io::Error::from_raw_os_error(libc::EAGAIN))
+ } else {
+ mem::forget(p); // ownership passed to the new thread
+ Ok(Thread { handle: handle })
+ };
+
+ extern "C" fn thread_func(start: *mut libc::c_void) {
+ unsafe { start_thread(start) }
+ }
+ }
+
+ pub fn yield_now() {
+ unimplemented!()
+ }
+
+ pub fn set_name(_name: &CStr) {
+ // can't set thread names on the 3DS
+ }
+
+ pub fn sleep(dur: Duration) {
+ unsafe {
+ let nanos = dur.as_secs() * 1_000_000_000 + dur.subsec_nanos() as u64;
+ svcSleepThread(nanos as i64)
+ }
+ }
+
+ pub fn join(self) {
+ unsafe {
+ let ret = threadJoin(self.handle, u64::max_value());
+ threadFree(self.handle);
+ mem::forget(self);
+ debug_assert_eq!(ret, 0);
+ }
+ }
+
+ pub fn id(&self) -> usize {
+ unimplemented!()
+ }
+
+ pub fn into_id(self) -> usize {
+ unimplemented!()
+ }
+}
+
+pub mod guard {
+ pub unsafe fn current() -> Option<usize> { None }
+ pub unsafe fn init() -> Option<usize> { None }
+}