From 890644ff526d22fe499a63afb493cd9f24c8c21b Mon Sep 17 00:00:00 2001 From: zhuxiujie Date: Wed, 26 Jan 2022 11:13:07 +0800 Subject: [PATCH] move mpmc,mpsc to channel --- Cargo.toml | 2 +- benches/channel.rs | 2 +- examples/src/channel.rs | 5 +---- examples/src/select.rs | 2 +- src/std/io/stream_chan.rs | 4 ++-- src/std/sync/{mpmc.rs => channel.rs} | 10 +++++----- src/std/sync/condvar.rs | 2 +- src/std/sync/mod.rs | 5 ++--- src/std/sync/mpsc.rs | 8 -------- src/std/sync/mutex.rs | 2 +- src/std/sync/once.rs | 2 +- src/std/sync/rwlock.rs | 2 +- src/std/sync/semphore.rs | 2 +- src/std/time/tick.rs | 2 +- tests/channel.rs | 2 +- tests/cqueue.rs | 6 +++--- tests/lib.rs | 4 ++-- 17 files changed, 25 insertions(+), 37 deletions(-) rename src/std/sync/{mpmc.rs => channel.rs} (99%) delete mode 100644 src/std/sync/mpsc.rs diff --git a/Cargo.toml b/Cargo.toml index a94d18a..e1f34b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ members = [ [package] name = "cogo" -version = "0.1.27" +version = "0.1.28" edition = "2018" authors = ["zhuxiujia@qq.com","Xudong Huang "] license = "MIT/Apache-2.0" diff --git a/benches/channel.rs b/benches/channel.rs index 03b80af..849912d 100644 --- a/benches/channel.rs +++ b/benches/channel.rs @@ -6,7 +6,7 @@ extern crate cogo; extern crate test; use test::Bencher; -use cogo::std::sync::mpmc::channel; +use cogo::std::sync::channel::channel; // improve performance from 39,294 ns/iter to 12,207 ns/iter (my computer) diff --git a/examples/src/channel.rs b/examples/src/channel.rs index 106668d..f81e834 100644 --- a/examples/src/channel.rs +++ b/examples/src/channel.rs @@ -1,10 +1,7 @@ use std::time::Duration; - - use cogo::coroutine::sleep; use cogo::{chan, go}; -use cogo::std::sync::mpsc; -use cogo::std::sync::mpsc::{bounded, channel, channel_buf, unbounded}; +use cogo::std::sync::channel::{bounded, channel, channel_buf, unbounded}; fn main() { diff --git a/examples/src/select.rs b/examples/src/select.rs index c2b9074..b36e7c8 100644 --- a/examples/src/select.rs +++ b/examples/src/select.rs @@ -5,7 +5,7 @@ use std::time::Duration; use cogo::coroutine; use cogo::net::TcpListener; -use cogo::std::sync::mpsc::channel; +use cogo::std::sync::channel::channel; // general select example that use cqueue fn main() { diff --git a/src/std/io/stream_chan.rs b/src/std/io/stream_chan.rs index 829a67b..cd03366 100644 --- a/src/std/io/stream_chan.rs +++ b/src/std/io/stream_chan.rs @@ -1,7 +1,7 @@ use std::sync::mpsc::RecvError; use crate::std::errors::Error; use crate::std::io::{Stream, TryStream}; -use crate::std::sync::mpsc::{Receiver, Sender, unbounded}; +use crate::std::sync::channel::{Receiver, Sender, unbounded}; /// ChanStream,based on mpsc channel.when send Err data stop next @@ -53,7 +53,7 @@ impl Stream for ChanStream { #[cfg(test)] mod test { use crate::std::io::{ChanStream, TryStream}; - use crate::std::sync::mpsc::channel; + use crate::std::sync::channel::channel; use crate::std::errors::Error; use std::ops::ControlFlow; diff --git a/src/std/sync/mpmc.rs b/src/std/sync/channel.rs similarity index 99% rename from src/std/sync/mpmc.rs rename to src/std/sync/channel.rs index 51a621c..322455f 100644 --- a/src/std/sync/mpmc.rs +++ b/src/std/sync/channel.rs @@ -52,13 +52,13 @@ pub fn bounded(buf: usize) -> (Sender, Receiver) { #[macro_export] macro_rules! chan { () => { - $crate::std::sync::mpsc::bounded(usize::MAX) + $crate::std::sync::channel::bounded(usize::MAX) }; ($num:expr) => { - $crate::std::sync::mpsc::bounded($num) + $crate::std::sync::channel::bounded($num) }; ($t:path,$num:expr) => { - $crate::std::sync::mpsc::bounded::<$t>($num) + $crate::std::sync::channel::bounded::<$t>($num) }; } @@ -1125,9 +1125,9 @@ mod tests { #[test] fn stress_mutli_recv() { - use crate::std::sync::mpsc; + use crate::std::sync::channel; let (tx, rx) = channel(); - let (tx1, rx1) = mpsc::channel(); + let (tx1, rx1) = channel(); let stress = stress_factor() + 100; for i in 0..10 { diff --git a/src/std/sync/condvar.rs b/src/std/sync/condvar.rs index 517c60e..79d3c96 100644 --- a/src/std/sync/condvar.rs +++ b/src/std/sync/condvar.rs @@ -197,7 +197,7 @@ impl Default for Condvar { mod tests { #![feature(test)] - use crate::std::sync::mpsc::channel; + use crate::std::sync::channel::channel; use crate::std::sync::{Condvar, Mutex}; use std::sync::Arc; use std::sync::mpsc::TryRecvError; diff --git a/src/std/sync/mod.rs b/src/std/sync/mod.rs index 7356b2b..8b623bb 100644 --- a/src/std/sync/mod.rs +++ b/src/std/sync/mod.rs @@ -14,9 +14,7 @@ pub(crate) mod atomic_dur; #[cfg(not(unix))] pub(crate) mod delay_drop; #[macro_use] -pub mod mpmc; -#[macro_use] -pub mod mpsc; +pub mod channel; pub use self::atomic_option::AtomicOption; pub use self::blocking::{Blocker, FastBlocker}; @@ -28,3 +26,4 @@ pub use self::sync_flag::SyncFlag; pub use self::wait_group::*; pub use self::sync_map::*; pub use self::once::*; +pub use self::channel::*; diff --git a/src/std/sync/mpsc.rs b/src/std/sync/mpsc.rs deleted file mode 100644 index 94bb217..0000000 --- a/src/std/sync/mpsc.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! compatible with std::sync::mpsc except for both thread and coroutine -//! please ref the doc from std::sync::mpsc - -pub use crate::std::sync::mpmc::channel; -pub use crate::std::sync::mpmc::channel_buf; -pub use crate::std::sync::mpmc::unbounded; -pub use crate::std::sync::mpmc::bounded; -pub use crate::std::sync::mpmc::{Sender, Receiver}; diff --git a/src/std/sync/mutex.rs b/src/std/sync/mutex.rs index 79d1bef..aa3f9f1 100644 --- a/src/std/sync/mutex.rs +++ b/src/std/sync/mutex.rs @@ -246,7 +246,7 @@ mod tests { #![feature(test)] use super::*; - use crate::std::sync::mpsc::channel; + use crate::std::sync::channel::channel; use crate::std::sync::Condvar; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; diff --git a/src/std/sync/once.rs b/src/std/sync/once.rs index bf66c69..ebcbb15 100644 --- a/src/std/sync/once.rs +++ b/src/std/sync/once.rs @@ -52,7 +52,7 @@ mod test { use std::panic::catch_unwind; use std::sync::Arc; use crate::{chan, defer}; - use crate::std::sync::mpmc::Sender; + use crate::std::sync::channel::Sender; use crate::std::sync::Once; pub struct One { diff --git a/src/std/sync/rwlock.rs b/src/std/sync/rwlock.rs index b7f1ca7..7786b4c 100644 --- a/src/std/sync/rwlock.rs +++ b/src/std/sync/rwlock.rs @@ -323,7 +323,7 @@ impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T> { mod tests { #![feature(test)] - use crate::std::sync::mpsc::channel; + use crate::std::sync::channel::channel; use crate::std::sync::{Condvar, Mutex, RwLock}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, TryLockError}; diff --git a/src/std/sync/semphore.rs b/src/std/sync/semphore.rs index 89cb64c..e499474 100644 --- a/src/std/sync/semphore.rs +++ b/src/std/sync/semphore.rs @@ -178,7 +178,7 @@ mod tests { use std::sync::mpsc::TryRecvError; use std::thread; use std::time::Duration; - use crate::std::sync::mpsc::channel; + use crate::std::sync::channel::channel; #[test] diff --git a/src/std/time/tick.rs b/src/std/time/tick.rs index 32edaa0..02699e2 100644 --- a/src/std/time/tick.rs +++ b/src/std/time/tick.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, LockResult}; use std::sync::mpsc::RecvError; use std::time::Duration; use crate::coroutine::sleep; -use crate::std::sync::mpmc::{Receiver, Sender}; +use crate::std::sync::channel::{Receiver, Sender}; use crate::std::sync::Mutex; use crate::std::time::time::Time; use crate::std::errors::Result; diff --git a/tests/channel.rs b/tests/channel.rs index 706b5fc..c423e3e 100644 --- a/tests/channel.rs +++ b/tests/channel.rs @@ -1,7 +1,7 @@ use std::time::Duration; use cogo::coroutine::sleep; use cogo::go; -use cogo::std::sync::mpmc::channel; +use cogo::std::sync::channel::channel; use cogo::std::sync::WaitGroup; #[test] diff --git a/tests/cqueue.rs b/tests/cqueue.rs index c8e6ddb..764854b 100644 --- a/tests/cqueue.rs +++ b/tests/cqueue.rs @@ -115,7 +115,7 @@ fn cqueue_poll() { #[test] fn cqueue_oneshot() { // oneshot only support open set_work_steal true - use cogo::std::sync::mpsc::channel; + use cogo::std::sync::channel::channel; let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); @@ -145,7 +145,7 @@ fn cqueue_oneshot() { #[test] fn cqueue_select() { - use cogo::std::sync::mpsc::channel; + use cogo::std::sync::channel::channel; let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); @@ -179,7 +179,7 @@ fn cqueue_timeout() { #[test] fn cqueue_loop() { - use cogo::std::sync::mpsc::channel; + use cogo::std::sync::channel::channel; let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); diff --git a/tests/lib.rs b/tests/lib.rs index c16d52e..d562508 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -285,7 +285,7 @@ fn test_sleep() { #[test] fn join_macro() { - use cogo::std::sync::mpsc::channel; + use cogo::std::sync::channel::channel; let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); @@ -314,7 +314,7 @@ fn join_macro() { #[test] fn go_with_macro() { - use cogo::std::sync::mpsc::channel; + use cogo::std::sync::channel::channel; let (tx1, rx1) = channel(); let (tx2, rx2) = channel();