Skip to content

Commit

Permalink
fix: fix drop ordering issue (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah authored Jul 6, 2023
1 parent 82b0007 commit d3a0689
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Monoio 就是这样一个 Runtime:它并不像 Tokio 那样通过公平调度

这是一个非常简单的例子,基于 Monoio 实现一个简单的 echo 服务。运行起来之后你可以通过 `nc 127.0.0.1 50002` 来连接它。

```rust
```rust,no_run
/// A echo example.
///
/// Run the example and `nc 127.0.0.1 50002` in another shell.
Expand All @@ -66,7 +66,7 @@ async fn main() {
}
}
async fn echo(stream: TcpStream) -> std::io::Result<()> {
async fn echo(mut stream: TcpStream) -> std::io::Result<()> {
let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024);
let mut res;
loop {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Also, if you want to use io_uring, you must make sure your kernel supports it([5

Here is a basic example of how to use Monoio.

```rust
```rust,no_run
/// A echo example.
///
/// Run the example and `nc 127.0.0.1 50002` in another shell.
Expand All @@ -63,7 +63,7 @@ async fn main() {
}
}
async fn echo(stream: TcpStream) -> std::io::Result<()> {
async fn echo(mut stream: TcpStream) -> std::io::Result<()> {
let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024);
let mut res;
loop {
Expand Down
4 changes: 2 additions & 2 deletions monoio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT/Apache-2.0"
name = "monoio"
readme = "../README.md"
repository = "https://github.com/bytedance/monoio"
version = "0.1.5"
version = "0.1.6"

# common dependencies
[dependencies]
Expand All @@ -35,7 +35,7 @@ tracing = { version = "0.1", default-features = false, features = [
ctrlc = { version = "3", optional = true }

# windows dependencies(will be added when windows support finished)
[dependencies.windows-sys]
[target.'cfg(windows)'.dependencies.windows-sys]
features = ["Win32_Foundation", "Win32_Networking_WinSock"]
version = "0.48.0"

Expand Down
4 changes: 2 additions & 2 deletions monoio/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Buildable for LegacyDriver {
let context = crate::runtime::Context::new(blocking_handle);
#[cfg(not(feature = "sync"))]
let context = crate::runtime::Context::new();
Ok(Runtime { driver, context })
Ok(Runtime::new(context, driver))
})
}
}
Expand All @@ -123,7 +123,7 @@ impl Buildable for IoUringDriver {
let context = crate::runtime::Context::new(blocking_handle);
#[cfg(not(feature = "sync"))]
let context = crate::runtime::Context::new();
Ok(Runtime { driver, context })
Ok(Runtime::new(context, driver))
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion monoio/src/driver/uring/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ impl<'a> Ref<'a, Lifecycle> {
if let Some(data) = data.take() {
*ref_mut = Lifecycle::Ignored(Box::new(data));
} else {
*ref_mut = Lifecycle::Ignored(Box::new(())); // () is a ZST, so it does not allocate
*ref_mut = Lifecycle::Ignored(Box::new(())); // () is a ZST, so it does not
// allocate
};
return false;
}
Expand Down
1 change: 1 addition & 0 deletions monoio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![feature(io_error_more)]
#![feature(lazy_cell)]
#![feature(slice_internals)]
#![feature(stmt_expr_attributes)]

#[macro_use]
pub mod macros;
Expand Down
17 changes: 8 additions & 9 deletions monoio/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ thread_local! {
scoped_thread_local!(pub(crate) static CURRENT: Context);

pub(crate) struct Context {
/// Owned task set and local run queue
pub(crate) tasks: TaskQueue,

/// Thread id(not the kernel thread id but a generated unique number)
pub(crate) thread_id: usize,

Expand All @@ -45,8 +48,6 @@ pub(crate) struct Context {
pub(crate) waker_sender_cache:
std::cell::RefCell<fxhash::FxHashMap<usize, flume::Sender<std::task::Waker>>>,

/// Owned task set and local run queue
pub(crate) tasks: TaskQueue,
/// Time Handle
pub(crate) time_handle: Option<TimeHandle>,

Expand Down Expand Up @@ -95,10 +96,7 @@ impl Context {
let w = v.clone();
self.unpark_cache.borrow_mut().insert(id, w);
v.unpark();
return;
}

panic!("thread to unpark has not been registered");
}

#[allow(unused)]
Expand All @@ -114,20 +112,21 @@ impl Context {
// Write back to local cache
let _ = s.send(w);
self.waker_sender_cache.borrow_mut().insert(id, s);
return;
}

panic!("sender has not been registered");
}
}

/// Monoio runtime
pub struct Runtime<D> {
pub(crate) driver: D,
pub(crate) context: Context,
pub(crate) driver: D,
}

impl<D> Runtime<D> {
pub(crate) fn new(context: Context, driver: D) -> Self {
Self { context, driver }
}

/// Block on
pub fn block_on<F>(&mut self, future: F) -> F::Output
where
Expand Down
2 changes: 1 addition & 1 deletion monoio/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Common utils

pub(crate) mod box_into_inner;
pub(crate) mod linked_list;
pub(crate) mod slab;
pub(crate) mod thread_id;
pub(crate) mod uring_detect;
pub(crate) mod box_into_inner;

mod rand;
pub use rand::thread_rng_n;
Expand Down

0 comments on commit d3a0689

Please sign in to comment.