Browse Source

Fixed nits and oversight in Soc

pull/50/head
Andrea Ciliberti 3 years ago
parent
commit
0f781ce7d7
  1. 1
      ctru-rs/src/gfx.rs
  2. 22
      ctru-rs/src/lib.rs
  3. 2
      ctru-rs/src/romfs.rs
  4. 8
      ctru-rs/src/services/reference.rs
  5. 12
      ctru-rs/src/services/soc.rs

1
ctru-rs/src/gfx.rs

@ -220,6 +220,7 @@ impl From<Side> for ctru_sys::gfx3dSide_t {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::Error;
#[test] #[test]
fn gfx_duplicate() { fn gfx_duplicate() {

22
ctru-rs/src/lib.rs

@ -27,10 +27,11 @@ pub fn init() {
} }
#[cfg(not(test))] #[cfg(not(test))]
_panic_hook_setup(); panic_hook_setup();
} }
fn _panic_hook_setup() { #[cfg(not(test))]
fn panic_hook_setup() {
use crate::services::hid::{Hid, KeyPad}; use crate::services::hid::{Hid, KeyPad};
use std::panic::PanicInfo; use std::panic::PanicInfo;
@ -45,14 +46,15 @@ fn _panic_hook_setup() {
if main_thread == std::thread::current().id() && console::Console::exists() { if main_thread == std::thread::current().id() && console::Console::exists() {
println!("\nPress SELECT to exit the software"); println!("\nPress SELECT to exit the software");
let hid = Hid::init().unwrap(); match Hid::init() {
Ok(hid) => loop {
loop { hid.scan_input();
hid.scan_input(); let keys = hid.keys_down();
let keys = hid.keys_down(); if keys.contains(KeyPad::KEY_SELECT) {
if keys.contains(KeyPad::KEY_SELECT) { break;
break; }
} },
Err(e) => println!("Error while intializing Hid controller during panic: {e}"),
} }
} }
}); });

2
ctru-rs/src/romfs.rs

@ -52,7 +52,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn romfs_duplicate() { fn romfs_counter() {
let _romfs = RomFS::init().unwrap(); let _romfs = RomFS::init().unwrap();
let value = *ROMFS_ACTIVE.lock().unwrap(); let value = *ROMFS_ACTIVE.lock().unwrap();

8
ctru-rs/src/services/reference.rs

@ -2,7 +2,7 @@ use crate::Error;
use std::sync::Mutex; use std::sync::Mutex;
pub(crate) struct ServiceReference { pub(crate) struct ServiceReference {
counter: &'static Mutex<usize>, counter: &'static Mutex<usize>,
close: Box<dyn Fn()>, close: Box<dyn Fn() + Send + Sync>,
} }
impl ServiceReference { impl ServiceReference {
@ -14,9 +14,9 @@ impl ServiceReference {
) -> crate::Result<Self> ) -> crate::Result<Self>
where where
S: FnOnce() -> crate::Result<()>, S: FnOnce() -> crate::Result<()>,
E: Fn() + 'static, E: Fn() + Send + Sync + 'static,
{ {
let mut value = counter.lock().unwrap(); // todo: handle poisoning let mut value = counter.lock().expect("Mutex Counter for ServiceReference is poisoned"); // todo: handle poisoning
if *value == 0 { if *value == 0 {
start()?; start()?;
@ -35,7 +35,7 @@ impl ServiceReference {
impl Drop for ServiceReference { impl Drop for ServiceReference {
fn drop(&mut self) { fn drop(&mut self) {
let mut value = self.counter.lock().unwrap(); // should probably handle poisoning - could just map_err to ignore it. let mut value = self.counter.lock().expect("Mutex Counter for ServiceReference is poisoned"); // todo: handle poisoning
*value -= 1; *value -= 1;
if *value == 0 { if *value == 0 {
(self.close)(); (self.close)();

12
ctru-rs/src/services/soc.rs

@ -33,7 +33,7 @@ impl Soc {
pub fn init_with_buffer_size(num_bytes: usize) -> crate::Result<Self> { pub fn init_with_buffer_size(num_bytes: usize) -> crate::Result<Self> {
let _service_handler = ServiceReference::new( let _service_handler = ServiceReference::new(
&SOC_ACTIVE, &SOC_ACTIVE,
true, false,
|| { || {
let soc_mem = unsafe { memalign(0x1000, num_bytes) } as *mut u32; let soc_mem = unsafe { memalign(0x1000, num_bytes) } as *mut u32;
let r = unsafe { ctru_sys::socInit(soc_mem, num_bytes as u32) }; let r = unsafe { ctru_sys::socInit(soc_mem, num_bytes as u32) };
@ -65,18 +65,12 @@ impl Soc {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::Error;
#[test] #[test]
fn soc_duplicate() { fn soc_duplicate() {
let _soc = Soc::init().unwrap(); let _soc = Soc::init().unwrap();
let value = *SOC_ACTIVE.lock().unwrap();
assert_eq!(value, 1); assert!(matches!(Soc::init(), Err(Error::ServiceAlreadyActive)))
drop(_soc);
let value = *SOC_ACTIVE.lock().unwrap();
assert_eq!(value, 0);
} }
} }

Loading…
Cancel
Save