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

Task creation should fall back to spawning a kernel thread #10647

Closed
alexcrichton opened this issue Nov 25, 2013 · 2 comments
Closed

Task creation should fall back to spawning a kernel thread #10647

alexcrichton opened this issue Nov 25, 2013 · 2 comments
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows

Comments

@alexcrichton
Copy link
Member

This is addressing the "Tasks" portion of #10493, and I'm opening this to become a clear action item.

If the task::spawn function is invoked, and there is no local Scheduler available (basically in_green_task_context() return false), then we should fall back to using a kernel thread to perform the spawn. The semantics, however, should remain the same. What this means is that the following steps need to happen:

  1. A new kernel thread is started, probably via rt::thread::Thread.
  2. This thread allocates ~Task and moves it into the appropriate thread-local-storage slot
  3. Invoke the equivalent of rt::task::Task.run. This will set up the C++ try/catch block, as well as cleaning up unconditionally after the task runs. (things like deallocating task-local storage, cleaning up GC, clearing out the stdout/logger handles, etc).
  4. Run the input closure.

In addition, to these steps, the current semantics of task::spawn is that nothing is immediately returned, meaning that just using Thread::start will not work (because it returns a handle). I believe this will involve adding a detach function to the Thread struct which would do something along the lines of pthread_detach on unix and CloseHandle on windows.

With this in place, I should be able to invoke task::spawn with and without the runtime. It is OK to require that a one-time runtime initialization function is run before any of this spawning code to run, but it is not OK for this one-time initialization function to require all future code to be run in a closure (i.e. what happens today).

Hence, a program like this should run successfully:

fn start(argc: int, argv: **u8) -> int {
  rt::init(argc, argv); // may optional, maybe not, depends on implementation
  do spawn {
    println!("hello from a new rust task running on a different thread!");
  }
  3
}
@alexcrichton
Copy link
Member Author

This will be closed by #10965

@alexcrichton
Copy link
Member Author

Done.

flip1995 pushed a commit to flip1995/rust that referenced this issue May 5, 2023
new lint: `manual_while_let_some`

This PR implements the lint I suggested [on zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/lint.20on.20while.20pop.20unwrap).
It looks for while loops like these:
```rs
let mut numbers = vec![0, 1, 2];
while !numbers.is_empty() {
  let number = numbers.pop().unwrap();
  // use `number`
}
```
and suggests replacing it with a while-let loop, like this:
```rs
let mut numbers = vec![0, 1, 2];
while let Some(number) = numbers.pop() {
  // use `number`
}
```
... which is more concise and idiomatic.

It only looks for `Vec::pop()` calls in the first statement of the loop body in an attempt to not trigger FPs (as pop might only be called conditionally).

changelog: new lint [`manual_while_let_some`]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows
Projects
None yet
Development

No branches or pull requests

1 participant