Commit Graph

45 Commits

Author SHA1 Message Date
Jacob Rothstein df57d9bc98
feat: reexport async_task::FallibleTask
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.
2024-04-11 16:33:17 -07:00
James Liu 649bdfda23
Support racy initialization of an Executor's state
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.
2024-04-08 19:41:14 -07:00
John Nunley 00f0b99fad chore: Silence clippy
Signed-off-by: John Nunley <dev@notgull.net>
2024-04-05 08:25:58 -07:00
John Nunley d3196999f4 feat: Add a way to batch spawn tasks
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>
2024-03-30 08:18:14 -07:00
John Nunley a2c1267c85 chore: Fix new nightly warnings
Signed-off-by: John Nunley <dev@notgull.net>
2024-03-25 06:51:06 -07:00
John Nunley 00dbbbf85d Revert "feat: Use actual thread local queues instead of using a RwLock"
This reverts commit 7592d4188a.
2024-03-25 06:51:06 -07:00
John Nunley c90fd306cd Revert "bugfix: Account for local queue corner cases"
This reverts commit 22a9e8b305.
2024-03-25 06:51:06 -07:00
John Nunley 22a9e8b305 bugfix: Account for local queue corner cases
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>
2024-03-12 20:38:37 -07:00
James Liu c7bbe489ab
Use wrapping add on ticks to avoid tick counter overflow in debug builds (#101) 2024-02-22 13:03:49 +09:00
James Liu 7592d4188a
feat: Use actual thread local queues instead of using a RwLock
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>
2024-02-21 19:53:40 -08:00
James Liu 188f976dc3
m: Weaken the atomic orderings for notification
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.
2024-02-17 12:20:57 -08:00
James Liu 568a314ad9
Avoid redundant lookups in the active slab when spawning new tasks (#96) 2024-02-17 17:02:59 +09:00
James Liu 7ffdf5ba92
m: Replace unnecessary atomics with non-atomic operations 2024-02-16 17:22:43 -08:00
John Nunley fa117dee27
Propagate panics in tasks (#78)
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>
2023-11-21 11:39:09 +01:00
John Nunley 6c3d45b23c
bugfix: Fix wasm32 compile errors
Signed-off-by: John Nunley <dev@notgull.net>
2023-11-11 10:15:04 -08:00
John Nunley f076528d27
Add a disclaimer saying this is a basic executor (#74)
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>
2023-11-11 08:34:46 -08:00
John Nunley 8a0832c090
m: Remove the thread-local executor optimization
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>
2023-10-16 18:50:00 -07:00
John Nunley 4154ad2190
Fix a bug where TLS would become None (#55)
* 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>
2023-09-27 20:01:15 -07:00
John Nunley a5ff8df7d9
bugfix: Ensure that ex.run() produces a Send future
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>
2023-08-20 17:08:35 -07:00
John Nunley aed7279805
Add smol-rs logo (#46) 2023-07-17 14:35:07 +09:00
John Nunley 9df3dd4974
alg: Push tasks directly to the local runner
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.
2023-07-02 11:29:19 -07:00
John Nunley 1a9e08ce73
Use fastrand v2.0.0 (#45) 2023-06-09 17:53:03 -07:00
John Nunley 8287e520b9
Implement debug output to be better (#33) 2022-12-25 07:12:59 -08:00
John Nunley 263ea89390
Replace once_cell with async-lock (#29) 2022-10-29 21:41:55 -07:00
Taiki Endo 2341801cd0 Fix clippy::redundant_closure warning 2021-12-30 09:38:02 +09:00
Taiki Endo b9ac443e56 Update slab to 0.4.4 2021-12-30 09:36:58 +09:00
Taiki Endo f25cd267ac Replace vec-arena with slab 2021-04-18 22:39:34 +09:00
Taiki Endo c4d019827f Replace deprecated compare_and_swap with compare_exchange 2020-12-24 21:22:43 +09:00
Stjepan Glavina 8dd3422176
Typo 2020-12-08 20:18:06 +01:00
Stjepan Glavina 38141bb5b4 Cleanup 2020-11-10 15:54:01 +01:00
Marc-Antoine Perennou 5a5ecd2763 add Executor::is_empty and LocalExecutor::is_empty
Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
2020-11-06 18:29:30 +01:00
Stjepan Glavina 29ba8a72ad Update futures-lite 2020-10-09 14:49:25 +02:00
Stjepan Glavina f9e28cd6d8 Make all executors scoped 2020-09-20 16:30:35 +02:00
Stjepan Glavina 525ac9fe7e Comments 2020-09-20 02:38:56 +02:00
Stjepan Glavina 8cea09da36 Update async-task 2020-09-20 02:36:54 +02:00
Stjepan Glavina 184185a7fa Refactor 2020-09-19 22:40:06 +02:00
Stjepan Glavina 6f2b0b8a49 Make executors scoped 2020-09-19 22:38:11 +02:00
Stjepan Glavina 31519f0cfc Replace AtomicU64 with AtomicUsize 2020-09-14 15:51:17 +02:00
Stjepan Glavina 5e08a9a351 Use atomics to make run() and tick() futures Send + Sync 2020-09-10 23:27:56 +02:00
Stjepan Glavina 2da645e6e0 Refactor 2020-08-29 19:57:21 +02:00
Stjepan Glavina d69638b2d3 simplify 2020-08-29 19:15:42 +02:00
Stjepan Glavina 6c6c1b1c2f Add tick() and try_tick() 2020-08-29 18:31:33 +02:00
Stjepan Glavina a28a9643c9 Redesign the whole interface 2020-08-26 23:46:56 +02:00
Marc-Antoine Perennou b9c846ec47 executor: add spawner
This allows spawning a task on an executor from outside of it

Fixes #1

Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
2020-07-23 15:15:53 +02:00
Stjepan Glavina e41f34cc00 Initial commit 2020-07-23 12:08:20 +02:00