diff options
| author | Fenrir <[email protected]> | 2017-03-05 00:25:16 -0700 |
|---|---|---|
| committer | Fenrir <[email protected]> | 2017-03-05 02:19:40 -0700 |
| commit | 5299b505b79c48e788067d66a727636ff933de92 (patch) | |
| tree | e882dbf4591a81bdefd7c721038eac3562eaacaf /ctr-std/src/sys_common | |
| parent | Merge pull request #24 from FenrirWolf/unit_type (diff) | |
| download | ctru-rs-5299b505b79c48e788067d66a727636ff933de92.tar.xz ctru-rs-5299b505b79c48e788067d66a727636ff933de92.zip | |
Initial thread support
Diffstat (limited to 'ctr-std/src/sys_common')
| -rw-r--r-- | ctr-std/src/sys_common/condvar.rs | 70 | ||||
| -rw-r--r-- | ctr-std/src/sys_common/mod.rs | 5 | ||||
| -rw-r--r-- | ctr-std/src/sys_common/rwlock.rs | 82 | ||||
| -rw-r--r-- | ctr-std/src/sys_common/thread.rs | 22 | ||||
| -rw-r--r-- | ctr-std/src/sys_common/thread_info.rs | 61 | ||||
| -rw-r--r-- | ctr-std/src/sys_common/util.rs | 49 |
6 files changed, 289 insertions, 0 deletions
diff --git a/ctr-std/src/sys_common/condvar.rs b/ctr-std/src/sys_common/condvar.rs new file mode 100644 index 0000000..b6f29dd --- /dev/null +++ b/ctr-std/src/sys_common/condvar.rs @@ -0,0 +1,70 @@ +// Copyright 2014 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 time::Duration; +use sys_common::mutex::{self, Mutex}; +use sys::condvar as imp; + +/// An OS-based condition variable. +/// +/// This structure is the lowest layer possible on top of the OS-provided +/// condition variables. It is consequently entirely unsafe to use. It is +/// recommended to use the safer types at the top level of this crate instead of +/// this type. +pub struct Condvar(imp::Condvar); + +impl Condvar { + /// Creates a new condition variable for use. + /// + /// Behavior is undefined if the condition variable is moved after it is + /// first used with any of the functions below. + pub const fn new() -> Condvar { Condvar(imp::Condvar::new()) } + + /// Prepares the condition variable for use. + /// + /// This should be called once the condition variable is at a stable memory + /// address. + #[inline] + pub unsafe fn init(&mut self) { self.0.init() } + + /// Signals one waiter on this condition variable to wake up. + #[inline] + pub unsafe fn notify_one(&self) { self.0.notify_one() } + + /// Awakens all current waiters on this condition variable. + #[inline] + pub unsafe fn notify_all(&self) { self.0.notify_all() } + + /// Waits for a signal on the specified mutex. + /// + /// Behavior is undefined if the mutex is not locked by the current thread. + /// Behavior is also undefined if more than one mutex is used concurrently + /// on this condition variable. + #[inline] + pub unsafe fn wait(&self, mutex: &Mutex) { self.0.wait(mutex::raw(mutex)) } + + /// Waits for a signal on the specified mutex with a timeout duration + /// specified by `dur` (a relative time into the future). + /// + /// Behavior is undefined if the mutex is not locked by the current thread. + /// Behavior is also undefined if more than one mutex is used concurrently + /// on this condition variable. + #[inline] + pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { + self.0.wait_timeout(mutex::raw(mutex), dur) + } + + /// Deallocates all resources associated with this condition variable. + /// + /// Behavior is undefined if there are current or will be future users of + /// this condition variable. + #[inline] + pub unsafe fn destroy(&self) { self.0.destroy() } +} diff --git a/ctr-std/src/sys_common/mod.rs b/ctr-std/src/sys_common/mod.rs index 7aedb69..936ff80 100644 --- a/ctr-std/src/sys_common/mod.rs +++ b/ctr-std/src/sys_common/mod.rs @@ -25,11 +25,16 @@ #![allow(missing_docs)] pub mod at_exit_imp; +pub mod condvar; pub mod io; pub mod mutex; pub mod poison; pub mod remutex; +pub mod rwlock; +pub mod thread; +pub mod thread_info; pub mod thread_local; +pub mod util; // common error constructors diff --git a/ctr-std/src/sys_common/rwlock.rs b/ctr-std/src/sys_common/rwlock.rs new file mode 100644 index 0000000..71a4f01 --- /dev/null +++ b/ctr-std/src/sys_common/rwlock.rs @@ -0,0 +1,82 @@ +// Copyright 2014 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 sys::rwlock as imp; + +/// An OS-based reader-writer lock. +/// +/// This structure is entirely unsafe and serves as the lowest layer of a +/// cross-platform binding of system rwlocks. It is recommended to use the +/// safer types at the top level of this crate instead of this type. +pub struct RWLock(imp::RWLock); + +impl RWLock { + /// Creates a new reader-writer lock for use. + /// + /// Behavior is undefined if the reader-writer lock is moved after it is + /// first used with any of the functions below. + pub const fn new() -> RWLock { RWLock(imp::RWLock::new()) } + + /// Acquires shared access to the underlying lock, blocking the current + /// thread to do so. + /// + /// Behavior is undefined if the rwlock has been moved between this and any + /// previous method call. + #[inline] + pub unsafe fn read(&self) { self.0.read() } + + /// Attempts to acquire shared access to this lock, returning whether it + /// succeeded or not. + /// + /// This function does not block the current thread. + /// + /// Behavior is undefined if the rwlock has been moved between this and any + /// previous method call. + #[inline] + pub unsafe fn try_read(&self) -> bool { self.0.try_read() } + + /// Acquires write access to the underlying lock, blocking the current thread + /// to do so. + /// + /// Behavior is undefined if the rwlock has been moved between this and any + /// previous method call. + #[inline] + pub unsafe fn write(&self) { self.0.write() } + + /// Attempts to acquire exclusive access to this lock, returning whether it + /// succeeded or not. + /// + /// This function does not block the current thread. + /// + /// Behavior is undefined if the rwlock has been moved between this and any + /// previous method call. + #[inline] + pub unsafe fn try_write(&self) -> bool { self.0.try_write() } + + /// Unlocks previously acquired shared access to this lock. + /// + /// Behavior is undefined if the current thread does not have shared access. + #[inline] + pub unsafe fn read_unlock(&self) { self.0.read_unlock() } + + /// Unlocks previously acquired exclusive access to this lock. + /// + /// Behavior is undefined if the current thread does not currently have + /// exclusive access. + #[inline] + pub unsafe fn write_unlock(&self) { self.0.write_unlock() } + + /// Destroys OS-related resources with this RWLock. + /// + /// Behavior is undefined if there are any currently active users of this + /// lock. + #[inline] + pub unsafe fn destroy(&self) { self.0.destroy() } +} diff --git a/ctr-std/src/sys_common/thread.rs b/ctr-std/src/sys_common/thread.rs new file mode 100644 index 0000000..bb6baae --- /dev/null +++ b/ctr-std/src/sys_common/thread.rs @@ -0,0 +1,22 @@ +// Copyright 2014 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 sys::stack_overflow; + +pub unsafe fn start_thread(main: *mut libc::c_void) { + // Next, set up our stack overflow handler which may get triggered if we run + // out of stack. + // let _handler = stack_overflow::Handler::new(); + + // Finally, let's run some code. + Box::from_raw(main as *mut Box<FnBox()>)() +} diff --git a/ctr-std/src/sys_common/thread_info.rs b/ctr-std/src/sys_common/thread_info.rs new file mode 100644 index 0000000..95d8b6c --- /dev/null +++ b/ctr-std/src/sys_common/thread_info.rs @@ -0,0 +1,61 @@ +// Copyright 2014 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(dead_code)] // stack_guard isn't used right now on all platforms + +use cell::RefCell; +use thread::Thread; +use thread::LocalKeyState; + +struct ThreadInfo { + stack_guard: Option<usize>, + thread: Thread, +} + +thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) } + +impl ThreadInfo { + fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R { + if THREAD_INFO.state() == LocalKeyState::Destroyed { + return None + } + + THREAD_INFO.with(move |c| { + if c.borrow().is_none() { + *c.borrow_mut() = Some(ThreadInfo { + stack_guard: None, + thread: NewThread::new(None), + }) + } + Some(f(c.borrow_mut().as_mut().unwrap())) + }) + } +} + +pub fn current_thread() -> Option<Thread> { + ThreadInfo::with(|info| info.thread.clone()) +} + +pub fn stack_guard() -> Option<usize> { + ThreadInfo::with(|info| info.stack_guard).and_then(|o| o) +} + +pub fn set(stack_guard: Option<usize>, thread: Thread) { + THREAD_INFO.with(|c| assert!(c.borrow().is_none())); + THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo{ + stack_guard: stack_guard, + thread: thread, + })); +} + +// a hack to get around privacy restrictions; implemented by `std::thread` +pub trait NewThread { + fn new(name: Option<String>) -> Self; +} diff --git a/ctr-std/src/sys_common/util.rs b/ctr-std/src/sys_common/util.rs new file mode 100644 index 0000000..aad0680 --- /dev/null +++ b/ctr-std/src/sys_common/util.rs @@ -0,0 +1,49 @@ +// 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. + +use fmt; +use io::prelude::*; +use sync::atomic::{self, Ordering}; +use sys::stdio::Stderr; + +pub fn min_stack() -> usize { + static MIN: atomic::AtomicUsize = atomic::AtomicUsize::new(0); + match MIN.load(Ordering::SeqCst) { + 0 => {} + n => return n - 1, + } + + // NOTE: We don't have env variable support on the 3DS so let's just use the + // default minimum + + // let amt = env::var("RUST_MIN_STACK").ok().and_then(|s| s.parse().ok()); + // let amt = amt.unwrap_or(2 * 1024 * 1024); + + let amt = 2 * 1024 * 1024; + + // 0 is our sentinel value, so ensure that we'll never see 0 after + // initialization has run + MIN.store(amt + 1, Ordering::SeqCst); + amt +} + +pub fn dumb_print(args: fmt::Arguments) { + let _ = Stderr::new().map(|mut stderr| stderr.write_fmt(args)); +} + +// Other platforms should use the appropriate platform-specific mechanism for +// aborting the process. If no platform-specific mechanism is available, +// ::intrinsics::abort() may be used instead. The above implementations cover +// all targets currently supported by libstd. + +pub fn abort(args: fmt::Arguments) -> ! { + dumb_print(format_args!("fatal runtime error: {}\n", args)); + unsafe { ::sys::abort_internal(); } +} |