1use core::cell::UnsafeCell;
2
3use sys::os::mutex::*;
4
5pub struct Mutex {
7 inner: UnsafeCell<MutexType>,
8}
9
10impl Mutex {
11 pub fn new() -> Self {
13 Self::new_internal(false)
14 }
15
16 pub fn recursive() -> Self {
18 Self::new_internal(true)
19 }
20
21 pub fn finalize(&self) {
23 unsafe { nnosFinalizeMutex(self.ptr()) };
24 }
25
26 pub fn lock(&self) {
28 unsafe { nnosLockMutex(self.ptr()) };
29 }
30
31 pub fn try_lock(&self) -> bool {
33 unsafe { nnosTryLockMutex(self.ptr()) }
34 }
35
36 pub fn unlock(&self) {
38 unsafe { nnosUnlockMutex(self.ptr()) };
39 }
40
41 pub(crate) fn ptr(&self) -> *mut MutexType {
42 self.inner.get()
43 }
44
45 fn new_internal(recursive: bool) -> Self {
46 let mut inner = MutexType::default();
47 unsafe { nnosInitializeMutex(&mut inner as *mut MutexType, recursive, 0) };
48 Self {
49 inner: UnsafeCell::new(inner),
50 }
51 }
52}
53
54impl Drop for Mutex {
55 fn drop(&mut self) {
56 todo!()
57 }
58}