├── .github └── workflows │ └── build.yml ├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── renovate.json ├── src ├── channel.rs ├── lib.rs └── timer.rs └── test ├── mod.rs ├── test_poll_channel.rs └── test_timer.rs /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | branches: 8 | - main 9 | schedule: 10 | - cron: 0 3 1 * * 11 | workflow_dispatch: 12 | jobs: 13 | test: 14 | name: Test 15 | runs-on: ${{ matrix.os }} 16 | strategy: 17 | matrix: 18 | build: 19 | - x86_64 20 | - x86_64-beta 21 | - x86_64-nightly 22 | - i686 23 | - macos 24 | - win64 25 | - win32 26 | include: 27 | - build: x86_64 28 | os: ubuntu-latest 29 | rust: stable 30 | target: x86_64-unknown-linux-gnu 31 | - build: x86_64-beta 32 | os: ubuntu-latest 33 | rust: beta 34 | target: x86_64-unknown-linux-gnu 35 | - build: x86_64-nightly 36 | os: ubuntu-latest 37 | rust: nightly 38 | target: x86_64-unknown-linux-gnu 39 | - build: i686 40 | os: ubuntu-latest 41 | rust: stable 42 | target: i686-unknown-linux-gnu 43 | - build: macos 44 | os: macos-latest 45 | rust: stable 46 | target: x86_64-apple-darwin 47 | - build: win32 48 | os: windows-latest 49 | rust: stable 50 | target: i686-pc-windows-msvc 51 | - build: win64 52 | os: windows-latest 53 | rust: stable 54 | target: x86_64-pc-windows-msvc 55 | steps: 56 | - uses: actions/checkout@v4 57 | with: 58 | persist-credentials: false 59 | - name: Install Rust (rustup) 60 | run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }} 61 | shell: bash 62 | - run: rustup target add ${{ matrix.target }} 63 | - run: rustup component add clippy 64 | - run: cargo fmt -- --check 65 | if: matrix.build == 'x86_64' 66 | name: Check formatting 67 | - run: cargo clippy -- -D warnings 68 | name: Run clippy 69 | - run: cargo build 70 | name: Build 71 | - run: cargo test 72 | name: Run tests 73 | - run: | 74 | cargo update -Z minimal-versions 75 | cargo build 76 | if: matrix.rust == 'nightly' 77 | name: Check minimal versions 78 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 2.0.6 (7 Dec 2019) 2 | 3 | - fix license metadata in `Cargo.toml` (thanks @ignatenkobrain) 4 | 5 | ## 2.0.5 (18 Jun 2018) 6 | 7 | - update `lazycell` from 0.6 -> 1.0 8 | 9 | ## 2.0.4 (7 Apr 2018) 10 | 11 | - Bump mio dependency (fixes minimal-versions build) 12 | 13 | ## 2.0.3 (28 Dec 2017) 14 | 15 | - update `log` from 0.3 -> 0.4 16 | 17 | ## 2.0.2 18 | 19 | - More docs tidying. 20 | 21 | ## 2.0.1 22 | 23 | - Another try at documenting the timer interface. 24 | 25 | ## 2.0.0 26 | 27 | - Remove channel implementation details from the API. Specifically, the 28 | following are no longer public: 29 | - `ctl_pair()` 30 | - `SenderCtl` 31 | - `ReceiverCtl` 32 | - Document all APIs 33 | 34 | ## 1.0.0 35 | 36 | - Initial release. Essentially identical to 37 | [mio-more](https://github.com/carllerche/mio-more). 38 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mio-extras" 3 | version = "2.0.6" 4 | license = "MIT OR Apache-2.0" 5 | authors = ["Carl Lerche ", "David Hotham"] 6 | description = "Extra components for use with Mio" 7 | documentation = "https://docs.rs/mio-extras" 8 | repository = "https://github.com/dimbleby/mio-extras" 9 | readme = "README.md" 10 | keywords = ["io", "async", "non-blocking"] 11 | categories = ["asynchronous"] 12 | exclude = [".gitignore"] 13 | edition = "2024" 14 | 15 | [dependencies] 16 | mio = "0.6.14" 17 | log = "0.4" 18 | lazycell = "1" 19 | slab = "0.4" 20 | 21 | [[test]] 22 | name = "test" 23 | path = "test/mod.rs" 24 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 Mio authors 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Mio authors 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mio-extras 2 | 3 | Extra components for use with [Mio](https://github.com/tokio-rs/mio): 4 | 5 | - a channel that implements `Evented` 6 | - a timer that implements `Evented` 7 | 8 | [![Crates.io][crates-badge]][crates-url] 9 | [![Build Status][actions-badge]][actions-url] 10 | 11 | [crates-badge]: https://img.shields.io/crates/v/mio-extras.svg 12 | [crates-url]: https://crates.io/crates/mio-extras 13 | [actions-badge]: https://github.com/dimbleby/mio-extras/actions/workflows/build.yml/badge.svg 14 | [actions-url]: https://github.com/dimbleby/mio-extras/actions?query=workflow%3ACI+branch%3Amain 15 | 16 | [Documentation](https://docs.rs/mio-extras). 17 | 18 | ## History and maintenance 19 | 20 | This repository is forked from 21 | [`mio-more`](https://github.com/carllerche/mio-more), which is unmaintained. 22 | 23 | This library is not compatible with mio 0.7. 24 | However, there's a similar project named [`mio-misc`](https://github.com/onurzdg/mio-misc) that works with mio 0.7. 25 | 26 | I don't intend to do very much with this except for routine maintenance - bug 27 | fixes, updating dependencies, and suchlike. 28 | 29 | However if you have some code that you think belongs here, then by all means 30 | raise an issue or open a pull request. 31 | 32 | # License 33 | 34 | `mio-extras` is primarily distributed under the terms of both the MIT license 35 | and the Apache License (Version 2.0), with portions covered by various BSD-like 36 | licenses. 37 | 38 | See LICENSE-APACHE, and LICENSE-MIT for details. 39 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /src/channel.rs: -------------------------------------------------------------------------------- 1 | //! Thread safe communication channel implementing `Evented` 2 | use lazycell::{AtomicLazyCell, LazyCell}; 3 | use mio::{Evented, Poll, PollOpt, Ready, Registration, SetReadiness, Token}; 4 | use std::any::Any; 5 | use std::error; 6 | use std::sync::atomic::{AtomicUsize, Ordering}; 7 | use std::sync::{Arc, mpsc}; 8 | use std::{fmt, io}; 9 | 10 | /// Creates a new asynchronous channel, where the `Receiver` can be registered 11 | /// with `Poll`. 12 | pub fn channel() -> (Sender, Receiver) { 13 | let (tx_ctl, rx_ctl) = ctl_pair(); 14 | let (tx, rx) = mpsc::channel(); 15 | 16 | let tx = Sender { tx, ctl: tx_ctl }; 17 | 18 | let rx = Receiver { rx, ctl: rx_ctl }; 19 | 20 | (tx, rx) 21 | } 22 | 23 | /// Creates a new synchronous, bounded channel where the `Receiver` can be 24 | /// registered with `Poll`. 25 | pub fn sync_channel(bound: usize) -> (SyncSender, Receiver) { 26 | let (tx_ctl, rx_ctl) = ctl_pair(); 27 | let (tx, rx) = mpsc::sync_channel(bound); 28 | 29 | let tx = SyncSender { tx, ctl: tx_ctl }; 30 | 31 | let rx = Receiver { rx, ctl: rx_ctl }; 32 | 33 | (tx, rx) 34 | } 35 | 36 | fn ctl_pair() -> (SenderCtl, ReceiverCtl) { 37 | let inner = Arc::new(Inner { 38 | pending: AtomicUsize::new(0), 39 | senders: AtomicUsize::new(1), 40 | set_readiness: AtomicLazyCell::new(), 41 | }); 42 | 43 | let tx = SenderCtl { 44 | inner: Arc::clone(&inner), 45 | }; 46 | 47 | let rx = ReceiverCtl { 48 | registration: LazyCell::new(), 49 | inner, 50 | }; 51 | 52 | (tx, rx) 53 | } 54 | 55 | /// Tracks messages sent on a channel in order to update readiness. 56 | struct SenderCtl { 57 | inner: Arc, 58 | } 59 | 60 | /// Tracks messages received on a channel in order to track readiness. 61 | struct ReceiverCtl { 62 | registration: LazyCell, 63 | inner: Arc, 64 | } 65 | 66 | /// The sending half of a channel. 67 | pub struct Sender { 68 | tx: mpsc::Sender, 69 | ctl: SenderCtl, 70 | } 71 | 72 | /// The sending half of a synchronous channel. 73 | pub struct SyncSender { 74 | tx: mpsc::SyncSender, 75 | ctl: SenderCtl, 76 | } 77 | 78 | /// The receiving half of a channel. 79 | pub struct Receiver { 80 | rx: mpsc::Receiver, 81 | ctl: ReceiverCtl, 82 | } 83 | 84 | /// An error returned from the `Sender::send` or `SyncSender::send` function. 85 | pub enum SendError { 86 | /// An IO error. 87 | Io(io::Error), 88 | 89 | /// The receiving half of the channel has disconnected. 90 | Disconnected(T), 91 | } 92 | 93 | /// An error returned from the `SyncSender::try_send` function. 94 | pub enum TrySendError { 95 | /// An IO error. 96 | Io(io::Error), 97 | 98 | /// Data could not be sent because it would require the callee to block. 99 | Full(T), 100 | 101 | /// The receiving half of the channel has disconnected. 102 | Disconnected(T), 103 | } 104 | 105 | struct Inner { 106 | // The number of outstanding messages for the receiver to read 107 | pending: AtomicUsize, 108 | // The number of sender handles 109 | senders: AtomicUsize, 110 | // The set readiness handle 111 | set_readiness: AtomicLazyCell, 112 | } 113 | 114 | impl Sender { 115 | /// Attempts to send a value on this channel, returning it back if it could not be sent. 116 | pub fn send(&self, t: T) -> Result<(), SendError> { 117 | self.tx.send(t).map_err(SendError::from).and_then(|_| { 118 | self.ctl.inc()?; 119 | Ok(()) 120 | }) 121 | } 122 | } 123 | 124 | impl Clone for Sender { 125 | fn clone(&self) -> Sender { 126 | Sender { 127 | tx: self.tx.clone(), 128 | ctl: self.ctl.clone(), 129 | } 130 | } 131 | } 132 | 133 | impl SyncSender { 134 | /// Sends a value on this synchronous channel. 135 | /// 136 | /// This function will *block* until space in the internal buffer becomes 137 | /// available or a receiver is available to hand off the message to. 138 | pub fn send(&self, t: T) -> Result<(), SendError> { 139 | self.tx.send(t).map_err(From::from).and_then(|_| { 140 | self.ctl.inc()?; 141 | Ok(()) 142 | }) 143 | } 144 | 145 | /// Attempts to send a value on this channel without blocking. 146 | /// 147 | /// This method differs from `send` by returning immediately if the channel's 148 | /// buffer is full or no receiver is waiting to acquire some data. 149 | pub fn try_send(&self, t: T) -> Result<(), TrySendError> { 150 | self.tx.try_send(t).map_err(From::from).and_then(|_| { 151 | self.ctl.inc()?; 152 | Ok(()) 153 | }) 154 | } 155 | } 156 | 157 | impl Clone for SyncSender { 158 | fn clone(&self) -> SyncSender { 159 | SyncSender { 160 | tx: self.tx.clone(), 161 | ctl: self.ctl.clone(), 162 | } 163 | } 164 | } 165 | 166 | impl Receiver { 167 | /// Attempts to return a pending value on this receiver without blocking. 168 | pub fn try_recv(&self) -> Result { 169 | let result = self.rx.try_recv(); 170 | if result.is_ok() { 171 | let _ = self.ctl.dec(); 172 | } 173 | result 174 | } 175 | } 176 | 177 | impl Evented for Receiver { 178 | fn register( 179 | &self, 180 | poll: &Poll, 181 | token: Token, 182 | interest: Ready, 183 | opts: PollOpt, 184 | ) -> io::Result<()> { 185 | self.ctl.register(poll, token, interest, opts) 186 | } 187 | 188 | fn reregister( 189 | &self, 190 | poll: &Poll, 191 | token: Token, 192 | interest: Ready, 193 | opts: PollOpt, 194 | ) -> io::Result<()> { 195 | self.ctl.reregister(poll, token, interest, opts) 196 | } 197 | 198 | fn deregister(&self, poll: &Poll) -> io::Result<()> { 199 | self.ctl.deregister(poll) 200 | } 201 | } 202 | 203 | /* 204 | * 205 | * ===== SenderCtl / ReceiverCtl ===== 206 | * 207 | */ 208 | 209 | impl SenderCtl { 210 | /// Call to track that a message has been sent 211 | fn inc(&self) -> io::Result<()> { 212 | let cnt = self.inner.pending.fetch_add(1, Ordering::Acquire); 213 | 214 | if 0 == cnt { 215 | // Toggle readiness to readable 216 | if let Some(set_readiness) = self.inner.set_readiness.borrow() { 217 | set_readiness.set_readiness(Ready::readable())?; 218 | } 219 | } 220 | 221 | Ok(()) 222 | } 223 | } 224 | 225 | impl Clone for SenderCtl { 226 | fn clone(&self) -> SenderCtl { 227 | self.inner.senders.fetch_add(1, Ordering::Relaxed); 228 | SenderCtl { 229 | inner: Arc::clone(&self.inner), 230 | } 231 | } 232 | } 233 | 234 | impl Drop for SenderCtl { 235 | fn drop(&mut self) { 236 | if self.inner.senders.fetch_sub(1, Ordering::Release) == 1 { 237 | let _ = self.inc(); 238 | } 239 | } 240 | } 241 | 242 | impl ReceiverCtl { 243 | fn dec(&self) -> io::Result<()> { 244 | let first = self.inner.pending.load(Ordering::Acquire); 245 | 246 | if first == 1 { 247 | // Unset readiness 248 | if let Some(set_readiness) = self.inner.set_readiness.borrow() { 249 | set_readiness.set_readiness(Ready::empty())?; 250 | } 251 | } 252 | 253 | // Decrement 254 | let second = self.inner.pending.fetch_sub(1, Ordering::AcqRel); 255 | 256 | if first == 1 && second > 1 { 257 | // There are still pending messages. Since readiness was 258 | // previously unset, it must be reset here 259 | if let Some(set_readiness) = self.inner.set_readiness.borrow() { 260 | set_readiness.set_readiness(Ready::readable())?; 261 | } 262 | } 263 | 264 | Ok(()) 265 | } 266 | } 267 | 268 | impl Evented for ReceiverCtl { 269 | fn register( 270 | &self, 271 | poll: &Poll, 272 | token: Token, 273 | interest: Ready, 274 | opts: PollOpt, 275 | ) -> io::Result<()> { 276 | if self.registration.borrow().is_some() { 277 | return Err(io::Error::other("receiver already registered")); 278 | } 279 | 280 | let (registration, set_readiness) = Registration::new2(); 281 | poll.register(®istration, token, interest, opts)?; 282 | 283 | if self.inner.pending.load(Ordering::Relaxed) > 0 { 284 | // TODO: Don't drop readiness 285 | let _ = set_readiness.set_readiness(Ready::readable()); 286 | } 287 | 288 | self.registration 289 | .fill(registration) 290 | .expect("unexpected state encountered"); 291 | self.inner 292 | .set_readiness 293 | .fill(set_readiness) 294 | .expect("unexpected state encountered"); 295 | 296 | Ok(()) 297 | } 298 | 299 | fn reregister( 300 | &self, 301 | poll: &Poll, 302 | token: Token, 303 | interest: Ready, 304 | opts: PollOpt, 305 | ) -> io::Result<()> { 306 | match self.registration.borrow() { 307 | Some(registration) => poll.reregister(registration, token, interest, opts), 308 | None => Err(io::Error::other("receiver not registered")), 309 | } 310 | } 311 | 312 | fn deregister(&self, poll: &Poll) -> io::Result<()> { 313 | match self.registration.borrow() { 314 | Some(registration) => poll.deregister(registration), 315 | None => Err(io::Error::other("receiver not registered")), 316 | } 317 | } 318 | } 319 | 320 | /* 321 | * 322 | * ===== Error conversions ===== 323 | * 324 | */ 325 | 326 | impl From> for SendError { 327 | fn from(src: mpsc::SendError) -> SendError { 328 | SendError::Disconnected(src.0) 329 | } 330 | } 331 | 332 | impl From for SendError { 333 | fn from(src: io::Error) -> SendError { 334 | SendError::Io(src) 335 | } 336 | } 337 | 338 | impl From> for TrySendError { 339 | fn from(src: mpsc::TrySendError) -> TrySendError { 340 | match src { 341 | mpsc::TrySendError::Full(v) => TrySendError::Full(v), 342 | mpsc::TrySendError::Disconnected(v) => TrySendError::Disconnected(v), 343 | } 344 | } 345 | } 346 | 347 | impl From> for TrySendError { 348 | fn from(src: mpsc::SendError) -> TrySendError { 349 | TrySendError::Disconnected(src.0) 350 | } 351 | } 352 | 353 | impl From for TrySendError { 354 | fn from(src: io::Error) -> TrySendError { 355 | TrySendError::Io(src) 356 | } 357 | } 358 | 359 | /* 360 | * 361 | * ===== Implement Error, Debug and Display for Errors ===== 362 | * 363 | */ 364 | 365 | impl error::Error for SendError {} 366 | 367 | impl error::Error for TrySendError {} 368 | 369 | impl fmt::Debug for SendError { 370 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 371 | format_send_error(self, f) 372 | } 373 | } 374 | 375 | impl fmt::Display for SendError { 376 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 377 | format_send_error(self, f) 378 | } 379 | } 380 | 381 | impl fmt::Debug for TrySendError { 382 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 383 | format_try_send_error(self, f) 384 | } 385 | } 386 | 387 | impl fmt::Display for TrySendError { 388 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 389 | format_try_send_error(self, f) 390 | } 391 | } 392 | 393 | #[inline] 394 | fn format_send_error(e: &SendError, f: &mut fmt::Formatter) -> fmt::Result { 395 | match *e { 396 | SendError::Io(ref io_err) => write!(f, "{io_err}"), 397 | SendError::Disconnected(..) => write!(f, "Disconnected"), 398 | } 399 | } 400 | 401 | #[inline] 402 | fn format_try_send_error(e: &TrySendError, f: &mut fmt::Formatter) -> fmt::Result { 403 | match *e { 404 | TrySendError::Io(ref io_err) => write!(f, "{io_err}"), 405 | TrySendError::Full(..) => write!(f, "Full"), 406 | TrySendError::Disconnected(..) => write!(f, "Disconnected"), 407 | } 408 | } 409 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Extra components for use with Mio. 2 | #![deny(missing_docs)] 3 | 4 | #[macro_use] 5 | extern crate log; 6 | 7 | pub mod channel; 8 | pub mod timer; 9 | 10 | // Conversion utilities 11 | mod convert { 12 | use std::time::Duration; 13 | 14 | const NANOS_PER_MILLI: u32 = 1_000_000; 15 | const MILLIS_PER_SEC: u64 = 1_000; 16 | 17 | /// Convert a `Duration` to milliseconds, rounding up and saturating at 18 | /// `u64::MAX`. 19 | /// 20 | /// The saturating is fine because `u64::MAX` milliseconds are still many 21 | /// million years. 22 | pub fn millis(duration: Duration) -> u64 { 23 | // Round up. 24 | let millis = duration.subsec_nanos().div_ceil(NANOS_PER_MILLI); 25 | duration 26 | .as_secs() 27 | .saturating_mul(MILLIS_PER_SEC) 28 | .saturating_add(u64::from(millis)) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/timer.rs: -------------------------------------------------------------------------------- 1 | //! Timer optimized for I/O related operations 2 | use crate::convert; 3 | use lazycell::LazyCell; 4 | use mio::{Evented, Poll, PollOpt, Ready, Registration, SetReadiness, Token}; 5 | use slab::Slab; 6 | use std::sync::Arc; 7 | use std::sync::atomic::{AtomicUsize, Ordering}; 8 | use std::time::{Duration, Instant}; 9 | use std::{cmp, fmt, io, iter, thread}; 10 | 11 | /// A timer. 12 | /// 13 | /// Typical usage goes like this: 14 | /// 15 | /// * register the timer with a `mio::Poll`. 16 | /// * set a timeout, by calling `Timer::set_timeout`. Here you provide some 17 | /// state to be associated with this timeout. 18 | /// * poll the `Poll`, to learn when a timeout has occurred. 19 | /// * retrieve state associated with the timeout by calling `Timer::poll`. 20 | /// 21 | /// You can omit use of the `Poll` altogether, if you like, and just poll the 22 | /// `Timer` directly. 23 | pub struct Timer { 24 | // Size of each tick in milliseconds 25 | tick_ms: u64, 26 | // Slab of timeout entries 27 | entries: Slab>, 28 | // Timeout wheel. Each tick, the timer will look at the next slot for 29 | // timeouts that match the current tick. 30 | wheel: Vec, 31 | // Tick 0's time instant 32 | start: Instant, 33 | // The current tick 34 | tick: Tick, 35 | // The next entry to possibly timeout 36 | next: Token, 37 | // Masks the target tick to get the slot 38 | mask: u64, 39 | // Set on registration with Poll 40 | inner: LazyCell, 41 | } 42 | 43 | /// Used to create a `Timer`. 44 | pub struct Builder { 45 | // Approximate duration of each tick 46 | tick: Duration, 47 | // Number of slots in the timer wheel 48 | num_slots: usize, 49 | // Max number of timeouts that can be in flight at a given time. 50 | capacity: usize, 51 | } 52 | 53 | /// A timeout, as returned by `Timer::set_timeout`. 54 | /// 55 | /// Use this as the argument to `Timer::cancel_timeout`, to cancel this timeout. 56 | #[derive(Clone, Debug)] 57 | pub struct Timeout { 58 | // Reference into the timer entry slab 59 | token: Token, 60 | // Tick that it should match up with 61 | tick: u64, 62 | } 63 | 64 | struct Inner { 65 | registration: Registration, 66 | set_readiness: SetReadiness, 67 | wakeup_state: WakeupState, 68 | wakeup_thread: thread::JoinHandle<()>, 69 | } 70 | 71 | impl Drop for Inner { 72 | fn drop(&mut self) { 73 | // 1. Set wakeup state to TERMINATE_THREAD 74 | self.wakeup_state.store(TERMINATE_THREAD, Ordering::Release); 75 | // 2. Wake him up 76 | self.wakeup_thread.thread().unpark(); 77 | } 78 | } 79 | 80 | #[derive(Copy, Clone, Debug)] 81 | struct WheelEntry { 82 | next_tick: Tick, 83 | head: Token, 84 | } 85 | 86 | // Doubly linked list of timer entries. Allows for efficient insertion / 87 | // removal of timeouts. 88 | struct Entry { 89 | state: T, 90 | links: EntryLinks, 91 | } 92 | 93 | #[derive(Copy, Clone)] 94 | struct EntryLinks { 95 | tick: Tick, 96 | prev: Token, 97 | next: Token, 98 | } 99 | 100 | type Tick = u64; 101 | 102 | const TICK_MAX: Tick = u64::MAX; 103 | 104 | // Manages communication with wakeup thread 105 | type WakeupState = Arc; 106 | 107 | const TERMINATE_THREAD: usize = 0; 108 | const EMPTY: Token = Token(usize::MAX); 109 | 110 | impl Builder { 111 | /// Set the tick duration. Default is 100ms. 112 | pub fn tick_duration(mut self, duration: Duration) -> Builder { 113 | self.tick = duration; 114 | self 115 | } 116 | 117 | /// Set the number of slots. Default is 256. 118 | pub fn num_slots(mut self, num_slots: usize) -> Builder { 119 | self.num_slots = num_slots; 120 | self 121 | } 122 | 123 | /// Set the capacity. Default is 65536. 124 | pub fn capacity(mut self, capacity: usize) -> Builder { 125 | self.capacity = capacity; 126 | self 127 | } 128 | 129 | /// Build a `Timer` with the parameters set on this `Builder`. 130 | pub fn build(self) -> Timer { 131 | Timer::new( 132 | convert::millis(self.tick), 133 | self.num_slots, 134 | self.capacity, 135 | Instant::now(), 136 | ) 137 | } 138 | } 139 | 140 | impl Default for Builder { 141 | fn default() -> Builder { 142 | Builder { 143 | tick: Duration::from_millis(100), 144 | num_slots: 1 << 8, 145 | capacity: 1 << 16, 146 | } 147 | } 148 | } 149 | 150 | impl Timer { 151 | fn new(tick_ms: u64, num_slots: usize, capacity: usize, start: Instant) -> Timer { 152 | let num_slots = num_slots.next_power_of_two(); 153 | let capacity = capacity.next_power_of_two(); 154 | let mask = (num_slots as u64) - 1; 155 | let wheel = iter::repeat_n( 156 | WheelEntry { 157 | next_tick: TICK_MAX, 158 | head: EMPTY, 159 | }, 160 | num_slots, 161 | ) 162 | .collect(); 163 | 164 | Timer { 165 | tick_ms, 166 | entries: Slab::with_capacity(capacity), 167 | wheel, 168 | start, 169 | tick: 0, 170 | next: EMPTY, 171 | mask, 172 | inner: LazyCell::new(), 173 | } 174 | } 175 | 176 | /// Set a timeout. 177 | /// 178 | /// When the timeout occurs, the given state becomes available via `poll`. 179 | pub fn set_timeout(&mut self, delay_from_now: Duration, state: T) -> Timeout { 180 | let delay_from_start = self.start.elapsed() + delay_from_now; 181 | self.set_timeout_at(delay_from_start, state) 182 | } 183 | 184 | fn set_timeout_at(&mut self, delay_from_start: Duration, state: T) -> Timeout { 185 | let mut tick = duration_to_tick(delay_from_start, self.tick_ms); 186 | trace!( 187 | "setting timeout; delay={:?}; tick={:?}; current-tick={:?}", 188 | delay_from_start, tick, self.tick 189 | ); 190 | 191 | // Always target at least 1 tick in the future 192 | if tick <= self.tick { 193 | tick = self.tick + 1; 194 | } 195 | 196 | self.insert(tick, state) 197 | } 198 | 199 | fn insert(&mut self, tick: Tick, state: T) -> Timeout { 200 | // Get the slot for the requested tick 201 | let slot = (tick & self.mask) as usize; 202 | let curr = self.wheel[slot]; 203 | 204 | // Insert the new entry 205 | let entry = Entry::new(state, tick, curr.head); 206 | let token = Token(self.entries.insert(entry)); 207 | 208 | if curr.head != EMPTY { 209 | // If there was a previous entry, set its prev pointer to the new 210 | // entry 211 | self.entries[curr.head.into()].links.prev = token; 212 | } 213 | 214 | // Update the head slot 215 | self.wheel[slot] = WheelEntry { 216 | next_tick: cmp::min(tick, curr.next_tick), 217 | head: token, 218 | }; 219 | 220 | self.schedule_readiness(tick); 221 | 222 | trace!("inserted timout; slot={slot}; token={token:?}"); 223 | 224 | // Return the new timeout 225 | Timeout { token, tick } 226 | } 227 | 228 | /// Cancel a timeout. 229 | /// 230 | /// If the timeout has not yet occurred, the return value holds the 231 | /// associated state. 232 | pub fn cancel_timeout(&mut self, timeout: &Timeout) -> Option { 233 | let links = match self.entries.get(timeout.token.into()) { 234 | Some(e) => e.links, 235 | None => return None, 236 | }; 237 | 238 | // Sanity check 239 | if links.tick != timeout.tick { 240 | return None; 241 | } 242 | 243 | self.unlink(&links, timeout.token); 244 | Some(self.entries.remove(timeout.token.into()).state) 245 | } 246 | 247 | /// Poll for an expired timer. 248 | /// 249 | /// The return value holds the state associated with the first expired 250 | /// timer, if any. 251 | pub fn poll(&mut self) -> Option { 252 | let target_tick = current_tick(self.start, self.tick_ms); 253 | self.poll_to(target_tick) 254 | } 255 | 256 | fn poll_to(&mut self, mut target_tick: Tick) -> Option { 257 | trace!( 258 | "tick_to; target_tick={}; current_tick={}", 259 | target_tick, self.tick 260 | ); 261 | 262 | if target_tick < self.tick { 263 | target_tick = self.tick; 264 | } 265 | 266 | while self.tick <= target_tick { 267 | let curr = self.next; 268 | 269 | trace!("ticking; curr={curr:?}"); 270 | 271 | if curr == EMPTY { 272 | self.tick += 1; 273 | 274 | let slot = self.slot_for(self.tick); 275 | self.next = self.wheel[slot].head; 276 | 277 | // Handle the case when a slot has a single timeout which gets 278 | // canceled before the timeout expires. In this case, the 279 | // slot's head is EMPTY but there is a value for next_tick. Not 280 | // resetting next_tick here causes the timer to get stuck in a 281 | // loop. 282 | if self.next == EMPTY { 283 | self.wheel[slot].next_tick = TICK_MAX; 284 | } 285 | } else { 286 | let slot = self.slot_for(self.tick); 287 | 288 | if curr == self.wheel[slot].head { 289 | self.wheel[slot].next_tick = TICK_MAX; 290 | } 291 | 292 | let links = self.entries[curr.into()].links; 293 | 294 | if links.tick <= self.tick { 295 | trace!("triggering; token={curr:?}"); 296 | 297 | // Unlink will also advance self.next 298 | self.unlink(&links, curr); 299 | 300 | // Remove and return the token 301 | return Some(self.entries.remove(curr.into()).state); 302 | } else { 303 | let next_tick = self.wheel[slot].next_tick; 304 | self.wheel[slot].next_tick = cmp::min(next_tick, links.tick); 305 | self.next = links.next; 306 | } 307 | } 308 | } 309 | 310 | // No more timeouts to poll 311 | if let Some(inner) = self.inner.borrow() { 312 | trace!("unsetting readiness"); 313 | let _ = inner.set_readiness.set_readiness(Ready::empty()); 314 | 315 | if let Some(tick) = self.next_tick() { 316 | self.schedule_readiness(tick); 317 | } 318 | } 319 | 320 | None 321 | } 322 | 323 | fn unlink(&mut self, links: &EntryLinks, token: Token) { 324 | trace!( 325 | "unlinking timeout; slot={}; token={:?}", 326 | self.slot_for(links.tick), 327 | token 328 | ); 329 | 330 | if links.prev == EMPTY { 331 | let slot = self.slot_for(links.tick); 332 | self.wheel[slot].head = links.next; 333 | } else { 334 | self.entries[links.prev.into()].links.next = links.next; 335 | } 336 | 337 | if links.next != EMPTY { 338 | self.entries[links.next.into()].links.prev = links.prev; 339 | 340 | if token == self.next { 341 | self.next = links.next; 342 | } 343 | } else if token == self.next { 344 | self.next = EMPTY; 345 | } 346 | } 347 | 348 | fn schedule_readiness(&self, tick: Tick) { 349 | if let Some(inner) = self.inner.borrow() { 350 | // Coordinate setting readiness w/ the wakeup thread 351 | let mut curr = inner.wakeup_state.load(Ordering::Acquire); 352 | 353 | loop { 354 | if curr as Tick <= tick { 355 | // Nothing to do, wakeup is already scheduled 356 | return; 357 | } 358 | 359 | // Attempt to move the wakeup time forward 360 | trace!("advancing the wakeup time; target={tick}; curr={curr}"); 361 | match inner.wakeup_state.compare_exchange_weak( 362 | curr, 363 | tick as usize, 364 | Ordering::Release, 365 | Ordering::Relaxed, 366 | ) { 367 | Ok(_) => { 368 | // Signal to the wakeup thread that the wakeup time has been changed. 369 | trace!("unparking wakeup thread"); 370 | inner.wakeup_thread.thread().unpark(); 371 | return; 372 | } 373 | Err(actual) => curr = actual, 374 | } 375 | } 376 | } 377 | } 378 | 379 | // Next tick containing a timeout 380 | fn next_tick(&self) -> Option { 381 | if self.next != EMPTY { 382 | let slot = self.slot_for(self.entries[self.next.into()].links.tick); 383 | 384 | if self.wheel[slot].next_tick == self.tick { 385 | // There is data ready right now 386 | return Some(self.tick); 387 | } 388 | } 389 | 390 | self.wheel.iter().map(|e| e.next_tick).min() 391 | } 392 | 393 | fn slot_for(&self, tick: Tick) -> usize { 394 | (self.mask & tick) as usize 395 | } 396 | } 397 | 398 | impl Default for Timer { 399 | fn default() -> Timer { 400 | Builder::default().build() 401 | } 402 | } 403 | 404 | impl Evented for Timer { 405 | fn register( 406 | &self, 407 | poll: &Poll, 408 | token: Token, 409 | interest: Ready, 410 | opts: PollOpt, 411 | ) -> io::Result<()> { 412 | if self.inner.borrow().is_some() { 413 | return Err(io::Error::other("timer already registered")); 414 | } 415 | 416 | let (registration, set_readiness) = Registration::new2(); 417 | poll.register(®istration, token, interest, opts)?; 418 | let wakeup_state = Arc::new(AtomicUsize::new(usize::MAX)); 419 | let thread_handle = spawn_wakeup_thread( 420 | Arc::clone(&wakeup_state), 421 | set_readiness.clone(), 422 | self.start, 423 | self.tick_ms, 424 | ); 425 | 426 | self.inner 427 | .fill(Inner { 428 | registration, 429 | set_readiness, 430 | wakeup_state, 431 | wakeup_thread: thread_handle, 432 | }) 433 | .expect("timer already registered"); 434 | 435 | if let Some(next_tick) = self.next_tick() { 436 | self.schedule_readiness(next_tick); 437 | } 438 | 439 | Ok(()) 440 | } 441 | 442 | fn reregister( 443 | &self, 444 | poll: &Poll, 445 | token: Token, 446 | interest: Ready, 447 | opts: PollOpt, 448 | ) -> io::Result<()> { 449 | match self.inner.borrow() { 450 | Some(inner) => poll.reregister(&inner.registration, token, interest, opts), 451 | None => Err(io::Error::other("receiver not registered")), 452 | } 453 | } 454 | 455 | fn deregister(&self, poll: &Poll) -> io::Result<()> { 456 | match self.inner.borrow() { 457 | Some(inner) => poll.deregister(&inner.registration), 458 | None => Err(io::Error::other("receiver not registered")), 459 | } 460 | } 461 | } 462 | 463 | impl fmt::Debug for Inner { 464 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 465 | fmt.debug_struct("Inner") 466 | .field("registration", &self.registration) 467 | .field("wakeup_state", &self.wakeup_state.load(Ordering::Relaxed)) 468 | .finish() 469 | } 470 | } 471 | 472 | fn spawn_wakeup_thread( 473 | state: WakeupState, 474 | set_readiness: SetReadiness, 475 | start: Instant, 476 | tick_ms: u64, 477 | ) -> thread::JoinHandle<()> { 478 | thread::Builder::new() 479 | .name(format!( 480 | "mio_extras::timer : {}", 481 | thread::current().name().unwrap_or("no name") 482 | )) 483 | .spawn(move || { 484 | let mut sleep_until_tick = state.load(Ordering::Acquire) as Tick; 485 | 486 | loop { 487 | if sleep_until_tick == TERMINATE_THREAD as Tick { 488 | return; 489 | } 490 | 491 | let now_tick = current_tick(start, tick_ms); 492 | 493 | trace!( "wakeup thread: sleep_until_tick={sleep_until_tick:?}; now_tick={now_tick:?}"); 494 | 495 | if now_tick < sleep_until_tick { 496 | // Calling park_timeout with u64::MAX leads to undefined 497 | // behavior in pthread, causing the park to return immediately 498 | // and causing the thread to tightly spin. Instead of u64::MAX 499 | // on large values, simply use a blocking park. 500 | match tick_ms.checked_mul(sleep_until_tick - now_tick) { 501 | Some(sleep_duration) => { 502 | trace!( 503 | "sleeping; tick_ms={tick_ms}; now_tick={now_tick}; sleep_until_tick={sleep_until_tick}; duration={sleep_duration:?}" 504 | ); 505 | thread::park_timeout(Duration::from_millis(sleep_duration)); 506 | } 507 | None => { 508 | trace!( 509 | "sleeping; tick_ms={tick_ms}; now_tick={now_tick}; blocking sleep" 510 | ); 511 | thread::park(); 512 | } 513 | } 514 | sleep_until_tick = state.load(Ordering::Acquire) as Tick; 515 | } else { 516 | match state.compare_exchange_weak( 517 | sleep_until_tick as usize, 518 | usize::MAX, 519 | Ordering::AcqRel, 520 | Ordering::Acquire, 521 | ) { 522 | Ok(_) => { 523 | trace!("setting readiness from wakeup thread"); 524 | let _ = set_readiness.set_readiness(Ready::readable()); 525 | sleep_until_tick = usize::MAX as Tick; 526 | } 527 | Err(actual) => sleep_until_tick = actual as Tick, 528 | } 529 | } 530 | } 531 | }) 532 | .unwrap() 533 | } 534 | 535 | fn duration_to_tick(elapsed: Duration, tick_ms: u64) -> Tick { 536 | // Calculate tick rounding up to the closest one 537 | let elapsed_ms = convert::millis(elapsed); 538 | elapsed_ms.saturating_add(tick_ms / 2) / tick_ms 539 | } 540 | 541 | fn current_tick(start: Instant, tick_ms: u64) -> Tick { 542 | duration_to_tick(start.elapsed(), tick_ms) 543 | } 544 | 545 | impl Entry { 546 | fn new(state: T, tick: u64, next: Token) -> Entry { 547 | Entry { 548 | state, 549 | links: EntryLinks { 550 | tick, 551 | prev: EMPTY, 552 | next, 553 | }, 554 | } 555 | } 556 | } 557 | 558 | #[cfg(test)] 559 | mod test { 560 | use super::*; 561 | use std::time::{Duration, Instant}; 562 | 563 | #[test] 564 | pub fn test_timeout_next_tick() { 565 | let mut t = timer(); 566 | let mut tick; 567 | 568 | t.set_timeout_at(Duration::from_millis(100), "a"); 569 | 570 | tick = ms_to_tick(&t, 50); 571 | assert_eq!(None, t.poll_to(tick)); 572 | 573 | tick = ms_to_tick(&t, 100); 574 | assert_eq!(Some("a"), t.poll_to(tick)); 575 | assert_eq!(None, t.poll_to(tick)); 576 | 577 | tick = ms_to_tick(&t, 150); 578 | assert_eq!(None, t.poll_to(tick)); 579 | 580 | tick = ms_to_tick(&t, 200); 581 | assert_eq!(None, t.poll_to(tick)); 582 | 583 | assert_eq!(count(&t), 0); 584 | } 585 | 586 | #[test] 587 | pub fn test_clearing_timeout() { 588 | let mut t = timer(); 589 | let mut tick; 590 | 591 | let to = t.set_timeout_at(Duration::from_millis(100), "a"); 592 | assert_eq!("a", t.cancel_timeout(&to).unwrap()); 593 | 594 | tick = ms_to_tick(&t, 100); 595 | assert_eq!(None, t.poll_to(tick)); 596 | 597 | tick = ms_to_tick(&t, 200); 598 | assert_eq!(None, t.poll_to(tick)); 599 | 600 | assert_eq!(count(&t), 0); 601 | } 602 | 603 | #[test] 604 | pub fn test_multiple_timeouts_same_tick() { 605 | let mut t = timer(); 606 | let mut tick; 607 | 608 | t.set_timeout_at(Duration::from_millis(100), "a"); 609 | t.set_timeout_at(Duration::from_millis(100), "b"); 610 | 611 | let mut rcv = vec![]; 612 | 613 | tick = ms_to_tick(&t, 100); 614 | rcv.push(t.poll_to(tick).unwrap()); 615 | rcv.push(t.poll_to(tick).unwrap()); 616 | 617 | assert_eq!(None, t.poll_to(tick)); 618 | 619 | rcv.sort_unstable(); 620 | assert!(rcv == ["a", "b"], "actual={:?}", rcv); 621 | 622 | tick = ms_to_tick(&t, 200); 623 | assert_eq!(None, t.poll_to(tick)); 624 | 625 | assert_eq!(count(&t), 0); 626 | } 627 | 628 | #[test] 629 | pub fn test_multiple_timeouts_diff_tick() { 630 | let mut t = timer(); 631 | let mut tick; 632 | 633 | t.set_timeout_at(Duration::from_millis(110), "a"); 634 | t.set_timeout_at(Duration::from_millis(220), "b"); 635 | t.set_timeout_at(Duration::from_millis(230), "c"); 636 | t.set_timeout_at(Duration::from_millis(440), "d"); 637 | t.set_timeout_at(Duration::from_millis(560), "e"); 638 | 639 | tick = ms_to_tick(&t, 100); 640 | assert_eq!(Some("a"), t.poll_to(tick)); 641 | assert_eq!(None, t.poll_to(tick)); 642 | 643 | tick = ms_to_tick(&t, 200); 644 | assert_eq!(Some("c"), t.poll_to(tick)); 645 | assert_eq!(Some("b"), t.poll_to(tick)); 646 | assert_eq!(None, t.poll_to(tick)); 647 | 648 | tick = ms_to_tick(&t, 300); 649 | assert_eq!(None, t.poll_to(tick)); 650 | 651 | tick = ms_to_tick(&t, 400); 652 | assert_eq!(Some("d"), t.poll_to(tick)); 653 | assert_eq!(None, t.poll_to(tick)); 654 | 655 | tick = ms_to_tick(&t, 500); 656 | assert_eq!(None, t.poll_to(tick)); 657 | 658 | tick = ms_to_tick(&t, 600); 659 | assert_eq!(Some("e"), t.poll_to(tick)); 660 | assert_eq!(None, t.poll_to(tick)); 661 | } 662 | 663 | #[test] 664 | pub fn test_catching_up() { 665 | let mut t = timer(); 666 | 667 | t.set_timeout_at(Duration::from_millis(110), "a"); 668 | t.set_timeout_at(Duration::from_millis(220), "b"); 669 | t.set_timeout_at(Duration::from_millis(230), "c"); 670 | t.set_timeout_at(Duration::from_millis(440), "d"); 671 | 672 | let tick = ms_to_tick(&t, 600); 673 | assert_eq!(Some("a"), t.poll_to(tick)); 674 | assert_eq!(Some("c"), t.poll_to(tick)); 675 | assert_eq!(Some("b"), t.poll_to(tick)); 676 | assert_eq!(Some("d"), t.poll_to(tick)); 677 | assert_eq!(None, t.poll_to(tick)); 678 | } 679 | 680 | #[test] 681 | pub fn test_timeout_hash_collision() { 682 | let mut t = timer(); 683 | let mut tick; 684 | 685 | t.set_timeout_at(Duration::from_millis(100), "a"); 686 | t.set_timeout_at(Duration::from_millis(100 + TICK * SLOTS as u64), "b"); 687 | 688 | tick = ms_to_tick(&t, 100); 689 | assert_eq!(Some("a"), t.poll_to(tick)); 690 | assert_eq!(1, count(&t)); 691 | 692 | tick = ms_to_tick(&t, 200); 693 | assert_eq!(None, t.poll_to(tick)); 694 | assert_eq!(1, count(&t)); 695 | 696 | tick = ms_to_tick(&t, 100 + TICK * SLOTS as u64); 697 | assert_eq!(Some("b"), t.poll_to(tick)); 698 | assert_eq!(0, count(&t)); 699 | } 700 | 701 | #[test] 702 | pub fn test_clearing_timeout_between_triggers() { 703 | let mut t = timer(); 704 | let mut tick; 705 | 706 | let a = t.set_timeout_at(Duration::from_millis(100), "a"); 707 | let _ = t.set_timeout_at(Duration::from_millis(100), "b"); 708 | let _ = t.set_timeout_at(Duration::from_millis(200), "c"); 709 | 710 | tick = ms_to_tick(&t, 100); 711 | assert_eq!(Some("b"), t.poll_to(tick)); 712 | assert_eq!(2, count(&t)); 713 | 714 | t.cancel_timeout(&a); 715 | assert_eq!(1, count(&t)); 716 | 717 | assert_eq!(None, t.poll_to(tick)); 718 | 719 | tick = ms_to_tick(&t, 200); 720 | assert_eq!(Some("c"), t.poll_to(tick)); 721 | assert_eq!(0, count(&t)); 722 | } 723 | 724 | const TICK: u64 = 100; 725 | const SLOTS: usize = 16; 726 | const CAPACITY: usize = 32; 727 | 728 | fn count(timer: &Timer) -> usize { 729 | timer.entries.len() 730 | } 731 | 732 | fn timer() -> Timer<&'static str> { 733 | Timer::new(TICK, SLOTS, CAPACITY, Instant::now()) 734 | } 735 | 736 | fn ms_to_tick(timer: &Timer, ms: u64) -> u64 { 737 | ms / timer.tick_ms 738 | } 739 | } 740 | -------------------------------------------------------------------------------- /test/mod.rs: -------------------------------------------------------------------------------- 1 | use mio::event::Event; 2 | use mio::{Events, Poll}; 3 | use std::time::Duration; 4 | 5 | mod test_poll_channel; 6 | mod test_timer; 7 | 8 | pub fn expect_events( 9 | poll: &Poll, 10 | event_buffer: &mut Events, 11 | poll_try_count: usize, 12 | mut expected: Vec, 13 | ) { 14 | const MS: u64 = 1_000; 15 | 16 | for _ in 0..poll_try_count { 17 | poll.poll(event_buffer, Some(Duration::from_millis(MS))) 18 | .unwrap(); 19 | for event in event_buffer.iter() { 20 | let pos_opt = expected.iter().position(|exp_event| { 21 | (event.token() == exp_event.token()) 22 | && event.readiness().contains(exp_event.readiness()) 23 | }); 24 | 25 | if let Some(pos) = pos_opt { 26 | expected.remove(pos); 27 | } 28 | } 29 | 30 | if expected.is_empty() { 31 | break; 32 | } 33 | } 34 | 35 | assert!( 36 | expected.is_empty(), 37 | "The following expected events were not found: {:?}", 38 | expected 39 | ); 40 | } 41 | -------------------------------------------------------------------------------- /test/test_poll_channel.rs: -------------------------------------------------------------------------------- 1 | use crate::expect_events; 2 | use mio::event::Event; 3 | use mio::{Events, Poll, PollOpt, Ready, Token}; 4 | use mio_extras::channel; 5 | use std::sync::mpsc::TryRecvError; 6 | use std::thread; 7 | use std::time::Duration; 8 | 9 | #[test] 10 | pub fn test_poll_channel_edge() { 11 | let poll = Poll::new().unwrap(); 12 | let mut events = Events::with_capacity(1024); 13 | let (tx, rx) = channel::channel(); 14 | 15 | poll.register(&rx, Token(123), Ready::readable(), PollOpt::edge()) 16 | .unwrap(); 17 | 18 | // Wait, but nothing should happen 19 | let num = poll 20 | .poll(&mut events, Some(Duration::from_millis(300))) 21 | .unwrap(); 22 | assert_eq!(0, num); 23 | 24 | // Push the value 25 | tx.send("hello").unwrap(); 26 | 27 | // Polling will contain the event 28 | let num = poll 29 | .poll(&mut events, Some(Duration::from_millis(300))) 30 | .unwrap(); 31 | assert_eq!(1, num); 32 | 33 | let event = events.iter().next().unwrap(); 34 | assert_eq!(event.token(), Token(123)); 35 | assert_eq!(event.readiness(), Ready::readable()); 36 | 37 | // Poll again and there should be no events 38 | let num = poll 39 | .poll(&mut events, Some(Duration::from_millis(300))) 40 | .unwrap(); 41 | assert_eq!(0, num); 42 | 43 | // Read the value 44 | assert_eq!("hello", rx.try_recv().unwrap()); 45 | 46 | // Poll again, nothing 47 | let num = poll 48 | .poll(&mut events, Some(Duration::from_millis(300))) 49 | .unwrap(); 50 | assert_eq!(0, num); 51 | 52 | // Push a value 53 | tx.send("goodbye").unwrap(); 54 | 55 | // Have an event 56 | let num = poll 57 | .poll(&mut events, Some(Duration::from_millis(300))) 58 | .unwrap(); 59 | assert_eq!(1, num); 60 | 61 | let event = events.iter().next().unwrap(); 62 | assert_eq!(event.token(), Token(123)); 63 | assert_eq!(event.readiness(), Ready::readable()); 64 | 65 | // Read the value 66 | rx.try_recv().unwrap(); 67 | 68 | // Drop the sender half 69 | drop(tx); 70 | 71 | let num = poll 72 | .poll(&mut events, Some(Duration::from_millis(300))) 73 | .unwrap(); 74 | assert_eq!(1, num); 75 | 76 | let event = events.iter().next().unwrap(); 77 | assert_eq!(event.token(), Token(123)); 78 | assert_eq!(event.readiness(), Ready::readable()); 79 | 80 | match rx.try_recv() { 81 | Err(TryRecvError::Disconnected) => {} 82 | no => panic!("unexpected value {:?}", no), 83 | } 84 | } 85 | 86 | #[test] 87 | pub fn test_poll_channel_oneshot() { 88 | let poll = Poll::new().unwrap(); 89 | let mut events = Events::with_capacity(1024); 90 | let (tx, rx) = channel::channel(); 91 | 92 | poll.register( 93 | &rx, 94 | Token(123), 95 | Ready::readable(), 96 | PollOpt::edge() | PollOpt::oneshot(), 97 | ) 98 | .unwrap(); 99 | 100 | // Wait, but nothing should happen 101 | let num = poll 102 | .poll(&mut events, Some(Duration::from_millis(300))) 103 | .unwrap(); 104 | assert_eq!(0, num); 105 | 106 | // Push the value 107 | tx.send("hello").unwrap(); 108 | 109 | // Polling will contain the event 110 | let num = poll 111 | .poll(&mut events, Some(Duration::from_millis(300))) 112 | .unwrap(); 113 | assert_eq!(1, num); 114 | 115 | let event = events.iter().next().unwrap(); 116 | assert_eq!(event.token(), Token(123)); 117 | assert_eq!(event.readiness(), Ready::readable()); 118 | 119 | // Poll again and there should be no events 120 | let num = poll 121 | .poll(&mut events, Some(Duration::from_millis(300))) 122 | .unwrap(); 123 | assert_eq!(0, num); 124 | 125 | // Read the value 126 | assert_eq!("hello", rx.try_recv().unwrap()); 127 | 128 | // Poll again, nothing 129 | let num = poll 130 | .poll(&mut events, Some(Duration::from_millis(300))) 131 | .unwrap(); 132 | assert_eq!(0, num); 133 | 134 | // Push a value 135 | tx.send("goodbye").unwrap(); 136 | 137 | // Poll again, nothing 138 | let num = poll 139 | .poll(&mut events, Some(Duration::from_millis(300))) 140 | .unwrap(); 141 | assert_eq!(0, num); 142 | 143 | // Reregistering will re-trigger the notification 144 | for _ in 0..3 { 145 | poll.reregister( 146 | &rx, 147 | Token(123), 148 | Ready::readable(), 149 | PollOpt::edge() | PollOpt::oneshot(), 150 | ) 151 | .unwrap(); 152 | 153 | // Have an event 154 | let num = poll 155 | .poll(&mut events, Some(Duration::from_millis(300))) 156 | .unwrap(); 157 | assert_eq!(1, num); 158 | 159 | let event = events.iter().next().unwrap(); 160 | assert_eq!(event.token(), Token(123)); 161 | assert_eq!(event.readiness(), Ready::readable()); 162 | } 163 | 164 | // Get the value 165 | assert_eq!("goodbye", rx.try_recv().unwrap()); 166 | 167 | poll.reregister( 168 | &rx, 169 | Token(123), 170 | Ready::readable(), 171 | PollOpt::edge() | PollOpt::oneshot(), 172 | ) 173 | .unwrap(); 174 | 175 | // Have an event 176 | let num = poll 177 | .poll(&mut events, Some(Duration::from_millis(300))) 178 | .unwrap(); 179 | assert_eq!(0, num); 180 | 181 | poll.reregister( 182 | &rx, 183 | Token(123), 184 | Ready::readable(), 185 | PollOpt::edge() | PollOpt::oneshot(), 186 | ) 187 | .unwrap(); 188 | 189 | // Have an event 190 | let num = poll 191 | .poll(&mut events, Some(Duration::from_millis(300))) 192 | .unwrap(); 193 | assert_eq!(0, num); 194 | } 195 | 196 | #[test] 197 | pub fn test_poll_channel_level() { 198 | let poll = Poll::new().unwrap(); 199 | let mut events = Events::with_capacity(1024); 200 | let (tx, rx) = channel::channel(); 201 | 202 | poll.register(&rx, Token(123), Ready::readable(), PollOpt::level()) 203 | .unwrap(); 204 | 205 | // Wait, but nothing should happen 206 | let num = poll 207 | .poll(&mut events, Some(Duration::from_millis(300))) 208 | .unwrap(); 209 | assert_eq!(0, num); 210 | 211 | // Push the value 212 | tx.send("hello").unwrap(); 213 | 214 | // Polling will contain the event 215 | for i in 0..5 { 216 | let num = poll 217 | .poll(&mut events, Some(Duration::from_millis(300))) 218 | .unwrap(); 219 | assert!(1 == num, "actually got {} on iteration {}", num, i); 220 | 221 | let event = events.iter().next().unwrap(); 222 | assert_eq!(event.token(), Token(123)); 223 | assert_eq!(event.readiness(), Ready::readable()); 224 | } 225 | 226 | // Read the value 227 | assert_eq!("hello", rx.try_recv().unwrap()); 228 | 229 | // Wait, but nothing should happen 230 | let num = poll 231 | .poll(&mut events, Some(Duration::from_millis(300))) 232 | .unwrap(); 233 | assert_eq!(0, num); 234 | } 235 | 236 | #[test] 237 | pub fn test_poll_channel_writable() { 238 | let poll = Poll::new().unwrap(); 239 | let mut events = Events::with_capacity(1024); 240 | let (tx, rx) = channel::channel(); 241 | 242 | poll.register(&rx, Token(123), Ready::writable(), PollOpt::edge()) 243 | .unwrap(); 244 | 245 | // Wait, but nothing should happen 246 | let num = poll 247 | .poll(&mut events, Some(Duration::from_millis(300))) 248 | .unwrap(); 249 | assert_eq!(0, num); 250 | 251 | // Push the value 252 | tx.send("hello").unwrap(); 253 | 254 | // Wait, but nothing should happen 255 | let num = poll 256 | .poll(&mut events, Some(Duration::from_millis(300))) 257 | .unwrap(); 258 | assert_eq!(0, num); 259 | } 260 | 261 | #[test] 262 | pub fn test_dropping_receive_before_poll() { 263 | let poll = Poll::new().unwrap(); 264 | let mut events = Events::with_capacity(1024); 265 | let (tx, rx) = channel::channel(); 266 | 267 | poll.register(&rx, Token(123), Ready::readable(), PollOpt::edge()) 268 | .unwrap(); 269 | 270 | // Push the value 271 | tx.send("hello").unwrap(); 272 | 273 | // Drop the receive end 274 | drop(rx); 275 | 276 | // Wait, but nothing should happen 277 | let num = poll 278 | .poll(&mut events, Some(Duration::from_millis(300))) 279 | .unwrap(); 280 | assert_eq!(0, num); 281 | } 282 | 283 | #[test] 284 | pub fn test_mixing_channel_with_socket() { 285 | use mio::net::{TcpListener, TcpStream}; 286 | 287 | let poll = Poll::new().unwrap(); 288 | let mut events = Events::with_capacity(1024); 289 | let (tx, rx) = channel::channel(); 290 | 291 | // Create the listener 292 | let l = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap(); 293 | 294 | // Register the listener with `Poll` 295 | poll.register(&l, Token(0), Ready::readable(), PollOpt::edge()) 296 | .unwrap(); 297 | poll.register(&rx, Token(1), Ready::readable(), PollOpt::edge()) 298 | .unwrap(); 299 | 300 | // Push a value onto the channel 301 | tx.send("hello").unwrap(); 302 | 303 | // Connect a TCP socket 304 | let s1 = TcpStream::connect(&l.local_addr().unwrap()).unwrap(); 305 | 306 | // Register the socket 307 | poll.register(&s1, Token(2), Ready::readable(), PollOpt::edge()) 308 | .unwrap(); 309 | 310 | // Sleep a bit to ensure it arrives at dest 311 | thread::sleep(Duration::from_millis(250)); 312 | 313 | expect_events( 314 | &poll, 315 | &mut events, 316 | 2, 317 | vec![ 318 | Event::new(Ready::empty(), Token(0)), 319 | Event::new(Ready::empty(), Token(1)), 320 | ], 321 | ); 322 | } 323 | 324 | #[test] 325 | pub fn test_sending_from_other_thread_while_polling() { 326 | const ITERATIONS: usize = 20; 327 | const THREADS: usize = 5; 328 | 329 | // Make sure to run multiple times 330 | let poll = Poll::new().unwrap(); 331 | let mut events = Events::with_capacity(1024); 332 | 333 | for _ in 0..ITERATIONS { 334 | let (tx, rx) = channel::channel(); 335 | poll.register(&rx, Token(0), Ready::readable(), PollOpt::edge()) 336 | .unwrap(); 337 | 338 | for _ in 0..THREADS { 339 | let tx = tx.clone(); 340 | 341 | thread::spawn(move || { 342 | thread::sleep(Duration::from_millis(50)); 343 | tx.send("ping").unwrap(); 344 | }); 345 | } 346 | 347 | let mut recv = 0; 348 | 349 | while recv < THREADS { 350 | let num = poll.poll(&mut events, None).unwrap(); 351 | 352 | if num != 0 { 353 | assert_eq!(1, num); 354 | assert_eq!(events.iter().next().unwrap().token(), Token(0)); 355 | 356 | while rx.try_recv().is_ok() { 357 | recv += 1; 358 | } 359 | } 360 | } 361 | } 362 | } 363 | -------------------------------------------------------------------------------- /test/test_timer.rs: -------------------------------------------------------------------------------- 1 | use mio::{Events, Poll, PollOpt, Ready, Token}; 2 | use mio_extras::timer::{self, Timer}; 3 | 4 | use std::thread; 5 | use std::time::Duration; 6 | 7 | #[test] 8 | fn test_basic_timer_without_poll() { 9 | let mut timer = Timer::default(); 10 | 11 | // Set the timeout 12 | timer.set_timeout(Duration::from_millis(200), "hello"); 13 | 14 | // Nothing when polled immediately 15 | assert!(timer.poll().is_none()); 16 | 17 | // Wait for the timeout 18 | thread::sleep(Duration::from_millis(250)); 19 | 20 | assert_eq!(Some("hello"), timer.poll()); 21 | assert!(timer.poll().is_none()); 22 | } 23 | 24 | #[test] 25 | fn test_basic_timer_with_poll_edge_set_timeout_after_register() { 26 | let poll = Poll::new().unwrap(); 27 | let mut events = Events::with_capacity(1024); 28 | let mut timer = Timer::default(); 29 | 30 | poll.register(&timer, Token(0), Ready::readable(), PollOpt::edge()) 31 | .unwrap(); 32 | timer.set_timeout(Duration::from_millis(200), "hello"); 33 | 34 | let elapsed = elapsed(|| { 35 | let num = poll.poll(&mut events, None).unwrap(); 36 | 37 | assert_eq!(num, 1); 38 | let event = events.iter().next().unwrap(); 39 | assert_eq!(Token(0), event.token()); 40 | assert_eq!(Ready::readable(), event.readiness()); 41 | }); 42 | 43 | assert!(is_about(200, elapsed), "actual={:?}", elapsed); 44 | assert_eq!("hello", timer.poll().unwrap()); 45 | assert_eq!(None, timer.poll()); 46 | } 47 | 48 | #[test] 49 | fn test_basic_timer_with_poll_edge_set_timeout_before_register() { 50 | let poll = Poll::new().unwrap(); 51 | let mut events = Events::with_capacity(1024); 52 | let mut timer = Timer::default(); 53 | 54 | timer.set_timeout(Duration::from_millis(200), "hello"); 55 | poll.register(&timer, Token(0), Ready::readable(), PollOpt::edge()) 56 | .unwrap(); 57 | 58 | let elapsed = elapsed(|| { 59 | let num = poll.poll(&mut events, None).unwrap(); 60 | 61 | assert_eq!(num, 1); 62 | let event = events.iter().next().unwrap(); 63 | assert_eq!(Token(0), event.token()); 64 | assert_eq!(Ready::readable(), event.readiness()); 65 | }); 66 | 67 | assert!(is_about(200, elapsed), "actual={:?}", elapsed); 68 | assert_eq!("hello", timer.poll().unwrap()); 69 | assert_eq!(None, timer.poll()); 70 | } 71 | 72 | #[test] 73 | fn test_setting_later_timeout_then_earlier_one() { 74 | let poll = Poll::new().unwrap(); 75 | let mut events = Events::with_capacity(1024); 76 | let mut timer = Timer::default(); 77 | 78 | poll.register(&timer, Token(0), Ready::readable(), PollOpt::edge()) 79 | .unwrap(); 80 | 81 | timer.set_timeout(Duration::from_millis(600), "hello"); 82 | timer.set_timeout(Duration::from_millis(200), "world"); 83 | 84 | let elapsed = elapsed(|| { 85 | let num = poll.poll(&mut events, None).unwrap(); 86 | 87 | assert_eq!(num, 1); 88 | let event = events.iter().next().unwrap(); 89 | assert_eq!(Token(0), event.token()); 90 | assert_eq!(Ready::readable(), event.readiness()); 91 | }); 92 | 93 | assert!(is_about(200, elapsed), "actual={:?}", elapsed); 94 | assert_eq!("world", timer.poll().unwrap()); 95 | assert_eq!(None, timer.poll()); 96 | 97 | let elapsed = self::elapsed(|| { 98 | let num = poll.poll(&mut events, None).unwrap(); 99 | 100 | assert_eq!(num, 1); 101 | let event = events.iter().next().unwrap(); 102 | assert_eq!(Token(0), event.token()); 103 | assert_eq!(Ready::readable(), event.readiness()); 104 | }); 105 | 106 | assert!(is_about(400, elapsed), "actual={:?}", elapsed); 107 | assert_eq!("hello", timer.poll().unwrap()); 108 | assert_eq!(None, timer.poll()); 109 | } 110 | 111 | #[test] 112 | fn test_timer_with_looping_wheel() { 113 | let poll = Poll::new().unwrap(); 114 | let mut events = Events::with_capacity(1024); 115 | let mut timer = timer::Builder::default().num_slots(2).build(); 116 | 117 | poll.register(&timer, Token(0), Ready::readable(), PollOpt::edge()) 118 | .unwrap(); 119 | 120 | const TOKENS: &[&str] = &["hello", "world", "some", "thing"]; 121 | 122 | for (i, msg) in TOKENS.iter().enumerate() { 123 | timer.set_timeout(Duration::from_millis(500 * (i as u64 + 1)), msg); 124 | } 125 | 126 | for msg in TOKENS { 127 | let elapsed = elapsed(|| { 128 | let num = poll.poll(&mut events, None).unwrap(); 129 | 130 | assert_eq!(num, 1); 131 | let event = events.iter().next().unwrap(); 132 | assert_eq!(Token(0), event.token()); 133 | assert_eq!(Ready::readable(), event.readiness()); 134 | }); 135 | 136 | assert!( 137 | is_about(500, elapsed), 138 | "actual={:?}; msg={:?}", 139 | elapsed, 140 | msg 141 | ); 142 | assert_eq!(Some(msg), timer.poll()); 143 | assert_eq!(None, timer.poll()); 144 | } 145 | } 146 | 147 | #[test] 148 | fn test_edge_without_polling() { 149 | let poll = Poll::new().unwrap(); 150 | let mut events = Events::with_capacity(1024); 151 | let mut timer = Timer::default(); 152 | 153 | poll.register(&timer, Token(0), Ready::readable(), PollOpt::edge()) 154 | .unwrap(); 155 | 156 | timer.set_timeout(Duration::from_millis(400), "hello"); 157 | 158 | let ms = elapsed(|| { 159 | let num = poll.poll(&mut events, None).unwrap(); 160 | assert_eq!(num, 1); 161 | let event = events.iter().next().unwrap(); 162 | assert_eq!(Token(0), event.token()); 163 | assert_eq!(Ready::readable(), event.readiness()); 164 | }); 165 | 166 | assert!(is_about(400, ms), "actual={:?}", ms); 167 | 168 | let ms = elapsed(|| { 169 | let num = poll 170 | .poll(&mut events, Some(Duration::from_millis(300))) 171 | .unwrap(); 172 | assert_eq!(num, 0); 173 | }); 174 | 175 | assert!(is_about(300, ms), "actual={:?}", ms); 176 | } 177 | 178 | #[test] 179 | fn test_level_triggered() { 180 | let poll = Poll::new().unwrap(); 181 | let mut events = Events::with_capacity(1024); 182 | let mut timer = Timer::default(); 183 | 184 | poll.register(&timer, Token(0), Ready::readable(), PollOpt::level()) 185 | .unwrap(); 186 | 187 | timer.set_timeout(Duration::from_millis(400), "hello"); 188 | 189 | let ms = elapsed(|| { 190 | let num = poll.poll(&mut events, None).unwrap(); 191 | assert_eq!(num, 1); 192 | let event = events.iter().next().unwrap(); 193 | assert_eq!(Token(0), event.token()); 194 | assert_eq!(Ready::readable(), event.readiness()); 195 | }); 196 | 197 | assert!(is_about(400, ms), "actual={:?}", ms); 198 | 199 | let ms = elapsed(|| { 200 | let num = poll.poll(&mut events, None).unwrap(); 201 | assert_eq!(num, 1); 202 | let event = events.iter().next().unwrap(); 203 | assert_eq!(Token(0), event.token()); 204 | assert_eq!(Ready::readable(), event.readiness()); 205 | }); 206 | 207 | assert!(is_about(0, ms), "actual={:?}", ms); 208 | } 209 | 210 | #[test] 211 | fn test_edge_oneshot_triggered() { 212 | let poll = Poll::new().unwrap(); 213 | let mut events = Events::with_capacity(1024); 214 | let mut timer = Timer::default(); 215 | 216 | poll.register( 217 | &timer, 218 | Token(0), 219 | Ready::readable(), 220 | PollOpt::edge() | PollOpt::oneshot(), 221 | ) 222 | .unwrap(); 223 | 224 | timer.set_timeout(Duration::from_millis(200), "hello"); 225 | 226 | let ms = elapsed(|| { 227 | let num = poll.poll(&mut events, None).unwrap(); 228 | assert_eq!(num, 1); 229 | }); 230 | 231 | assert!(is_about(200, ms), "actual={:?}", ms); 232 | 233 | let ms = elapsed(|| { 234 | let num = poll 235 | .poll(&mut events, Some(Duration::from_millis(300))) 236 | .unwrap(); 237 | assert_eq!(num, 0); 238 | }); 239 | 240 | assert!(is_about(300, ms), "actual={:?}", ms); 241 | 242 | poll.reregister( 243 | &timer, 244 | Token(0), 245 | Ready::readable(), 246 | PollOpt::edge() | PollOpt::oneshot(), 247 | ) 248 | .unwrap(); 249 | 250 | let ms = elapsed(|| { 251 | let num = poll.poll(&mut events, None).unwrap(); 252 | assert_eq!(num, 1); 253 | }); 254 | 255 | assert!(is_about(0, ms)); 256 | } 257 | 258 | #[test] 259 | fn test_cancel_timeout() { 260 | use std::time::Instant; 261 | 262 | let mut timer: Timer = Default::default(); 263 | let timeout = timer.set_timeout(Duration::from_millis(200), 1); 264 | timer.cancel_timeout(&timeout); 265 | 266 | let poll = Poll::new().unwrap(); 267 | poll.register(&timer, Token(0), Ready::readable(), PollOpt::edge()) 268 | .unwrap(); 269 | 270 | let mut events = Events::with_capacity(16); 271 | 272 | let now = Instant::now(); 273 | let dur = Duration::from_millis(500); 274 | let mut i = 0; 275 | 276 | while Instant::now() - now < dur { 277 | if i > 10 { 278 | panic!("iterated too many times"); 279 | } 280 | 281 | i += 1; 282 | 283 | let elapsed = Instant::now() - now; 284 | 285 | poll.poll(&mut events, Some(dur - elapsed)).unwrap(); 286 | 287 | while timer.poll().is_some() { 288 | panic!("did not expect to receive timeout"); 289 | } 290 | } 291 | } 292 | 293 | fn elapsed(mut f: F) -> u64 { 294 | use std::time::Instant; 295 | 296 | let now = Instant::now(); 297 | 298 | f(); 299 | 300 | let elapsed = now.elapsed(); 301 | elapsed.as_secs() * 1000 + u64::from(elapsed.subsec_millis()) 302 | } 303 | 304 | fn is_about(expect: u64, val: u64) -> bool { 305 | const WINDOW: i64 = 200; 306 | 307 | ((expect as i64) - (val as i64)).abs() <= WINDOW 308 | } 309 | --------------------------------------------------------------------------------