From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/thread_local/.cargo-checksum.json | 1 + vendor/thread_local/Cargo.toml | 37 ++ vendor/thread_local/LICENSE-APACHE | 201 +++++++++ vendor/thread_local/LICENSE-MIT | 25 ++ vendor/thread_local/README.md | 39 ++ vendor/thread_local/benches/thread_local.rs | 28 ++ vendor/thread_local/src/cached.rs | 161 +++++++ vendor/thread_local/src/lib.rs | 638 ++++++++++++++++++++++++++++ vendor/thread_local/src/thread_id.rs | 127 ++++++ vendor/thread_local/src/unreachable.rs | 57 +++ 10 files changed, 1314 insertions(+) create mode 100644 vendor/thread_local/.cargo-checksum.json create mode 100644 vendor/thread_local/Cargo.toml create mode 100644 vendor/thread_local/LICENSE-APACHE create mode 100644 vendor/thread_local/LICENSE-MIT create mode 100644 vendor/thread_local/README.md create mode 100644 vendor/thread_local/benches/thread_local.rs create mode 100644 vendor/thread_local/src/cached.rs create mode 100644 vendor/thread_local/src/lib.rs create mode 100644 vendor/thread_local/src/thread_id.rs create mode 100644 vendor/thread_local/src/unreachable.rs (limited to 'vendor/thread_local') diff --git a/vendor/thread_local/.cargo-checksum.json b/vendor/thread_local/.cargo-checksum.json new file mode 100644 index 000000000..f9cb3e1bd --- /dev/null +++ b/vendor/thread_local/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"db2c006192c59fb9465a813ff0a8012fae8ca13e839a1ff92b0c7ef3ecfbe032","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"c9a75f18b9ab2927829a208fc6aa2cf4e63b8420887ba29cdb265d6619ae82d5","README.md":"44a77143b8ea0cba5a64873fa77c76aad209d8d80068b2bd11986dadb9f97ed7","benches/thread_local.rs":"29f8fbc4fc7cf0b93b02bbf20e73113cbde2388d962cd5c617918f900a6893ec","src/cached.rs":"253cb48da470265f4c4702a4f9f6b8670860cb092d8304d807717d751d0b3350","src/lib.rs":"92ae38e941e1b4c0f32e1f415e272f76374cb6f5aa8752275202fa856d3876e9","src/thread_id.rs":"53ae035d18d31eae822c9ff82219da34deed1234686d6fe3a82ca5da51ffd6a1","src/unreachable.rs":"f0c65f1b0516cc92fbd7df2c8c8edfdac5362377d27ae5b91a12204673e2fd73"},"package":"5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"} \ No newline at end of file diff --git a/vendor/thread_local/Cargo.toml b/vendor/thread_local/Cargo.toml new file mode 100644 index 000000000..90e53190d --- /dev/null +++ b/vendor/thread_local/Cargo.toml @@ -0,0 +1,37 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "thread_local" +version = "1.1.4" +authors = ["Amanieu d'Antras "] +description = "Per-object thread-local storage" +documentation = "https://docs.rs/thread_local/" +readme = "README.md" +keywords = ["thread_local", "concurrent", "thread"] +license = "Apache-2.0/MIT" +repository = "https://github.com/Amanieu/thread_local-rs" + +[[bench]] +name = "thread_local" +harness = false +required-features = ["criterion"] +[dependencies.criterion] +version = "0.3.3" +optional = true + +[dependencies.once_cell] +version = "1.5.2" + +[dev-dependencies] +[badges.travis-ci] +repository = "Amanieu/thread_local-rs" diff --git a/vendor/thread_local/LICENSE-APACHE b/vendor/thread_local/LICENSE-APACHE new file mode 100644 index 000000000..16fe87b06 --- /dev/null +++ b/vendor/thread_local/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/vendor/thread_local/LICENSE-MIT b/vendor/thread_local/LICENSE-MIT new file mode 100644 index 000000000..40b8817a4 --- /dev/null +++ b/vendor/thread_local/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2016 The Rust Project Developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/thread_local/README.md b/vendor/thread_local/README.md new file mode 100644 index 000000000..6560356f2 --- /dev/null +++ b/vendor/thread_local/README.md @@ -0,0 +1,39 @@ +thread_local +============ + +[![Build Status](https://travis-ci.org/Amanieu/thread_local-rs.svg?branch=master)](https://travis-ci.org/Amanieu/thread_local-rs) [![Crates.io](https://img.shields.io/crates/v/thread_local.svg)](https://crates.io/crates/thread_local) + +This library provides the `ThreadLocal` type which allow a separate copy of an +object to be used for each thread. This allows for per-object thread-local +storage, unlike the standard library's `thread_local!` macro which only allows +static thread-local storage. + +[Documentation](https://docs.rs/thread_local/) + +## Usage + +Add this to your `Cargo.toml`: + +```toml +[dependencies] +thread_local = "1.1" +``` + +## Minimum Rust version + +This crate's minimum supported Rust version (MSRV) is 1.36.0. + +## License + +Licensed under either of + + * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any +additional terms or conditions. diff --git a/vendor/thread_local/benches/thread_local.rs b/vendor/thread_local/benches/thread_local.rs new file mode 100644 index 000000000..ccad6655e --- /dev/null +++ b/vendor/thread_local/benches/thread_local.rs @@ -0,0 +1,28 @@ +extern crate criterion; +extern crate thread_local; + +use criterion::{black_box, BatchSize}; + +use thread_local::ThreadLocal; + +fn main() { + let mut c = criterion::Criterion::default().configure_from_args(); + + c.bench_function("get", |b| { + let local = ThreadLocal::new(); + local.get_or(|| Box::new(0)); + b.iter(|| { + black_box(local.get()); + }); + }); + + c.bench_function("insert", |b| { + b.iter_batched_ref( + ThreadLocal::new, + |local| { + black_box(local.get_or(|| 0)); + }, + BatchSize::SmallInput, + ) + }); +} diff --git a/vendor/thread_local/src/cached.rs b/vendor/thread_local/src/cached.rs new file mode 100644 index 000000000..16f6516de --- /dev/null +++ b/vendor/thread_local/src/cached.rs @@ -0,0 +1,161 @@ +#![allow(deprecated)] + +use super::{IntoIter, IterMut, ThreadLocal}; +use std::fmt; +use std::panic::UnwindSafe; +use std::usize; + +/// Wrapper around [`ThreadLocal`]. +/// +/// This used to add a fast path for a single thread, however that has been +/// obsoleted by performance improvements to [`ThreadLocal`] itself. +#[deprecated(since = "1.1.0", note = "Use `ThreadLocal` instead")] +pub struct CachedThreadLocal { + inner: ThreadLocal, +} + +impl Default for CachedThreadLocal { + fn default() -> CachedThreadLocal { + CachedThreadLocal::new() + } +} + +impl CachedThreadLocal { + /// Creates a new empty `CachedThreadLocal`. + #[inline] + pub fn new() -> CachedThreadLocal { + CachedThreadLocal { + inner: ThreadLocal::new(), + } + } + + /// Returns the element for the current thread, if it exists. + #[inline] + pub fn get(&self) -> Option<&T> { + self.inner.get() + } + + /// Returns the element for the current thread, or creates it if it doesn't + /// exist. + #[inline] + pub fn get_or(&self, create: F) -> &T + where + F: FnOnce() -> T, + { + self.inner.get_or(create) + } + + /// Returns the element for the current thread, or creates it if it doesn't + /// exist. If `create` fails, that error is returned and no element is + /// added. + #[inline] + pub fn get_or_try(&self, create: F) -> Result<&T, E> + where + F: FnOnce() -> Result, + { + self.inner.get_or_try(create) + } + + /// Returns a mutable iterator over the local values of all threads. + /// + /// Since this call borrows the `ThreadLocal` mutably, this operation can + /// be done safely---the mutable borrow statically guarantees no other + /// threads are currently accessing their associated values. + #[inline] + pub fn iter_mut(&mut self) -> CachedIterMut { + CachedIterMut { + inner: self.inner.iter_mut(), + } + } + + /// Removes all thread-specific values from the `ThreadLocal`, effectively + /// reseting it to its original state. + /// + /// Since this call borrows the `ThreadLocal` mutably, this operation can + /// be done safely---the mutable borrow statically guarantees no other + /// threads are currently accessing their associated values. + #[inline] + pub fn clear(&mut self) { + self.inner.clear(); + } +} + +impl IntoIterator for CachedThreadLocal { + type Item = T; + type IntoIter = CachedIntoIter; + + fn into_iter(self) -> CachedIntoIter { + CachedIntoIter { + inner: self.inner.into_iter(), + } + } +} + +impl<'a, T: Send + 'a> IntoIterator for &'a mut CachedThreadLocal { + type Item = &'a mut T; + type IntoIter = CachedIterMut<'a, T>; + + fn into_iter(self) -> CachedIterMut<'a, T> { + self.iter_mut() + } +} + +impl CachedThreadLocal { + /// Returns the element for the current thread, or creates a default one if + /// it doesn't exist. + pub fn get_or_default(&self) -> &T { + self.get_or(T::default) + } +} + +impl fmt::Debug for CachedThreadLocal { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "ThreadLocal {{ local_data: {:?} }}", self.get()) + } +} + +impl UnwindSafe for CachedThreadLocal {} + +/// Mutable iterator over the contents of a `CachedThreadLocal`. +#[deprecated(since = "1.1.0", note = "Use `IterMut` instead")] +pub struct CachedIterMut<'a, T: Send + 'a> { + inner: IterMut<'a, T>, +} + +impl<'a, T: Send + 'a> Iterator for CachedIterMut<'a, T> { + type Item = &'a mut T; + + #[inline] + fn next(&mut self) -> Option<&'a mut T> { + self.inner.next() + } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl<'a, T: Send + 'a> ExactSizeIterator for CachedIterMut<'a, T> {} + +/// An iterator that moves out of a `CachedThreadLocal`. +#[deprecated(since = "1.1.0", note = "Use `IntoIter` instead")] +pub struct CachedIntoIter { + inner: IntoIter, +} + +impl Iterator for CachedIntoIter { + type Item = T; + + #[inline] + fn next(&mut self) -> Option { + self.inner.next() + } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl ExactSizeIterator for CachedIntoIter {} diff --git a/vendor/thread_local/src/lib.rs b/vendor/thread_local/src/lib.rs new file mode 100644 index 000000000..33b79d6a5 --- /dev/null +++ b/vendor/thread_local/src/lib.rs @@ -0,0 +1,638 @@ +// Copyright 2017 Amanieu d'Antras +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +//! Per-object thread-local storage +//! +//! This library provides the `ThreadLocal` type which allows a separate copy of +//! an object to be used for each thread. This allows for per-object +//! thread-local storage, unlike the standard library's `thread_local!` macro +//! which only allows static thread-local storage. +//! +//! Per-thread objects are not destroyed when a thread exits. Instead, objects +//! are only destroyed when the `ThreadLocal` containing them is destroyed. +//! +//! You can also iterate over the thread-local values of all thread in a +//! `ThreadLocal` object using the `iter_mut` and `into_iter` methods. This can +//! only be done if you have mutable access to the `ThreadLocal` object, which +//! guarantees that you are the only thread currently accessing it. +//! +//! Note that since thread IDs are recycled when a thread exits, it is possible +//! for one thread to retrieve the object of another thread. Since this can only +//! occur after a thread has exited this does not lead to any race conditions. +//! +//! # Examples +//! +//! Basic usage of `ThreadLocal`: +//! +//! ```rust +//! use thread_local::ThreadLocal; +//! let tls: ThreadLocal = ThreadLocal::new(); +//! assert_eq!(tls.get(), None); +//! assert_eq!(tls.get_or(|| 5), &5); +//! assert_eq!(tls.get(), Some(&5)); +//! ``` +//! +//! Combining thread-local values into a single result: +//! +//! ```rust +//! use thread_local::ThreadLocal; +//! use std::sync::Arc; +//! use std::cell::Cell; +//! use std::thread; +//! +//! let tls = Arc::new(ThreadLocal::new()); +//! +//! // Create a bunch of threads to do stuff +//! for _ in 0..5 { +//! let tls2 = tls.clone(); +//! thread::spawn(move || { +//! // Increment a counter to count some event... +//! let cell = tls2.get_or(|| Cell::new(0)); +//! cell.set(cell.get() + 1); +//! }).join().unwrap(); +//! } +//! +//! // Once all threads are done, collect the counter values and return the +//! // sum of all thread-local counter values. +//! let tls = Arc::try_unwrap(tls).unwrap(); +//! let total = tls.into_iter().fold(0, |x, y| x + y.get()); +//! assert_eq!(total, 5); +//! ``` + +#![warn(missing_docs)] +#![allow(clippy::mutex_atomic)] + +mod cached; +mod thread_id; +mod unreachable; + +#[allow(deprecated)] +pub use cached::{CachedIntoIter, CachedIterMut, CachedThreadLocal}; + +use std::cell::UnsafeCell; +use std::fmt; +use std::iter::FusedIterator; +use std::mem; +use std::mem::MaybeUninit; +use std::panic::UnwindSafe; +use std::ptr; +use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicUsize, Ordering}; +use std::sync::Mutex; +use thread_id::Thread; +use unreachable::UncheckedResultExt; + +// Use usize::BITS once it has stabilized and the MSRV has been bumped. +#[cfg(target_pointer_width = "16")] +const POINTER_WIDTH: u8 = 16; +#[cfg(target_pointer_width = "32")] +const POINTER_WIDTH: u8 = 32; +#[cfg(target_pointer_width = "64")] +const POINTER_WIDTH: u8 = 64; + +/// The total number of buckets stored in each thread local. +const BUCKETS: usize = (POINTER_WIDTH + 1) as usize; + +/// Thread-local variable wrapper +/// +/// See the [module-level documentation](index.html) for more. +pub struct ThreadLocal { + /// The buckets in the thread local. The nth bucket contains `2^(n-1)` + /// elements. Each bucket is lazily allocated. + buckets: [AtomicPtr>; BUCKETS], + + /// The number of values in the thread local. This can be less than the real number of values, + /// but is never more. + values: AtomicUsize, + + /// Lock used to guard against concurrent modifications. This is taken when + /// there is a possibility of allocating a new bucket, which only occurs + /// when inserting values. + lock: Mutex<()>, +} + +struct Entry { + present: AtomicBool, + value: UnsafeCell>, +} + +impl Drop for Entry { + fn drop(&mut self) { + unsafe { + if *self.present.get_mut() { + ptr::drop_in_place((*self.value.get()).as_mut_ptr()); + } + } + } +} + +// ThreadLocal is always Sync, even if T isn't +unsafe impl Sync for ThreadLocal {} + +impl Default for ThreadLocal { + fn default() -> ThreadLocal { + ThreadLocal::new() + } +} + +impl Drop for ThreadLocal { + fn drop(&mut self) { + let mut bucket_size = 1; + + // Free each non-null bucket + for (i, bucket) in self.buckets.iter_mut().enumerate() { + let bucket_ptr = *bucket.get_mut(); + + let this_bucket_size = bucket_size; + if i != 0 { + bucket_size <<= 1; + } + + if bucket_ptr.is_null() { + continue; + } + + unsafe { Box::from_raw(std::slice::from_raw_parts_mut(bucket_ptr, this_bucket_size)) }; + } + } +} + +impl ThreadLocal { + /// Creates a new empty `ThreadLocal`. + pub fn new() -> ThreadLocal { + Self::with_capacity(2) + } + + /// Creates a new `ThreadLocal` with an initial capacity. If less than the capacity threads + /// access the thread local it will never reallocate. The capacity may be rounded up to the + /// nearest power of two. + pub fn with_capacity(capacity: usize) -> ThreadLocal { + let allocated_buckets = capacity + .checked_sub(1) + .map(|c| usize::from(POINTER_WIDTH) - (c.leading_zeros() as usize) + 1) + .unwrap_or(0); + + let mut buckets = [ptr::null_mut(); BUCKETS]; + let mut bucket_size = 1; + for (i, bucket) in buckets[..allocated_buckets].iter_mut().enumerate() { + *bucket = allocate_bucket::(bucket_size); + + if i != 0 { + bucket_size <<= 1; + } + } + + ThreadLocal { + // Safety: AtomicPtr has the same representation as a pointer and arrays have the same + // representation as a sequence of their inner type. + buckets: unsafe { mem::transmute(buckets) }, + values: AtomicUsize::new(0), + lock: Mutex::new(()), + } + } + + /// Returns the element for the current thread, if it exists. + pub fn get(&self) -> Option<&T> { + let thread = thread_id::get(); + self.get_inner(thread) + } + + /// Returns the element for the current thread, or creates it if it doesn't + /// exist. + pub fn get_or(&self, create: F) -> &T + where + F: FnOnce() -> T, + { + unsafe { + self.get_or_try(|| Ok::(create())) + .unchecked_unwrap_ok() + } + } + + /// Returns the element for the current thread, or creates it if it doesn't + /// exist. If `create` fails, that error is returned and no element is + /// added. + pub fn get_or_try(&self, create: F) -> Result<&T, E> + where + F: FnOnce() -> Result, + { + let thread = thread_id::get(); + match self.get_inner(thread) { + Some(x) => Ok(x), + None => Ok(self.insert(thread, create()?)), + } + } + + fn get_inner(&self, thread: Thread) -> Option<&T> { + let bucket_ptr = + unsafe { self.buckets.get_unchecked(thread.bucket) }.load(Ordering::Acquire); + if bucket_ptr.is_null() { + return None; + } + unsafe { + let entry = &*bucket_ptr.add(thread.index); + // Read without atomic operations as only this thread can set the value. + if (&entry.present as *const _ as *const bool).read() { + Some(&*(&*entry.value.get()).as_ptr()) + } else { + None + } + } + } + + #[cold] + fn insert(&self, thread: Thread, data: T) -> &T { + // Lock the Mutex to ensure only a single thread is allocating buckets at once + let _guard = self.lock.lock().unwrap(); + + let bucket_atomic_ptr = unsafe { self.buckets.get_unchecked(thread.bucket) }; + + let bucket_ptr: *const _ = bucket_atomic_ptr.load(Ordering::Acquire); + let bucket_ptr = if bucket_ptr.is_null() { + // Allocate a new bucket + let bucket_ptr = allocate_bucket(thread.bucket_size); + bucket_atomic_ptr.store(bucket_ptr, Ordering::Release); + bucket_ptr + } else { + bucket_ptr + }; + + drop(_guard); + + // Insert the new element into the bucket + let entry = unsafe { &*bucket_ptr.add(thread.index) }; + let value_ptr = entry.value.get(); + unsafe { value_ptr.write(MaybeUninit::new(data)) }; + entry.present.store(true, Ordering::Release); + + self.values.fetch_add(1, Ordering::Release); + + unsafe { &*(&*value_ptr).as_ptr() } + } + + /// Returns an iterator over the local values of all threads in unspecified + /// order. + /// + /// This call can be done safely, as `T` is required to implement [`Sync`]. + pub fn iter(&self) -> Iter<'_, T> + where + T: Sync, + { + Iter { + thread_local: self, + raw: RawIter::new(), + } + } + + /// Returns a mutable iterator over the local values of all threads in + /// unspecified order. + /// + /// Since this call borrows the `ThreadLocal` mutably, this operation can + /// be done safely---the mutable borrow statically guarantees no other + /// threads are currently accessing their associated values. + pub fn iter_mut(&mut self) -> IterMut { + IterMut { + thread_local: self, + raw: RawIter::new(), + } + } + + /// Removes all thread-specific values from the `ThreadLocal`, effectively + /// reseting it to its original state. + /// + /// Since this call borrows the `ThreadLocal` mutably, this operation can + /// be done safely---the mutable borrow statically guarantees no other + /// threads are currently accessing their associated values. + pub fn clear(&mut self) { + *self = ThreadLocal::new(); + } +} + +impl IntoIterator for ThreadLocal { + type Item = T; + type IntoIter = IntoIter; + + fn into_iter(self) -> IntoIter { + IntoIter { + thread_local: self, + raw: RawIter::new(), + } + } +} + +impl<'a, T: Send + Sync> IntoIterator for &'a ThreadLocal { + type Item = &'a T; + type IntoIter = Iter<'a, T>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +impl<'a, T: Send> IntoIterator for &'a mut ThreadLocal { + type Item = &'a mut T; + type IntoIter = IterMut<'a, T>; + + fn into_iter(self) -> IterMut<'a, T> { + self.iter_mut() + } +} + +impl ThreadLocal { + /// Returns the element for the current thread, or creates a default one if + /// it doesn't exist. + pub fn get_or_default(&self) -> &T { + self.get_or(Default::default) + } +} + +impl fmt::Debug for ThreadLocal { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "ThreadLocal {{ local_data: {:?} }}", self.get()) + } +} + +impl UnwindSafe for ThreadLocal {} + +#[derive(Debug)] +struct RawIter { + yielded: usize, + bucket: usize, + bucket_size: usize, + index: usize, +} +impl RawIter { + #[inline] + fn new() -> Self { + Self { + yielded: 0, + bucket: 0, + bucket_size: 1, + index: 0, + } + } + + fn next<'a, T: Send + Sync>(&mut self, thread_local: &'a ThreadLocal) -> Option<&'a T> { + while self.bucket < BUCKETS { + let bucket = unsafe { thread_local.buckets.get_unchecked(self.bucket) }; + let bucket = bucket.load(Ordering::Acquire); + + if !bucket.is_null() { + while self.index < self.bucket_size { + let entry = unsafe { &*bucket.add(self.index) }; + self.index += 1; + if entry.present.load(Ordering::Acquire) { + self.yielded += 1; + return Some(unsafe { &*(&*entry.value.get()).as_ptr() }); + } + } + } + + self.next_bucket(); + } + None + } + fn next_mut<'a, T: Send>( + &mut self, + thread_local: &'a mut ThreadLocal, + ) -> Option<&'a mut Entry> { + if *thread_local.values.get_mut() == self.yielded { + return None; + } + + loop { + let bucket = unsafe { thread_local.buckets.get_unchecked_mut(self.bucket) }; + let bucket = *bucket.get_mut(); + + if !bucket.is_null() { + while self.index < self.bucket_size { + let entry = unsafe { &mut *bucket.add(self.index) }; + self.index += 1; + if *entry.present.get_mut() { + self.yielded += 1; + return Some(entry); + } + } + } + + self.next_bucket(); + } + } + + #[inline] + fn next_bucket(&mut self) { + if self.bucket != 0 { + self.bucket_size <<= 1; + } + self.bucket += 1; + self.index = 0; + } + + fn size_hint(&self, thread_local: &ThreadLocal) -> (usize, Option) { + let total = thread_local.values.load(Ordering::Acquire); + (total - self.yielded, None) + } + fn size_hint_frozen(&self, thread_local: &ThreadLocal) -> (usize, Option) { + let total = unsafe { *(&thread_local.values as *const AtomicUsize as *const usize) }; + let remaining = total - self.yielded; + (remaining, Some(remaining)) + } +} + +/// Iterator over the contents of a `ThreadLocal`. +#[derive(Debug)] +pub struct Iter<'a, T: Send + Sync> { + thread_local: &'a ThreadLocal, + raw: RawIter, +} + +impl<'a, T: Send + Sync> Iterator for Iter<'a, T> { + type Item = &'a T; + fn next(&mut self) -> Option { + self.raw.next(self.thread_local) + } + fn size_hint(&self) -> (usize, Option) { + self.raw.size_hint(self.thread_local) + } +} +impl FusedIterator for Iter<'_, T> {} + +/// Mutable iterator over the contents of a `ThreadLocal`. +pub struct IterMut<'a, T: Send> { + thread_local: &'a mut ThreadLocal, + raw: RawIter, +} + +impl<'a, T: Send> Iterator for IterMut<'a, T> { + type Item = &'a mut T; + fn next(&mut self) -> Option<&'a mut T> { + self.raw + .next_mut(self.thread_local) + .map(|entry| unsafe { &mut *(&mut *entry.value.get()).as_mut_ptr() }) + } + fn size_hint(&self) -> (usize, Option) { + self.raw.size_hint_frozen(self.thread_local) + } +} + +impl ExactSizeIterator for IterMut<'_, T> {} +impl FusedIterator for IterMut<'_, T> {} + +// Manual impl so we don't call Debug on the ThreadLocal, as doing so would create a reference to +// this thread's value that potentially aliases with a mutable reference we have given out. +impl<'a, T: Send + fmt::Debug> fmt::Debug for IterMut<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("IterMut").field("raw", &self.raw).finish() + } +} + +/// An iterator that moves out of a `ThreadLocal`. +#[derive(Debug)] +pub struct IntoIter { + thread_local: ThreadLocal, + raw: RawIter, +} + +impl Iterator for IntoIter { + type Item = T; + fn next(&mut self) -> Option { + self.raw.next_mut(&mut self.thread_local).map(|entry| { + *entry.present.get_mut() = false; + unsafe { + std::mem::replace(&mut *entry.value.get(), MaybeUninit::uninit()).assume_init() + } + }) + } + fn size_hint(&self) -> (usize, Option) { + self.raw.size_hint_frozen(&self.thread_local) + } +} + +impl ExactSizeIterator for IntoIter {} +impl FusedIterator for IntoIter {} + +fn allocate_bucket(size: usize) -> *mut Entry { + Box::into_raw( + (0..size) + .map(|_| Entry:: { + present: AtomicBool::new(false), + value: UnsafeCell::new(MaybeUninit::uninit()), + }) + .collect(), + ) as *mut _ +} + +#[cfg(test)] +mod tests { + use super::ThreadLocal; + use std::cell::RefCell; + use std::sync::atomic::AtomicUsize; + use std::sync::atomic::Ordering::Relaxed; + use std::sync::Arc; + use std::thread; + + fn make_create() -> Arc usize + Send + Sync> { + let count = AtomicUsize::new(0); + Arc::new(move || count.fetch_add(1, Relaxed)) + } + + #[test] + fn same_thread() { + let create = make_create(); + let mut tls = ThreadLocal::new(); + assert_eq!(None, tls.get()); + assert_eq!("ThreadLocal { local_data: None }", format!("{:?}", &tls)); + assert_eq!(0, *tls.get_or(|| create())); + assert_eq!(Some(&0), tls.get()); + assert_eq!(0, *tls.get_or(|| create())); + assert_eq!(Some(&0), tls.get()); + assert_eq!(0, *tls.get_or(|| create())); + assert_eq!(Some(&0), tls.get()); + assert_eq!("ThreadLocal { local_data: Some(0) }", format!("{:?}", &tls)); + tls.clear(); + assert_eq!(None, tls.get()); + } + + #[test] + fn different_thread() { + let create = make_create(); + let tls = Arc::new(ThreadLocal::new()); + assert_eq!(None, tls.get()); + assert_eq!(0, *tls.get_or(|| create())); + assert_eq!(Some(&0), tls.get()); + + let tls2 = tls.clone(); + let create2 = create.clone(); + thread::spawn(move || { + assert_eq!(None, tls2.get()); + assert_eq!(1, *tls2.get_or(|| create2())); + assert_eq!(Some(&1), tls2.get()); + }) + .join() + .unwrap(); + + assert_eq!(Some(&0), tls.get()); + assert_eq!(0, *tls.get_or(|| create())); + } + + #[test] + fn iter() { + let tls = Arc::new(ThreadLocal::new()); + tls.get_or(|| Box::new(1)); + + let tls2 = tls.clone(); + thread::spawn(move || { + tls2.get_or(|| Box::new(2)); + let tls3 = tls2.clone(); + thread::spawn(move || { + tls3.get_or(|| Box::new(3)); + }) + .join() + .unwrap(); + drop(tls2); + }) + .join() + .unwrap(); + + let mut tls = Arc::try_unwrap(tls).unwrap(); + + let mut v = tls.iter().map(|x| **x).collect::>(); + v.sort_unstable(); + assert_eq!(vec![1, 2, 3], v); + + let mut v = tls.iter_mut().map(|x| **x).collect::>(); + v.sort_unstable(); + assert_eq!(vec![1, 2, 3], v); + + let mut v = tls.into_iter().map(|x| *x).collect::>(); + v.sort_unstable(); + assert_eq!(vec![1, 2, 3], v); + } + + #[test] + fn test_drop() { + let local = ThreadLocal::new(); + struct Dropped(Arc); + impl Drop for Dropped { + fn drop(&mut self) { + self.0.fetch_add(1, Relaxed); + } + } + + let dropped = Arc::new(AtomicUsize::new(0)); + local.get_or(|| Dropped(dropped.clone())); + assert_eq!(dropped.load(Relaxed), 0); + drop(local); + assert_eq!(dropped.load(Relaxed), 1); + } + + #[test] + fn is_sync() { + fn foo() {} + foo::>(); + foo::>>(); + } +} diff --git a/vendor/thread_local/src/thread_id.rs b/vendor/thread_local/src/thread_id.rs new file mode 100644 index 000000000..6eb0f616f --- /dev/null +++ b/vendor/thread_local/src/thread_id.rs @@ -0,0 +1,127 @@ +// Copyright 2017 Amanieu d'Antras +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use crate::POINTER_WIDTH; +use once_cell::sync::Lazy; +use std::cmp::Reverse; +use std::collections::BinaryHeap; +use std::sync::Mutex; +use std::usize; + +/// Thread ID manager which allocates thread IDs. It attempts to aggressively +/// reuse thread IDs where possible to avoid cases where a ThreadLocal grows +/// indefinitely when it is used by many short-lived threads. +struct ThreadIdManager { + free_from: usize, + free_list: BinaryHeap>, +} +impl ThreadIdManager { + fn new() -> ThreadIdManager { + ThreadIdManager { + free_from: 0, + free_list: BinaryHeap::new(), + } + } + fn alloc(&mut self) -> usize { + if let Some(id) = self.free_list.pop() { + id.0 + } else { + let id = self.free_from; + self.free_from = self + .free_from + .checked_add(1) + .expect("Ran out of thread IDs"); + id + } + } + fn free(&mut self, id: usize) { + self.free_list.push(Reverse(id)); + } +} +static THREAD_ID_MANAGER: Lazy> = + Lazy::new(|| Mutex::new(ThreadIdManager::new())); + +/// Data which is unique to the current thread while it is running. +/// A thread ID may be reused after a thread exits. +#[derive(Clone, Copy)] +pub(crate) struct Thread { + /// The thread ID obtained from the thread ID manager. + pub(crate) id: usize, + /// The bucket this thread's local storage will be in. + pub(crate) bucket: usize, + /// The size of the bucket this thread's local storage will be in. + pub(crate) bucket_size: usize, + /// The index into the bucket this thread's local storage is in. + pub(crate) index: usize, +} +impl Thread { + fn new(id: usize) -> Thread { + let bucket = usize::from(POINTER_WIDTH) - id.leading_zeros() as usize; + let bucket_size = 1 << bucket.saturating_sub(1); + let index = if id != 0 { id ^ bucket_size } else { 0 }; + + Thread { + id, + bucket, + bucket_size, + index, + } + } +} + +/// Wrapper around `Thread` that allocates and deallocates the ID. +struct ThreadHolder(Thread); +impl ThreadHolder { + fn new() -> ThreadHolder { + ThreadHolder(Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc())) + } +} +impl Drop for ThreadHolder { + fn drop(&mut self) { + THREAD_ID_MANAGER.lock().unwrap().free(self.0.id); + } +} + +thread_local!(static THREAD_HOLDER: ThreadHolder = ThreadHolder::new()); + +/// Get the current thread. +pub(crate) fn get() -> Thread { + THREAD_HOLDER.with(|holder| holder.0) +} + +#[test] +fn test_thread() { + let thread = Thread::new(0); + assert_eq!(thread.id, 0); + assert_eq!(thread.bucket, 0); + assert_eq!(thread.bucket_size, 1); + assert_eq!(thread.index, 0); + + let thread = Thread::new(1); + assert_eq!(thread.id, 1); + assert_eq!(thread.bucket, 1); + assert_eq!(thread.bucket_size, 1); + assert_eq!(thread.index, 0); + + let thread = Thread::new(2); + assert_eq!(thread.id, 2); + assert_eq!(thread.bucket, 2); + assert_eq!(thread.bucket_size, 2); + assert_eq!(thread.index, 0); + + let thread = Thread::new(3); + assert_eq!(thread.id, 3); + assert_eq!(thread.bucket, 2); + assert_eq!(thread.bucket_size, 2); + assert_eq!(thread.index, 1); + + let thread = Thread::new(19); + assert_eq!(thread.id, 19); + assert_eq!(thread.bucket, 5); + assert_eq!(thread.bucket_size, 16); + assert_eq!(thread.index, 3); +} diff --git a/vendor/thread_local/src/unreachable.rs b/vendor/thread_local/src/unreachable.rs new file mode 100644 index 000000000..db4d831ac --- /dev/null +++ b/vendor/thread_local/src/unreachable.rs @@ -0,0 +1,57 @@ +// Copyright 2017 Amanieu d'Antras +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use std::hint::unreachable_unchecked; + +/// An extension trait for `Option` providing unchecked unwrapping methods. +pub trait UncheckedOptionExt { + /// Get the value out of this Option without checking for None. + unsafe fn unchecked_unwrap(self) -> T; + + /// Assert that this Option is a None to the optimizer. + unsafe fn unchecked_unwrap_none(self); +} + +/// An extension trait for `Result` providing unchecked unwrapping methods. +pub trait UncheckedResultExt { + /// Get the value out of this Result without checking for Err. + unsafe fn unchecked_unwrap_ok(self) -> T; + + /// Get the error out of this Result without checking for Ok. + unsafe fn unchecked_unwrap_err(self) -> E; +} + +impl UncheckedOptionExt for Option { + unsafe fn unchecked_unwrap(self) -> T { + match self { + Some(x) => x, + None => unreachable_unchecked(), + } + } + + unsafe fn unchecked_unwrap_none(self) { + if self.is_some() { + unreachable_unchecked() + } + } +} + +impl UncheckedResultExt for Result { + unsafe fn unchecked_unwrap_ok(self) -> T { + match self { + Ok(x) => x, + Err(_) => unreachable_unchecked(), + } + } + + unsafe fn unchecked_unwrap_err(self) -> E { + match self { + Ok(_) => unreachable_unchecked(), + Err(e) => e, + } + } +} -- cgit v1.2.3