aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys
diff options
context:
space:
mode:
authorFenrir <[email protected]>2017-07-26 01:53:40 -0600
committerFenrir <[email protected]>2017-07-26 01:53:40 -0600
commit361c812e6aba4a414304457fb9170014159e4978 (patch)
tree6ac98462bd1eb469b2b09623719142bc87d62861 /ctr-std/src/sys
parentMerge pull request #36 from FenrirWolf/errDisp (diff)
downloadctru-rs-361c812e6aba4a414304457fb9170014159e4978.tar.xz
ctru-rs-361c812e6aba4a414304457fb9170014159e4978.zip
Thread fixes + module update
Diffstat (limited to 'ctr-std/src/sys')
-rw-r--r--ctr-std/src/sys/unix/fast_thread_local.rs19
-rw-r--r--ctr-std/src/sys/unix/thread.rs30
2 files changed, 29 insertions, 20 deletions
diff --git a/ctr-std/src/sys/unix/fast_thread_local.rs b/ctr-std/src/sys/unix/fast_thread_local.rs
index 6eeae2d..9f0eee0 100644
--- a/ctr-std/src/sys/unix/fast_thread_local.rs
+++ b/ctr-std/src/sys/unix/fast_thread_local.rs
@@ -12,9 +12,10 @@
#![unstable(feature = "thread_local_internals", issue = "0")]
use cell::{Cell, UnsafeCell};
-use intrinsics;
+use mem;
use ptr;
+
pub struct Key<T> {
inner: UnsafeCell<Option<T>>,
@@ -37,7 +38,7 @@ impl<T> Key<T> {
pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
unsafe {
- if intrinsics::needs_drop::<T>() && self.dtor_running.get() {
+ if mem::needs_drop::<T>() && self.dtor_running.get() {
return None
}
self.register_dtor();
@@ -46,7 +47,7 @@ impl<T> Key<T> {
}
unsafe fn register_dtor(&self) {
- if !intrinsics::needs_drop::<T>() || self.dtor_registered.get() {
+ if !mem::needs_drop::<T>() || self.dtor_registered.get() {
return
}
@@ -56,7 +57,7 @@ impl<T> Key<T> {
}
}
-unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
+pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
// The fallback implementation uses a vanilla OS-based TLS key to track
// the list of destructors that need to be run for this thread. The key
// then has its own destructor which runs all the other destructors.
@@ -96,17 +97,17 @@ pub unsafe extern fn destroy_value<T>(ptr: *mut u8) {
// `None`.
(*ptr).dtor_running.set(true);
- // The OSX implementation of TLS apparently had an odd aspect to it
+ // The macOS implementation of TLS apparently had an odd aspect to it
// where the pointer we have may be overwritten while this destructor
// is running. Specifically if a TLS destructor re-accesses TLS it may
// trigger a re-initialization of all TLS variables, paving over at
// least some destroyed ones with initial values.
//
- // This means that if we drop a TLS value in place on OSX that we could
+ // This means that if we drop a TLS value in place on macOS that we could
// revert the value to its original state halfway through the
// destructor, which would be bad!
//
- // Hence, we use `ptr::read` on OSX (to move to a "safe" location)
+ // Hence, we use `ptr::read` on macOS (to move to a "safe" location)
// instead of drop_in_place.
if cfg!(target_os = "macos") {
ptr::read((*ptr).inner.get());
@@ -114,3 +115,7 @@ pub unsafe extern fn destroy_value<T>(ptr: *mut u8) {
ptr::drop_in_place((*ptr).inner.get());
}
}
+
+pub fn requires_move_before_drop() -> bool {
+ false
+}
diff --git a/ctr-std/src/sys/unix/thread.rs b/ctr-std/src/sys/unix/thread.rs
index a7178a3..0b21bff 100644
--- a/ctr-std/src/sys/unix/thread.rs
+++ b/ctr-std/src/sys/unix/thread.rs
@@ -18,9 +18,9 @@ use ptr;
use sys_common::thread::start_thread;
use time::Duration;
-use libctru::{svcSleepThread, svcGetThreadPriority};
-use libctru::{threadCreate, threadJoin, threadFree};
-use libctru::Thread as ThreadHandle;
+use libctru::{svcSleepThread, svcGetThreadPriority,
+ threadCreate, threadJoin, threadFree, threadDetach,
+ Thread as ThreadHandle};
pub struct Thread {
handle: ThreadHandle,
@@ -36,12 +36,8 @@ impl Thread {
let p = box p;
let stack_size = cmp::max(stack, 0x10000);
- // this retrieves the main thread's priority value. child threads need
- // to be spawned with a greater priority (smaller priority value) than
- // the main thread
let mut priority = 0;
svcGetThreadPriority(&mut priority, 0xFFFF8000);
- priority -= 1;
let handle = threadCreate(Some(thread_func), &*p as *const _ as *mut _,
stack_size, priority, -2, false);
@@ -59,11 +55,11 @@ impl Thread {
}
pub fn yield_now() {
- unimplemented!()
+ unsafe { svcSleepThread(0) }
}
pub fn set_name(_name: &CStr) {
- // can't set thread names on the 3DS
+ // threads aren't named in libctru
}
pub fn sleep(dur: Duration) {
@@ -82,12 +78,20 @@ impl Thread {
}
}
- pub fn id(&self) -> usize {
- unimplemented!()
+ pub fn id(&self) -> ThreadHandle {
+ self.handle
}
- pub fn into_id(self) -> usize {
- unimplemented!()
+ 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) }
}
}