Andrea Ciliberti
3 years ago
26 changed files with 320 additions and 118 deletions
@ -1,20 +1,44 @@ |
|||||||
|
use std::sync::atomic::{AtomicBool, Ordering}; |
||||||
|
|
||||||
|
use crate::error::Error; |
||||||
|
|
||||||
|
#[non_exhaustive] |
||||||
pub struct Srv(()); |
pub struct Srv(()); |
||||||
|
|
||||||
|
static SRV_ACTIVE: AtomicBool = AtomicBool::new(false); |
||||||
|
|
||||||
impl Srv { |
impl Srv { |
||||||
pub fn init() -> crate::Result<Srv> { |
pub fn init() -> crate::Result<Self> { |
||||||
unsafe { |
match SRV_ACTIVE.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { |
||||||
let r = ctru_sys::srvInit(); |
Ok(_) => { |
||||||
|
let r = unsafe { ctru_sys::srvInit() }; |
||||||
if r < 0 { |
if r < 0 { |
||||||
Err(r.into()) |
Err(r.into()) |
||||||
} else { |
} else { |
||||||
Ok(Srv(())) |
Ok(Self(())) |
||||||
} |
} |
||||||
} |
} |
||||||
|
Err(_) => Err(Error::ServiceAlreadyActive("Srv")), |
||||||
|
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
impl Drop for Srv { |
impl Drop for Srv { |
||||||
fn drop(&mut self) { |
fn drop(&mut self) { |
||||||
unsafe { ctru_sys::srvExit() }; |
unsafe { ctru_sys::srvExit() }; |
||||||
|
|
||||||
|
SRV_ACTIVE.store(false, Ordering::Release); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[cfg(test)] |
||||||
|
mod tests { |
||||||
|
use super::*; |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn srv_duplicate() { |
||||||
|
let _srv = Srv::init().unwrap(); |
||||||
|
|
||||||
|
assert!(Srv::init().is_err()); |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue