Skip to content

Commit 6f3437d

Browse files
authored
Use once_cell from async-lock (#95)
* Use once_cell from async-lock * Remove accidental loom experiment. * Use released version of async-lock * Remove loom experiment
1 parent 7e5a7bc commit 6f3437d

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ name = "io"
1919
harness = false
2020

2121
[dependencies]
22+
async-lock = "2.6"
2223
concurrent-queue = "1.2.2"
2324
futures-lite = "1.11.0"
2425
log = "0.4.11"
25-
once_cell = "1.4.1"
2626
parking = "2.0.0"
2727
polling = "2.0.0"
2828
slab = "0.4.2"

src/driver.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::task::{Context, Poll};
66
use std::thread;
77
use std::time::{Duration, Instant};
88

9+
use async_lock::OnceCell;
910
use futures_lite::pin;
10-
use once_cell::sync::Lazy;
1111
use waker_fn::waker_fn;
1212

1313
use crate::reactor::Reactor;
@@ -16,25 +16,29 @@ use crate::reactor::Reactor;
1616
static BLOCK_ON_COUNT: AtomicUsize = AtomicUsize::new(0);
1717

1818
/// Unparker for the "async-io" thread.
19-
static UNPARKER: Lazy<parking::Unparker> = Lazy::new(|| {
20-
let (parker, unparker) = parking::pair();
21-
22-
// Spawn a helper thread driving the reactor.
23-
//
24-
// Note that this thread is not exactly necessary, it's only here to help push things
25-
// forward if there are no `Parker`s around or if `Parker`s are just idling and never
26-
// parking.
27-
thread::Builder::new()
28-
.name("async-io".to_string())
29-
.spawn(move || main_loop(parker))
30-
.expect("cannot spawn async-io thread");
31-
32-
unparker
33-
});
19+
fn unparker() -> &'static parking::Unparker {
20+
static UNPARKER: OnceCell<parking::Unparker> = OnceCell::new();
21+
22+
UNPARKER.get_or_init_blocking(|| {
23+
let (parker, unparker) = parking::pair();
24+
25+
// Spawn a helper thread driving the reactor.
26+
//
27+
// Note that this thread is not exactly necessary, it's only here to help push things
28+
// forward if there are no `Parker`s around or if `Parker`s are just idling and never
29+
// parking.
30+
thread::Builder::new()
31+
.name("async-io".to_string())
32+
.spawn(move || main_loop(parker))
33+
.expect("cannot spawn async-io thread");
34+
35+
unparker
36+
})
37+
}
3438

3539
/// Initializes the "async-io" thread.
3640
pub(crate) fn init() {
37-
Lazy::force(&UNPARKER);
41+
let _ = unparker();
3842
}
3943

4044
/// The main loop for the "async-io" thread.
@@ -109,7 +113,7 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
109113
// Make sure to decrement `BLOCK_ON_COUNT` at the end and wake the "async-io" thread.
110114
let _guard = CallOnDrop(|| {
111115
BLOCK_ON_COUNT.fetch_sub(1, Ordering::SeqCst);
112-
UNPARKER.unpark();
116+
unparker().unpark();
113117
});
114118

115119
// Parker and unparker for notifying the current thread.
@@ -205,7 +209,7 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
205209

206210
// Unpark the "async-io" thread in case no other thread is ready to start
207211
// processing I/O events. This way we prevent a potential latency spike.
208-
UNPARKER.unpark();
212+
unparker().unpark();
209213

210214
// Wait for a notification.
211215
p.park();

src/reactor.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use std::sync::{Arc, Mutex, MutexGuard};
1616
use std::task::{Context, Poll, Waker};
1717
use std::time::{Duration, Instant};
1818

19+
use async_lock::OnceCell;
1920
use concurrent_queue::ConcurrentQueue;
2021
use futures_lite::ready;
21-
use once_cell::sync::Lazy;
2222
use polling::{Event, Poller};
2323
use slab::Slab;
2424

@@ -67,7 +67,9 @@ pub(crate) struct Reactor {
6767
impl Reactor {
6868
/// Returns a reference to the reactor.
6969
pub(crate) fn get() -> &'static Reactor {
70-
static REACTOR: Lazy<Reactor> = Lazy::new(|| {
70+
static REACTOR: OnceCell<Reactor> = OnceCell::new();
71+
72+
REACTOR.get_or_init_blocking(|| {
7173
crate::driver::init();
7274
Reactor {
7375
poller: Poller::new().expect("cannot initialize I/O event notification"),
@@ -77,8 +79,7 @@ impl Reactor {
7779
timers: Mutex::new(BTreeMap::new()),
7880
timer_ops: ConcurrentQueue::bounded(1000),
7981
}
80-
});
81-
&REACTOR
82+
})
8283
}
8384

8485
/// Returns the current ticker.

0 commit comments

Comments
 (0)