aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys/unix/thread.rs
diff options
context:
space:
mode:
authorFenrir <[email protected]>2018-01-21 14:06:28 -0700
committerFenrirWolf <[email protected]>2018-01-21 19:16:33 -0700
commit23be3f4885688e5e0011005e2295c75168854c0a (patch)
treedd0850f9c73c489e114a761d5c0757f3dbec3a65 /ctr-std/src/sys/unix/thread.rs
parentUpdate CI for Rust nightly-2017-12-01 + other fixes (diff)
downloadctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.tar.xz
ctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.zip
Recreate ctr-std from latest nightly
Diffstat (limited to 'ctr-std/src/sys/unix/thread.rs')
-rw-r--r--ctr-std/src/sys/unix/thread.rs53
1 files changed, 20 insertions, 33 deletions
diff --git a/ctr-std/src/sys/unix/thread.rs b/ctr-std/src/sys/unix/thread.rs
index 0b21bff..21224ad 100644
--- a/ctr-std/src/sys/unix/thread.rs
+++ b/ctr-std/src/sys/unix/thread.rs
@@ -1,4 +1,4 @@
-// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@@ -18,29 +18,28 @@ use ptr;
use sys_common::thread::start_thread;
use time::Duration;
-use libctru::{svcSleepThread, svcGetThreadPriority,
- threadCreate, threadJoin, threadFree, threadDetach,
- Thread as ThreadHandle};
+use libctru::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 {}
+pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
+
+
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, 0x10000);
+ let stack_size = cmp::max(stack, DEFAULT_MIN_STACK_SIZE);
let mut priority = 0;
- svcGetThreadPriority(&mut priority, 0xFFFF8000);
+ ::libctru::svcGetThreadPriority(&mut priority, 0xFFFF8000);
- let handle = threadCreate(Some(thread_func), &*p as *const _ as *mut _,
- stack_size, priority, -2, false);
+ let handle = ::libctru::threadCreate(Some(thread_func), &*p as *const _ as *mut _,
+ stack_size, priority, -2, false);
return if handle == ptr::null_mut() {
Err(io::Error::from_raw_os_error(libc::EAGAIN))
@@ -50,12 +49,14 @@ impl Thread {
};
extern "C" fn thread_func(start: *mut libc::c_void) {
- unsafe { start_thread(start) }
+ unsafe { start_thread(start as *mut u8) }
}
}
-
+
pub fn yield_now() {
- unsafe { svcSleepThread(0) }
+ unsafe {
+ ::libctru::svcSleepThread(0)
+ }
}
pub fn set_name(_name: &CStr) {
@@ -64,35 +65,21 @@ impl Thread {
pub fn sleep(dur: Duration) {
unsafe {
- let nanos = dur.as_secs() * 1_000_000_000 + dur.subsec_nanos() as u64;
- svcSleepThread(nanos as i64)
+ let nanos = dur.as_secs()
+ .saturating_mul(1_000_000_000)
+ .saturating_add(dur.subsec_nanos() as u64);
+ ::libctru::svcSleepThread(nanos as i64)
}
}
pub fn join(self) {
unsafe {
- let ret = threadJoin(self.handle, u64::max_value());
- threadFree(self.handle);
+ let ret = ::libctru::threadJoin(self.handle, u64::max_value());
+ ::libctru::threadFree(self.handle);
mem::forget(self);
debug_assert_eq!(ret, 0);
}
}
-
- pub fn id(&self) -> ThreadHandle {
- self.handle
- }
-
- pub fn into_id(self) -> ThreadHandle {
- let handle = self.handle;
- mem::forget(self);
- handle
- }
-}
-
-impl Drop for Thread {
- fn drop(&mut self) {
- unsafe { threadDetach(self.handle) }
- }
}
pub mod guard {