From c82ce62c9be0fdc86a4998fa03549a36915edb4e Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Sun, 6 Feb 2022 20:14:34 -0800 Subject: [PATCH] Add back basic pthread threading support Had to fix a few compile errors since we switched to using ctru_sys directly. This is also just the basic support that was there before. More work needs to be done to support std. --- src/lib.rs | 58 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 16dc8b3..a19c3de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,48 +13,78 @@ pub fn init() {} #[no_mangle] pub unsafe extern "C" fn pthread_create( - _native: *mut libc::pthread_t, - _attr: *const libc::pthread_attr_t, + native: *mut libc::pthread_t, + attr: *const libc::pthread_attr_t, _f: extern "C" fn(_: *mut libc::c_void) -> *mut libc::c_void, - _value: *mut libc::c_void, + value: *mut libc::c_void, ) -> libc::c_int { - 1 + let mut priority = 0; + ctru_sys::svcGetThreadPriority(&mut priority, 0xFFFF8000); + + extern "C" fn thread_start(main: *mut libc::c_void) { + unsafe { + Box::from_raw(main as *mut Box)(); + } + } + + let handle = ctru_sys::threadCreate( + Some(thread_start), + value, + *(attr as *mut ctru_sys::size_t), + priority, + -2, + false, + ); + + *native = handle as _; + + 0 } #[no_mangle] pub unsafe extern "C" fn pthread_join( - _native: libc::pthread_t, + native: libc::pthread_t, _value: *mut *mut libc::c_void, ) -> libc::c_int { - 1 + ctru_sys::threadJoin(native as ctru_sys::Thread, u64::max_value()); + ctru_sys::threadFree(native as ctru_sys::Thread); + + 0 } #[no_mangle] -pub unsafe extern "C" fn pthread_detach(_thread: libc::pthread_t) -> libc::c_int { - 1 +pub unsafe extern "C" fn pthread_detach(thread: libc::pthread_t) -> libc::c_int { + ctru_sys::threadDetach(thread as _); + + 0 } #[no_mangle] pub unsafe extern "C" fn pthread_attr_init(_attr: *mut libc::pthread_attr_t) -> libc::c_int { - 1 + 0 } #[no_mangle] pub unsafe extern "C" fn pthread_attr_destroy(_attr: *mut libc::pthread_attr_t) -> libc::c_int { - 1 + 0 } #[no_mangle] pub unsafe extern "C" fn pthread_attr_setstacksize( - _attr: *mut libc::pthread_attr_t, - _stack_size: libc::size_t, + attr: *mut libc::pthread_attr_t, + stack_size: libc::size_t, ) -> libc::c_int { - 1 + let pointer = attr as *mut libc::size_t; + *pointer = stack_size; + + 0 } #[no_mangle] pub unsafe extern "C" fn sched_yield() -> libc::c_int { - 1 + ctru_sys::svcSleepThread(0); + + 0 } #[no_mangle]