diff options
| author | Vivian Lim <[email protected]> | 2021-02-06 22:11:59 -0800 |
|---|---|---|
| committer | Vivian Lim <[email protected]> | 2021-02-06 22:11:59 -0800 |
| commit | 64423f0e34cc4a7d78c15b345b3b8f58243d8286 (patch) | |
| tree | cc20e2e7f0fc35abf470e20e61d3d48f0d954f3b /ctr-std/src/sys/cloudabi/thread.rs | |
| parent | Support libctru 2.0 (diff) | |
| download | ctru-rs-64423f0e34cc4a7d78c15b345b3b8f58243d8286.tar.xz ctru-rs-64423f0e34cc4a7d78c15b345b3b8f58243d8286.zip | |
Delete ctr-std to use my fork of the rust repo instead
Diffstat (limited to 'ctr-std/src/sys/cloudabi/thread.rs')
| -rw-r--r-- | ctr-std/src/sys/cloudabi/thread.rs | 126 |
1 files changed, 0 insertions, 126 deletions
diff --git a/ctr-std/src/sys/cloudabi/thread.rs b/ctr-std/src/sys/cloudabi/thread.rs deleted file mode 100644 index 8cca47e..0000000 --- a/ctr-std/src/sys/cloudabi/thread.rs +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright 2014-2018 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 boxed::FnBox; -use cmp; -use ffi::CStr; -use io; -use libc; -use mem; -use ptr; -use sys::cloudabi::abi; -use sys::time::dur2intervals; -use sys_common::thread::*; -use time::Duration; - -pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024; - -pub struct Thread { - id: libc::pthread_t, -} - -// CloudABI has 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<dyn FnBox() + 'a>) -> io::Result<Thread> { - let p = box p; - let mut native: libc::pthread_t = mem::zeroed(); - let mut attr: libc::pthread_attr_t = mem::zeroed(); - assert_eq!(libc::pthread_attr_init(&mut attr), 0); - - let stack_size = cmp::max(stack, min_stack_size(&attr)); - assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0); - - let ret = libc::pthread_create(&mut native, &attr, thread_start, &*p as *const _ as *mut _); - assert_eq!(libc::pthread_attr_destroy(&mut attr), 0); - - return if ret != 0 { - Err(io::Error::from_raw_os_error(ret)) - } else { - mem::forget(p); // ownership passed to pthread_create - Ok(Thread { id: native }) - }; - - extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void { - unsafe { - start_thread(main as *mut u8); - } - ptr::null_mut() - } - } - - pub fn yield_now() { - let ret = unsafe { abi::thread_yield() }; - debug_assert_eq!(ret, abi::errno::SUCCESS); - } - - pub fn set_name(_name: &CStr) { - // CloudABI has no way to set a thread name. - } - - pub fn sleep(dur: Duration) { - unsafe { - let subscription = abi::subscription { - type_: abi::eventtype::CLOCK, - union: abi::subscription_union { - clock: abi::subscription_clock { - clock_id: abi::clockid::MONOTONIC, - timeout: dur2intervals(&dur), - ..mem::zeroed() - }, - }, - ..mem::zeroed() - }; - let mut event: abi::event = mem::uninitialized(); - let mut nevents: usize = mem::uninitialized(); - let ret = abi::poll(&subscription, &mut event, 1, &mut nevents); - assert_eq!(ret, abi::errno::SUCCESS); - assert_eq!(event.error, abi::errno::SUCCESS); - } - } - - pub fn join(self) { - unsafe { - let ret = libc::pthread_join(self.id, ptr::null_mut()); - mem::forget(self); - assert!( - ret == 0, - "failed to join thread: {}", - io::Error::from_raw_os_error(ret) - ); - } - } -} - -impl Drop for Thread { - fn drop(&mut self) { - let ret = unsafe { libc::pthread_detach(self.id) }; - debug_assert_eq!(ret, 0); - } -} - -#[cfg_attr(test, allow(dead_code))] -pub mod guard { - pub type Guard = !; - pub unsafe fn current() -> Option<Guard> { - None - } - pub unsafe fn init() -> Option<Guard> { - None - } - pub unsafe fn deinit() {} -} - -fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { - libc::PTHREAD_STACK_MIN -} |