Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ impl<T: ?Sized> Mutex<T> {
let listener = self.lock_ops.listen();

// Try locking if nobody is being starved.
match self.state.compare_and_swap(0, 1, Ordering::Acquire) {
match self
.state
.compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire)
.unwrap_or_else(|x| x)
{
// Lock acquired!
0 => return,

Expand All @@ -132,7 +136,11 @@ impl<T: ?Sized> Mutex<T> {
listener.await;

// Try locking if nobody is being starved.
match self.state.compare_and_swap(0, 1, Ordering::Acquire) {
match self
.state
.compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire)
.unwrap_or_else(|x| x)
{
// Lock acquired!
0 => return,

Expand Down Expand Up @@ -171,7 +179,11 @@ impl<T: ?Sized> Mutex<T> {
let listener = self.lock_ops.listen();

// Try locking if nobody else is being starved.
match self.state.compare_and_swap(2, 2 | 1, Ordering::Acquire) {
match self
.state
.compare_exchange(2, 2 | 1, Ordering::Acquire, Ordering::Acquire)
.unwrap_or_else(|x| x)
{
// Lock acquired!
2 => return,

Expand Down Expand Up @@ -213,7 +225,11 @@ impl<T: ?Sized> Mutex<T> {
/// ```
#[inline]
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
if self.state.compare_and_swap(0, 1, Ordering::Acquire) == 0 {
if self
.state
.compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire)
.is_ok()
{
Some(MutexGuard(self))
} else {
None
Expand Down Expand Up @@ -286,7 +302,11 @@ impl<T: ?Sized> Mutex<T> {
/// ```
#[inline]
pub fn try_lock_arc(self: &Arc<Self>) -> Option<MutexGuardArc<T>> {
if self.state.compare_and_swap(0, 1, Ordering::Acquire) == 0 {
if self
.state
.compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire)
.is_ok()
{
Some(MutexGuardArc(self.clone()))
} else {
None
Expand Down
10 changes: 7 additions & 3 deletions src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ impl<T: ?Sized> RwLock<T> {
let lock = self.mutex.try_lock()?;

// If there are no readers, grab the write lock.
if self.state.compare_and_swap(0, WRITER_BIT, Ordering::AcqRel) == 0 {
if self
.state
.compare_exchange(0, WRITER_BIT, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
Some(RwLockWriteGuard {
writer: RwLockWriteGuardInner(self),
reserved: lock,
Expand Down Expand Up @@ -555,8 +559,8 @@ impl<'a, T: ?Sized> RwLockUpgradableReadGuard<'a, T> {
.reader
.0
.state
.compare_and_swap(ONE_READER, WRITER_BIT, Ordering::AcqRel)
== ONE_READER
.compare_exchange(ONE_READER, WRITER_BIT, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
Ok(guard.into_writer())
} else {
Expand Down