summaryrefslogtreecommitdiffstats
path: root/tokio/src/time
AgeCommit message (Collapse)Author
2020-12-08time: Fix race condition in timer drop (#3229)bdonlan
Dropping a timer on the millisecond that it was scheduled for, when it was on the pending list, could result in a panic previously, as we did not record the pending-list state in cached_when. Hopefully fixes: ZcashFoundation/zebra#1452
2020-12-07rt: fix deadlock in shutdown (#3228)bdonlan
Previously, the runtime shutdown logic would first-hand control over all cores to a single thread, which would sequentially shut down all tasks on the core and then wait for them to complete. This could deadlock when one task is waiting for a later core's task to complete. For example, in the newly added test, we have a `block_in_place` task that is waiting for another task to be dropped. If the latter task adds its core to the shutdown list later than the former, we end up waiting forever for the `block_in_place` task to complete. Additionally, there also was a bug wherein we'd attempt to park on the parker after shutting it down which was fixed as part of the refactors above. This change restructures the code to bring all tasks to a halt (and do any parking needed) before we collapse to a single thread to avoid this deadlock. There was also an issue in which canceled tasks would not unpark the originating thread, due to what appears to be some sort of optimization gone wrong. This has been fixed to be much more conservative in selecting when not to unpark the source thread (this may be too conservative; please take a look at the changes to `release()`). Fixes: #2789
2020-11-24time: replace 'ouClockTimeide' in internal docs with 'outside' (#3171)Max Sharnoff
2020-11-23time: use intrusive lists for timer tracking (#3080)bdonlan
More-or-less a half-rewrite of the current time driver, supporting the use of intrusive futures for timer registration. Fixes: #3028, #3069
2020-11-11time: document maximum sleep duration (#3126)Alice Ryhl
2020-10-12rt: simplify rt-* features (#2949)Taiki Endo
tokio: merge rt-core and rt-util as rt rename rt-threaded to rt-multi-thread tokio-util: rename rt-core to rt Closes #2942
2020-10-12rt: Remove `threaded_scheduler()` and `basic_scheduler()` (#2876)Lucio Franco
Co-authored-by: Alice Ryhl <alice@ryhl.io> Co-authored-by: Carl Lerche <me@carllerche.com>
2020-10-12time: move error types into `time::error` (#2938)Juan Alvarez
2020-10-12time: Clean up `Instant` docs to align with `std` (#2946)Lucio Franco
Co-authored-by: Alice Ryhl <alice@ryhl.io>
2020-10-08time: rename `Delay` future to `Sleep` (#2932)Juan Alvarez
2020-10-08chore: Fix clippy lints (#2931)bdonlan
Closes: #2929 Co-authored-by: Bryan Donlan <bdonlan@amazon.com>
2020-10-06time: clean time driver (#2905)greenwoodcm
* remove unnecessary wheel::Poll the timer wheel uses the `wheel::Poll` struct as input when advancing the timer to the next time step. the `Poll` struct contains an instant representing the time step to advance to and also contains an optional and mutable reference to an `Expiration` struct. from what I can tell, the latter field is only used in the context of polling the wheel and does not need to be exposed outside of that method. without the expiration field the `Poll` struct is nothing more than a wrapper around the instant being polled. this change removes the `Poll` struct and updates integration points accordingly. * remove Stack trait in favor of concrete Stack implementation * remove timer Registration struct
2020-10-05time: move DelayQueue to tokio-util (#2897)bdonlan
This change is intended to do the minimum to unblock 0.3; as such, for now, we duplicate the internal `time::wheel` structures in tokio-util, rather than trying to refactor things at this stage. Co-authored-by: Bryan Donlan <bdonlan@amazon.com>
2020-10-01chore: make #[doc(hidden)] apis private (#2901)Alice Ryhl
2020-10-01time: introduce `sleep` and `sleep_until` functions (#2826)Juan Alvarez
2020-09-24docs: use #[doc(no_inline)] on re-exports (#2874)Taiki Endo
2020-09-19time: remove outdated todo comment (#2848)Taiki Endo
2020-09-11docs: more doc fixes (#2831)Zephyr Shannon
Previous docs look like they were based on the docs for `insert_at`. Changed names of variables referred to and the explanation of when the value will be returned and under what condition it will be immediately available to make sense for a Duration argument instead of an Instant.
2020-09-05tokio: document missing timer panics (#2801)Blas Rodriguez Irizar
Fixes: #2696 Co-authored-by: Mikail Bagishov <bagishov.mikail@yandex.ru>
2020-09-02time: move throttle to StreamExt (#2752)Blas Rodriguez Irizar
Ref: #2727
2020-08-27Docs delay queue (#2793)Blas Rodriguez Irizar
2020-07-28rt: fix potential leak during runtime shutdown (#2649)Émile Grégoire
JoinHandle of threads created by the pool are now tracked and properly joined at shutdown. If the thread does not return within the timeout, then it's not joined and left to the OS for cleanup. Also, break a cycle between wakers held by the timer and the runtime. Fixes #2641, #2535
2020-07-26time: interval Stream impl requires stream feature (#2695)Alice Ryhl
Fixes: #1878
2020-07-26time: fix incorrect argument name in doc (#2691)Alice Ryhl
2020-07-26time: fix resetting expired timers causing panics (#2587)Felix Giese
* Add Unit Test demonstrating the issue This test demonstrates a panic that occurs when the user inserts an item with an instant in the past, and then tries to reset the timeout using the returned key * Guard reset_at against removals of expired items Trying to remove an already expired Timer Wheel entry (called by DelayQueue.reset()) causes panics in some cases as described in (#2573) This prevents this panic by removing the item from the expired queue and not the wheel in these cases Fixes: #2473
2020-07-24time: report correct error for timers that exceed max duration (#2023)Nikhil Benesch
Closes #1953
2020-07-24chore: complete CI migration to Github Actions (#2680)Carl Lerche
2020-07-20Update doc comments (#2572)Mikail Bagishov
* Update doc comments * Remove trailing whitespace
2020-07-20time: improve Entry field comment (#2671)Blas Rodriguez Irizar
Applying a suggestion from #2617 to make the sentence more clear.
2020-07-10fix: Update the docs of "pause" to state that time will still advance (#2647)Markus Westerlind
* doc: Update the docs of "pause" to state that time will still advance This was changed in #2059. This had me extremely confused for some time as my timeouts fired immediately, without the wrapped future that were waiting on IO to actually run long enough. I am not sure about the exact wording here but this had me very confused for some time. Deprecating "pause" and giving it a more accurate name may be a good idea as well. ```rust async fn timeout_advances() { time::pause(); timeout(ms(1), async { // Change to 1 and the this future resolve, 2 or // more and the timeout resolves for _ in 0..2 { tokio::task::yield_now().await } }) .await .unwrap(); } ``` * Update tokio/src/time/clock.rs Co-authored-by: Alice Ryhl <alice@ryhl.io> Co-authored-by: Alice Ryhl <alice@ryhl.io>
2020-06-16time: add example using `interval` to the time module (#2623)Craig Pastro
2020-06-12chore: reduce pin related unsafe code (#2613)Taiki Endo
2020-06-10doc: add sleep alias to delay_for (#2589)Alice Ryhl
2020-05-31docs: use intra-links in the docs (#2575)xliiv
2020-05-30docs: replace method links with intra-links (#2540)xliiv
2020-05-21coop: Undo budget decrement on Pending (#2549)Jon Gjengset
This patch updates the coop logic so that the budget is only decremented if a future makes progress (that is, if it returns `Ready`). This is realized by restoring the budget to its former value after `poll_proceed` _unless_ the caller indicates that it made progress. The thinking here is that we always want tasks to make progress when we poll them. With the way things were, if a task polled 128 resources that could make no progress, and just returned `Pending`, then a 129th resource that _could_ make progress would not be polled. Worse yet, this could manifest as a deadlock, if the first 128 resources were all _waiting_ for the 129th resource, since it would _never_ be polled. The downside of this change is that `Pending` resources now do not take up any part of the budget, even though they _do_ take up time on the executor. If a task is particularly aggressive (or unoptimized), and polls a large number of resources that cannot make progress whenever it is polled, then coop will allow it to run potentially much longer before yielding than it could before. The impact of this should be relatively contained though, because tasks that behaved in this way in the past probably ignored `Pending` _anyway_, so whether a resource returned `Pending` due to coop or due to lack of progress may not make a difference to it.
2020-05-12chore: change norun to no_run (#2518)Alice Ryhl
I was building the docs and got the following documentation warning: warning: unknown attribute `norun`. Did you mean `no_run`? --> tokio/src/time/throttle.rs:13:1 | 13 | / /// Slows down a stream by enforcing a delay between items. 14 | | /// They will be produced not more often than the specified interval. 15 | | /// 16 | | /// # Example ... | 31 | | /// # } 32 | | /// ``` | |_______^ | = help: the code block will either not be tested if not marked as a rust one or will be run (which you might not want)
2020-04-29time: notify when resetting a Delay to a time in the past (#2290)Thomas Whiteway
If a Delay has been polled, then the task that polled it may be waiting for a notification. If the delay gets reset to a time in the past, then it immediately becomes elapsed, so it should notify the relevant task.
2020-04-13doc: fix a few broken links (#2400)xliiv
2020-04-12docs: replace some html links with rustdoc paths (#2381)xliiv
Included changes - all simple references like `<type>.<name>.html` for these types - enum - fn - struct - trait - type - simple references for methods, like struct.DelayQueue.html#method.poll Refs: #1473
2020-04-02Expose time::deplay_queue::Expired::deadline (#2300)MOZGIII
* Expose time::deplay_queue::Expired::deadline * Return by value
2020-03-26timer: fix loom test (#2346)Carl Lerche
Fixes a test from a PR that was written before the recent loom upgrade. A change in the details how loom executes models resulted in the test to start failing. The fix is to reduce the number of iterations performed by the test.
2020-03-26timer: improve memory ordering in Inner's increment (#2107)Brian L. Troutwine
This commit improves the memory ordering in the implementation of Inner's increment function. The former code did a sequentially consistent load of self.num, then entered a loop with a sequentially consistent compare and swap on the same, bailing out with and Err only if the loaded value was MAX_TIMEOUTS. The use of SeqCst means that all threads must observe all relevant memory operations in the same order, implying synchronization between all CPUs. This commit adjusts the implementation in two key ways. First, the initial load of self.num is now down with Relaxed ordering. If two threads entered this code simultaneously, formerly, tokio required that one proceed before the other, negating their parallelism. Now, either thread may proceed without coordination. Second, the SeqCst compare_and_swap is changed to a Release, Relaxed compare_exchange_weak. The first memory ordering referrs to success: if the value is swapped the load of that value for comparison will be Relaxed and the store will be Release. The second memory ordering referrs to failure: if the value is not swapped the load is Relaxed. The _weak variant may spuriously fail but will generate better code. These changes mean that it is possible for more loops to be taken per call than strictly necessary but with greater parallelism available on this operation, improved energy consumption as CPUs don't have to coordinate as much.
2020-03-26time: fix DelayQueue rewriting delay on insert after Poll::Ready (#2285)Christofer Nolander
When the queue was polled and yielded an index from the wheel, the delay until the next item was never updated. As a result, when one item was yielded from `poll_idx` the following insert erronously updated the delay to the instant of the inserted item. Fixes: #1700
2020-03-23time: fix repeated pause/resume of time (#2253)Tudor Sidea
The resume function was breaking the guarantee that Instants should never be less than any previously measured Instants when created. Altered the pause and resume function such that they will not break this guarantee. After resume, the time should continue from where it left off. Created test to prove that the advanced function still works as expected. Added additional tests for the pause/advance/resume functions.
2020-03-16Add cooperative task yielding (#2160)Jon Gjengset
A single call to `poll` on a top-level task may potentially do a lot of work before it returns `Poll::Pending`. If a task runs for a long period of time without yielding back to the executor, it can starve other tasks waiting on that executor to execute them, or drive underlying resources. See for example rust-lang/futures-rs#2047, rust-lang/futures-rs#1957, and rust-lang/futures-rs#869. Since Rust does not have a runtime, it is difficult to forcibly preempt a long-running task. Consider a future like this one: ```rust use tokio::stream::StreamExt; async fn drop_all<I: Stream>(input: I) { while let Some(_) = input.next().await {} } ``` It may look harmless, but consider what happens under heavy load if the input stream is _always_ ready. If we spawn `drop_all`, the task will never yield, and will starve other tasks and resources on the same executor. This patch adds a `coop` module that provides an opt-in mechanism for futures to cooperate with the executor to avoid starvation. This alleviates the problem above: ``` use tokio::stream::StreamExt; async fn drop_all<I: Stream>(input: I) { while let Some(_) = input.next().await { tokio::coop::proceed().await; } } ``` The call to [`proceed`] will coordinate with the executor to make sure that every so often control is yielded back to the executor so it can run other tasks. The implementation uses a thread-local counter that simply counts how many "cooperation points" we have passed since the task was first polled. Once the "budget" has been spent, any subsequent points will return `Poll::Pending`, eventually making the top-level task yield. When it finally does yield, the executor resets the budget before running the next task. The budget per task poll is currently hard-coded to 128. Eventually, we may want to make it dynamic as more cooperation points are added. The number 128 was chosen more or less arbitrarily to balance the cost of yielding unnecessarily against the time an executor may be "held up". At the moment, all the tokio leaf futures ("resources") call into coop, but external futures have no way of doing so. We probably want to continue limiting coop points to leaf futures in the future, but may want to also enable third-party leaf futures to cooperate to benefit the ecosystem as a whole. This is reflected in the methods marked as `pub` in `mod coop` (even though the module is only `pub(crate)`). We will likely also eventually want to expose `coop::limit`, which enables sub-executors and manual `impl Future` blocks to avoid one sub-task spending all of their poll budget. Benchmarks (see tokio-rs/tokio#2160) suggest that the overhead of `coop` is marginal.
2020-02-26time: avoid needing to `poll` DelayQueue after insertion (#2217)Thomas Whiteway
If an entry is inserted in the queue before the next deadline, the DelayQueue needs to update the Delay tracking the next time to poll. If there is an existing Delay, reset that rather than replacing it as if it's already been polled the task will be waiting for a notification before it will poll again, and dropping the Delay means that that notification will never be performed.
2020-02-17Added Ord and Hash as derived traits for tokio::time::Instant (#2239)Tudor Sidea
The added derived traits mirror the traits of std::time::Instant. As tokio::time::Instant is just a wrapper around std::time::Instant, it should also derive all the traits std::time::Instant derives.
2020-02-01time: Add comment about cancelation of timed out futures (#2206)Daniel Müller
While the module documentation explains that a timed out future (as created through the tokio::time::timeout function) is canceled, the function's actual documentation doesn't mention that at all. This change adds this relevant information to the documentation.
2020-01-27timer: fix out of bounds error (#2184)Juan Alvarez