Remove unsafety from unsupported/rwlosck.rs by using a Cell.
Replacing the UnsafeCell by a Cell makes it all safe.
This commit is contained in:
parent
c25f69a1e3
commit
3d192ace34
1 changed files with 16 additions and 18 deletions
|
@ -1,7 +1,9 @@
|
||||||
use crate::cell::UnsafeCell;
|
#![deny(unsafe_op_in_unsafe_fn)]
|
||||||
|
|
||||||
|
use crate::cell::Cell;
|
||||||
|
|
||||||
pub struct RWLock {
|
pub struct RWLock {
|
||||||
mode: UnsafeCell<isize>,
|
mode: Cell<isize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for RWLock {}
|
unsafe impl Send for RWLock {}
|
||||||
|
@ -9,14 +11,14 @@ unsafe impl Sync for RWLock {} // no threads on this platform
|
||||||
|
|
||||||
impl RWLock {
|
impl RWLock {
|
||||||
pub const fn new() -> RWLock {
|
pub const fn new() -> RWLock {
|
||||||
RWLock { mode: UnsafeCell::new(0) }
|
RWLock { mode: Cell::new(0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn read(&self) {
|
pub unsafe fn read(&self) {
|
||||||
let mode = self.mode.get();
|
let m = self.mode.get();
|
||||||
if *mode >= 0 {
|
if m >= 0 {
|
||||||
*mode += 1;
|
self.mode.set(m + 1);
|
||||||
} else {
|
} else {
|
||||||
rtabort!("rwlock locked for writing");
|
rtabort!("rwlock locked for writing");
|
||||||
}
|
}
|
||||||
|
@ -24,9 +26,9 @@ impl RWLock {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn try_read(&self) -> bool {
|
pub unsafe fn try_read(&self) -> bool {
|
||||||
let mode = self.mode.get();
|
let m = self.mode.get();
|
||||||
if *mode >= 0 {
|
if m >= 0 {
|
||||||
*mode += 1;
|
self.mode.set(m + 1);
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -35,19 +37,15 @@ impl RWLock {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn write(&self) {
|
pub unsafe fn write(&self) {
|
||||||
let mode = self.mode.get();
|
if self.mode.replace(-1) != 0 {
|
||||||
if *mode == 0 {
|
|
||||||
*mode = -1;
|
|
||||||
} else {
|
|
||||||
rtabort!("rwlock locked for reading")
|
rtabort!("rwlock locked for reading")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn try_write(&self) -> bool {
|
pub unsafe fn try_write(&self) -> bool {
|
||||||
let mode = self.mode.get();
|
if self.mode.get() == 0 {
|
||||||
if *mode == 0 {
|
self.mode.set(-1);
|
||||||
*mode = -1;
|
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -56,12 +54,12 @@ impl RWLock {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn read_unlock(&self) {
|
pub unsafe fn read_unlock(&self) {
|
||||||
*self.mode.get() -= 1;
|
self.mode.set(self.mode.get() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn write_unlock(&self) {
|
pub unsafe fn write_unlock(&self) {
|
||||||
*self.mode.get() += 1;
|
self.mode.set(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue