Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't make MainExecutor private/hidden #10

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 5 additions & 126 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ macro_rules! main {
) => {
$(#[$attr])*
fn $name () $(-> $ret)? {
$crate::__private::block_on(async {
async_io::block_on(async {
$bl
})
}
Expand All @@ -149,8 +149,8 @@ macro_rules! main {
) => {
$(#[$post_attr])*
fn $name () $(-> $ret)? {
<$exty as $crate::__private::MainExecutor>::with_main(|ex| {
$crate::__private::block_on(ex.run(async move {
<$exty as $crate::main_executor::MainExecutor>::with_main(|ex| {
async_io::block_on(ex.run(async move {
let $ex = ex;
$bl
}))
Expand Down Expand Up @@ -216,126 +216,5 @@ macro_rules! test {
};
}

#[doc(hidden)]
pub mod __private {
pub use async_io::block_on;
pub use std::rc::Rc;

use crate::{Executor, LocalExecutor};
use event_listener::Event;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;

/// Something that can be set up as an executor.
#[doc(hidden)]
pub trait MainExecutor: Sized {
/// Create this type and pass it into `main`.
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T;
}

impl MainExecutor for Arc<Executor<'_>> {
#[inline]
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
let ex = Arc::new(Executor::new());
with_thread_pool(&ex, || f(&ex))
}
}

impl MainExecutor for Executor<'_> {
#[inline]
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
let ex = Executor::new();
with_thread_pool(&ex, || f(&ex))
}
}

impl MainExecutor for Rc<LocalExecutor<'_>> {
#[inline]
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
f(&Rc::new(LocalExecutor::new()))
}
}

impl MainExecutor for LocalExecutor<'_> {
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
f(&LocalExecutor::new())
}
}

/// Run a function that takes an `Executor` inside of a thread pool.
#[inline]
fn with_thread_pool<T>(ex: &Executor<'_>, f: impl FnOnce() -> T) -> T {
let stopper = WaitForStop::new();

// Create a thread for each CPU.
thread::scope(|scope| {
let num_threads = thread::available_parallelism().map_or(1, |num| num.get());
for i in 0..num_threads {
let ex = &ex;
let stopper = &stopper;

thread::Builder::new()
.name(format!("smol-macros-{i}"))
.spawn_scoped(scope, || {
block_on(ex.run(stopper.wait()));
})
.expect("failed to spawn thread");
}

let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));

stopper.stop();

match result {
Ok(value) => value,
Err(err) => std::panic::resume_unwind(err),
}
})
}

/// Wait for the executor to stop.
struct WaitForStop {
/// Whether or not we need to stop.
stopped: AtomicBool,

/// Wait for the stop.
events: Event,
}

impl WaitForStop {
/// Create a new wait for stop.
#[inline]
fn new() -> Self {
Self {
stopped: AtomicBool::new(false),
events: Event::new(),
}
}

/// Wait for the event to stop.
#[inline]
async fn wait(&self) {
loop {
if self.stopped.load(Ordering::Relaxed) {
return;
}

event_listener::listener!(&self.events => listener);

if self.stopped.load(Ordering::Acquire) {
return;
}

listener.await;
}
}

/// Stop the waiter.
#[inline]
fn stop(&self) {
self.stopped.store(true, Ordering::SeqCst);
self.events.notify_additional(usize::MAX);
}
}
}
pub mod main_executor;
pub mod wait_for_stop;
72 changes: 72 additions & 0 deletions src/main_executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::rc::Rc;
use std::sync::Arc;
use std::thread;

use crate::wait_for_stop::WaitForStop;
use crate::{Executor, LocalExecutor};

/// Something that can be set up as an executor.
pub trait MainExecutor: Sized {
/// Create this type and pass it into `main`.
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T;
}

impl MainExecutor for Arc<Executor<'_>> {
#[inline]
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
let ex = Arc::new(Executor::new());
with_thread_pool(&ex, || f(&ex))
}
}

impl MainExecutor for Executor<'_> {
#[inline]
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
let ex = Executor::new();
with_thread_pool(&ex, || f(&ex))
}
}

impl MainExecutor for Rc<LocalExecutor<'_>> {
#[inline]
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
f(&Rc::new(LocalExecutor::new()))
}
}

impl MainExecutor for LocalExecutor<'_> {
fn with_main<T, F: FnOnce(&Self) -> T>(f: F) -> T {
f(&LocalExecutor::new())
}
}

/// Run a function that takes an `Executor` inside of a thread pool.
#[inline]
fn with_thread_pool<T>(ex: &Executor<'_>, f: impl FnOnce() -> T) -> T {
let stopper = WaitForStop::new();

// Create a thread for each CPU.
thread::scope(|scope| {
let num_threads = thread::available_parallelism().map_or(1, |num| num.get());
for i in 0..num_threads {
let ex = &ex;
let stopper = &stopper;

thread::Builder::new()
.name(format!("smol-macros-{i}"))
.spawn_scoped(scope, || {
async_io::block_on(ex.run(stopper.wait()));
})
.expect("failed to spawn thread");
}

let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));

stopper.stop();

match result {
Ok(value) => value,
Err(err) => std::panic::resume_unwind(err),
}
})
}
47 changes: 47 additions & 0 deletions src/wait_for_stop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use event_listener::Event;
use std::sync::atomic::{AtomicBool, Ordering};

/// Wait for the executor to stop.
pub(crate) struct WaitForStop {
/// Whether or not we need to stop.
stopped: AtomicBool,

/// Wait for the stop.
events: Event,
}

impl WaitForStop {
/// Create a new wait for stop.
#[inline]
pub(crate) fn new() -> Self {
Self {
stopped: AtomicBool::new(false),
events: Event::new(),
}
}

/// Wait for the event to stop.
#[inline]
pub(crate) async fn wait(&self) {
loop {
if self.stopped.load(Ordering::Relaxed) {
return;
}

event_listener::listener!(&self.events => listener);

if self.stopped.load(Ordering::Acquire) {
return;
}

listener.await;
}
}

/// Stop the waiter.
#[inline]
pub(crate) fn stop(&self) {
self.stopped.store(true, Ordering::SeqCst);
self.events.notify_additional(usize::MAX);
}
}
Loading