Motivation: FallibleTask is part of the public interface of this crate, in that Task::fallible returns FallibleTask. However, in order to name that type, users need to add a direct dependency on async_task and ensure the crates versions are compatible. Reexporting allows crate users to name the type directly.
Fixes#89. Uses @notgull's suggestion of using a `AtomicPtr` with a racy initialization instead of a `OnceCell`.
For the addition of more `unsafe`, I added the `clippy::undocumented_unsafe_blocks` lint at a warn, and fixed a few of the remaining open clippy issues (i.e. `Waker::clone_from` already handling the case where they're equal).
Removing `async_lock` as a dependency shouldn't be a SemVer breaking change.
For some workloads many tasks are spawned at a time. This requires
locking and unlocking the executor's inner lock every time you spawn a
task. If you spawn many tasks this can be expensive.
This commit exposes a new "spawn_batch" method on both types. This
method allows the user to spawn an entire set of tasks at a time.
Closes#91
Signed-off-by: John Nunley <dev@notgull.net>
It turns out that with the current strategy it is possible for tasks to
be stuck in the local queue without any hope of being picked back up.
In practice this seems to happen when the only entities polling the
system are tickers, as opposed to runners. Since tickets don't steal
tasks, it is possible for tasks to be left over in the local queue that
don't filter out.
One possible solution is to make it so tickers steal tasks, but this
kind of defeats the point of tickers. So I've instead elected to replace
the current strategy with one that accounts for the corner cases with
local queues.
The main difference is that I replace the Sleepers struct with two
event_listener::Event's. One that handles tickers subscribed to the
global queue and one that handles tickers subscribed to the local queue.
The other main difference is that each local queue now has a reference
counter. If this count reaches zero, no tasks will be pushed to this
queue. Only runners increment or decrement this counter.
This makes the previously instituted tests pass, so hopefully this works
for most use cases.
Signed-off-by: John Nunley <dev@notgull.net>
Currently, runner local queues rely on a RwLock<Vec<Arc<ConcurrentQueue>>>> to store the queues instead of using actual thread-local storage.
This adds thread_local as a dependency, but this should allow the executor to work steal without needing to hold a lock, as well as allow tasks to schedule onto the local queue directly, where possible, instead of always relying on the global injector queue.
Fixes#62
Co-authored-by: John Nunley <jtnunley01@gmail.com>
The atomic orderings on State::notified might be too strong, as it's primarily
being used as a deterrent against waking up too many threads. This PR weakens
their sequentially consistent operations to Acquire/Release.
Whoops, I accidentally reinvented a semaphore and made the example a lot
more complicated than it needed to be.
Signed-off-by: John Nunley <dev@notgull.net>
After smol-rs/async-task#37 I meant to add this to the executor. This
commit makes it so all panics are surfaced in the tasks that the user
calls. Hopefully this improves ergonomics.
Signed-off-by: John Nunley <dev@notgull.net>
Signed-off-by: Alain Zscheile <fogti+devel@ytrizja.de>
`futures-lite` in the dev dependencies added a `block_on` call that was
not present in the WASM build, causing a compile error. This PR makes
sure that the `std` feature of `futures-lite` is enabled in Cargo.toml.
This also adds a CI check to ensure that this doesn't happen again
Signed-off-by: John Nunley <dev@notgull.net>
In many issues I've mentioned that the executors in this crate are just
reference executors. However, this is not documented in the crate
itself.
This commit adds a disclaimer to the crate documentation and to
README.md that these are reference executors that shouldn't be relied on
for performance.
Signed-off-by: John Nunley <dev@notgull.net>
This was added in #37 as an optimization, but has since lead to many bugs. See
the issues #53, #57 and #60 for more information. I do not have the bandwidth
to address all of these bugs, so I'm taking the path of least resistance by
just removing the problematic code.
CLoses#53, #57 and #60
Signed-off-by: John Nunley <dev@notgull.net>
* Fix a bug where TLS would become None
The bug is invoked as follows:
- Runner 1 is created and stores the current version of the TLS
LOCAL_QUEUE variable, which is None.
- Runner 2 is also created. It stores the current version of the TLS
variable as well, which is Runner 1's queue.
- Runner 1 is dropped. It stores None into the LOCAL_QUEUE variable.
- Runner 2 tries to run. It reads from the LOCAL_QUEUE variable, sees
that it is None, and panics.
This could be solved by just not using the local queue if the variable
is None. However, we can do one better; if the slot is open, we can
optimize the runner by replacing it with our own queue. This should
allow for the local queue to be used more often.
Closes#54
Signed-off-by: John Nunley <dev@notgull.net>
This commit makes sure that the run() and tick() functions produce
futures that are Send and Sync, to prevent a regression introduced in
PR #37. Tests are also added to prevent this regression in the future.
Signed-off-by: John Nunley <dev@notgull.net>
This commit adds an optimization where a thread-local variable contains the queue of the
current runner. Rather than pushing to the global queue and hoping that a local queue
eventually picks it up, tasks are pushed directly to this local queue if available.
This has led to speedups of up to 70% in some cases and up to 10% in other workloads.