summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2023-03-06 10:16:27 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2023-03-06 10:16:27 +0900
commit57cc1c96abfe96c31b79debdbfaa91f5764e4011 (patch)
treec6fdd771f7e05782a539beb3ad2da266fd5a7c27
downloadrust-async-executor-57cc1c96abfe96c31b79debdbfaa91f5764e4011.tar.gz
rust-async-executor-57cc1c96abfe96c31b79debdbfaa91f5764e4011.tar.bz2
rust-async-executor-57cc1c96abfe96c31b79debdbfaa91f5764e4011.zip
Import async-executor 1.5.0upstream/1.5.0upstream
-rw-r--r--.cargo_vcs_info.json6
-rw-r--r--CHANGELOG.md48
-rw-r--r--Cargo.lock303
-rw-r--r--Cargo.toml66
-rw-r--r--Cargo.toml.orig30
-rw-r--r--LICENSE-APACHE201
-rw-r--r--LICENSE-MIT23
-rw-r--r--README.md45
-rw-r--r--benches/executor.rs109
-rw-r--r--examples/priority.rs85
-rw-r--r--src/lib.rs843
-rw-r--r--tests/drop.rs128
12 files changed, 1887 insertions, 0 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
new file mode 100644
index 0000000..46757e0
--- /dev/null
+++ b/.cargo_vcs_info.json
@@ -0,0 +1,6 @@
+{
+ "git": {
+ "sha1": "00ea6cf6a19d23c1464790e53fe3f61ea0e94271"
+ },
+ "path_in_vcs": ""
+} \ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..0105c9e
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,48 @@
+# Version 1.5.0
+
+- Remove the dependency on the `once_cell` crate to restore the MSRV. (#29)
+- Update `concurrent-queue` to v2.
+
+# Version 1.4.1
+
+- Remove dependency on deprecated `vec-arena`. (#23)
+
+# Version 1.4.0
+
+- Add `Executor::is_empty()` and `LocalExecutor::is_empty()`.
+
+# Version 1.3.0
+
+- Parametrize executors over a lifetime to allow spawning non-`static` futures.
+
+# Version 1.2.0
+
+- Update `async-task` to v4.
+
+# Version 1.1.1
+
+- Replace `AtomicU64` with `AtomicUsize`.
+
+# Version 1.1.0
+
+- Use atomics to make `Executor::run()` and `Executor::tick()` futures `Send + Sync`.
+
+# Version 1.0.0
+
+- Stabilize.
+
+# Version 0.2.1
+
+- Add `try_tick()` and `tick()` methods.
+
+# Version 0.2.0
+
+- Redesign the whole API.
+
+# Version 0.1.2
+
+- Add the `Spawner` API.
+
+# Version 0.1.1
+
+- Initial version
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..4c46d67
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,303 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "async-channel"
+version = "1.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e14485364214912d3b19cc3435dde4df66065127f05fa0d75c712f36f12c2f28"
+dependencies = [
+ "concurrent-queue 1.2.4",
+ "event-listener",
+ "futures-core",
+]
+
+[[package]]
+name = "async-executor"
+version = "1.5.0"
+dependencies = [
+ "async-channel",
+ "async-io",
+ "async-lock",
+ "async-task",
+ "concurrent-queue 2.0.0",
+ "easy-parallel",
+ "fastrand",
+ "futures-lite",
+ "num_cpus",
+ "once_cell",
+ "slab",
+]
+
+[[package]]
+name = "async-io"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "83e21f3a490c72b3b0cf44962180e60045de2925d8dff97918f7ee43c8f637c7"
+dependencies = [
+ "autocfg",
+ "concurrent-queue 1.2.4",
+ "futures-lite",
+ "libc",
+ "log",
+ "once_cell",
+ "parking",
+ "polling",
+ "slab",
+ "socket2",
+ "waker-fn",
+ "winapi",
+]
+
+[[package]]
+name = "async-lock"
+version = "2.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c8101efe8695a6c17e02911402145357e718ac92d3ff88ae8419e84b1707b685"
+dependencies = [
+ "event-listener",
+ "futures-lite",
+]
+
+[[package]]
+name = "async-task"
+version = "4.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a40729d2133846d9ed0ea60a8b9541bccddab49cd30f0715a1da672fe9a2524"
+
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
+[[package]]
+name = "cache-padded"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c"
+
+[[package]]
+name = "cc"
+version = "1.0.73"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "concurrent-queue"
+version = "1.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af4780a44ab5696ea9e28294517f1fffb421a83a25af521333c838635509db9c"
+dependencies = [
+ "cache-padded",
+]
+
+[[package]]
+name = "concurrent-queue"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bd7bef69dc86e3c610e4e7aed41035e2a7ed12e72dd7530f61327a6579a4390b"
+dependencies = [
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "easy-parallel"
+version = "3.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6907e25393cdcc1f4f3f513d9aac1e840eb1cc341a0fccb01171f7d14d10b946"
+
+[[package]]
+name = "event-listener"
+version = "2.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
+
+[[package]]
+name = "fastrand"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499"
+dependencies = [
+ "instant",
+]
+
+[[package]]
+name = "futures-core"
+version = "0.3.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac"
+
+[[package]]
+name = "futures-io"
+version = "0.3.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "00f5fb52a06bdcadeb54e8d3671f8888a39697dcb0b81b23b55174030427f4eb"
+
+[[package]]
+name = "futures-lite"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48"
+dependencies = [
+ "fastrand",
+ "futures-core",
+ "futures-io",
+ "memchr",
+ "parking",
+ "pin-project-lite",
+ "waker-fn",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "instant"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.137"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
+
+[[package]]
+name = "log"
+version = "0.4.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "memchr"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
+
+[[package]]
+name = "num_cpus"
+version = "1.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
+dependencies = [
+ "hermit-abi",
+ "libc",
+]
+
+[[package]]
+name = "once_cell"
+version = "1.16.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
+
+[[package]]
+name = "parking"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
+
+[[package]]
+name = "pin-project-lite"
+version = "0.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
+
+[[package]]
+name = "polling"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ab4609a838d88b73d8238967b60dd115cc08d38e2bbaf51ee1e4b695f89122e2"
+dependencies = [
+ "autocfg",
+ "cfg-if",
+ "libc",
+ "log",
+ "wepoll-ffi",
+ "winapi",
+]
+
+[[package]]
+name = "slab"
+version = "0.4.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "socket2"
+version = "0.4.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd"
+dependencies = [
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "waker-fn"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
+
+[[package]]
+name = "wepoll-ffi"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d743fdedc5c64377b5fc2bc036b01c7fd642205a0d96356034ae3404d49eb7fb"
+dependencies = [
+ "cc",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..2628cdf
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,66 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2018"
+rust-version = "1.47"
+name = "async-executor"
+version = "1.5.0"
+authors = ["Stjepan Glavina <stjepang@gmail.com>"]
+exclude = ["/.*"]
+description = "Async executor"
+readme = "README.md"
+keywords = [
+ "asynchronous",
+ "executor",
+ "single",
+ "multi",
+ "spawn",
+]
+categories = [
+ "asynchronous",
+ "concurrency",
+]
+license = "Apache-2.0 OR MIT"
+repository = "https://github.com/smol-rs/async-executor"
+
+[dependencies.async-lock]
+version = "2.6"
+
+[dependencies.async-task]
+version = "4.0.0"
+
+[dependencies.concurrent-queue]
+version = "2.0.0"
+
+[dependencies.fastrand]
+version = "1.3.4"
+
+[dependencies.futures-lite]
+version = "1.11.0"
+
+[dependencies.slab]
+version = "0.4.4"
+
+[dev-dependencies.async-channel]
+version = "1.4.1"
+
+[dev-dependencies.async-io]
+version = "1.1.9"
+
+[dev-dependencies.easy-parallel]
+version = "3.1.0"
+
+[dev-dependencies.num_cpus]
+version = "1.13.0"
+
+[dev-dependencies.once_cell]
+version = "1.16.0"
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
new file mode 100644
index 0000000..faee198
--- /dev/null
+++ b/Cargo.toml.orig
@@ -0,0 +1,30 @@
+[package]
+name = "async-executor"
+# When publishing a new version:
+# - Update CHANGELOG.md
+# - Create "v1.x.y" git tag
+version = "1.5.0"
+authors = ["Stjepan Glavina <stjepang@gmail.com>"]
+edition = "2018"
+rust-version = "1.47"
+description = "Async executor"
+license = "Apache-2.0 OR MIT"
+repository = "https://github.com/smol-rs/async-executor"
+keywords = ["asynchronous", "executor", "single", "multi", "spawn"]
+categories = ["asynchronous", "concurrency"]
+exclude = ["/.*"]
+
+[dependencies]
+async-lock = "2.6"
+async-task = "4.0.0"
+concurrent-queue = "2.0.0"
+fastrand = "1.3.4"
+futures-lite = "1.11.0"
+slab = "0.4.4"
+
+[dev-dependencies]
+async-channel = "1.4.1"
+async-io = "1.1.9"
+easy-parallel = "3.1.0"
+num_cpus = "1.13.0"
+once_cell = "1.16.0"
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644
index 0000000..16fe87b
--- /dev/null
+++ b/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..31aa793
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,23 @@
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c330bd6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,45 @@
+# async-executor
+
+[![Build](https://github.com/smol-rs/async-executor/workflows/Build%20and%20test/badge.svg)](
+https://github.com/smol-rs/async-executor/actions)
+[![License](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue.svg)](
+https://github.com/smol-rs/async-executor)
+[![Cargo](https://img.shields.io/crates/v/async-executor.svg)](
+https://crates.io/crates/async-executor)
+[![Documentation](https://docs.rs/async-executor/badge.svg)](
+https://docs.rs/async-executor)
+
+Async executors.
+
+## Examples
+
+```rust
+use async_executor::Executor;
+use futures_lite::future;
+
+// Create a new executor.
+let ex = Executor::new();
+
+// Spawn a task.
+let task = ex.spawn(async {
+ println!("Hello world");
+});
+
+// Run the executor until the task completes.
+future::block_on(ex.run(task));
+```
+
+## License
+
+Licensed under either of
+
+ * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
+ * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
+
+at your option.
+
+#### Contribution
+
+Unless you explicitly state otherwise, any contribution intentionally submitted
+for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
+dual licensed as above, without any additional terms or conditions.
diff --git a/benches/executor.rs b/benches/executor.rs
new file mode 100644
index 0000000..98f1cb5
--- /dev/null
+++ b/benches/executor.rs
@@ -0,0 +1,109 @@
+#![feature(test)]
+
+extern crate test;
+
+use std::future::Future;
+
+use async_executor::Executor;
+use futures_lite::{future, prelude::*};
+
+const TASKS: usize = 300;
+const STEPS: usize = 300;
+const LIGHT_TASKS: usize = 25_000;
+
+static EX: Executor<'_> = Executor::new();
+
+fn run(f: impl FnOnce()) {
+ let (s, r) = async_channel::bounded::<()>(1);
+ easy_parallel::Parallel::new()
+ .each(0..num_cpus::get(), |_| future::block_on(EX.run(r.recv())))
+ .finish(move || {
+ let _s = s;
+ f()
+ });
+}
+
+#[bench]
+fn create(b: &mut test::Bencher) {
+ b.iter(move || {
+ let ex = Executor::new();
+ let task = ex.spawn(async {});
+ future::block_on(ex.run(task));
+ });
+}
+
+#[bench]
+fn spawn_one(b: &mut test::Bencher) {
+ run(|| {
+ b.iter(move || {
+ future::block_on(async { EX.spawn(async {}).await });
+ });
+ });
+}
+
+#[bench]
+fn spawn_many(b: &mut test::Bencher) {
+ run(|| {
+ b.iter(move || {
+ future::block_on(async {
+ let mut tasks = Vec::new();
+ for _ in 0..LIGHT_TASKS {
+ tasks.push(EX.spawn(async {}));
+ }
+ for task in tasks {
+ task.await;
+ }
+ });
+ });
+ });
+}
+
+#[bench]
+fn spawn_recursively(b: &mut test::Bencher) {
+ fn go(i: usize) -> impl Future<Output = ()> + Send + 'static {
+ async move {
+ if i != 0 {
+ EX.spawn(async move {
+ let fut = go(i - 1).boxed();
+ fut.await;
+ })
+ .await;
+ }
+ }
+ }
+
+ run(|| {
+ b.iter(move || {
+ future::block_on(async {
+ let mut tasks = Vec::new();
+ for _ in 0..TASKS {
+ tasks.push(EX.spawn(go(STEPS)));
+ }
+ for task in tasks {
+ task.await;
+ }
+ });
+ });
+ });
+}
+
+#[bench]
+fn yield_now(b: &mut test::Bencher) {
+ run(|| {
+ b.iter(move || {
+ future::block_on(async {
+ let mut tasks = Vec::new();
+ for _ in 0..TASKS {
+ tasks.push(EX.spawn(async move {
+ for _ in 0..STEPS {
+ future::yield_now().await;
+ }
+ }));
+ }
+ for task in tasks {
+ task.await;
+ }
+ });
+ });
+ });
+}
diff --git a/examples/priority.rs b/examples/priority.rs
new file mode 100644
index 0000000..df77dd1
--- /dev/null
+++ b/examples/priority.rs
@@ -0,0 +1,85 @@
+//! An executor with task priorities.
+
+use std::future::Future;
+use std::thread;
+
+use async_executor::{Executor, Task};
+use futures_lite::{future, prelude::*};
+
+/// Task priority.
+#[repr(usize)]
+#[derive(Debug, Clone, Copy)]
+enum Priority {
+ High = 0,
+ Medium = 1,
+ Low = 2,
+}
+
+/// An executor with task priorities.
+///
+/// Tasks with lower priorities only get polled when there are no tasks with higher priorities.
+struct PriorityExecutor<'a> {
+ ex: [Executor<'a>; 3],
+}
+
+impl<'a> PriorityExecutor<'a> {
+ /// Creates a new executor.
+ const fn new() -> PriorityExecutor<'a> {
+ PriorityExecutor {
+ ex: [Executor::new(), Executor::new(), Executor::new()],
+ }
+ }
+
+ /// Spawns a task with the given priority.
+ fn spawn<T: Send + 'a>(
+ &self,
+ priority: Priority,
+ future: impl Future<Output = T> + Send + 'a,
+ ) -> Task<T> {
+ self.ex[priority as usize].spawn(future)
+ }
+
+ /// Runs the executor forever.
+ async fn run(&self) {
+ loop {
+ for _ in 0..200 {
+ let t0 = self.ex[0].tick();
+ let t1 = self.ex[1].tick();
+ let t2 = self.ex[2].tick();
+
+ // Wait until one of the ticks completes, trying them in order from highest
+ // priority to lowest priority.
+ t0.or(t1).or(t2).await;
+ }
+
+ // Yield every now and then.
+ future::yield_now().await;
+ }
+ }
+}
+
+fn main() {
+ static EX: PriorityExecutor<'_> = PriorityExecutor::new();
+
+ // Spawn a thread running the executor forever.
+ thread::spawn(|| future::block_on(EX.run()));
+
+ let mut tasks = Vec::new();
+
+ for _ in 0..20 {
+ // Choose a random priority.
+ let choice = [Priority::High, Priority::Medium, Priority::Low];
+ let priority = choice[fastrand::usize(..choice.len())];
+
+ // Spawn a task with this priority.
+ tasks.push(EX.spawn(priority, async move {
+ println!("{:?}", priority);
+ future::yield_now().await;
+ println!("{:?}", priority);
+ }));
+ }
+
+ for task in tasks {
+ future::block_on(task);
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..24fbfac
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,843 @@
+//! Async executors.
+//!
+//! # Examples
+//!
+//! ```
+//! use async_executor::Executor;
+//! use futures_lite::future;
+//!
+//! // Create a new executor.
+//! let ex = Executor::new();
+//!
+//! // Spawn a task.
+//! let task = ex.spawn(async {
+//! println!("Hello world");
+//! });
+//!
+//! // Run the executor until the task completes.
+//! future::block_on(ex.run(task));
+//! ```
+
+#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
+
+use std::future::Future;
+use std::marker::PhantomData;
+use std::panic::{RefUnwindSafe, UnwindSafe};
+use std::rc::Rc;
+use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
+use std::sync::{Arc, Mutex, RwLock};
+use std::task::{Poll, Waker};
+
+use async_lock::OnceCell;
+use async_task::Runnable;
+use concurrent_queue::ConcurrentQueue;
+use futures_lite::{future, prelude::*};
+use slab::Slab;
+
+#[doc(no_inline)]
+pub use async_task::Task;
+
+/// An async executor.
+///
+/// # Examples
+///
+/// A multi-threaded executor:
+///
+/// ```
+/// use async_channel::unbounded;
+/// use async_executor::Executor;
+/// use easy_parallel::Parallel;
+/// use futures_lite::future;
+///
+/// let ex = Executor::new();
+/// let (signal, shutdown) = unbounded::<()>();
+///
+/// Parallel::new()
+/// // Run four executor threads.
+/// .each(0..4, |_| future::block_on(ex.run(shutdown.recv())))
+/// // Run the main future on the current thread.
+/// .finish(|| future::block_on(async {
+/// println!("Hello world!");
+/// drop(signal);
+/// }));
+/// ```
+#[derive(Debug)]
+pub struct Executor<'a> {
+ /// The executor state.
+ state: OnceCell<Arc<State>>,
+
+ /// Makes the `'a` lifetime invariant.
+ _marker: PhantomData<std::cell::UnsafeCell<&'a ()>>,
+}
+
+unsafe impl Send for Executor<'_> {}
+unsafe impl Sync for Executor<'_> {}
+
+impl UnwindSafe for Executor<'_> {}
+impl RefUnwindSafe for Executor<'_> {}
+
+impl<'a> Executor<'a> {
+ /// Creates a new executor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::Executor;
+ ///
+ /// let ex = Executor::new();
+ /// ```
+ pub const fn new() -> Executor<'a> {
+ Executor {
+ state: OnceCell::new(),
+ _marker: PhantomData,
+ }
+ }
+
+ /// Returns `true` if there are no unfinished tasks.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::Executor;
+ ///
+ /// let ex = Executor::new();
+ /// assert!(ex.is_empty());
+ ///
+ /// let task = ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// assert!(!ex.is_empty());
+ ///
+ /// assert!(ex.try_tick());
+ /// assert!(ex.is_empty());
+ /// ```
+ pub fn is_empty(&self) -> bool {
+ self.state().active.lock().unwrap().is_empty()
+ }
+
+ /// Spawns a task onto the executor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::Executor;
+ ///
+ /// let ex = Executor::new();
+ ///
+ /// let task = ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// ```
+ pub fn spawn<T: Send + 'a>(&self, future: impl Future<Output = T> + Send + 'a) -> Task<T> {
+ let mut active = self.state().active.lock().unwrap();
+
+ // Remove the task from the set of active tasks when the future finishes.
+ let index = active.vacant_entry().key();
+ let state = self.state().clone();
+ let future = async move {
+ let _guard = CallOnDrop(move || drop(state.active.lock().unwrap().try_remove(index)));
+ future.await
+ };
+
+ // Create the task and register it in the set of active tasks.
+ let (runnable, task) = unsafe { async_task::spawn_unchecked(future, self.schedule()) };
+ active.insert(runnable.waker());
+
+ runnable.schedule();
+ task
+ }
+
+ /// Attempts to run a task if at least one is scheduled.
+ ///
+ /// Running a scheduled task means simply polling its future once.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::Executor;
+ ///
+ /// let ex = Executor::new();
+ /// assert!(!ex.try_tick()); // no tasks to run
+ ///
+ /// let task = ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// assert!(ex.try_tick()); // a task was found
+ /// ```
+ pub fn try_tick(&self) -> bool {
+ match self.state().queue.pop() {
+ Err(_) => false,
+ Ok(runnable) => {
+ // Notify another ticker now to pick up where this ticker left off, just in case
+ // running the task takes a long time.
+ self.state().notify();
+
+ // Run the task.
+ runnable.run();
+ true
+ }
+ }
+ }
+
+ /// Runs a single task.
+ ///
+ /// Running a task means simply polling its future once.
+ ///
+ /// If no tasks are scheduled when this method is called, it will wait until one is scheduled.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::Executor;
+ /// use futures_lite::future;
+ ///
+ /// let ex = Executor::new();
+ ///
+ /// let task = ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// future::block_on(ex.tick()); // runs the task
+ /// ```
+ pub async fn tick(&self) {
+ let state = self.state();
+ let runnable = Ticker::new(state).runnable().await;
+ runnable.run();
+ }
+
+ /// Runs the executor until the given future completes.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::Executor;
+ /// use futures_lite::future;
+ ///
+ /// let ex = Executor::new();
+ ///
+ /// let task = ex.spawn(async { 1 + 2 });
+ /// let res = future::block_on(ex.run(async { task.await * 2 }));
+ ///
+ /// assert_eq!(res, 6);
+ /// ```
+ pub async fn run<T>(&self, future: impl Future<Output = T>) -> T {
+ let runner = Runner::new(self.state());
+
+ // A future that runs tasks forever.
+ let run_forever = async {
+ loop {
+ for _ in 0..200 {
+ let runnable = runner.runnable().await;
+ runnable.run();
+ }
+ future::yield_now().await;
+ }
+ };
+
+ // Run `future` and `run_forever` concurrently until `future` completes.
+ future.or(run_forever).await
+ }
+
+ /// Returns a function that schedules a runnable task when it gets woken up.
+ fn schedule(&self) -> impl Fn(Runnable) + Send + Sync + 'static {
+ let state = self.state().clone();
+
+ // TODO(stjepang): If possible, push into the current local queue and notify the ticker.
+ move |runnable| {
+ state.queue.push(runnable).unwrap();
+ state.notify();
+ }
+ }
+
+ /// Returns a reference to the inner state.
+ fn state(&self) -> &Arc<State> {
+ self.state.get_or_init_blocking(|| Arc::new(State::new()))
+ }
+}
+
+impl Drop for Executor<'_> {
+ fn drop(&mut self) {
+ if let Some(state) = self.state.get() {
+ let mut active = state.active.lock().unwrap();
+ for w in active.drain() {
+ w.wake();
+ }
+ drop(active);
+
+ while state.queue.pop().is_ok() {}
+ }
+ }
+}
+
+impl<'a> Default for Executor<'a> {
+ fn default() -> Executor<'a> {
+ Executor::new()
+ }
+}
+
+/// A thread-local executor.
+///
+/// The executor can only be run on the thread that created it.
+///
+/// # Examples
+///
+/// ```
+/// use async_executor::LocalExecutor;
+/// use futures_lite::future;
+///
+/// let local_ex = LocalExecutor::new();
+///
+/// future::block_on(local_ex.run(async {
+/// println!("Hello world!");
+/// }));
+/// ```
+#[derive(Debug)]
+pub struct LocalExecutor<'a> {
+ /// The inner executor.
+ inner: Executor<'a>,
+
+ /// Makes the type `!Send` and `!Sync`.
+ _marker: PhantomData<Rc<()>>,
+}
+
+impl UnwindSafe for LocalExecutor<'_> {}
+impl RefUnwindSafe for LocalExecutor<'_> {}
+
+impl<'a> LocalExecutor<'a> {
+ /// Creates a single-threaded executor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::LocalExecutor;
+ ///
+ /// let local_ex = LocalExecutor::new();
+ /// ```
+ pub const fn new() -> LocalExecutor<'a> {
+ LocalExecutor {
+ inner: Executor::new(),
+ _marker: PhantomData,
+ }
+ }
+
+ /// Returns `true` if there are no unfinished tasks.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::LocalExecutor;
+ ///
+ /// let local_ex = LocalExecutor::new();
+ /// assert!(local_ex.is_empty());
+ ///
+ /// let task = local_ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// assert!(!local_ex.is_empty());
+ ///
+ /// assert!(local_ex.try_tick());
+ /// assert!(local_ex.is_empty());
+ /// ```
+ pub fn is_empty(&self) -> bool {
+ self.inner().is_empty()
+ }
+
+ /// Spawns a task onto the executor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::LocalExecutor;
+ ///
+ /// let local_ex = LocalExecutor::new();
+ ///
+ /// let task = local_ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// ```
+ pub fn spawn<T: 'a>(&self, future: impl Future<Output = T> + 'a) -> Task<T> {
+ let mut active = self.inner().state().active.lock().unwrap();
+
+ // Remove the task from the set of active tasks when the future finishes.
+ let index = active.vacant_entry().key();
+ let state = self.inner().state().clone();
+ let future = async move {
+ let _guard = CallOnDrop(move || drop(state.active.lock().unwrap().try_remove(index)));
+ future.await
+ };
+
+ // Create the task and register it in the set of active tasks.
+ let (runnable, task) = unsafe { async_task::spawn_unchecked(future, self.schedule()) };
+ active.insert(runnable.waker());
+
+ runnable.schedule();
+ task
+ }
+
+ /// Attempts to run a task if at least one is scheduled.
+ ///
+ /// Running a scheduled task means simply polling its future once.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::LocalExecutor;
+ ///
+ /// let ex = LocalExecutor::new();
+ /// assert!(!ex.try_tick()); // no tasks to run
+ ///
+ /// let task = ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// assert!(ex.try_tick()); // a task was found
+ /// ```
+ pub fn try_tick(&self) -> bool {
+ self.inner().try_tick()
+ }
+
+ /// Runs a single task.
+ ///
+ /// Running a task means simply polling its future once.
+ ///
+ /// If no tasks are scheduled when this method is called, it will wait until one is scheduled.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::LocalExecutor;
+ /// use futures_lite::future;
+ ///
+ /// let ex = LocalExecutor::new();
+ ///
+ /// let task = ex.spawn(async {
+ /// println!("Hello world");
+ /// });
+ /// future::block_on(ex.tick()); // runs the task
+ /// ```
+ pub async fn tick(&self) {
+ self.inner().tick().await
+ }
+
+ /// Runs the executor until the given future completes.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use async_executor::LocalExecutor;
+ /// use futures_lite::future;
+ ///
+ /// let local_ex = LocalExecutor::new();
+ ///
+ /// let task = local_ex.spawn(async { 1 + 2 });
+ /// let res = future::block_on(local_ex.run(async { task.await * 2 }));
+ ///
+ /// assert_eq!(res, 6);
+ /// ```
+ pub async fn run<T>(&self, future: impl Future<Output = T>) -> T {
+ self.inner().run(future).await
+ }
+
+ /// Returns a function that schedules a runnable task when it gets woken up.
+ fn schedule(&self) -> impl Fn(Runnable) + Send + Sync + 'static {
+ let state = self.inner().state().clone();
+
+ move |runnable| {
+ state.queue.push(runnable).unwrap();
+ state.notify();
+ }
+ }
+
+ /// Returns a reference to the inner executor.
+ fn inner(&self) -> &Executor<'a> {
+ &self.inner
+ }
+}
+
+impl<'a> Default for LocalExecutor<'a> {
+ fn default() -> LocalExecutor<'a> {
+ LocalExecutor::new()
+ }
+}
+
+/// The state of a executor.
+#[derive(Debug)]
+struct State {
+ /// The global queue.
+ queue: ConcurrentQueue<Runnable>,
+
+ /// Local queues created by runners.
+ local_queues: RwLock<Vec<Arc<ConcurrentQueue<Runnable>>>>,
+
+ /// Set to `true` when a sleeping ticker is notified or no tickers are sleeping.
+ notified: AtomicBool,
+
+ /// A list of sleeping tickers.
+ sleepers: Mutex<Sleepers>,
+
+ /// Currently active tasks.
+ active: Mutex<Slab<Waker>>,
+}
+
+impl State {
+ /// Creates state for a new executor.
+ fn new() -> State {
+ State {
+ queue: ConcurrentQueue::unbounded(),
+ local_queues: RwLock::new(Vec::new()),
+ notified: AtomicBool::new(true),
+ sleepers: Mutex::new(Sleepers {
+ count: 0,
+ wakers: Vec::new(),
+ free_ids: Vec::new(),
+ }),
+ active: Mutex::new(Slab::new()),
+ }
+ }
+
+ /// Notifies a sleeping ticker.
+ #[inline]
+ fn notify(&self) {
+ if self
+ .notified
+ .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
+ .is_ok()
+ {
+ let waker = self.sleepers.lock().unwrap().notify();
+ if let Some(w) = waker {
+ w.wake();
+ }
+ }
+ }
+}
+
+/// A list of sleeping tickers.
+#[derive(Debug)]
+struct Sleepers {
+ /// Number of sleeping tickers (both notified and unnotified).
+ count: usize,
+
+ /// IDs and wakers of sleeping unnotified tickers.
+ ///
+ /// A sleeping ticker is notified when its waker is missing from this list.
+ wakers: Vec<(usize, Waker)>,
+
+ /// Reclaimed IDs.
+ free_ids: Vec<usize>,
+}
+
+impl Sleepers {
+ /// Inserts a new sleeping ticker.
+ fn insert(&mut self, waker: &Waker) -> usize {
+ let id = match self.free_ids.pop() {
+ Some(id) => id,
+ None => self.count + 1,
+ };
+ self.count += 1;
+ self.wakers.push((id, waker.clone()));
+ id
+ }
+
+ /// Re-inserts a sleeping ticker's waker if it was notified.
+ ///
+ /// Returns `true` if the ticker was notified.
+ fn update(&mut self, id: usize, waker: &Waker) -> bool {
+ for item in &mut self.wakers {
+ if item.0 == id {
+ if !item.1.will_wake(waker) {
+ item.1 = waker.clone();
+ }
+ return false;
+ }
+ }
+
+ self.wakers.push((id, waker.clone()));
+ true
+ }
+
+ /// Removes a previously inserted sleeping ticker.
+ ///
+ /// Returns `true` if the ticker was notified.
+ fn remove(&mut self, id: usize) -> bool {
+ self.count -= 1;
+ self.free_ids.push(id);
+
+ for i in (0..self.wakers.len()).rev() {
+ if self.wakers[i].0 == id {
+ self.wakers.remove(i);
+ return false;
+ }
+ }
+ true
+ }
+
+ /// Returns `true` if a sleeping ticker is notified or no tickers are sleeping.
+ fn is_notified(&self) -> bool {
+ self.count == 0 || self.count > self.wakers.len()
+ }
+
+ /// Returns notification waker for a sleeping ticker.
+ ///
+ /// If a ticker was notified already or there are no tickers, `None` will be returned.
+ fn notify(&mut self) -> Option<Waker> {
+ if self.wakers.len() == self.count {
+ self.wakers.pop().map(|item| item.1)
+ } else {
+ None
+ }
+ }
+}
+
+/// Runs task one by one.
+#[derive(Debug)]
+struct Ticker<'a> {
+ /// The executor state.
+ state: &'a State,
+
+ /// Set to a non-zero sleeper ID when in sleeping state.
+ ///
+ /// States a ticker can be in:
+ /// 1) Woken.
+ /// 2a) Sleeping and unnotified.
+ /// 2b) Sleeping and notified.
+ sleeping: AtomicUsize,
+}
+
+impl Ticker<'_> {
+ /// Creates a ticker.
+ fn new(state: &State) -> Ticker<'_> {
+ Ticker {
+ state,
+ sleeping: AtomicUsize::new(0),
+ }
+ }
+
+ /// Moves the ticker into sleeping and unnotified state.
+ ///
+ /// Returns `false` if the ticker was already sleeping and unnotified.
+ fn sleep(&self, waker: &Waker) -> bool {
+ let mut sleepers = self.state.sleepers.lock().unwrap();
+
+ match self.sleeping.load(Ordering::SeqCst) {
+ // Move to sleeping state.
+ 0 => self
+ .sleeping
+ .store(sleepers.insert(waker), Ordering::SeqCst),
+
+ // Already sleeping, check if notified.
+ id => {
+ if !sleepers.update(id, waker) {
+ return false;
+ }
+ }
+ }
+
+ self.state
+ .notified
+ .swap(sleepers.is_notified(), Ordering::SeqCst);
+
+ true
+ }
+
+ /// Moves the ticker into woken state.
+ fn wake(&self) {
+ let id = self.sleeping.swap(0, Ordering::SeqCst);
+ if id != 0 {
+ let mut sleepers = self.state.sleepers.lock().unwrap();
+ sleepers.remove(id);
+
+ self.state
+ .notified
+ .swap(sleepers.is_notified(), Ordering::SeqCst);
+ }
+ }
+
+ /// Waits for the next runnable task to run.
+ async fn runnable(&self) -> Runnable {
+ self.runnable_with(|| self.state.queue.pop().ok()).await
+ }
+
+ /// Waits for the next runnable task to run, given a function that searches for a task.
+ async fn runnable_with(&self, mut search: impl FnMut() -> Option<Runnable>) -> Runnable {
+ future::poll_fn(|cx| {
+ loop {
+ match search() {
+ None => {
+ // Move to sleeping and unnotified state.
+ if !self.sleep(cx.waker()) {
+ // If already sleeping and unnotified, return.
+ return Poll::Pending;
+ }
+ }
+ Some(r) => {
+ // Wake up.
+ self.wake();
+
+ // Notify another ticker now to pick up where this ticker left off, just in
+ // case running the task takes a long time.
+ self.state.notify();
+
+ return Poll::Ready(r);
+ }
+ }
+ }
+ })
+ .await
+ }
+}
+
+impl Drop for Ticker<'_> {
+ fn drop(&mut self) {
+ // If this ticker is in sleeping state, it must be removed from the sleepers list.
+ let id = self.sleeping.swap(0, Ordering::SeqCst);
+ if id != 0 {
+ let mut sleepers = self.state.sleepers.lock().unwrap();
+ let notified = sleepers.remove(id);
+
+ self.state
+ .notified
+ .swap(sleepers.is_notified(), Ordering::SeqCst);
+
+ // If this ticker was notified, then notify another ticker.
+ if notified {
+ drop(sleepers);
+ self.state.notify();
+ }
+ }
+ }
+}
+
+/// A worker in a work-stealing executor.
+///
+/// This is just a ticker that also has an associated local queue for improved cache locality.
+#[derive(Debug)]
+struct Runner<'a> {
+ /// The executor state.
+ state: &'a State,
+
+ /// Inner ticker.
+ ticker: Ticker<'a>,
+
+ /// The local queue.
+ local: Arc<ConcurrentQueue<Runnable>>,
+
+ /// Bumped every time a runnable task is found.
+ ticks: AtomicUsize,
+}
+
+impl Runner<'_> {
+ /// Creates a runner and registers it in the executor state.
+ fn new(state: &State) -> Runner<'_> {
+ let runner = Runner {
+ state,
+ ticker: Ticker::new(state),
+ local: Arc::new(ConcurrentQueue::bounded(512)),
+ ticks: AtomicUsize::new(0),
+ };
+ state
+ .local_queues
+ .write()
+ .unwrap()
+ .push(runner.local.clone());
+ runner
+ }
+
+ /// Waits for the next runnable task to run.
+ async fn runnable(&self) -> Runnable {
+ let runnable = self
+ .ticker
+ .runnable_with(|| {
+ // Try the local queue.
+ if let Ok(r) = self.local.pop() {
+ return Some(r);
+ }
+
+ // Try stealing from the global queue.
+ if let Ok(r) = self.state.queue.pop() {
+ steal(&self.state.queue, &self.local);
+ return Some(r);
+ }
+
+ // Try stealing from other runners.
+ let local_queues = self.state.local_queues.read().unwrap();
+
+ // Pick a random starting point in the iterator list and rotate the list.
+ let n = local_queues.len();
+ let start = fastrand::usize(..n);
+ let iter = local_queues
+ .iter()
+ .chain(local_queues.iter())
+ .skip(start)
+ .take(n);
+
+ // Remove this runner's local queue.
+ let iter = iter.filter(|local| !Arc::ptr_eq(local, &self.local));
+
+ // Try stealing from each local queue in the list.
+ for local in iter {
+ steal(local, &self.local);
+ if let Ok(r) = self.local.pop() {
+ return Some(r);
+ }
+ }
+
+ None
+ })
+ .await;
+
+ // Bump the tick counter.
+ let ticks = self.ticks.fetch_add(1, Ordering::SeqCst);
+
+ if ticks % 64 == 0 {
+ // Steal tasks from the global queue to ensure fair task scheduling.
+ steal(&self.state.queue, &self.local);
+ }
+
+ runnable
+ }
+}
+
+impl Drop for Runner<'_> {
+ fn drop(&mut self) {
+ // Remove the local queue.
+ self.state
+ .local_queues
+ .write()
+ .unwrap()
+ .retain(|local| !Arc::ptr_eq(local, &self.local));
+
+ // Re-schedule remaining tasks in the local queue.
+ while let Ok(r) = self.local.pop() {
+ r.schedule();
+ }
+ }
+}
+
+/// Steals some items from one queue into another.
+fn steal<T>(src: &ConcurrentQueue<T>, dest: &ConcurrentQueue<T>) {
+ // Half of `src`'s length rounded up.
+ let mut count = (src.len() + 1) / 2;
+
+ if count > 0 {
+ // Don't steal more than fits into the queue.
+ if let Some(cap) = dest.capacity() {
+ count = count.min(cap - dest.len());
+ }
+
+ // Steal tasks.
+ for _ in 0..count {
+ if let Ok(t) = src.pop() {
+ assert!(dest.push(t).is_ok());
+ } else {
+ break;
+ }
+ }
+ }
+}
+
+/// Runs a closure when dropped.
+struct CallOnDrop<F: Fn()>(F);
+
+impl<F: Fn()> Drop for CallOnDrop<F> {
+ fn drop(&mut self) {
+ (self.0)();
+ }
+}
diff --git a/tests/drop.rs b/tests/drop.rs
new file mode 100644
index 0000000..dd20add
--- /dev/null
+++ b/tests/drop.rs
@@ -0,0 +1,128 @@
+use std::mem;
+use std::panic::catch_unwind;
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::sync::Mutex;
+use std::task::{Poll, Waker};
+
+use async_executor::{Executor, Task};
+use futures_lite::future;
+use once_cell::sync::Lazy;
+
+#[test]
+fn executor_cancels_everything() {
+ static DROP: AtomicUsize = AtomicUsize::new(0);
+ static WAKER: Lazy<Mutex<Option<Waker>>> = Lazy::new(Default::default);
+
+ let ex = Executor::new();
+
+ let task = ex.spawn(async {
+ let _guard = CallOnDrop(|| {
+ DROP.fetch_add(1, Ordering::SeqCst);
+ });
+
+ future::poll_fn(|cx| {
+ *WAKER.lock().unwrap() = Some(cx.waker().clone());
+ Poll::Pending::<()>
+ })
+ .await;
+ });
+
+ future::block_on(ex.tick());
+ assert!(WAKER.lock().unwrap().is_some());
+ assert_eq!(DROP.load(Ordering::SeqCst), 0);
+
+ drop(ex);
+ assert_eq!(DROP.load(Ordering::SeqCst), 1);
+
+ assert!(catch_unwind(|| future::block_on(task)).is_err());
+ assert_eq!(DROP.load(Ordering::SeqCst), 1);
+}
+
+#[test]
+fn leaked_executor_leaks_everything() {
+ static DROP: AtomicUsize = AtomicUsize::new(0);
+ static WAKER: Lazy<Mutex<Option<Waker>>> = Lazy::new(Default::default);
+
+ let ex = Executor::new();
+
+ let task = ex.spawn(async {
+ let _guard = CallOnDrop(|| {
+ DROP.fetch_add(1, Ordering::SeqCst);
+ });
+
+ future::poll_fn(|cx| {
+ *WAKER.lock().unwrap() = Some(cx.waker().clone());
+ Poll::Pending::<()>
+ })
+ .await;
+ });
+
+ future::block_on(ex.tick());
+ assert!(WAKER.lock().unwrap().is_some());
+ assert_eq!(DROP.load(Ordering::SeqCst), 0);
+
+ mem::forget(ex);
+ assert_eq!(DROP.load(Ordering::SeqCst), 0);
+
+ assert!(future::block_on(future::poll_once(task)).is_none());
+ assert_eq!(DROP.load(Ordering::SeqCst), 0);
+}
+
+#[test]
+fn await_task_after_dropping_executor() {
+ let s: String = "hello".into();
+
+ let ex = Executor::new();
+ let task: Task<&str> = ex.spawn(async { &*s });
+ assert!(ex.try_tick());
+
+ drop(ex);
+ assert_eq!(future::block_on(task), "hello");
+ drop(s);
+}
+
+#[test]
+fn drop_executor_and_then_drop_finished_task() {
+ static DROP: AtomicUsize = AtomicUsize::new(0);
+
+ let ex = Executor::new();
+ let task = ex.spawn(async {
+ CallOnDrop(|| {
+ DROP.fetch_add(1, Ordering::SeqCst);
+ })
+ });
+ assert!(ex.try_tick());
+
+ assert_eq!(DROP.load(Ordering::SeqCst), 0);
+ drop(ex);
+ assert_eq!(DROP.load(Ordering::SeqCst), 0);
+ drop(task);
+ assert_eq!(DROP.load(Ordering::SeqCst), 1);
+}
+
+#[test]
+fn drop_finished_task_and_then_drop_executor() {
+ static DROP: AtomicUsize = AtomicUsize::new(0);
+
+ let ex = Executor::new();
+ let task = ex.spawn(async {
+ CallOnDrop(|| {
+ DROP.fetch_add(1, Ordering::SeqCst);
+ })
+ });
+ assert!(ex.try_tick());
+
+ assert_eq!(DROP.load(Ordering::SeqCst), 0);
+ drop(task);
+ assert_eq!(DROP.load(Ordering::SeqCst), 1);
+ drop(ex);
+ assert_eq!(DROP.load(Ordering::SeqCst), 1);
+}
+
+struct CallOnDrop<F: Fn()>(F);
+
+impl<F: Fn()> Drop for CallOnDrop<F> {
+ fn drop(&mut self) {
+ (self.0)();
+ }
+}