Skip to content

Commit 44f9796

Browse files
committed
feat(runtime): use scoped-tls
1 parent 9af3dbf commit 44f9796

File tree

19 files changed

+251
-456
lines changed

19 files changed

+251
-456
lines changed

compio-dispatcher/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ impl Dispatcher {
5656
.build()
5757
.expect("cannot create compio runtime")
5858
.block_on(async move {
59-
let rt = Runtime::current();
6059
while let Ok(f) = receiver.recv_async().await {
6160
let fut = (f)();
6261
if builder.concurrent {
63-
rt.spawn(fut).detach()
62+
compio_runtime::spawn(fut).detach()
6463
} else {
6564
fut.await
6665
}

compio-fs/src/async_fd.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use compio_driver::{
1212
AsRawFd, SharedFd, ToSharedFd,
1313
};
1414
use compio_io::{AsyncRead, AsyncWrite};
15-
use compio_runtime::{Attacher, Runtime};
15+
use compio_runtime::Attacher;
1616
#[cfg(unix)]
1717
use {
1818
compio_buf::{IoVectoredBuf, IoVectoredBufMut},
@@ -63,22 +63,14 @@ impl<T: AsRawFd + 'static> AsyncRead for &AsyncFd<T> {
6363
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
6464
let fd = self.inner.to_shared_fd();
6565
let op = Recv::new(fd, buf);
66-
Runtime::current()
67-
.submit(op)
68-
.await
69-
.into_inner()
70-
.map_advanced()
66+
compio_runtime::submit(op).await.into_inner().map_advanced()
7167
}
7268

7369
#[cfg(unix)]
7470
async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
7571
let fd = self.inner.to_shared_fd();
7672
let op = RecvVectored::new(fd, buf);
77-
Runtime::current()
78-
.submit(op)
79-
.await
80-
.into_inner()
81-
.map_advanced()
73+
compio_runtime::submit(op).await.into_inner().map_advanced()
8274
}
8375
}
8476

@@ -109,14 +101,14 @@ impl<T: AsRawFd + 'static> AsyncWrite for &AsyncFd<T> {
109101
async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> {
110102
let fd = self.inner.to_shared_fd();
111103
let op = Send::new(fd, buf);
112-
Runtime::current().submit(op).await.into_inner()
104+
compio_runtime::submit(op).await.into_inner()
113105
}
114106

115107
#[cfg(unix)]
116108
async fn write_vectored<V: IoVectoredBuf>(&mut self, buf: V) -> BufResult<usize, V> {
117109
let fd = self.inner.to_shared_fd();
118110
let op = SendVectored::new(fd, buf);
119-
Runtime::current().submit(op).await.into_inner()
111+
compio_runtime::submit(op).await.into_inner()
120112
}
121113

122114
async fn flush(&mut self) -> io::Result<()> {

compio-fs/src/file.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use compio_driver::{
77
ToSharedFd,
88
};
99
use compio_io::{AsyncReadAt, AsyncWriteAt};
10-
use compio_runtime::{Attacher, Runtime};
10+
use compio_runtime::Attacher;
1111
#[cfg(unix)]
1212
use {
1313
compio_buf::{IoVectoredBuf, IoVectoredBufMut},
@@ -71,7 +71,7 @@ impl File {
7171
.await;
7272
if let Some(fd) = fd {
7373
let op = CloseFile::new(fd.into());
74-
Runtime::current().submit(op).await.0?;
74+
compio_runtime::submit(op).await.0?;
7575
}
7676
Ok(())
7777
}
@@ -90,7 +90,7 @@ impl File {
9090
#[cfg(unix)]
9191
pub async fn metadata(&self) -> io::Result<Metadata> {
9292
let op = FileStat::new(self.to_shared_fd());
93-
let BufResult(res, op) = Runtime::current().submit(op).await;
93+
let BufResult(res, op) = compio_runtime::submit(op).await;
9494
res.map(|_| Metadata::from_stat(op.into_inner()))
9595
}
9696

@@ -121,7 +121,7 @@ impl File {
121121

122122
async fn sync_impl(&self, datasync: bool) -> io::Result<()> {
123123
let op = Sync::new(self.to_shared_fd(), datasync);
124-
Runtime::current().submit(op).await.0?;
124+
compio_runtime::submit(op).await.0?;
125125
Ok(())
126126
}
127127

@@ -153,11 +153,7 @@ impl AsyncReadAt for File {
153153
async fn read_at<T: IoBufMut>(&self, buffer: T, pos: u64) -> BufResult<usize, T> {
154154
let fd = self.inner.to_shared_fd();
155155
let op = ReadAt::new(fd, pos, buffer);
156-
Runtime::current()
157-
.submit(op)
158-
.await
159-
.into_inner()
160-
.map_advanced()
156+
compio_runtime::submit(op).await.into_inner().map_advanced()
161157
}
162158

163159
#[cfg(unix)]
@@ -168,11 +164,7 @@ impl AsyncReadAt for File {
168164
) -> BufResult<usize, T> {
169165
let fd = self.inner.to_shared_fd();
170166
let op = ReadVectoredAt::new(fd, pos, buffer);
171-
Runtime::current()
172-
.submit(op)
173-
.await
174-
.into_inner()
175-
.map_advanced()
167+
compio_runtime::submit(op).await.into_inner().map_advanced()
176168
}
177169
}
178170

@@ -197,7 +189,7 @@ impl AsyncWriteAt for &File {
197189
async fn write_at<T: IoBuf>(&mut self, buffer: T, pos: u64) -> BufResult<usize, T> {
198190
let fd = self.inner.to_shared_fd();
199191
let op = WriteAt::new(fd, pos, buffer);
200-
Runtime::current().submit(op).await.into_inner()
192+
compio_runtime::submit(op).await.into_inner()
201193
}
202194

203195
#[cfg(unix)]
@@ -208,7 +200,7 @@ impl AsyncWriteAt for &File {
208200
) -> BufResult<usize, T> {
209201
let fd = self.inner.to_shared_fd();
210202
let op = WriteVectoredAt::new(fd, pos, buffer);
211-
Runtime::current().submit(op).await.into_inner()
203+
compio_runtime::submit(op).await.into_inner()
212204
}
213205
}
214206

compio-fs/src/metadata/unix.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use std::{
88

99
use compio_buf::{BufResult, IntoInner};
1010
use compio_driver::{op::PathStat, syscall};
11-
use compio_runtime::Runtime;
1211

1312
use crate::path_string;
1413

1514
async fn metadata_impl(path: impl AsRef<Path>, follow_symlink: bool) -> io::Result<Metadata> {
1615
let path = path_string(path)?;
1716
let op = PathStat::new(path, follow_symlink);
18-
let BufResult(res, op) = Runtime::current().submit(op).await;
17+
let BufResult(res, op) = compio_runtime::submit(op).await;
1918
res.map(|_| Metadata::from_stat(op.into_inner()))
2019
}
2120

compio-fs/src/named_pipe.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{ffi::OsStr, io, os::windows::io::FromRawHandle, ptr::null};
99
use compio_buf::{BufResult, IoBuf, IoBufMut};
1010
use compio_driver::{impl_raw_fd, op::ConnectNamedPipe, syscall, AsRawFd, RawFd, ToSharedFd};
1111
use compio_io::{AsyncRead, AsyncReadAt, AsyncWrite, AsyncWriteAt};
12-
use compio_runtime::Runtime;
1312
use widestring::U16CString;
1413
use windows_sys::Win32::{
1514
Storage::FileSystem::{
@@ -142,7 +141,7 @@ impl NamedPipeServer {
142141
/// ```
143142
pub async fn connect(&self) -> io::Result<()> {
144143
let op = ConnectNamedPipe::new(self.handle.to_shared_fd());
145-
Runtime::current().submit(op).await.0?;
144+
compio_runtime::submit(op).await.0?;
146145
Ok(())
147146
}
148147

compio-fs/src/open_options/unix.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{io, os::fd::FromRawFd, path::Path};
22

33
use compio_driver::{op::OpenFile, RawFd};
4-
use compio_runtime::Runtime;
54

65
use crate::{path_string, File};
76

@@ -87,7 +86,7 @@ impl OpenOptions {
8786
| (self.custom_flags as libc::c_int & !libc::O_ACCMODE);
8887
let p = path_string(p)?;
8988
let op = OpenFile::new(p, flags, self.mode);
90-
let fd = Runtime::current().submit(op).await.0? as RawFd;
89+
let fd = compio_runtime::submit(op).await.0? as RawFd;
9190
File::from_std(unsafe { std::fs::File::from_raw_fd(fd) })
9291
}
9392
}

compio-fs/src/pipe.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use compio_driver::{
1414
syscall, AsRawFd, ToSharedFd,
1515
};
1616
use compio_io::{AsyncRead, AsyncWrite};
17-
use compio_runtime::Runtime;
1817

1918
use crate::File;
2019

@@ -370,13 +369,13 @@ impl AsyncWrite for &Sender {
370369
async fn write<T: IoBuf>(&mut self, buffer: T) -> BufResult<usize, T> {
371370
let fd = self.to_shared_fd();
372371
let op = Send::new(fd, buffer);
373-
Runtime::current().submit(op).await.into_inner()
372+
compio_runtime::submit(op).await.into_inner()
374373
}
375374

376375
async fn write_vectored<T: IoVectoredBuf>(&mut self, buffer: T) -> BufResult<usize, T> {
377376
let fd = self.to_shared_fd();
378377
let op = SendVectored::new(fd, buffer);
379-
Runtime::current().submit(op).await.into_inner()
378+
compio_runtime::submit(op).await.into_inner()
380379
}
381380

382381
#[inline]
@@ -493,21 +492,13 @@ impl AsyncRead for &Receiver {
493492
async fn read<B: IoBufMut>(&mut self, buffer: B) -> BufResult<usize, B> {
494493
let fd = self.to_shared_fd();
495494
let op = Recv::new(fd, buffer);
496-
Runtime::current()
497-
.submit(op)
498-
.await
499-
.into_inner()
500-
.map_advanced()
495+
compio_runtime::submit(op).await.into_inner().map_advanced()
501496
}
502497

503498
async fn read_vectored<V: IoVectoredBufMut>(&mut self, buffer: V) -> BufResult<usize, V> {
504499
let fd = self.to_shared_fd();
505500
let op = RecvVectored::new(fd, buffer);
506-
Runtime::current()
507-
.submit(op)
508-
.await
509-
.into_inner()
510-
.map_advanced()
501+
compio_runtime::submit(op).await.into_inner().map_advanced()
511502
}
512503
}
513504

compio-fs/src/stdio/windows.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ impl Stdin {
114114
let stdin = io::stdin();
115115
let isatty = *STDIN_ISATTY.get_or_init(|| {
116116
stdin.is_terminal()
117-
|| Runtime::current()
118-
.attach(stdin.as_raw_handle() as _)
119-
.is_err()
117+
|| Runtime::with_current(|r| r.attach(stdin.as_raw_handle() as _)).is_err()
120118
});
121119
Self {
122120
fd: SharedFd::new(stdin.as_raw_handle() as _),
@@ -127,13 +125,12 @@ impl Stdin {
127125

128126
impl AsyncRead for Stdin {
129127
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
130-
let runtime = Runtime::current();
131128
if self.isatty {
132129
let op = StdRead::new(io::stdin(), buf);
133-
runtime.submit(op).await.into_inner()
130+
compio_runtime::submit(op).await.into_inner()
134131
} else {
135132
let op = Recv::new(self.fd.clone(), buf);
136-
runtime.submit(op).await.into_inner()
133+
compio_runtime::submit(op).await.into_inner()
137134
}
138135
.map_advanced()
139136
}
@@ -161,9 +158,7 @@ impl Stdout {
161158
let stdout = io::stdout();
162159
let isatty = *STDOUT_ISATTY.get_or_init(|| {
163160
stdout.is_terminal()
164-
|| Runtime::current()
165-
.attach(stdout.as_raw_handle() as _)
166-
.is_err()
161+
|| Runtime::with_current(|r| r.attach(stdout.as_raw_handle() as _)).is_err()
167162
});
168163
Self {
169164
fd: SharedFd::new(stdout.as_raw_handle() as _),
@@ -174,13 +169,12 @@ impl Stdout {
174169

175170
impl AsyncWrite for Stdout {
176171
async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
177-
let runtime = Runtime::current();
178172
if self.isatty {
179173
let op = StdWrite::new(io::stdout(), buf);
180-
runtime.submit(op).await.into_inner()
174+
compio_runtime::submit(op).await.into_inner()
181175
} else {
182176
let op = Send::new(self.fd.clone(), buf);
183-
runtime.submit(op).await.into_inner()
177+
compio_runtime::submit(op).await.into_inner()
184178
}
185179
}
186180

@@ -215,9 +209,7 @@ impl Stderr {
215209
let stderr = io::stderr();
216210
let isatty = *STDERR_ISATTY.get_or_init(|| {
217211
stderr.is_terminal()
218-
|| Runtime::current()
219-
.attach(stderr.as_raw_handle() as _)
220-
.is_err()
212+
|| Runtime::with_current(|r| r.attach(stderr.as_raw_handle() as _)).is_err()
221213
});
222214
Self {
223215
fd: SharedFd::new(stderr.as_raw_handle() as _),
@@ -228,13 +220,12 @@ impl Stderr {
228220

229221
impl AsyncWrite for Stderr {
230222
async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
231-
let runtime = Runtime::current();
232223
if self.isatty {
233224
let op = StdWrite::new(io::stderr(), buf);
234-
runtime.submit(op).await.into_inner()
225+
compio_runtime::submit(op).await.into_inner()
235226
} else {
236227
let op = Send::new(self.fd.clone(), buf);
237-
runtime.submit(op).await.into_inner()
228+
compio_runtime::submit(op).await.into_inner()
238229
}
239230
}
240231

compio-fs/src/utils/unix.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::{io, path::Path};
22

33
use compio_driver::op::{CreateDir, HardLink, Rename, Symlink, Unlink};
4-
use compio_runtime::Runtime;
54

65
use crate::path_string;
76

87
async fn unlink(path: impl AsRef<Path>, dir: bool) -> io::Result<()> {
98
let path = path_string(path)?;
109
let op = Unlink::new(path, dir);
11-
Runtime::current().submit(op).await.0?;
10+
compio_runtime::submit(op).await.0?;
1211
Ok(())
1312
}
1413

@@ -24,23 +23,23 @@ pub async fn rename(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<
2423
let from = path_string(from)?;
2524
let to = path_string(to)?;
2625
let op = Rename::new(from, to);
27-
Runtime::current().submit(op).await.0?;
26+
compio_runtime::submit(op).await.0?;
2827
Ok(())
2928
}
3029

3130
pub async fn symlink(original: impl AsRef<Path>, link: impl AsRef<Path>) -> io::Result<()> {
3231
let original = path_string(original)?;
3332
let link = path_string(link)?;
3433
let op = Symlink::new(original, link);
35-
Runtime::current().submit(op).await.0?;
34+
compio_runtime::submit(op).await.0?;
3635
Ok(())
3736
}
3837

3938
pub async fn hard_link(original: impl AsRef<Path>, link: impl AsRef<Path>) -> io::Result<()> {
4039
let original = path_string(original)?;
4140
let link = path_string(link)?;
4241
let op = HardLink::new(original, link);
43-
Runtime::current().submit(op).await.0?;
42+
compio_runtime::submit(op).await.0?;
4443
Ok(())
4544
}
4645

@@ -60,7 +59,7 @@ impl DirBuilder {
6059
pub async fn create(&self, path: &Path) -> io::Result<()> {
6160
let path = path_string(path)?;
6261
let op = CreateDir::new(path, self.mode as _);
63-
Runtime::current().submit(op).await.0?;
62+
compio_runtime::submit(op).await.0?;
6463
Ok(())
6564
}
6665
}

0 commit comments

Comments
 (0)