diff options
| author | Fenrir <[email protected]> | 2017-02-20 22:24:01 -0700 |
|---|---|---|
| committer | Fenrir <[email protected]> | 2017-02-20 22:24:01 -0700 |
| commit | 1e3655e44af528844be2c4ef32e123be1421c7e4 (patch) | |
| tree | e7432275077b8ba29b39160eec9a17cab62e8138 /ctr-std/src/sys | |
| parent | Merge pull request #19 from FenrirWolf/thread_local (diff) | |
| download | ctru-rs-1e3655e44af528844be2c4ef32e123be1421c7e4.tar.xz ctru-rs-1e3655e44af528844be2c4ef32e123be1421c7e4.zip | |
Add sync::mutex
Diffstat (limited to 'ctr-std/src/sys')
| -rw-r--r-- | ctr-std/src/sys/unix/mod.rs | 1 | ||||
| -rw-r--r-- | ctr-std/src/sys/unix/mutex.rs | 84 |
2 files changed, 85 insertions, 0 deletions
diff --git a/ctr-std/src/sys/unix/mod.rs b/ctr-std/src/sys/unix/mod.rs index 13639eb..5ea31af 100644 --- a/ctr-std/src/sys/unix/mod.rs +++ b/ctr-std/src/sys/unix/mod.rs @@ -13,6 +13,7 @@ pub mod ext; pub mod fast_thread_local; pub mod memchr; +pub mod mutex; pub mod os; pub mod os_str; pub mod path; diff --git a/ctr-std/src/sys/unix/mutex.rs b/ctr-std/src/sys/unix/mutex.rs new file mode 100644 index 0000000..d4fd2c2 --- /dev/null +++ b/ctr-std/src/sys/unix/mutex.rs @@ -0,0 +1,84 @@ +// Copyright 2014 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 cell::UnsafeCell; +use mem; + +use libctru::synchronization; + +pub struct Mutex { inner: UnsafeCell<synchronization::LightLock> } + +#[inline] +pub unsafe fn raw(m: &Mutex) -> *mut synchronization::LightLock { + m.inner.get() +} + +unsafe impl Send for Mutex {} +unsafe impl Sync for Mutex {} + +#[allow(dead_code)] // sys isn't exported yet +impl Mutex { + pub const fn new() -> Mutex { + Mutex { inner: UnsafeCell::new(0) } + } + #[inline] + pub unsafe fn init(&mut self) { + synchronization::LightLock_Init(self.inner.get()); + } + #[inline] + pub unsafe fn lock(&self) { + synchronization::LightLock_Lock(self.inner.get()); + } + #[inline] + pub unsafe fn unlock(&self) { + synchronization::LightLock_Unlock(self.inner.get()); + } + #[inline] + pub unsafe fn try_lock(&self) -> bool { + match synchronization::LightLock_TryLock(self.inner.get()) { + 0 => true, + _ => false, + } + } + #[inline] + pub unsafe fn destroy(&self) {} +} + +pub struct ReentrantMutex { inner: UnsafeCell<synchronization::RecursiveLock> } + +unsafe impl Send for ReentrantMutex {} +unsafe impl Sync for ReentrantMutex {} + +impl ReentrantMutex { + pub unsafe fn uninitialized() -> ReentrantMutex { + ReentrantMutex { inner: mem::uninitialized() } + } + #[inline] + pub unsafe fn init(&mut self) { + synchronization::RecursiveLock_Init(self.inner.get()); + } + #[inline] + pub unsafe fn lock(&self) { + synchronization::RecursiveLock_Lock(self.inner.get()); + } + #[inline] + pub unsafe fn unlock(&self) { + synchronization::RecursiveLock_Unlock(self.inner.get()); + } + #[inline] + pub unsafe fn try_lock(&self) -> bool { + match synchronization::RecursiveLock_TryLock(self.inner.get()) { + 0 => true, + _ => false, + } + } + #[inline] + pub unsafe fn destroy(&self) {} +} |