Skip to content

[discussion] task::block_on -> thread::block_on? #289

Closed
@yoshuawuyts

Description

@yoshuawuyts

Okay so this might be a terrible idea, and I'm not convinced myself -- but I think it's still worth bringing up because there might be value to it.

In #251 we're going to add task::blocking, but the name is kind of similar to task::block_on. The difference between the two is that one takes a closure that will block a thread, while the other one blocks the current thread:

fn block_on(fut: impl Future<Output = T>) -> T;
fn blocking(fut: impl Future<Output = T>) -> JoinHandle<T>;

What I'm slightly worried about is that people will not catch the difference between the two methods, and use block_on instead of blocking. And to be fair; this will probably work in most situations -- except performance will be terrible because this is not at all intended for this.

So instead I'm wondering if it would perhaps make sense to move block_on to be part of the thread submodule. It already has several other functions related to blocking the current thread, and this would be another one in that line.

// current
fn thread::park();
fn thread::park_timeout(dur: Duration);
fn thread::sleep(dur: Duration);
fn thread::yield_now();

// added
fn block_on(fut: impl Future<Output = T>) -> T;

A reason why I'm kind of wondering about using the thread submodule is because we probably want to give people access to the threadpool to configure it, and I feel thread may be a good option to put it (though I have no idea which knobs we'd want to allow people to turn). Which if we have that submodule anyway we may want to consider this direction also. I'm not sure though!

Perhaps another way to go about this would be to make block_on fail if called inside of a task context:

use crate::task::worker::get_task;

let task = get_task(|task| task.clone());
debug_assert!(task.is_none(), "`task::block_on()` called inside the context of a task. Did you mean to call `task::blocking?`");

Oh also; none of this is pressing at all, and just wanted to bring this up as something that may be interesting to consider. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    api designOpen design questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions