pax_global_header00006660000000000000000000000064142350462670014523gustar00rootroot0000000000000052 comment=d56805869ba3cbe47021d5660bbaf19ac5ec4bfb rusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/000077500000000000000000000000001423504626700211555ustar00rootroot00000000000000rusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/.gitignore000066400000000000000000000000421423504626700231410ustar00rootroot00000000000000/target Cargo.lock /.idea /.vscoderusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/CHANGELOG.md000066400000000000000000000224631423504626700227750ustar00rootroot00000000000000## [0.7.0] - 2022-05-05 * improve joining * use wait_while and wait_timeout_while to protect against spurious wakeups * add join_generation field to WorkerData to make sure all threads waiting for a join leave at once by having the first awakened thread increment the counter * otherwise the pool might become non-idle again before all notified threads were awakened, causing them to keep waiting * implement ThreadPool::start_core_threads * starts all core workers by creating core idle workers until the total worker count reaches the core count * implement WorkerCountData::try_increment_worker_count as an alternative to try_increment_worker_total allowing to specify a custom increment * replace INCREMENT_TOTAL + INCREMENT_IDLE with INCREMENT_TOTAL | INCREMENT_IDLE as a bitwise or is enough to combine the increments as they have no overlapping bit and addition is not required * make field JoinHandle.receiver public ## [0.6.0] - 2021-05-02 * replace Atomic64 with AtomicUsize for worker count bookkeeping * refactor worker count bookkeeping arithmetics to work based on the size of usize on a given platform instead of requiring 64 bits * enables using the crate on non 64 bit platforms ## [0.5.1] - 2021-02-27 * Replace compare_and_swap with compare_exchange_weak. * compare_and_swap has been deprecated as of rust 1.50.0 ## [0.5.0] - 2021-01-30 * Instead of incrementing the worker total and using the returned previous value to check if the worker is still required and then decrementing the counter again if that's not the case, use a compare and swap mechanism implemented by WorkerCountData::try_increment_worker_total to increment the total count if the current workerCountData matches the expected value and retry until it succeeds or until the observed value reaches the provided maximum value (either the core pool size when attempting to create a core worker, else the max pool size), at which point the pool proceeds to creating a non-core worker or to sending the task to the channel. * This only requires one atomic operation per retry instead of 2 (if contention is high it may lead to more retries as increments could not fail before but retries are much cheaper as it does not have to decrement the counter and recursively call the method) and guarantees that the total worker count is always accurate. * Use Ordering::Relaxed instead of SeqCst as the relaxed ordering suffices when incrementing a counter. * ThreadPool::default: Handle possible u32 overflow when calculating max_size. * Update dependencies. * Use twice the core count instead of 4 times the core count as default max_size. * Worker::mark_idle_and_notify_joiners_if_no_work: Check whether old_total_count equals old_idle_count + 1 before checking whether the receiver is empty as it is the cheaper operation. ## [0.4.3] - 2020-08-11 * _do_join(): recheck whether the pool has become idle after acquiring the lock ## [0.4.2] - 2020-06-30 * Builder: make sure core size is lower or equal to max size if max size was specified explicitly but core size wasn't. * build() panicked if max size was specified to be lower than default core size. * Set the core size to the specified max size if lower than the number of CPUs. * Handle possible u32 overflow when calculating default max size based on core size. * Remove unneeded `Sync` trait bounds. * Remove unneeded boxing. * Add additional tests. ## [0.4.1] - 2020-06-02 * Add `spawn_await()` and `try_spawn_await()` as an extension of `spawn()` that creates a new top-level future that awaits the provided future and sends the result to a `JoinHandle`. This enables awaiting the final result outside of an async context like `complete()` while still polling the future lazily instead of eagerly blocking the worker until the future is done. * Ignore dropped oneshot receiver for `try_evaluate()`. * It is perfectly valid that the caller does not care about the result and drops the `JoinHandle` before the result has been completed. In that case attempting to send the result should not panic. ## [0.4.0] - 2020-06-02 * Add async support * Add "async" feature that enables using the rusty_pool as a fully featured futures executor that may poll features and handle awakened futures. * Add "complete" function to simply block a worker until a future is polled to completion. * Add `JoinHandle` to enable awaiting a task's completion. * The `JoinHandle` holds the receiving end of a oneshot channel that receives the result of the task or a cancellation error if the task panics. The `await_complete()` and `try_await_complete()` functions can be used to block the calling thread until the task has completed or failed. The `JoinHandle` may also be sent to the `ThreadPool` to create a task that awaits the completion of the other task and then performs work using the result. * Add Builder struct to create a new `ThreadPool` more conveniently. * Name the `ThreadPool` and use the name as prefix for each worker thread's name. * Create `Task` trait representing everything that can be executed by the `ThreadPool`. * Add an implementation for any `FnOnce` closure that returns a thread-safe result * Add an implementation for `AsyncTask` representing a future that may be polled and awakened if the "async" feature is enabled. * Implement `Default` for `ThreadPool`. * Implement `Clone` for `ThreadPool`. * All clones will use the same channel and the same worker pool, meaning shutting down and dropping one clone will not cause the channel to break and other clones will still be intact. This also means that `shutdown_join()` will behave the same as calling `join()` on a live `ThreadPool` while only dropping said clone, meaning tasks submitted to other clones after `shutdown()` is called on this clone are joined as well. * `AsyncTask` instances hold a clone of the `ThreadPool` they were created for used to re-submit themselves when being awakened. This means the channel stays alive as long as there are pending `AsyncTask`s. * Update readme and documentation and add doctests. * Cleanup worker and channel data by putting them into separate structs. * Only 2 Arcs needed now by wrapping the entire `ChannelData` and `WorkerData` structs into an Arc. ## [0.3.2] - 2020-05-03 * Add proper handling for panicking threads using a `Sentinel` that implements Drop and manages handling post-execution steps for workers that panicked while executing a submitted task and clones and replaces the worker. * Improve joins by also checking whether the `Receiver` is empty before fast-returning to eliminate a race condition where if a thread calls join in a brief window between one worker finishing a task and the next worker beginning a task. ## [0.3.1] - 2020-05-02 * No longer wrap crossbeam Receivers inside an Arc because they already can be cloned and sent to other threads. ## [0.3.0] - 2020-05-01 * Removed `Result` from `ThreadPool::execute()` and added `ThreadPool::try_execute()` instead. * Most often the user does not want to handle the result and prefers a panic in the very rare case the function returns an error (should not be possible with safe code). ## [0.2.0] - 2020-05-01 * Added `shutdown_join()` and `shutdown_join_timeout(Duration)` to block the current thread until work in the pool is finished when shutting down the pool. * Improved bookkeeping of the total and idle worker counters by putting both values into one `AtomicU64`, which enables updating both counters in one single atomic operation. * this is part of a series of otherwise small changes to make joining the threadpool fully reliable and eliminating races where a thread could join the pool, see that the idle worker counter is lower than the total worker counter and assume that there is work to be joined when in fact the read was performed just before the idle worker counter was incremented, leading the thread to wait for work to be completed when all threads are idle. This should never happen now because an increment to both counter is one single atomic operation and the idle worker counter is incremented immediately after a worker finishes a task and before joining threads are notified. Because even if a thread would join exactly after the worker finished but before the idle worker count is incremented back to being equal to the total worker count it will get notified by the worker immediately after. * Improved Worker creation. * The workers now only spawn a thread once the re-check passes successfully, meaning the new worker is committed. The new worker is now started explicitly when the re_check succeeds and receives it's first task via the `start()` function directly. The `create_worker()` function now returns a Result where the Err variant contains the submitted task in case the re-check fails and the new worker is abandoned in which case the task will be sent to the main multi consumer channel instead. * If the re-recheck fails when creating new core worker try creating a non-core thread instead. * Fix workers notifying joining threads when the channel is empty but there's still threads executing work. rusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/Cargo.toml000066400000000000000000000014631423504626700231110ustar00rootroot00000000000000[package] name = "rusty_pool" version = "0.7.0" authors = ["robinfriedli "] edition = "2018" license = "Apache-2.0" description = "Self growing / shrinking `ThreadPool` implementation based on crossbeam's multi-producer multi-consumer channels that enables awaiting the result of a task and offers async support" repository = "https://github.com/robinfriedli/rusty_pool" homepage = "https://github.com/robinfriedli/rusty_pool" readme = "README.md" keywords = ["concurrency", "thread", "threadpool", "async", "futures"] categories = ["asynchronous", "concurrency"] [features] default = ["async"] async = ["futures"] [dependencies] crossbeam-channel = "0.5.4" futures-channel = "0.3.21" futures-executor = "0.3.21" num_cpus = "1.13.1" [dependencies.futures] version = "0.3.21" optional = true rusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/LICENSE000066400000000000000000000261351423504626700221710ustar00rootroot00000000000000 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. rusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/README.md000066400000000000000000000316161423504626700224430ustar00rootroot00000000000000# rusty_pool Self growing / shrinking `ThreadPool` implementation based on crossbeam's multi-producer multi-consumer channels that enables awaiting the result of a task and offers async support. This `ThreadPool` has two different pool sizes; a core pool size filled with threads that live for as long as the channel and a max pool size which describes the maximum amount of worker threads that may live at the same time. Those additional non-core threads have a specific keep_alive time described when creating the `ThreadPool` that defines how long such threads may be idle for without receiving any work before giving up and terminating their work loop. This `ThreadPool` does not spawn any threads until a task is submitted to it. Then it will create a new thread for each task until the core pool size is full. After that a new thread will only be created upon an `execute()` call if the current pool is lower than the max pool size and there are no idle threads. Functions like `evaluate()` and `complete()` return a `JoinHandle` that may be used to await the result of a submitted task or future. JoinHandles may be sent to the thread pool to create a task that blocks a worker thread until it receives the result of the other task and then operates on the result. If the task panics the `JoinHandle` receives a cancellation error. This is implemented using a futures oneshot channel to communicate with the worker thread. This `ThreadPool` may be used as a futures executor if the "async" feature is enabled, which is the case by default. The "async" feature includes the `spawn()` and `try_spawn()` functions which create a task that polls the future one by one and creates a waker that re-submits the future to the pool when it can make progress. Without the "async" feature, futures can simply be executed to completion using the `complete` function, which simply blocks a worker thread until the future has been polled to completion. The "async" feature can be disabled if not need by adding the following to your Cargo dependency: ```toml [dependencies.rusty_pool] default-features = false version = "*" ``` When creating a new worker this `ThreadPool` tries to increment the worker count using a compare-and-swap mechanism, if the increment fails because the total worker count has been incremented to the specified limit (the core_size when trying to create a core thread, else the max_size) by another thread, the pool tries to create a non-core worker instead (if previously trying to create a core worker and no idle worker exists) or sends the task to the channel instead. Panicking workers are always cloned and replaced. Locks are only used for the join functions to lock the `Condvar`, apart from that this `ThreadPool` implementation fully relies on crossbeam and atomic operations. This `ThreadPool` decides whether it is currently idle (and should fast-return join attempts) by comparing the total worker count to the idle worker count, which are two values stored in one `AtomicUsize` (both half the size of usize) making sure that if both are updated they may be updated in a single atomic operation. The thread pool and its crossbeam channel can be destroyed by using the shutdown function, however that does not stop tasks that are already running but will terminate the thread the next time it will try to fetch work from the channel. The channel is only destroyed once all clones of the `ThreadPool` have been shut down / dropped. # Installation To add rusty_pool to your project simply add the following Cargo dependency: ```toml [dependencies] rusty_pool = "0.7.0" ``` Or to exclude the "async" feature: ```toml [dependencies.rusty_pool] version = "0.7.0" default-features = false ``` # Usage Create a new `ThreadPool`: ```rust use rusty_pool::Builder; use rusty_pool::ThreadPool; // Create default `ThreadPool` configuration with the number of CPUs as core pool size let pool = ThreadPool::default(); // Create a `ThreadPool` with default naming: use std::time::Duration; let pool2 = ThreadPool::new(5, 50, Duration::from_secs(60)); // Create a `ThreadPool` with a custom name: let pool3 = ThreadPool::new_named(String::from("my_pool"), 5, 50, Duration::from_secs(60)); // using the Builder struct: let pool4 = Builder::new().core_size(5).max_size(50).build(); ``` Submit a closure for execution in the `ThreadPool`: ```rust use rusty_pool::ThreadPool; use std::thread; use std::time::Duration; let pool = ThreadPool::default(); pool.execute(|| { thread::sleep(Duration::from_secs(5)); print!("hello"); }); ``` Submit a task and await the result: ```rust use rusty_pool::ThreadPool; use std::thread; use std::time::Duration; let pool = ThreadPool::default(); let handle = pool.evaluate(|| { thread::sleep(Duration::from_secs(5)); return 4; }); let result = handle.await_complete(); assert_eq!(result, 4); ``` Spawn futures using the `ThreadPool`: ```rust async fn some_async_fn(x: i32, y: i32) -> i32 { x + y } async fn other_async_fn(x: i32, y: i32) -> i32 { x - y } use rusty_pool::ThreadPool; let pool = ThreadPool::default(); // simply complete future by blocking a worker until the future has been completed let handle = pool.complete(async { let a = some_async_fn(4, 6).await; // 10 let b = some_async_fn(a, 3).await; // 13 let c = other_async_fn(b, a).await; // 3 some_async_fn(c, 5).await // 8 }); assert_eq!(handle.await_complete(), 8); use std::sync::{Arc, atomic::{AtomicI32, Ordering}}; // spawn future and create waker that automatically re-submits itself to the threadpool if ready to make progress, this requires the "async" feature which is enabled by default let count = Arc::new(AtomicI32::new(0)); let clone = count.clone(); pool.spawn(async move { let a = some_async_fn(3, 6).await; // 9 let b = other_async_fn(a, 4).await; // 5 let c = some_async_fn(b, 7).await; // 12 clone.fetch_add(c, Ordering::SeqCst); }); pool.join(); assert_eq!(count.load(Ordering::SeqCst), 12); ``` Join and shut down the `ThreadPool`: ```rust use std::thread; use std::time::Duration; use rusty_pool::ThreadPool; use std::sync::{Arc, atomic::{AtomicI32, Ordering}}; let pool = ThreadPool::default(); for _ in 0..10 { pool.execute(|| { thread::sleep(Duration::from_secs(10)) }) } // wait for all threads to become idle, i.e. all tasks to be completed including tasks added by other threads after join() is called by this thread or for the timeout to be reached pool.join_timeout(Duration::from_secs(5)); let count = Arc::new(AtomicI32::new(0)); for _ in 0..15 { let clone = count.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(5)); clone.fetch_add(1, Ordering::SeqCst); }); } // shut down and drop the only instance of this `ThreadPool` (no clones) causing the channel to be broken leading all workers to exit after completing their current work // and wait for all workers to become idle, i.e. finish their work. pool.shutdown_join(); assert_eq!(count.load(Ordering::SeqCst), 15); ``` # Performance In terms of performance from the perspective of a thread submitting tasks to the pool, rusty_pool should offer better performance than any pool using std::sync::mpsc (such as rust-threadpool) in most scenarios thanks to the great work of the crossbeam team. In some cases with extreme contention rusty_pool might fall behind rust-threadpool, though the scenarios where this has been found to be the case are hardly practical as they require to submit empty tasks in a loop and it depends on the platform. macOS seems to perform particularly well in the tested scenario, presumably macOS has spent a lot of effort optimising atomic operations as Swift's reference counting depends on it. Apparently this should be amplified on Apple Silicon but rusty_pool has not been tested on that platform. The following tests were executed on a PC with an AMD Ryzen 9 3950X for Linux and Windows and on a MacBook Pro 15" 2019 with an Intel i9-9880H for macOS. ### Test 1: No contention All tasks are submitted by the same thread and the task lasts longer than the test, meaning all atomic operations (reading and incrementing the worker counter) are performed by the main thread, since newly created workers do not alter the counter until after they completed their initial task and increment the idle counter. ```rust fn main() { let now = std::time::Instant::now(); let pool = rusty_pool::Builder::new().core_size(10).max_size(10).build(); //let pool = threadpool::ThreadPool::new(10); for _ in 0..10000000 { pool.execute(|| { thread::sleep(std::time::Duration::from_secs(1)); }); } let millis = now.elapsed().as_millis(); println!("millis: {}", millis); } ``` Results (in milliseconds, average value): rusty_pool 0.5.1: | Windows | MacOS | Linux | |---------|-------|-------| | 221.6 | 293.07| 183.73| rusty_pool 0.5.0: | Windows | MacOS | Linux | |---------|-------|-------| | 224.6 | 315.6 | 187.0 | rust-threadpool 1.8.1: | Windows | MacOS | Linux | |---------|-------|-------| | 476.4 | 743.4 | 354.3 | rusty_pool 0.4.3: | Windows | MacOS | Linux | |---------|-------|-------| | 237.5 | 318.1 | 181.3 | ### Test 2: Multiple producers Next to the main thread there are 10 other threads submitting tasks to the pool. Unlike the previous test, the task no longer lasts longer than the test, thus there not only is contention between the producers for the worker counter but also between the worker threads updating the idle counter. This is a somewhat realistic albeit extreme example. ```rust fn main() { let now = std::time::Instant::now(); let pool = rusty_pool::Builder::new().core_size(10).max_size(10).build(); //let pool = threadpool::ThreadPool::new(10); for _ in 0..10 { let pool = pool.clone(); std::thread::spawn(move || { for _ in 0..10000000 { pool.execute(|| { std::thread::sleep(std::time::Duration::from_secs(1)); }); } }); } for _ in 0..10000000 { pool.execute(|| { std::thread::sleep(std::time::Duration::from_secs(1)); }); } let millis = now.elapsed().as_millis(); println!("millis: {}", millis); } ``` Results (in milliseconds, average value): rusty_pool 0.5.1: | Windows* | MacOS | Linux | |----------|--------|--------| | 7692.4 | 3656.2 | 7514.53| rusty_pool 0.5.0: | Windows | MacOS | Linux | Windows*| |----------|--------|--------|---------| | 6251.0 | 4417.7 | 7903.1 | 7774.67 | rust-threadpool 1.8.1: | Windows | MacOS | Linux | |----------|--------|--------| | 10030.5 | 5810.5 | 9743.3 | rusty_pool 0.4.3: | Windows | MacOS | Linux | Windows*| |----------|--------|--------|---------| | 6342.2 | 4444.6 | 7962.0 | 8564.93 | * When testing 0.5.1 the performance for Windows appeared to be considerably worse, so the results for previous versions of rusty_pool were recalculated and also found to be worse than when originally recorded, probably due to external influence (e.g. background task taking a lot of CPU time, though the test was retried with realtime priority with similar results). The results for rust-threadpool 1.8.1 were not fully recalculated as they appeared to be similar to the last recording. ### Test 3: Worst case This test case highlights the aforementioned worst-case scenario for rusty_pool where the pool is spammed with empty tasks. Since workers increment the idle counter after completing a task and the task is executed practically immediately, the increment of the idle counter coincides with the next execute() call in the loop reading the counter. The higher the number of workers the higher contention gets and the worse performance becomes. ```rust fn main() { let now = std::time::Instant::now(); let pool = rusty_pool::Builder::new().core_size(10).max_size(10).build(); //let pool = threadpool::ThreadPool::new(10); for _ in 0..10000000 { pool.execute(|| {}); } let millis = now.elapsed().as_millis(); println!("millis: {}", millis); } ``` rusty_pool 0.5.1: | Windows | MacOS | Linux | |----------|--------|--------| | 1967.93 | 698.8 | 2150.0 | rusty_pool 0.5.0: | Windows | MacOS | Linux | |----------|--------|--------| | 1991.6 | 679.93 | 2175.1 | rust-threadpool 1.8.1: | Windows | MacOS | Linux | |----------|--------|--------| | 980.33 | 1224.6 | 677.0 | rusty_pool 0.4.3: | Windows | MacOS | Linux | |----------|--------|--------| | 2016.8 | 683.13 | 2175.1 | Curiously, macOS heavily favours rusty_pool in this case while Windows and Linux favour rust-threadpool. However, this test case should hardly occur in a real world scenario. In all other tested scenarios rusty_pool performs better when submitting tasks, where macOS seems to gain a lead in cases where there is a lot of contention but falling behind in other cases, possibly due to the weaker hardware of the specific device used for testing. Linux seems to perform best in cases with little to no contention but performs the worst when contention is high.rusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/src/000077500000000000000000000000001423504626700217445ustar00rootroot00000000000000rusty_pool-d56805869ba3cbe47021d5660bbaf19ac5ec4bfb/src/lib.rs000066400000000000000000002457371423504626700231020ustar00rootroot00000000000000#[cfg(feature = "async")] use futures::{ future::BoxFuture, task::{waker_ref, ArcWake}, }; use futures_channel::oneshot; use futures_executor::block_on; use std::future::Future; use std::option::Option; use std::sync::{ atomic::{AtomicUsize, Ordering}, Arc, Condvar, Mutex, }; #[cfg(feature = "async")] use std::task::Context; use std::thread; use std::time::Duration; const BITS: usize = std::mem::size_of::() * 8; /// The absolute maximum number of workers. This corresponds to the maximum value that can be stored within half the bits of usize, /// as two counters (total workers and idle workers) are stored in one AtomicUsize. pub const MAX_SIZE: usize = (1 << (BITS / 2)) - 1; type Job = Box; /// Trait to implement for all items that may be executed by the `ThreadPool`. pub trait Task: Send { /// Execute this task and return its result. fn run(self) -> R; /// Transform this `Task` into a heap allocated `FnOnce` if possible. /// /// Used by [`ThreadPool::execute`](struct.ThreadPool.html#method.execute) to turn this `Task` into a `Job` /// directly without having to create an additional `Job` that calls this `Task`. fn into_fn(self) -> Option R + Send + 'static>>; /// Return `true` if calling [`Task::into_fn`] on this `Task` returns `Some`. fn is_fn(&self) -> bool; } /// Implement the `Task` trait for any FnOnce closure that returns a thread-safe result. impl Task for F where R: Send, F: FnOnce() -> R + Send + 'static, { fn run(self) -> R { self() } fn into_fn(self) -> Option R + Send + 'static>> { Some(Box::new(self)) } fn is_fn(&self) -> bool { true } } /// Handle returned by [`ThreadPool::evaluate`](struct.ThreadPool.html#method.evaluate) and [`ThreadPool::complete`](struct.ThreadPool.html#method.complete) /// that allows to block the current thread and wait for the result of a submitted task. The returned `JoinHandle` may also be sent to the [`ThreadPool`](struct.ThreadPool.html) /// to create a task that blocks a worker thread until the task is completed and then does something with the result. This handle communicates with the worker thread /// using a oneshot channel blocking the thread when [`try_await_complete()`](struct.JoinHandle.html#method.try_await_complete) is called until a message, i.e. the result of the /// task, is received. pub struct JoinHandle { pub receiver: oneshot::Receiver, } impl JoinHandle { /// Block the current thread until the result of the task is received. /// /// # Errors /// /// This function might return a `oneshot::Canceled` if the channel was broken /// before the result was received. This is generally the case if execution of /// the task panicked. pub fn try_await_complete(self) -> Result { block_on(self.receiver) } /// Block the current thread until the result of the task is received. /// /// # Panics /// /// This function might panic if [`try_await_complete()`](struct.JoinHandle.html#method.try_await_complete) returns `oneshot::Canceled`. /// This is generally the case if execution of the task panicked and the sender was dropped before sending a result to the receiver. pub fn await_complete(self) -> T { self.try_await_complete() .expect("could not receive message because channel was cancelled") } } #[cfg(feature = "async")] struct AsyncTask { future: Mutex>>, pool: ThreadPool, } /// Implement `ArcWake` for `AsyncTask` by re-submitting the `AsyncTask` i.e. the `Future` to the pool. #[cfg(feature = "async")] impl ArcWake for AsyncTask { fn wake_by_ref(arc_self: &Arc) { let cloned_task = arc_self.clone(); arc_self .pool .try_execute(cloned_task) .expect("failed to wake future because message could not be sent to pool"); } } /// Implement the `Task` trait for `AsyncTask` in order to make it executable for the pool by /// creating a waker and polling the future. #[cfg(feature = "async")] impl Task<()> for Arc { fn run(self) { let mut future_slot = self.future.lock().expect("failed to acquire mutex"); if let Some(mut future) = future_slot.take() { let waker = waker_ref(&self); let context = &mut Context::from_waker(&*waker); if future.as_mut().poll(context).is_pending() { *future_slot = Some(future); } } } fn into_fn(self) -> Option> { None } fn is_fn(&self) -> bool { false } } // assert that Send is implemented trait ThreadSafe: Send {} impl ThreadSafe for dyn Task {} impl ThreadSafe for JoinHandle {} impl ThreadSafe for ThreadPool {} /// Self growing / shrinking `ThreadPool` implementation based on crossbeam's /// multi-producer multi-consumer channels that enables awaiting the result of a /// task and offers async support. /// /// This `ThreadPool` has two different pool sizes; a core pool size filled with /// threads that live for as long as the channel and a max pool size which describes /// the maximum amount of worker threads that may live at the same time. /// Those additional non-core threads have a specific keep_alive time described when /// creating the `ThreadPool` that defines how long such threads may be idle for /// without receiving any work before giving up and terminating their work loop. /// /// This `ThreadPool` does not spawn any threads until a task is submitted to it. /// Then it will create a new thread for each task until the core pool size is full. /// After that a new thread will only be created upon an `execute()` call if the /// current pool is lower than the max pool size and there are no idle threads. /// /// Functions like `evaluate()` and `complete()` return a `JoinHandle` that may be used /// to await the result of a submitted task or future. JoinHandles may be sent to the /// thread pool to create a task that blocks a worker thread until it receives the /// result of the other task and then operates on the result. If the task panics the /// `JoinHandle` receives a cancellation error. This is implemented using a futures /// oneshot channel to communicate with the worker thread. /// /// This `ThreadPool` may be used as a futures executor if the "async" feature is enabled, /// which is the case by default. The "async" feature includes the `spawn()` and /// `try_spawn()` functions which create a task that polls the future one by one and /// creates a waker that re-submits the future to the pool when it can make progress. /// Without the "async" feature, futures can simply be executed to completion using /// the `complete` function, which simply blocks a worker thread until the future has /// been polled to completion. /// /// The "async" feature can be disabled if not need by adding the following to your /// Cargo dependency: /// ```toml /// [dependencies.rusty_pool] /// default-features = false /// version = "*" /// ``` /// /// When creating a new worker this `ThreadPool` tries to increment the worker count /// using a compare-and-swap mechanism, if the increment fails because the total worker /// count has been incremented to the specified limit (the core_size when trying to /// create a core thread, else the max_size) by another thread, the pool tries to create /// a non-core worker instead (if previously trying to create a core worker and no idle /// worker exists) or sends the task to the channel instead. Panicking workers are always /// cloned and replaced. /// /// Locks are only used for the join functions to lock the `Condvar`, apart from that /// this `ThreadPool` implementation fully relies on crossbeam and atomic operations. /// This `ThreadPool` decides whether it is currently idle (and should fast-return /// join attempts) by comparing the total worker count to the idle worker count, which /// are two values stored in one `AtomicUsize` (both half the size of usize) making sure /// that if both are updated they may be updated in a single atomic operation. /// /// The thread pool and its crossbeam channel can be destroyed by using the shutdown /// function, however that does not stop tasks that are already running but will /// terminate the thread the next time it will try to fetch work from the channel. /// The channel is only destroyed once all clones of the `ThreadPool` have been /// shut down / dropped. /// /// # Usage /// Create a new `ThreadPool`: /// ```rust /// use rusty_pool::Builder; /// use rusty_pool::ThreadPool; /// // Create default `ThreadPool` configuration with the number of CPUs as core pool size /// let pool = ThreadPool::default(); /// // Create a `ThreadPool` with default naming: /// use std::time::Duration; /// let pool2 = ThreadPool::new(5, 50, Duration::from_secs(60)); /// // Create a `ThreadPool` with a custom name: /// let pool3 = ThreadPool::new_named(String::from("my_pool"), 5, 50, Duration::from_secs(60)); /// // using the Builder struct: /// let pool4 = Builder::new().core_size(5).max_size(50).build(); /// ``` /// /// Submit a closure for execution in the `ThreadPool`: /// ```rust /// use rusty_pool::ThreadPool; /// use std::thread; /// use std::time::Duration; /// let pool = ThreadPool::default(); /// pool.execute(|| { /// thread::sleep(Duration::from_secs(5)); /// print!("hello"); /// }); /// ``` /// /// Submit a task and await the result: /// ```rust /// use rusty_pool::ThreadPool; /// use std::thread; /// use std::time::Duration; /// let pool = ThreadPool::default(); /// let handle = pool.evaluate(|| { /// thread::sleep(Duration::from_secs(5)); /// return 4; /// }); /// let result = handle.await_complete(); /// assert_eq!(result, 4); /// ``` /// /// Spawn futures using the `ThreadPool`: /// ```rust /// async fn some_async_fn(x: i32, y: i32) -> i32 { /// x + y /// } /// /// async fn other_async_fn(x: i32, y: i32) -> i32 { /// x - y /// } /// /// use rusty_pool::ThreadPool; /// let pool = ThreadPool::default(); /// /// // simply complete future by blocking a worker until the future has been completed /// let handle = pool.complete(async { /// let a = some_async_fn(4, 6).await; // 10 /// let b = some_async_fn(a, 3).await; // 13 /// let c = other_async_fn(b, a).await; // 3 /// some_async_fn(c, 5).await // 8 /// }); /// assert_eq!(handle.await_complete(), 8); /// /// use std::sync::{Arc, atomic::{AtomicI32, Ordering}}; /// /// // spawn future and create waker that automatically re-submits itself to the threadpool if ready to make progress, this requires the "async" feature which is enabled by default /// let count = Arc::new(AtomicI32::new(0)); /// let clone = count.clone(); /// pool.spawn(async move { /// let a = some_async_fn(3, 6).await; // 9 /// let b = other_async_fn(a, 4).await; // 5 /// let c = some_async_fn(b, 7).await; // 12 /// clone.fetch_add(c, Ordering::Relaxed); /// }); /// pool.join(); /// assert_eq!(count.load(Ordering::Relaxed), 12); /// ``` /// /// Join and shut down the `ThreadPool`: /// ```rust /// use std::thread; /// use std::time::Duration; /// use rusty_pool::ThreadPool; /// use std::sync::{Arc, atomic::{AtomicI32, Ordering}}; /// /// let pool = ThreadPool::default(); /// for _ in 0..10 { /// pool.execute(|| { thread::sleep(Duration::from_secs(10)) }) /// } /// // wait for all threads to become idle, i.e. all tasks to be completed including tasks added by other threads after join() is called by this thread or for the timeout to be reached /// pool.join_timeout(Duration::from_secs(5)); /// /// let count = Arc::new(AtomicI32::new(0)); /// for _ in 0..15 { /// let clone = count.clone(); /// pool.execute(move || { /// thread::sleep(Duration::from_secs(5)); /// clone.fetch_add(1, Ordering::Relaxed); /// }); /// } /// /// // shut down and drop the only instance of this `ThreadPool` (no clones) causing the channel to be broken leading all workers to exit after completing their current work /// // and wait for all workers to become idle, i.e. finish their work. /// pool.shutdown_join(); /// assert_eq!(count.load(Ordering::Relaxed), 15); /// ``` #[derive(Clone)] pub struct ThreadPool { core_size: usize, max_size: usize, keep_alive: Duration, channel_data: Arc, worker_data: Arc, } impl ThreadPool { /// Construct a new `ThreadPool` with the specified core pool size, max pool size /// and keep_alive time for non-core threads. This function does not spawn any /// threads. This `ThreadPool` will receive a default name in the following format: /// "rusty_pool_" + pool number. /// /// `core_size` specifies the amount of threads to keep alive for as long as /// the `ThreadPool` exists and its channel remains connected. /// /// `max_size` specifies the maximum number of worker threads that may exist /// at the same time. /// /// `keep_alive` specifies the duration for which to keep non-core pool /// worker threads alive while they do not receive any work. /// /// # Panics /// /// This function will panic if max_size is 0, lower than core_size or exceeds half /// the size of usize. This restriction exists because two counters (total workers and /// idle counters) are stored within one AtomicUsize. pub fn new(core_size: usize, max_size: usize, keep_alive: Duration) -> Self { static POOL_COUNTER: AtomicUsize = AtomicUsize::new(1); let name = format!( "rusty_pool_{}", POOL_COUNTER.fetch_add(1, Ordering::Relaxed) ); ThreadPool::new_named(name, core_size, max_size, keep_alive) } /// Construct a new `ThreadPool` with the specified name, core pool size, max pool size /// and keep_alive time for non-core threads. This function does not spawn any /// threads. /// /// `name` the name of the `ThreadPool` that will be used as prefix for each /// thread. /// /// `core_size` specifies the amount of threads to keep alive for as long as /// the `ThreadPool` exists and its channel remains connected. /// /// `max_size` specifies the maximum number of worker threads that may exist /// at the same time. /// /// `keep_alive` specifies the duration for which to keep non-core pool /// worker threads alive while they do not receive any work. /// /// # Panics /// /// This function will panic if max_size is 0, lower than core_size or exceeds half /// the size of usize. This restriction exists because two counters (total workers and /// idle counters) are stored within one AtomicUsize. pub fn new_named( name: String, core_size: usize, max_size: usize, keep_alive: Duration, ) -> Self { let (sender, receiver) = crossbeam_channel::unbounded(); if max_size == 0 || max_size < core_size { panic!("max_size must be greater than 0 and greater or equal to the core pool size"); } else if max_size > MAX_SIZE { panic!( "max_size may not exceed {}, the maximum value that can be stored within half the bits of usize ({} -> {} bits in this case)", MAX_SIZE, BITS, BITS / 2 ); } let worker_data = WorkerData { pool_name: name, worker_count_data: WorkerCountData::default(), worker_number: AtomicUsize::new(1), join_notify_condvar: Condvar::new(), join_notify_mutex: Mutex::new(()), join_generation: AtomicUsize::new(0), }; let channel_data = ChannelData { sender, receiver }; Self { core_size, max_size, keep_alive, channel_data: Arc::new(channel_data), worker_data: Arc::new(worker_data), } } /// Get the number of live workers, includes all workers waiting for work or executing tasks. /// /// This counter is incremented when creating a new worker. The value is increment just before /// the worker starts executing its initial task. Incrementing the worker total might fail /// if the total has already reached the specified limit (either core_size or max_size) after /// being incremented by another thread, as of rusty_pool 0.5.0 failed attempts to create a worker /// no longer skews the worker total as failed attempts to increment the worker total does not /// increment the value at all. /// This counter is decremented when a worker reaches the end of its working loop, which for non-core /// threads might happen if it does not receive any work during its keep alive time, /// for core threads this only happens once the channel is disconnected. pub fn get_current_worker_count(&self) -> usize { self.worker_data.worker_count_data.get_total_worker_count() } /// Get the number of workers currently waiting for work. Those threads are currently /// polling from the crossbeam receiver. Core threads wait indefinitely and might remain /// in this state until the `ThreadPool` is dropped. The remaining threads give up after /// waiting for the specified keep_alive time. pub fn get_idle_worker_count(&self) -> usize { self.worker_data.worker_count_data.get_idle_worker_count() } /// Send a new task to the worker threads. This function is responsible for sending the message through the /// channel and creating new workers if needed. If the current worker count is lower than the core pool size /// this function will always create a new worker. If the current worker count is equal to or greater than /// the core pool size this function only creates a new worker if the worker count is below the max pool size /// and there are no idle threads. /// /// When attempting to increment the total worker count before creating a worker fails due to the /// counter reaching the provided limit (core_size when attempting to create core thread, else /// max_size) after being incremented by another thread, the pool tries to create /// a non-core worker instead (if previously trying to create a core worker and no idle /// worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, /// either because the current value of the counter matched the expected value or because the /// last observed value was still below the limit, the worker starts with the provided task as /// initial task and spawns its thread. /// /// # Panics /// /// This function might panic if `try_execute` returns an error when the crossbeam channel has been /// closed unexpectedly. /// This should never occur under normal circumstances using safe code, as shutting down the `ThreadPool` /// consumes ownership and the crossbeam channel is never dropped unless dropping the `ThreadPool`. pub fn execute + 'static>(&self, task: T) { if self.try_execute(task).is_err() { panic!("the channel of the thread pool has been closed"); } } /// Send a new task to the worker threads. This function is responsible for sending the message through the /// channel and creating new workers if needed. If the current worker count is lower than the core pool size /// this function will always create a new worker. If the current worker count is equal to or greater than /// the core pool size this function only creates a new worker if the worker count is below the max pool size /// and there are no idle threads. /// /// When attempting to increment the total worker count before creating a worker fails due to the /// counter reaching the provided limit (core_size when attempting to create core thread, else /// max_size) after being incremented by another thread, the pool tries to create /// a non-core worker instead (if previously trying to create a core worker and no idle /// worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, /// either because the current value of the counter matched the expected value or because the /// last observed value was still below the limit, the worker starts with the provided task as /// initial task and spawns its thread. /// /// # Errors /// /// This function might return `crossbeam_channel::SendError` if the sender was dropped unexpectedly. pub fn try_execute + 'static>( &self, task: T, ) -> Result<(), crossbeam_channel::SendError> { if task.is_fn() { self.try_execute_task( task.into_fn() .expect("Task::into_fn returned None despite is_fn returning true"), ) } else { self.try_execute_task(Box::new(move || { task.run(); })) } } /// Send a new task to the worker threads and return a [`JoinHandle`](struct.JoinHandle.html) that may be used to await /// the result. This function is responsible for sending the message through the channel and creating new /// workers if needed. If the current worker count is lower than the core pool size this function will always /// create a new worker. If the current worker count is equal to or greater than the core pool size this /// function only creates a new worker if the worker count is below the max pool size and there are no idle /// threads. /// /// When attempting to increment the total worker count before creating a worker fails due to the /// counter reaching the provided limit (core_size when attempting to create core thread, else /// max_size) after being incremented by another thread, the pool tries to create /// a non-core worker instead (if previously trying to create a core worker and no idle /// worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, /// either because the current value of the counter matched the expected value or because the /// last observed value was still below the limit, the worker starts with the provided task as /// initial task and spawns its thread. /// /// # Panics /// /// This function might panic if `try_execute` returns an error when the crossbeam channel has been /// closed unexpectedly. /// This should never occur under normal circumstances using safe code, as shutting down the `ThreadPool` /// consumes ownership and the crossbeam channel is never dropped unless dropping the `ThreadPool`. pub fn evaluate + 'static>(&self, task: T) -> JoinHandle { match self.try_evaluate(task) { Ok(handle) => handle, Err(e) => panic!("the channel of the thread pool has been closed: {:?}", e), } } /// Send a new task to the worker threads and return a [`JoinHandle`](struct.JoinHandle.html) that may be used to await /// the result. This function is responsible for sending the message through the channel and creating new /// workers if needed. If the current worker count is lower than the core pool size this function will always /// create a new worker. If the current worker count is equal to or greater than the core pool size this /// function only creates a new worker if the worker count is below the max pool size and there are no idle /// threads. /// /// When attempting to increment the total worker count before creating a worker fails due to the /// counter reaching the provided limit (core_size when attempting to create core thread, else /// max_size) after being incremented by another thread, the pool tries to create /// a non-core worker instead (if previously trying to create a core worker and no idle /// worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, /// either because the current value of the counter matched the expected value or because the /// last observed value was still below the limit, the worker starts with the provided task as /// initial task and spawns its thread. /// /// # Errors /// /// This function might return `crossbeam_channel::SendError` if the sender was dropped unexpectedly. pub fn try_evaluate + 'static>( &self, task: T, ) -> Result, crossbeam_channel::SendError> { let (sender, receiver) = oneshot::channel::(); let join_handle = JoinHandle { receiver }; let job = || { let result = task.run(); // if the receiver was dropped that means the caller was not interested in the result let _ignored_result = sender.send(result); }; let execute_attempt = self.try_execute_task(Box::new(job)); execute_attempt.map(|_| join_handle) } /// Send a task to the `ThreadPool` that completes the given `Future` and return a [`JoinHandle`](struct.JoinHandle.html) /// that may be used to await the result. This function simply calls [`evaluate()`](struct.ThreadPool.html#method.evaluate) /// with a closure that calls `block_on` with the provided future. /// /// # Panic /// /// This function panics if the task fails to be sent to the `ThreadPool` due to the channel being broken. pub fn complete( &self, future: impl Future + 'static + Send, ) -> JoinHandle { self.evaluate(|| block_on(future)) } /// Send a task to the `ThreadPool` that completes the given `Future` and return a [`JoinHandle`](struct.JoinHandle.html) /// that may be used to await the result. This function simply calls [`try_evaluate()`](struct.ThreadPool.html#method.try_evaluate) /// with a closure that calls `block_on` with the provided future. /// /// # Errors /// /// This function returns `crossbeam_channel::SendError` if the task fails to be sent to the `ThreadPool` due to the channel being broken. pub fn try_complete( &self, future: impl Future + 'static + Send, ) -> Result, crossbeam_channel::SendError> { self.try_evaluate(|| block_on(future)) } /// Submit a `Future` to be polled by this `ThreadPool`. Unlike [`complete()`](struct.ThreadPool.html#method.complete) this does not /// block a worker until the `Future` has been completed but polls the `Future` once at a time and creates a `Waker` /// that re-submits the Future to this pool when awakened. Since `Arc` implements the [`Task`](trait.Task.html) trait this /// function simply constructs the `AsyncTask` and calls [`execute()`](struct.ThreadPool.html#method.execute). /// /// # Panic /// /// This function panics if the task fails to be sent to the `ThreadPool` due to the channel being broken. #[cfg(feature = "async")] pub fn spawn(&self, future: impl Future + 'static + Send) { let future_task = Arc::new(AsyncTask { future: Mutex::new(Some(Box::pin(future))), pool: self.clone(), }); self.execute(future_task) } /// Submit a `Future` to be polled by this `ThreadPool`. Unlike [`try_complete()`](struct.ThreadPool.html#method.try_complete) this does not /// block a worker until the `Future` has been completed but polls the `Future` once at a time and creates a `Waker` /// that re-submits the Future to this pool when awakened. Since `Arc` implements the [`Task`](trait.Task.html) trait this /// function simply constructs the `AsyncTask` and calls [`try_execute()`](struct.ThreadPool.html#method.try_execute). /// /// # Errors /// /// This function returns `crossbeam_channel::SendError` if the task fails to be sent to the `ThreadPool` due to the channel being broken. #[cfg(feature = "async")] pub fn try_spawn( &self, future: impl Future + 'static + Send, ) -> Result<(), crossbeam_channel::SendError> { let future_task = Arc::new(AsyncTask { future: Mutex::new(Some(Box::pin(future))), pool: self.clone(), }); self.try_execute(future_task) } /// Create a top-level `Future` that awaits the provided `Future` and then sends the result to the /// returned [`JoinHandle`](struct.JoinHandle.html). Unlike [`complete()`](struct.ThreadPool.html#method.complete) this does not /// block a worker until the `Future` has been completed but polls the `Future` once at a time and creates a `Waker` /// that re-submits the Future to this pool when awakened. Since `Arc` implements the [`Task`](trait.Task.html) trait this /// function simply constructs the `AsyncTask` and calls [`execute()`](struct.ThreadPool.html#method.execute). /// /// This enables awaiting the final result outside of an async context like [`complete()`](struct.ThreadPool.html#method.complete) while still /// polling the future lazily instead of eagerly blocking the worker until the future is done. /// /// # Panic /// /// This function panics if the task fails to be sent to the `ThreadPool` due to the channel being broken. #[cfg(feature = "async")] pub fn spawn_await( &self, future: impl Future + 'static + Send, ) -> JoinHandle { match self.try_spawn_await(future) { Ok(handle) => handle, Err(e) => panic!("the channel of the thread pool has been closed: {:?}", e), } } /// Create a top-level `Future` that awaits the provided `Future` and then sends the result to the /// returned [`JoinHandle`](struct.JoinHandle.html). Unlike [`try_complete()`](struct.ThreadPool.html#method.try_complete) this does not /// block a worker until the `Future` has been completed but polls the `Future` once at a time and creates a `Waker` /// that re-submits the Future to this pool when awakened. Since `Arc` implements the [`Task`](trait.Task.html) trait this /// function simply constructs the `AsyncTask` and calls [`try_execute()`](struct.ThreadPool.html#method.try_execute). /// /// This enables awaiting the final result outside of an async context like [`complete()`](struct.ThreadPool.html#method.complete) while still /// polling the future lazily instead of eagerly blocking the worker until the future is done. /// /// # Errors /// /// This function returns `crossbeam_channel::SendError` if the task fails to be sent to the `ThreadPool` due to the channel being broken. #[cfg(feature = "async")] pub fn try_spawn_await( &self, future: impl Future + 'static + Send, ) -> Result, crossbeam_channel::SendError> { let (sender, receiver) = oneshot::channel::(); let join_handle = JoinHandle { receiver }; self.try_spawn(async { let result = future.await; // if the receiver was dropped that means the caller was not interested in the result let _ignored_result = sender.send(result); }) .map(|_| join_handle) } #[inline] fn try_execute_task(&self, task: Job) -> Result<(), crossbeam_channel::SendError> { // create a new worker either if the current worker count is lower than the core pool size // or if there are no idle threads and the current worker count is lower than the max pool size let worker_count_data = &self.worker_data.worker_count_data; let mut worker_count_val = worker_count_data.worker_count.load(Ordering::Relaxed); let (mut curr_worker_count, idle_worker_count) = WorkerCountData::split(worker_count_val); let mut curr_idle_count = idle_worker_count; // always create a new worker if current pool size is below core size if curr_worker_count < self.core_size { let witnessed = worker_count_data.try_increment_worker_total(worker_count_val, self.core_size); // the witnessed value matched the expected value, meaning the initial exchange succeeded, or the final witnessed // value is still below the coreSize, meaning the increment eventually succeeded if witnessed == worker_count_val || WorkerCountData::get_total_count(witnessed) < self.core_size { let worker = Worker::new( self.channel_data.receiver.clone(), Arc::clone(&self.worker_data), None, ); worker.start(Some(task)); return Ok(()); } curr_worker_count = WorkerCountData::get_total_count(witnessed); curr_idle_count = WorkerCountData::get_idle_count(witnessed); worker_count_val = witnessed; } // create a new worker if the current worker count is below the maxSize and the pool has been observed to be busy // (no idle workers) during the invocation of this function if curr_worker_count < self.max_size && (idle_worker_count == 0 || curr_idle_count == 0) { let witnessed = worker_count_data.try_increment_worker_total(worker_count_val, self.max_size); if witnessed == worker_count_val || WorkerCountData::get_total_count(witnessed) < self.max_size { let worker = Worker::new( self.channel_data.receiver.clone(), Arc::clone(&self.worker_data), Some(self.keep_alive), ); worker.start(Some(task)); return Ok(()); } } self.send_task_to_channel(task) } /// Blocks the current thread until there aren't any non-idle threads anymore. /// This includes work started after calling this function. /// This function blocks until the next time this `ThreadPool` completes all of its work, /// except if all threads are idle and the channel is empty at the time of calling this /// function, in which case it will fast-return. /// /// This utilizes a `Condvar` that is notified by workers when they complete a job and notice /// that the channel is currently empty and it was the last thread to finish the current /// generation of work (i.e. when incrementing the idle worker counter brings the value /// up to the total worker counter, meaning it's the last thread to become idle). pub fn join(&self) { self.inner_join(None); } /// Blocks the current thread until there aren't any non-idle threads anymore or until the /// specified time_out Duration passes, whichever happens first. /// This includes work started after calling this function. /// This function blocks until the next time this `ThreadPool` completes all of its work, /// (or until the time_out is reached) except if all threads are idle and the channel is /// empty at the time of calling this function, in which case it will fast-return. /// /// This utilizes a `Condvar` that is notified by workers when they complete a job and notice /// that the channel is currently empty and it was the last thread to finish the current /// generation of work (i.e. when incrementing the idle worker counter brings the value /// up to the total worker counter, meaning it's the last thread to become idle). pub fn join_timeout(&self, time_out: Duration) { self.inner_join(Some(time_out)); } /// Destroy this `ThreadPool` by claiming ownership and dropping the value, /// causing the `Sender` to drop thus disconnecting the channel. /// Threads in this pool that are currently executing a task will finish what /// they're doing until they check the channel, discovering that it has been /// disconnected from the sender and thus terminate their work loop. /// /// If other clones of this `ThreadPool` exist the sender will remain intact /// and tasks submitted to those clones will succeed, this includes pending /// `AsyncTask` instances as they hold an owned clone of the `ThreadPool` /// to re-submit awakened futures. pub fn shutdown(self) { drop(self); } /// Destroy this `ThreadPool` by claiming ownership and dropping the value, /// causing the `Sender` to drop thus disconnecting the channel. /// Threads in this pool that are currently executing a task will finish what /// they're doing until they check the channel, discovering that it has been /// disconnected from the sender and thus terminate their work loop. /// /// If other clones of this `ThreadPool` exist the sender will remain intact /// and tasks submitted to those clones will succeed, this includes pending /// `AsyncTask` instances as they hold an owned clone of the `ThreadPool` /// to re-submit awakened futures. /// /// This function additionally joins all workers after dropping the pool to /// wait for all work to finish. /// Blocks the current thread until there aren't any non-idle threads anymore. /// This function blocks until this `ThreadPool` completes all of its work, /// except if all threads are idle and the channel is empty at the time of /// calling this function, in which case the join will fast-return. /// If other live clones of this `ThreadPool` exist this behaves the same as /// calling [`join`](struct.ThreadPool.html#method.join) on a live `ThreadPool` as tasks submitted /// to one of the clones will be joined as well. /// /// The join utilizes a `Condvar` that is notified by workers when they complete a job and notice /// that the channel is currently empty and it was the last thread to finish the current /// generation of work (i.e. when incrementing the idle worker counter brings the value /// up to the total worker counter, meaning it's the last thread to become idle). pub fn shutdown_join(self) { self.inner_shutdown_join(None); } /// Destroy this `ThreadPool` by claiming ownership and dropping the value, /// causing the `Sender` to drop thus disconnecting the channel. /// Threads in this pool that are currently executing a task will finish what /// they're doing until they check the channel, discovering that it has been /// disconnected from the sender and thus terminate their work loop. /// /// If other clones of this `ThreadPool` exist the sender will remain intact /// and tasks submitted to those clones will succeed, this includes pending /// `AsyncTask` instances as they hold an owned clone of the `ThreadPool` /// to re-submit awakened futures. /// /// This function additionally joins all workers after dropping the pool to /// wait for all work to finish. /// Blocks the current thread until there aren't any non-idle threads anymore or until the /// specified time_out Duration passes, whichever happens first. /// This function blocks until this `ThreadPool` completes all of its work, /// (or until the time_out is reached) except if all threads are idle and the channel is /// empty at the time of calling this function, in which case the join will fast-return. /// If other live clones of this `ThreadPool` exist this behaves the same as /// calling [`join`](struct.ThreadPool.html#method.join) on a live `ThreadPool` as tasks submitted /// to one of the clones will be joined as well. /// /// The join utilizes a `Condvar` that is notified by workers when they complete a job and notice /// that the channel is currently empty and it was the last thread to finish the current /// generation of work (i.e. when incrementing the idle worker counter brings the value /// up to the total worker counter, meaning it's the last thread to become idle). pub fn shutdown_join_timeout(self, timeout: Duration) { self.inner_shutdown_join(Some(timeout)); } /// Return the name of this pool, used as prefix for each worker thread. pub fn get_name(&self) -> &str { &self.worker_data.pool_name } /// Starts all core workers by creating core idle workers until the total worker count reaches the core count. /// /// Returns immediately if the current worker count is already >= core size. pub fn start_core_threads(&self) { let worker_count_data = &self.worker_data.worker_count_data; let core_size = self.core_size; let mut curr_worker_count = worker_count_data.worker_count.load(Ordering::Relaxed); if WorkerCountData::get_total_count(curr_worker_count) >= core_size { return; } loop { let witnessed = worker_count_data.try_increment_worker_count( curr_worker_count, INCREMENT_TOTAL | INCREMENT_IDLE, core_size, ); if WorkerCountData::get_total_count(witnessed) >= core_size { return; } let worker = Worker::new( self.channel_data.receiver.clone(), Arc::clone(&self.worker_data), None, ); worker.start(None); curr_worker_count = witnessed; } } #[inline] fn send_task_to_channel(&self, task: Job) -> Result<(), crossbeam_channel::SendError> { self.channel_data.sender.send(task)?; Ok(()) } #[inline] fn inner_join(&self, time_out: Option) { ThreadPool::_do_join(&self.worker_data, &self.channel_data.receiver, time_out); } #[inline] fn inner_shutdown_join(self, timeout: Option) { let current_worker_data = self.worker_data.clone(); let receiver = self.channel_data.receiver.clone(); drop(self); ThreadPool::_do_join(¤t_worker_data, &receiver, timeout); } #[inline] fn _do_join( current_worker_data: &Arc, receiver: &crossbeam_channel::Receiver, time_out: Option, ) { // no thread is currently doing any work, return if ThreadPool::is_idle(current_worker_data, receiver) { return; } let join_generation = current_worker_data.join_generation.load(Ordering::SeqCst); let guard = current_worker_data .join_notify_mutex .lock() .expect("could not get join notify mutex lock"); match time_out { Some(time_out) => { let _ret_guard = current_worker_data .join_notify_condvar .wait_timeout_while(guard, time_out, |_| { join_generation == current_worker_data.join_generation.load(Ordering::Relaxed) && !ThreadPool::is_idle(current_worker_data, receiver) }) .expect("could not wait for join condvar"); } None => { let _ret_guard = current_worker_data .join_notify_condvar .wait_while(guard, |_| { join_generation == current_worker_data.join_generation.load(Ordering::Relaxed) && !ThreadPool::is_idle(current_worker_data, receiver) }) .expect("could not wait for join condvar"); } }; // increment generation if current thread is first thread to be awakened from wait in current generation let _ = current_worker_data.join_generation.compare_exchange( join_generation, join_generation.wrapping_add(1), Ordering::SeqCst, Ordering::SeqCst, ); } #[inline] fn is_idle( current_worker_data: &Arc, receiver: &crossbeam_channel::Receiver, ) -> bool { let (current_worker_count, current_idle_count) = current_worker_data.worker_count_data.get_both(); current_idle_count == current_worker_count && receiver.is_empty() } } impl Default for ThreadPool { /// create default ThreadPool with the core pool size being equal to the number of cpus /// and the max_size being twice the core size with a 60 second timeout fn default() -> Self { let num_cpus = num_cpus::get(); ThreadPool::new( num_cpus, std::cmp::max(num_cpus, num_cpus * 2), Duration::from_secs(60), ) } } /// A helper struct to aid creating a new `ThreadPool` using default values where no value was /// explicitly specified. #[derive(Default)] pub struct Builder { name: Option, core_size: Option, max_size: Option, keep_alive: Option, } impl Builder { /// Create a new `Builder`. pub fn new() -> Builder { Builder::default() } /// Specify the name of the `ThreadPool` that will be used as prefix for the name of each worker thread. /// By default the name is "rusty_pool_x" with x being a static pool counter. pub fn name(mut self, name: String) -> Builder { self.name = Some(name); self } /// Specify the core pool size for the `ThreadPool`. The core pool size is the number of threads that stay alive /// for the entire lifetime of the `ThreadPool` or, to be more precise, its channel. These threads are spawned if /// a task is submitted to the `ThreadPool` and the current worker count is below the core pool size. pub fn core_size(mut self, size: usize) -> Builder { self.core_size = Some(size); self } /// Specify the maximum pool size this `ThreadPool` may scale up to. This numbers represents the maximum number /// of threads that may be alive at the same time within this pool. Additional threads above the core pool size /// only remain idle for the duration specified by the `keep_alive` parameter before terminating. If the core pool /// is full, the current pool size is below the max size and there are no idle threads then additional threads /// will be spawned. pub fn max_size(mut self, size: usize) -> Builder { self.max_size = Some(size); self } /// Specify the duration for which additional threads outside the core pool remain alive while not receiving any /// work before giving up and terminating. pub fn keep_alive(mut self, keep_alive: Duration) -> Builder { self.keep_alive = Some(keep_alive); self } /// Build the `ThreadPool` using the parameters previously supplied to this `Builder` using the number of CPUs as /// default core size if none provided, twice the core size as max size if none provided, 60 seconds keep_alive /// if none provided and the default naming (rusty_pool_{pool_number}) if none provided. /// This function calls [`ThreadPool::new`](struct.ThreadPool.html#method.new) or /// [`ThreadPool::new_named`](struct.ThreadPool.html#method.new_named) depending on whether a name was provided. /// /// # Panics /// /// Building might panic if the `max_size` is 0 or lower than `core_size` or exceeds half /// the size of usize. This restriction exists because two counters (total workers and /// idle counters) are stored within one AtomicUsize. pub fn build(self) -> ThreadPool { use std::cmp::{max, min}; let core_size = self.core_size.unwrap_or_else(|| { let num_cpus = num_cpus::get(); if let Some(max_size) = self.max_size { min(MAX_SIZE, min(num_cpus, max_size)) } else { min(MAX_SIZE, num_cpus) } }); // handle potential overflow: try using twice the core_size or return core_size let max_size = self .max_size .unwrap_or_else(|| min(MAX_SIZE, max(core_size, core_size * 2))); let keep_alive = self.keep_alive.unwrap_or_else(|| Duration::from_secs(60)); if let Some(name) = self.name { ThreadPool::new_named(name, core_size, max_size, keep_alive) } else { ThreadPool::new(core_size, max_size, keep_alive) } } } #[derive(Clone)] struct Worker { receiver: crossbeam_channel::Receiver, worker_data: Arc, keep_alive: Option, } impl Worker { fn new( receiver: crossbeam_channel::Receiver, worker_data: Arc, keep_alive: Option, ) -> Self { Worker { receiver, worker_data, keep_alive, } } fn start(self, task: Option) { let worker_name = format!( "{}_thread_{}", self.worker_data.pool_name, self.worker_data .worker_number .fetch_add(1, Ordering::Relaxed) ); thread::Builder::new() .name(worker_name) .spawn(move || { let mut sentinel = Sentinel::new(&self); if let Some(task) = task { self.exec_task_and_notify(&mut sentinel, task); } loop { // the two functions return different error types, but since the error type doesn't matter it is mapped to unit to make them compatible let received_task: Result = match self.keep_alive { Some(keep_alive) => self.receiver.recv_timeout(keep_alive).map_err(|_| ()), None => self.receiver.recv().map_err(|_| ()), }; match received_task { Ok(task) => { // mark current as no longer idle and execute task self.worker_data.worker_count_data.decrement_worker_idle(); self.exec_task_and_notify(&mut sentinel, task); } Err(_) => { // either channel was broken because the sender disconnected or, if can_timeout is true, the Worker has not received any work during // its keep_alive period and will now terminate, break working loop break; } } } // can decrement both at once as the thread only gets here from an idle state // (if waiting for work and receiving an error) self.worker_data.worker_count_data.decrement_both(); }) .expect("could not spawn thread"); } #[inline] fn exec_task_and_notify(&self, sentinel: &mut Sentinel, task: Job) { sentinel.is_working = true; task(); sentinel.is_working = false; // can already mark as idle as this thread will continue the work loop self.mark_idle_and_notify_joiners_if_no_work(); } #[inline] fn mark_idle_and_notify_joiners_if_no_work(&self) { let (old_total_count, old_idle_count) = self .worker_data .worker_count_data .increment_worker_idle_ret_both(); // if the last task was the last one in the current generation, // i.e. if incrementing the idle count leads to the idle count // being equal to the total worker count, notify joiners if old_total_count == old_idle_count + 1 && self.receiver.is_empty() { let _lock = self .worker_data .join_notify_mutex .lock() .expect("could not get join notify mutex lock"); self.worker_data.join_notify_condvar.notify_all(); } } } /// Type that exists to manage worker exit on panic. /// /// This type is constructed once per `Worker` and implements `Drop` to handle proper worker exit /// in case the worker panics when executing the current task or anywhere else in its work loop. /// If the `Sentinel` is dropped at the end of the worker's work loop and the current thread is /// panicking, handle worker exit the same way as if the task completed normally (if the worker /// panicked while executing a submitted task) then clone the worker and start it with an initial /// task of `None`. struct Sentinel<'s> { is_working: bool, worker_ref: &'s Worker, } impl Sentinel<'_> { fn new(worker_ref: &Worker) -> Sentinel<'_> { Sentinel { is_working: false, worker_ref, } } } impl Drop for Sentinel<'_> { fn drop(&mut self) { if thread::panicking() { if self.is_working { // worker thread panicked in the process of executing a submitted task, // run the same logic as if the task completed normally and mark it as // idle, since a clone of this worker will start the work loop as idle // thread self.worker_ref.mark_idle_and_notify_joiners_if_no_work(); } let worker = self.worker_ref.clone(); worker.start(None); } } } const WORKER_IDLE_MASK: usize = MAX_SIZE; const INCREMENT_TOTAL: usize = 1 << (BITS / 2); const INCREMENT_IDLE: usize = 1; /// Struct that stores and handles an `AtomicUsize` that stores the total worker count /// in the higher half of bits and the idle worker count in the lower half of bits. /// This allows to to increment / decrement both counters in a single atomic operation. #[derive(Default)] struct WorkerCountData { worker_count: AtomicUsize, } impl WorkerCountData { fn get_total_worker_count(&self) -> usize { let curr_val = self.worker_count.load(Ordering::Relaxed); WorkerCountData::get_total_count(curr_val) } fn get_idle_worker_count(&self) -> usize { let curr_val = self.worker_count.load(Ordering::Relaxed); WorkerCountData::get_idle_count(curr_val) } fn get_both(&self) -> (usize, usize) { let curr_val = self.worker_count.load(Ordering::Relaxed); WorkerCountData::split(curr_val) } // keep for testing and completion's sake #[allow(dead_code)] fn increment_both(&self) -> (usize, usize) { let old_val = self .worker_count .fetch_add(INCREMENT_TOTAL | INCREMENT_IDLE, Ordering::Relaxed); WorkerCountData::split(old_val) } fn decrement_both(&self) -> (usize, usize) { let old_val = self .worker_count .fetch_sub(INCREMENT_TOTAL | INCREMENT_IDLE, Ordering::Relaxed); WorkerCountData::split(old_val) } fn try_increment_worker_total(&self, expected: usize, max_total: usize) -> usize { self.try_increment_worker_count(expected, INCREMENT_TOTAL, max_total) } fn try_increment_worker_count( &self, mut expected: usize, increment: usize, max_total: usize, ) -> usize { loop { match self.worker_count.compare_exchange_weak( expected, expected + increment, Ordering::Relaxed, Ordering::Relaxed, ) { Ok(witnessed) => return witnessed, Err(witnessed) if WorkerCountData::get_total_count(witnessed) >= max_total => { return witnessed } Err(witnessed) => expected = witnessed, } } } // keep for testing and completion's sake #[allow(dead_code)] fn increment_worker_total(&self) -> usize { let old_val = self .worker_count .fetch_add(INCREMENT_TOTAL, Ordering::Relaxed); WorkerCountData::get_total_count(old_val) } // keep for testing and completion's sake #[allow(dead_code)] fn increment_worker_total_ret_both(&self) -> (usize, usize) { let old_val = self .worker_count .fetch_add(INCREMENT_TOTAL, Ordering::Relaxed); WorkerCountData::split(old_val) } // keep for testing and completion's sake #[allow(dead_code)] fn decrement_worker_total(&self) -> usize { let old_val = self .worker_count .fetch_sub(INCREMENT_TOTAL, Ordering::Relaxed); WorkerCountData::get_total_count(old_val) } // keep for testing and completion's sake #[allow(dead_code)] fn decrement_worker_total_ret_both(&self) -> (usize, usize) { let old_val = self .worker_count .fetch_sub(INCREMENT_TOTAL, Ordering::Relaxed); WorkerCountData::split(old_val) } // keep for testing and completion's sake #[allow(dead_code)] fn increment_worker_idle(&self) -> usize { let old_val = self .worker_count .fetch_add(INCREMENT_IDLE, Ordering::Relaxed); WorkerCountData::get_idle_count(old_val) } fn increment_worker_idle_ret_both(&self) -> (usize, usize) { let old_val = self .worker_count .fetch_add(INCREMENT_IDLE, Ordering::Relaxed); WorkerCountData::split(old_val) } fn decrement_worker_idle(&self) -> usize { let old_val = self .worker_count .fetch_sub(INCREMENT_IDLE, Ordering::Relaxed); WorkerCountData::get_idle_count(old_val) } // keep for testing and completion's sake #[allow(dead_code)] fn decrement_worker_idle_ret_both(&self) -> (usize, usize) { let old_val = self .worker_count .fetch_sub(INCREMENT_IDLE, Ordering::Relaxed); WorkerCountData::split(old_val) } #[inline] fn split(val: usize) -> (usize, usize) { let total_count = val >> (BITS / 2); let idle_count = val & WORKER_IDLE_MASK; (total_count, idle_count) } #[inline] fn get_total_count(val: usize) -> usize { val >> (BITS / 2) } #[inline] fn get_idle_count(val: usize) -> usize { val & WORKER_IDLE_MASK } } /// struct containing data shared between workers struct WorkerData { pool_name: String, worker_count_data: WorkerCountData, worker_number: AtomicUsize, join_notify_condvar: Condvar, join_notify_mutex: Mutex<()>, join_generation: AtomicUsize, } struct ChannelData { sender: crossbeam_channel::Sender, receiver: crossbeam_channel::Receiver, } #[cfg(test)] mod tests { use std::sync::{ atomic::{AtomicUsize, Ordering}, Arc, }; use std::thread; use std::time::Duration; use super::Builder; use super::ThreadPool; use super::WorkerCountData; #[test] fn it_works() { let pool = ThreadPool::new(2, 10, Duration::from_secs(5)); let count = Arc::new(AtomicUsize::new(0)); let count1 = count.clone(); pool.execute(move || { count1.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); let count2 = count.clone(); pool.execute(move || { count2.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); let count3 = count.clone(); pool.execute(move || { count3.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); let count4 = count.clone(); pool.execute(move || { count4.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); thread::sleep(std::time::Duration::from_secs(20)); let count5 = count.clone(); pool.execute(move || { count5.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); let count6 = count.clone(); pool.execute(move || { count6.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); let count7 = count.clone(); pool.execute(move || { count7.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); let count8 = count.clone(); pool.execute(move || { count8.fetch_add(1, Ordering::Relaxed); thread::sleep(std::time::Duration::from_secs(4)); }); thread::sleep(std::time::Duration::from_secs(20)); let count = count.load(Ordering::Relaxed); let worker_count = pool.get_current_worker_count(); assert_eq!(count, 8); // assert that non-core threads were dropped assert_eq!(worker_count, 2); assert_eq!(pool.get_idle_worker_count(), 2); } #[test] #[ignore] fn stress_test() { let pool = Arc::new(ThreadPool::new(3, 50, Duration::from_secs(30))); let counter = Arc::new(AtomicUsize::new(0)); for _ in 0..5 { let pool_1 = pool.clone(); let clone = counter.clone(); pool.execute(move || { for _ in 0..160 { let clone = clone.clone(); pool_1.execute(move || { clone.fetch_add(1, Ordering::Relaxed); thread::sleep(Duration::from_secs(10)); }); } thread::sleep(Duration::from_secs(20)); for _ in 0..160 { let clone = clone.clone(); pool_1.execute(move || { clone.fetch_add(1, Ordering::Relaxed); thread::sleep(Duration::from_secs(10)); }); } }); } thread::sleep(Duration::from_secs(10)); assert_eq!(pool.get_current_worker_count(), 50); pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 1600); thread::sleep(Duration::from_secs(31)); assert_eq!(pool.get_current_worker_count(), 3); } #[test] fn test_join() { // use a thread pool with one thread max to make sure the second task starts after // pool.join() is called to make sure it joins future tasks as well let pool = ThreadPool::new(0, 1, Duration::from_secs(5)); let counter = Arc::new(AtomicUsize::new(0)); let clone_1 = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(5)); clone_1.fetch_add(1, Ordering::Relaxed); }); let clone_2 = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(5)); clone_2.fetch_add(1, Ordering::Relaxed); }); pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 2); } #[test] fn test_join_timeout() { let pool = ThreadPool::new(0, 1, Duration::from_secs(5)); let counter = Arc::new(AtomicUsize::new(0)); let clone = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(10)); clone.fetch_add(1, Ordering::Relaxed); }); pool.join_timeout(Duration::from_secs(5)); assert_eq!(counter.load(Ordering::Relaxed), 0); pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 1); } #[test] fn test_shutdown() { let pool = ThreadPool::new(1, 3, Duration::from_secs(5)); let counter = Arc::new(AtomicUsize::new(0)); let clone_1 = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(5)); clone_1.fetch_add(1, Ordering::Relaxed); }); let clone_2 = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(5)); clone_2.fetch_add(1, Ordering::Relaxed); }); let clone_3 = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(5)); clone_3.fetch_add(1, Ordering::Relaxed); }); // since the pool only allows three threads this won't get the chance to run let clone_4 = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(5)); clone_4.fetch_add(1, Ordering::Relaxed); }); pool.join_timeout(Duration::from_secs(2)); pool.shutdown(); thread::sleep(Duration::from_secs(5)); assert_eq!(counter.load(Ordering::Relaxed), 3); } #[should_panic( expected = "max_size must be greater than 0 and greater or equal to the core pool size" )] #[test] fn test_panic_on_0_max_pool_size() { ThreadPool::new(0, 0, Duration::from_secs(2)); } #[should_panic( expected = "max_size must be greater than 0 and greater or equal to the core pool size" )] #[test] fn test_panic_on_smaller_max_than_core_pool_size() { ThreadPool::new(10, 4, Duration::from_secs(2)); } #[should_panic(expected = "max_size may not exceed")] #[test] fn test_panic_on_max_size_exceeds_half_usize() { ThreadPool::new( 10, 1 << ((std::mem::size_of::() * 8) / 2), Duration::from_secs(2), ); } #[test] fn test_empty_join() { let pool = ThreadPool::new(3, 10, Duration::from_secs(10)); pool.join(); } #[test] fn test_join_when_complete() { let pool = ThreadPool::new(3, 10, Duration::from_secs(5)); pool.execute(|| { thread::sleep(Duration::from_millis(5000)); }); thread::sleep(Duration::from_millis(5000)); pool.join(); } #[test] fn test_full_usage() { let pool = ThreadPool::new(5, 50, Duration::from_secs(10)); for _ in 0..100 { pool.execute(|| { thread::sleep(Duration::from_secs(30)); }); } thread::sleep(Duration::from_secs(10)); assert_eq!(pool.get_current_worker_count(), 50); pool.join(); thread::sleep(Duration::from_secs(15)); assert_eq!(pool.get_current_worker_count(), 5); } #[test] fn test_shutdown_join() { let pool = ThreadPool::new(1, 1, Duration::from_secs(5)); let counter = Arc::new(AtomicUsize::new(0)); let clone = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(10)); clone.fetch_add(1, Ordering::Relaxed); }); pool.shutdown_join(); assert_eq!(counter.load(Ordering::Relaxed), 1); } #[test] fn test_shutdown_join_timeout() { let pool = ThreadPool::new(1, 1, Duration::from_secs(5)); let counter = Arc::new(AtomicUsize::new(0)); let clone = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(10)); clone.fetch_add(1, Ordering::Relaxed); }); pool.shutdown_join_timeout(Duration::from_secs(5)); assert_eq!(counter.load(Ordering::Relaxed), 0); } #[test] fn test_empty_shutdown_join() { let pool = ThreadPool::new(1, 5, Duration::from_secs(5)); pool.shutdown_join(); } #[test] fn test_shutdown_core_pool() { let pool = ThreadPool::new(5, 5, Duration::from_secs(1)); let counter = Arc::new(AtomicUsize::new(0)); let worker_data = pool.worker_data.clone(); for _ in 0..7 { let clone = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(2)); clone.fetch_add(1, Ordering::Relaxed); }); } assert_eq!(pool.get_current_worker_count(), 5); assert_eq!(pool.get_idle_worker_count(), 0); pool.shutdown_join(); assert_eq!(counter.load(Ordering::Relaxed), 7); // give the workers time to exit thread::sleep(Duration::from_millis(50)); assert_eq!(worker_data.worker_count_data.get_total_worker_count(), 0); assert_eq!(worker_data.worker_count_data.get_idle_worker_count(), 0); } #[test] fn test_shutdown_idle_core_pool() { let pool = ThreadPool::new(5, 5, Duration::from_secs(1)); let counter = Arc::new(AtomicUsize::new(0)); let worker_data = pool.worker_data.clone(); for _ in 0..5 { let clone = counter.clone(); pool.execute(move || { clone.fetch_add(1, Ordering::Relaxed); }); } pool.shutdown_join(); assert_eq!(counter.load(Ordering::Relaxed), 5); // give the workers time to exit thread::sleep(Duration::from_millis(50)); assert_eq!(worker_data.worker_count_data.get_total_worker_count(), 0); assert_eq!(worker_data.worker_count_data.get_idle_worker_count(), 0); } #[test] fn test_shutdown_on_complete() { let pool = ThreadPool::new(3, 10, Duration::from_secs(5)); pool.execute(|| { thread::sleep(Duration::from_millis(5000)); }); thread::sleep(Duration::from_millis(5000)); pool.shutdown_join(); } #[test] fn test_shutdown_after_complete() { let pool = ThreadPool::new(3, 10, Duration::from_secs(5)); pool.execute(|| { thread::sleep(Duration::from_millis(5000)); }); thread::sleep(Duration::from_millis(7000)); pool.shutdown_join(); } #[test] fn worker_count_test() { let worker_count_data = WorkerCountData::default(); assert_eq!(worker_count_data.get_total_worker_count(), 0); assert_eq!(worker_count_data.get_idle_worker_count(), 0); worker_count_data.increment_both(); assert_eq!(worker_count_data.get_total_worker_count(), 1); assert_eq!(worker_count_data.get_idle_worker_count(), 1); for _ in 0..10 { worker_count_data.increment_both(); } assert_eq!(worker_count_data.get_total_worker_count(), 11); assert_eq!(worker_count_data.get_idle_worker_count(), 11); for _ in 0..15 { worker_count_data.increment_worker_total(); } for _ in 0..7 { worker_count_data.increment_worker_idle(); } assert_eq!(worker_count_data.get_total_worker_count(), 26); assert_eq!(worker_count_data.get_idle_worker_count(), 18); assert_eq!(worker_count_data.get_both(), (26, 18)); for _ in 0..5 { worker_count_data.decrement_both(); } assert_eq!(worker_count_data.get_total_worker_count(), 21); assert_eq!(worker_count_data.get_idle_worker_count(), 13); for _ in 0..13 { worker_count_data.decrement_worker_total(); } for _ in 0..4 { worker_count_data.decrement_worker_idle(); } assert_eq!(worker_count_data.get_total_worker_count(), 8); assert_eq!(worker_count_data.get_idle_worker_count(), 9); for _ in 0..456789 { worker_count_data.increment_worker_total(); } assert_eq!(worker_count_data.get_total_worker_count(), 456797); assert_eq!(worker_count_data.get_idle_worker_count(), 9); assert_eq!(worker_count_data.get_both(), (456797, 9)); for _ in 0..23456 { worker_count_data.increment_worker_idle(); } assert_eq!(worker_count_data.get_total_worker_count(), 456797); assert_eq!(worker_count_data.get_idle_worker_count(), 23465); for _ in 0..150000 { worker_count_data.decrement_worker_total(); } assert_eq!(worker_count_data.get_total_worker_count(), 306797); assert_eq!(worker_count_data.get_idle_worker_count(), 23465); for _ in 0..10000 { worker_count_data.decrement_worker_idle(); } assert_eq!(worker_count_data.get_total_worker_count(), 306797); assert_eq!(worker_count_data.get_idle_worker_count(), 13465); } #[test] fn test_try_increment_worker_total() { let worker_count_data = WorkerCountData::default(); let witness = worker_count_data.try_increment_worker_total(0, 5); assert_eq!(witness, 0); assert_eq!(worker_count_data.get_total_worker_count(), 1); assert_eq!(worker_count_data.get_idle_worker_count(), 0); let witness = worker_count_data.try_increment_worker_total(0, 5); assert_eq!(witness, 0x0000_0001_0000_0000); assert_eq!(worker_count_data.get_total_worker_count(), 2); assert_eq!(worker_count_data.get_idle_worker_count(), 0); worker_count_data.try_increment_worker_total(2, 5); worker_count_data.try_increment_worker_total(2, 5); worker_count_data.try_increment_worker_total(4, 5); worker_count_data.try_increment_worker_total(4, 5); let witness = worker_count_data.try_increment_worker_total(2, 5); assert_eq!(WorkerCountData::get_total_count(witness), 5); assert_eq!(WorkerCountData::get_idle_count(witness), 0); assert_eq!(worker_count_data.get_total_worker_count(), 5); assert_eq!(worker_count_data.get_idle_worker_count(), 0); let worker_count_data = Arc::new(worker_count_data); let mut join_handles = Vec::with_capacity(5); for _ in 0..5 { let worker_count_data = worker_count_data.clone(); let join_handle = thread::spawn(move || { for i in 0..5 { worker_count_data.try_increment_worker_total(5 + i, 15); } }); join_handles.push(join_handle); } for join_handle in join_handles { join_handle.join().unwrap(); } assert_eq!(worker_count_data.get_total_worker_count(), 15); assert_eq!(worker_count_data.get_idle_worker_count(), 0); } #[test] fn test_join_enqueued_task() { let pool = ThreadPool::new(3, 50, Duration::from_secs(20)); let counter = Arc::new(AtomicUsize::new(0)); for _ in 0..160 { let clone = counter.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(10)); clone.fetch_add(1, Ordering::Relaxed); }); } thread::sleep(Duration::from_secs(5)); assert_eq!(pool.get_current_worker_count(), 50); pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 160); thread::sleep(Duration::from_secs(21)); assert_eq!(pool.get_current_worker_count(), 3); } #[test] fn test_panic_all() { let pool = ThreadPool::new(3, 10, Duration::from_secs(2)); for _ in 0..10 { pool.execute(|| { panic!("test"); }) } pool.join(); thread::sleep(Duration::from_secs(5)); assert_eq!(pool.get_current_worker_count(), 3); assert_eq!(pool.get_idle_worker_count(), 3); } #[test] fn test_panic_some() { let pool = ThreadPool::new(3, 10, Duration::from_secs(5)); let counter = Arc::new(AtomicUsize::new(0)); for i in 0..10 { let clone = counter.clone(); pool.execute(move || { if i < 3 || i % 2 == 0 { thread::sleep(Duration::from_secs(5)); clone.fetch_add(1, Ordering::Relaxed); } else { thread::sleep(Duration::from_secs(5)); panic!("test"); } }) } pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 6); assert_eq!(pool.get_current_worker_count(), 10); assert_eq!(pool.get_idle_worker_count(), 10); thread::sleep(Duration::from_secs(10)); assert_eq!(pool.get_current_worker_count(), 3); assert_eq!(pool.get_idle_worker_count(), 3); } #[test] fn test_panic_all_core_threads() { let pool = ThreadPool::new(3, 3, Duration::from_secs(1)); let counter = Arc::new(AtomicUsize::new(0)); for _ in 0..3 { pool.execute(|| { panic!("test"); }) } pool.join(); for i in 0..10 { let clone = counter.clone(); pool.execute(move || { if i < 3 || i % 2 == 0 { clone.fetch_add(1, Ordering::Relaxed); } else { thread::sleep(Duration::from_secs(5)); panic!("test"); } }) } pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 6); assert_eq!(pool.get_current_worker_count(), 3); assert_eq!(pool.get_idle_worker_count(), 3); } #[test] fn test_drop_all_receivers() { let pool = ThreadPool::new(0, 3, Duration::from_secs(5)); let counter = Arc::new(AtomicUsize::new(0)); for _ in 0..3 { let clone = counter.clone(); pool.execute(move || { clone.fetch_add(1, Ordering::Relaxed); }) } pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 3); thread::sleep(Duration::from_secs(10)); assert_eq!(pool.get_current_worker_count(), 0); for _ in 0..3 { let clone = counter.clone(); pool.execute(move || { clone.fetch_add(1, Ordering::Relaxed); }) } pool.join(); assert_eq!(counter.load(Ordering::Relaxed), 6); } #[test] fn test_evaluate() { let pool = ThreadPool::new(0, 3, Duration::from_secs(5)); let count = AtomicUsize::new(0); let handle = pool.evaluate(move || { count.fetch_add(1, Ordering::Relaxed); thread::sleep(Duration::from_secs(5)); count.fetch_add(1, Ordering::Relaxed) }); let result = handle.await_complete(); assert_eq!(result, 1); } #[test] fn test_multiple_evaluate() { let pool = ThreadPool::new(0, 3, Duration::from_secs(5)); let count = AtomicUsize::new(0); let handle_1 = pool.evaluate(move || { for _ in 0..10000 { count.fetch_add(1, Ordering::Relaxed); } thread::sleep(Duration::from_secs(5)); for _ in 0..10000 { count.fetch_add(1, Ordering::Relaxed); } count.load(Ordering::Relaxed) }); let handle_2 = pool.evaluate(move || { let result = handle_1.await_complete(); let mut count = result; count += 15000; thread::sleep(Duration::from_secs(5)); count += 20000; count }); let result = handle_2.await_complete(); assert_eq!(result, 55000); } #[should_panic(expected = "could not receive message because channel was cancelled")] #[test] fn test_evaluate_panic() { let pool = Builder::new().core_size(5).max_size(50).build(); let handle = pool.evaluate(|| { let x = 3; if x == 3 { panic!("expected panic") } return x; }); handle.await_complete(); } #[test] fn test_complete_fut() { let pool = ThreadPool::new(0, 3, Duration::from_secs(5)); async fn async_fn() -> i8 { 8 } let fut = async_fn(); let handle = pool.complete(fut); assert_eq!(handle.await_complete(), 8); } #[cfg(feature = "async")] #[test] fn test_spawn() { let pool = ThreadPool::default(); async fn add(x: i32, y: i32) -> i32 { x + y } async fn multiply(x: i32, y: i32) -> i32 { x * y } let count = Arc::new(AtomicUsize::new(0)); let clone = count.clone(); pool.spawn(async move { let a = add(2, 3).await; // 5 let b = add(2, a).await; // 7 let c = multiply(2, b).await; // 14 let d = multiply(a, add(2, 1).await).await; // 15 let e = add(c, d).await; // 29 clone.fetch_add(e as usize, Ordering::Relaxed); }); pool.join(); assert_eq!(count.load(Ordering::Relaxed), 29); } #[cfg(feature = "async")] #[test] fn test_spawn_await() { let pool = ThreadPool::default(); async fn sub(x: i32, y: i32) -> i32 { x - y } async fn div(x: i32, y: i32) -> i32 { x / y } let handle = pool.spawn_await(async { let a = sub(120, 10).await; // 110 let b = div(sub(a, 10).await, 4).await; // 25 div(sub(b, div(10, 2).await).await, 5).await // 4 }); assert_eq!(handle.await_complete(), 4) } #[test] fn test_drop_oneshot_receiver() { let pool = Builder::new().core_size(1).max_size(1).build(); let handle = pool.evaluate(|| { thread::sleep(Duration::from_secs(5)); 5 }); drop(handle); thread::sleep(Duration::from_secs(10)); let current_thread_index = pool.worker_data.worker_number.load(Ordering::Relaxed); // current worker number of 2 means that one worker has started (initial number is 1 -> first worker gets and increments number) // indicating that the worker did not panic else it would have been replaced. assert_eq!(current_thread_index, 2); } #[test] fn test_builder_max_size() { Builder::new().max_size(1).build(); } #[test] fn test_multi_thread_join() { let pool = ThreadPool::default(); let count = Arc::new(AtomicUsize::new(0)); let clone1 = count.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(10)); clone1.fetch_add(1, Ordering::Relaxed); }); let clone2 = count.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(10)); clone2.fetch_add(1, Ordering::Relaxed); }); let clone3 = count.clone(); pool.execute(move || { thread::sleep(Duration::from_secs(10)); clone3.fetch_add(1, Ordering::Relaxed); }); let pool2 = pool.clone(); let clone4 = count.clone(); thread::spawn(move || { thread::sleep(Duration::from_secs(5)); pool2.execute(move || { thread::sleep(Duration::from_secs(15)); clone4.fetch_add(2, Ordering::Relaxed); }); }); let pool3 = pool.clone(); let pool4 = pool.clone(); let pool5 = pool.clone(); let h1 = thread::spawn(move || { pool3.join(); }); let h2 = thread::spawn(move || { pool4.join(); }); let h3 = thread::spawn(move || { pool5.join(); }); h1.join().unwrap(); h2.join().unwrap(); h3.join().unwrap(); assert_eq!(count.load(Ordering::Relaxed), 5); } #[test] fn test_start_core_threads() { let pool = Builder::new().core_size(5).build(); pool.start_core_threads(); assert_eq!(pool.get_current_worker_count(), 5); assert_eq!(pool.get_idle_worker_count(), 5); } #[test] fn test_start_and_use_core_threads() { let pool = Builder::new() .core_size(5) .max_size(10) .keep_alive(Duration::from_secs(u64::MAX)) .build(); pool.start_core_threads(); let result = pool.evaluate(|| 5 + 5).await_complete(); assert_eq!(result, 10); assert_eq!(pool.get_current_worker_count(), 5); } }