Skip to content

Commit 1f32082

Browse files
author
Stjepan Glavina
committed
Remove spinlock
1 parent 95f3eae commit 1f32082

File tree

1 file changed

+6
-55
lines changed

1 file changed

+6
-55
lines changed

src/lib.rs

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ use std::ops::{Deref, DerefMut};
7070
use std::panic::{RefUnwindSafe, UnwindSafe};
7171
use std::pin::Pin;
7272
use std::ptr::{self, NonNull};
73-
use std::sync::atomic::{self, AtomicBool, AtomicPtr, AtomicUsize, Ordering};
74-
use std::sync::Arc;
73+
use std::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
74+
use std::sync::{Arc, Mutex, MutexGuard};
7575
use std::task::{Context, Poll, Waker};
7676
use std::thread::{self, Thread};
7777
use std::time::{Duration, Instant};
@@ -85,7 +85,7 @@ struct Inner {
8585
notified: AtomicUsize,
8686

8787
/// A linked list holding registered listeners.
88-
list: Spinlock<List>,
88+
list: Mutex<List>,
8989

9090
/// A single cached list entry to avoid allocations on the fast path of the insertion.
9191
cache: UnsafeCell<Entry>,
@@ -96,7 +96,7 @@ impl Inner {
9696
fn lock(&self) -> ListGuard<'_> {
9797
ListGuard {
9898
inner: self,
99-
guard: self.list.lock(),
99+
guard: self.list.lock().unwrap(),
100100
}
101101
}
102102

@@ -374,7 +374,7 @@ impl Event {
374374
// Allocate on the heap.
375375
let new = Arc::new(Inner {
376376
notified: AtomicUsize::new(usize::MAX),
377-
list: Spinlock::new(List {
377+
list: std::sync::Mutex::new(List {
378378
head: None,
379379
tail: None,
380380
start: None,
@@ -722,7 +722,7 @@ struct ListGuard<'a> {
722722
inner: &'a Inner,
723723

724724
/// The actual guard that acquired the linked list.
725-
guard: SpinlockGuard<'a, List>,
725+
guard: MutexGuard<'a, List>,
726726
}
727727

728728
impl Drop for ListGuard<'_> {
@@ -984,52 +984,3 @@ fn full_fence() {
984984
atomic::fence(Ordering::SeqCst);
985985
}
986986
}
987-
988-
/// A simple spinlock.
989-
struct Spinlock<T> {
990-
flag: AtomicBool,
991-
value: UnsafeCell<T>,
992-
}
993-
994-
impl<T> Spinlock<T> {
995-
/// Returns a new spinlock initialized with `value`.
996-
fn new(value: T) -> Spinlock<T> {
997-
Spinlock {
998-
flag: AtomicBool::new(false),
999-
value: UnsafeCell::new(value),
1000-
}
1001-
}
1002-
1003-
/// Locks the spinlock.
1004-
fn lock(&self) -> SpinlockGuard<'_, T> {
1005-
while self.flag.swap(true, Ordering::Acquire) {
1006-
thread::yield_now();
1007-
}
1008-
SpinlockGuard { parent: self }
1009-
}
1010-
}
1011-
1012-
/// A guard holding a spinlock locked.
1013-
struct SpinlockGuard<'a, T> {
1014-
parent: &'a Spinlock<T>,
1015-
}
1016-
1017-
impl<T> Drop for SpinlockGuard<'_, T> {
1018-
fn drop(&mut self) {
1019-
self.parent.flag.store(false, Ordering::Release);
1020-
}
1021-
}
1022-
1023-
impl<T> Deref for SpinlockGuard<'_, T> {
1024-
type Target = T;
1025-
1026-
fn deref(&self) -> &T {
1027-
unsafe { &*self.parent.value.get() }
1028-
}
1029-
}
1030-
1031-
impl<T> DerefMut for SpinlockGuard<'_, T> {
1032-
fn deref_mut(&mut self) -> &mut T {
1033-
unsafe { &mut *self.parent.value.get() }
1034-
}
1035-
}

0 commit comments

Comments
 (0)