Bug 1655699 - mach vendor rust. r=cubeb-reviewers,achronop

Depends on D85143

Differential Revision: https://phabricator.services.mozilla.com/D85144
This commit is contained in:
Paul Adenot 2020-07-28 12:42:45 +00:00
Родитель 837926bb15
Коммит 16a7ea803c
20 изменённых файлов: 2932 добавлений и 222 удалений

Просмотреть файл

@ -85,7 +85,7 @@ rev = "5e870faf6f95d79d11efc813e56370ad124bbed5"
[source."https://github.com/ChunMinChang/cubeb-coreaudio-rs"]
git = "https://github.com/ChunMinChang/cubeb-coreaudio-rs"
replace-with = "vendored-sources"
rev = "260be23d1166a22e2ff6a89844efaca8bb4b61e2"
rev = "b071bbe2119885605f64de6b63356a7501018841"
[source.crates-io]
replace-with = "vendored-sources"

20
Cargo.lock сгенерированный
Просмотреть файл

@ -467,6 +467,12 @@ dependencies = [
"ppv-lite86",
]
[[package]]
name = "cache-padded"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba"
[[package]]
name = "cascade_bloom_filter"
version = "0.1.0"
@ -720,7 +726,7 @@ dependencies = [
[[package]]
name = "coreaudio-sys-utils"
version = "0.1.0"
source = "git+https://github.com/ChunMinChang/cubeb-coreaudio-rs?rev=260be23d1166a22e2ff6a89844efaca8bb4b61e2#260be23d1166a22e2ff6a89844efaca8bb4b61e2"
source = "git+https://github.com/ChunMinChang/cubeb-coreaudio-rs?rev=b071bbe2119885605f64de6b63356a7501018841#b071bbe2119885605f64de6b63356a7501018841"
dependencies = [
"core-foundation-sys",
"coreaudio-sys",
@ -968,7 +974,7 @@ dependencies = [
[[package]]
name = "cubeb-coreaudio"
version = "0.1.0"
source = "git+https://github.com/ChunMinChang/cubeb-coreaudio-rs?rev=260be23d1166a22e2ff6a89844efaca8bb4b61e2#260be23d1166a22e2ff6a89844efaca8bb4b61e2"
source = "git+https://github.com/ChunMinChang/cubeb-coreaudio-rs?rev=b071bbe2119885605f64de6b63356a7501018841#b071bbe2119885605f64de6b63356a7501018841"
dependencies = [
"atomic",
"audio-mixer",
@ -980,6 +986,7 @@ dependencies = [
"libc",
"mach",
"ringbuf",
"triple_buffer",
]
[[package]]
@ -5177,6 +5184,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbbb439715d4c258415c7cbf137be32a4f54f7348724076145ede565ee2142e8"
[[package]]
name = "triple_buffer"
version = "5.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06577fa2229f6eff69f06ba2e08a27458f32f87a7985abb5047d7bd2e0006512"
dependencies = [
"cache-padded",
]
[[package]]
name = "try-lock"
version = "0.2.2"

1
third_party/rust/cache-padded/.cargo-checksum.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1 @@
{"files":{"CHANGELOG.md":"50d6fbb00aa801cffefd2ecbbb43bba8686925658b3bf5e304b5af735f90bf08","Cargo.toml":"7ff90cd0fd3bc7449b8f55dea7de4d7d51f7c11948119512a274369733a94321","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"106c91d873d7c2681ca6264195737d7f2ea73f91cc74dd4c200f3fe9c5ea4907","src/lib.rs":"d2b1a7280c5cb8c1e946a5839862a1496749c445851f7ed196debf8386a4e646","tests/padding.rs":"120cc65d1fcebbdb0ed8b0c287680ede2483e3ab040746e1a3616ffb39d8f414"},"package":"631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba"}

11
third_party/rust/cache-padded/CHANGELOG.md поставляемый Normal file
Просмотреть файл

@ -0,0 +1,11 @@
# Version 1.1.1
- Forbid unsafe code.
# Version 1.1.0
- Mark `CachePadded::new()` as const fn.
# Version 1.0.0
- Initial version

25
third_party/rust/cache-padded/Cargo.toml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,25 @@
# 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 believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
edition = "2018"
name = "cache-padded"
version = "1.1.1"
authors = ["Stjepan Glavina <stjepang@gmail.com>"]
description = "Prevent false sharing by padding and aligning to the length of a cache line"
homepage = "https://github.com/stjepang/cache-padded"
documentation = "https://docs.rs/cache-padded"
readme = "README.md"
keywords = ["cache", "padding", "lock-free", "atomic"]
categories = ["concurrency", "no-std"]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/stjepang/cache-padded"

201
third_party/rust/cache-padded/LICENSE-APACHE поставляемый Normal file
Просмотреть файл

@ -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.

23
third_party/rust/cache-padded/LICENSE-MIT поставляемый Normal file
Просмотреть файл

@ -0,0 +1,23 @@
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

79
third_party/rust/cache-padded/README.md поставляемый Normal file
Просмотреть файл

@ -0,0 +1,79 @@
# cache-padded
[![Build](https://github.com/stjepang/cache-padded/workflows/Build%20and%20test/badge.svg)](
https://github.com/stjepang/cache-padded/actions)
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](
https://github.com/stjepang/cache-padded)
[![Cargo](https://img.shields.io/crates/v/cache-padded.svg)](
https://crates.io/crates/cache-padded)
[![Documentation](https://docs.rs/cache-padded/badge.svg)](
https://docs.rs/cache-padded)
Prevent false sharing by padding and aligning to the length of a cache line.
In concurrent programming, sometimes it is desirable to make sure commonly accessed shared data
is not all placed into the same cache line. Updating an atomic value invalides the whole cache
line it belongs to, which makes the next access to the same cache line slower for other CPU
cores. Use `CachePadded` to ensure updating one piece of data doesn't invalidate other cached
data.
## Size and alignment
Cache lines are assumed to be N bytes long, depending on the architecture:
* On x86-64 and aarch64, N = 128.
* On all others, N = 64.
Note that N is just a reasonable guess and is not guaranteed to match the actual cache line
length of the machine the program is running on.
The size of `CachePadded<T>` is the smallest multiple of N bytes large enough to accommodate
a value of type `T`.
The alignment of `CachePadded<T>` is the maximum of N bytes and the alignment of `T`.
## Examples
Alignment and padding:
```rust
use cache_padded::CachePadded;
let array = [CachePadded::new(1i8), CachePadded::new(2i8)];
let addr1 = &*array[0] as *const i8 as usize;
let addr2 = &*array[1] as *const i8 as usize;
assert!(addr2 - addr1 >= 64);
assert_eq!(addr1 % 64, 0);
assert_eq!(addr2 % 64, 0);
```
When building a concurrent queue with a head and a tail index, it is wise to place indices in
different cache lines so that concurrent threads pushing and popping elements don't invalidate
each other's cache lines:
```rust
use cache_padded::CachePadded;
use std::sync::atomic::AtomicUsize;
struct Queue<T> {
head: CachePadded<AtomicUsize>,
tail: CachePadded<AtomicUsize>,
buffer: *mut T,
}
```
## 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.

137
third_party/rust/cache-padded/src/lib.rs поставляемый Normal file
Просмотреть файл

@ -0,0 +1,137 @@
//! Prevent false sharing by padding and aligning to the length of a cache line.
//!
//! In concurrent programming, sometimes it is desirable to make sure commonly accessed shared data
//! is not all placed into the same cache line. Updating an atomic value invalides the whole cache
//! line it belongs to, which makes the next access to the same cache line slower for other CPU
//! cores. Use [`CachePadded`] to ensure updating one piece of data doesn't invalidate other cached
//! data.
//!
//! # Size and alignment
//!
//! Cache lines are assumed to be N bytes long, depending on the architecture:
//!
//! * On x86-64 and aarch64, N = 128.
//! * On all others, N = 64.
//!
//! Note that N is just a reasonable guess and is not guaranteed to match the actual cache line
//! length of the machine the program is running on.
//!
//! The size of `CachePadded<T>` is the smallest multiple of N bytes large enough to accommodate
//! a value of type `T`.
//!
//! The alignment of `CachePadded<T>` is the maximum of N bytes and the alignment of `T`.
//!
//! # Examples
//!
//! Alignment and padding:
//!
//! ```
//! use cache_padded::CachePadded;
//!
//! let array = [CachePadded::new(1i8), CachePadded::new(2i8)];
//! let addr1 = &*array[0] as *const i8 as usize;
//! let addr2 = &*array[1] as *const i8 as usize;
//!
//! assert!(addr2 - addr1 >= 64);
//! assert_eq!(addr1 % 64, 0);
//! assert_eq!(addr2 % 64, 0);
//! ```
//!
//! When building a concurrent queue with a head and a tail index, it is wise to place indices in
//! different cache lines so that concurrent threads pushing and popping elements don't invalidate
//! each other's cache lines:
//!
//! ```
//! use cache_padded::CachePadded;
//! use std::sync::atomic::AtomicUsize;
//!
//! struct Queue<T> {
//! head: CachePadded<AtomicUsize>,
//! tail: CachePadded<AtomicUsize>,
//! buffer: *mut T,
//! }
//! ```
#![no_std]
#![forbid(unsafe_code)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
use core::fmt;
use core::ops::{Deref, DerefMut};
/// Pads and aligns data to the length of a cache line.
// Starting from Intel's Sandy Bridge, spatial prefetcher is now pulling pairs of 64-byte cache
// lines at a time, so we have to align to 128 bytes rather than 64.
//
// Sources:
// - https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
// - https://github.com/facebook/folly/blob/1b5288e6eea6df074758f877c849b6e73bbb9fbb/folly/lang/Align.h#L107
//
// ARM's big.LITTLE architecture has asymmetric cores and "big" cores have 128-byte cache lines.
//
// Sources:
// - https://www.mono-project.com/news/2016/09/12/arm64-icache/
//
#[cfg_attr(any(target_arch = "x86_64", target_arch = "aarch64"), repr(align(128)))]
#[cfg_attr(
not(any(target_arch = "x86_64", target_arch = "aarch64")),
repr(align(64))
)]
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)]
pub struct CachePadded<T>(T);
impl<T> CachePadded<T> {
/// Pads and aligns a piece of data to the length of a cache line.
///
/// # Examples
///
/// ```
/// use cache_padded::CachePadded;
///
/// let padded = CachePadded::new(1);
/// ```
pub const fn new(t: T) -> CachePadded<T> {
CachePadded(t)
}
/// Returns the inner data.
///
/// # Examples
///
/// ```
/// use cache_padded::CachePadded;
///
/// let padded = CachePadded::new(7);
/// let data = padded.into_inner();
/// assert_eq!(data, 7);
/// ```
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> Deref for CachePadded<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
impl<T> DerefMut for CachePadded<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T: fmt::Debug> fmt::Debug for CachePadded<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("CachePadded").field(&self.0).finish()
}
}
impl<T> From<T> for CachePadded<T> {
fn from(t: T) -> Self {
CachePadded::new(t)
}
}

107
third_party/rust/cache-padded/tests/padding.rs поставляемый Normal file
Просмотреть файл

@ -0,0 +1,107 @@
use std::cell::Cell;
use std::mem;
use cache_padded::CachePadded;
#[test]
fn default() {
let x: CachePadded<u64> = Default::default();
assert_eq!(*x, 0);
}
#[test]
fn store_u64() {
let x: CachePadded<u64> = CachePadded::new(17);
assert_eq!(*x, 17);
}
#[test]
fn store_pair() {
let x: CachePadded<(u64, u64)> = CachePadded::new((17, 37));
assert_eq!(x.0, 17);
assert_eq!(x.1, 37);
}
#[test]
fn distance() {
let arr = [CachePadded::new(17u8), CachePadded::new(37u8)];
let a = &*arr[0] as *const u8;
let b = &*arr[1] as *const u8;
assert!(unsafe { a.offset(64) } <= b);
}
#[test]
fn different_sizes() {
CachePadded::new(17u8);
CachePadded::new(17u16);
CachePadded::new(17u32);
CachePadded::new([17u64; 0]);
CachePadded::new([17u64; 1]);
CachePadded::new([17u64; 2]);
CachePadded::new([17u64; 3]);
CachePadded::new([17u64; 4]);
CachePadded::new([17u64; 5]);
CachePadded::new([17u64; 6]);
CachePadded::new([17u64; 7]);
CachePadded::new([17u64; 8]);
}
#[test]
fn large() {
let a = [17u64; 9];
let b = CachePadded::new(a);
assert!(mem::size_of_val(&a) <= mem::size_of_val(&b));
}
#[test]
fn debug() {
assert_eq!(format!("{:?}", CachePadded::new(17u64)), "CachePadded(17)");
}
#[test]
fn drops() {
let count = Cell::new(0);
struct Foo<'a>(&'a Cell<usize>);
impl<'a> Drop for Foo<'a> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
let a = CachePadded::new(Foo(&count));
let b = CachePadded::new(Foo(&count));
assert_eq!(count.get(), 0);
drop(a);
assert_eq!(count.get(), 1);
drop(b);
assert_eq!(count.get(), 2);
}
#[test]
fn clone() {
let a = CachePadded::new(17);
let b = a.clone();
assert_eq!(*a, *b);
}
#[test]
fn runs_custom_clone() {
let count = Cell::new(0);
struct Foo<'a>(&'a Cell<usize>);
impl<'a> Clone for Foo<'a> {
fn clone(&self) -> Foo<'a> {
self.0.set(self.0.get() + 1);
Foo::<'a>(self.0)
}
}
let a = CachePadded::new(Foo(&count));
let _ = a.clone();
assert_eq!(count.get(), 1);
}

Просмотреть файл

@ -1 +1 @@
{"files":{".editorconfig":"4e53b182bcc78b83d7e1b5c03efa14d22d4955c4ed2514d1ba4e99c1eb1a50ba",".githooks/pre-push":"8b8b26544cd56f54c0c33812551f786bb25cb08c86dbfeb6bf3daad881c826a1",".travis.yml":"dc07bac53f70f16c9bdf52264bdc58500ae6018c1b4c567bc7642f6b4ca3cc35","Cargo.toml":"93449267612163fe621c75c6195fcf5f961c2bddd975468d67fc2121d538f1c7","LICENSE":"6e6f56aff5bbf3cbc60747e152fb1a719bd0716aaf6d711c554f57d92e96297c","README.md":"d420509677fc444eb2ad8542e1e0e54cec800eb0fbbe50ba0ae2f89ac687c2a5","install_git_hook.sh":"d38c8e51e636f6b90b489621ac34ccd1d1b1f40dccce3d178ed1da1c5068f16d","install_rustfmt_clippy.sh":"4ae90d8dcb9757cb3ae4ae142ef80e5377c0dde61c63f4a3c32418646e80ca7b","run_device_tests.sh":"4b7d65eb638c1a278ffc8ecb6d30d47b3b8405392e976cae38c6f744e2bed532","run_sanitizers.sh":"2f0934ba01cbcd74485f19d50147f6b604cf9730bbd3a3d3f3d958e40d0f799f","run_tests.sh":"3dd76659f6dceeb0490dd92b355e113301ba0d0a8f034993a56f40e09edd25b2","src/backend/aggregate_device.rs":"ae21129aa6b3c7bd3376751b6a94d1ebe6c9f7afcd1db3107fb4d703d04da6b3","src/backend/auto_release.rs":"050fdcee74cf46b9a8a85a877e166d72a853d33220f59cf734cbb6ea09daa441","src/backend/buffer_manager.rs":"9c10a261792e32e75833b5f976b18547c338ca6beb2330eeab1ad203cc8c32bf","src/backend/device_property.rs":"d43642ea6e5f40e29c2a59ec7d81b42c154134685e417585045785359aa31686","src/backend/mixer.rs":"14e2156a8c1aeabcd4adb3336c3c9401b9c8526ec82a8c78942af7a79648f0f8","src/backend/mod.rs":"97d579a3cc0141b33c5f7c5362685bb44620fdd2aa8b22af164823c59c3a9aa4","src/backend/resampler.rs":"fd1281d28a4db1659d2f75e43b8457651745e1b6eb5a53a77f04d752135f6dc7","src/backend/tests/aggregate_device.rs":"107f5c637844cd5ae43d2b42cec4ef3369bb702751586078c0a9d50f039161cd","src/backend/tests/api.rs":"9ce44a867519d7b7a2b43c7f833327c35be38af7ba6fcc3d277ed1d7d8e7c8c2","src/backend/tests/backlog.rs":"3b189a7e036543c467cc242af0ed3332721179ee2b1c8847a6db563546f1ac52","src/backend/tests/device_change.rs":"8261f561f69dabd374ac47d69aa484812b65070a9e9581dfb2605e8404eaad6d","src/backend/tests/device_property.rs":"373f76d3bee83b263db3f02be3b94b408bdf852d84e4b5153273fda34b11a374","src/backend/tests/interfaces.rs":"14943e84a79976a7ef52882edeb9330350705d5190db6647f98b4ffa851a8396","src/backend/tests/manual.rs":"87210af9527feece99ad2b7b85651fbc8f02ec306ba8082da0c0705070e882af","src/backend/tests/mod.rs":"8dba770023d7f9c4228f0e11915347f0e07da5fd818e3ee4478c4b197af9aa2a","src/backend/tests/parallel.rs":"f9e1883660d6146b6e5075806561f5f689810e25c5e7764dfd28c9b939821a49","src/backend/tests/tone.rs":"16150438317ce501986734167b5fb97bfec567228acbcd8f3b4c4484c22f29e0","src/backend/tests/utils.rs":"1bb99ef71d3c18545bca49767e7b6bfffbe11896246994f41be7ed372772fd48","src/backend/utils.rs":"5ce1b753af0ffb654b6b2038d649aea88eebd27390a607a6d5988a9e40a4a717","src/capi.rs":"21b66b70545bf04ec719928004d1d9adb45b24ced51288f5b2993d79aaf78f5f","src/lib.rs":"5e586d45cd6b3722f0a6736d9252593299269817a153eef1930a5fb9bfbb56f5","todo.md":"b1700be0fd28f8f226ddcbff59f844ca57e1d225f21ca1d6aa613d6269da6f2c"},"package":null}
{"files":{".editorconfig":"4e53b182bcc78b83d7e1b5c03efa14d22d4955c4ed2514d1ba4e99c1eb1a50ba",".githooks/pre-push":"8b8b26544cd56f54c0c33812551f786bb25cb08c86dbfeb6bf3daad881c826a1",".travis.yml":"dc07bac53f70f16c9bdf52264bdc58500ae6018c1b4c567bc7642f6b4ca3cc35","Cargo.toml":"4a25bbb1d243aedb93ec65197aa1312c1995fc466b2c707663d3f4dc8340e851","LICENSE":"6e6f56aff5bbf3cbc60747e152fb1a719bd0716aaf6d711c554f57d92e96297c","README.md":"d420509677fc444eb2ad8542e1e0e54cec800eb0fbbe50ba0ae2f89ac687c2a5","install_git_hook.sh":"d38c8e51e636f6b90b489621ac34ccd1d1b1f40dccce3d178ed1da1c5068f16d","install_rustfmt_clippy.sh":"4ae90d8dcb9757cb3ae4ae142ef80e5377c0dde61c63f4a3c32418646e80ca7b","run_device_tests.sh":"4b7d65eb638c1a278ffc8ecb6d30d47b3b8405392e976cae38c6f744e2bed532","run_sanitizers.sh":"2f0934ba01cbcd74485f19d50147f6b604cf9730bbd3a3d3f3d958e40d0f799f","run_tests.sh":"3dd76659f6dceeb0490dd92b355e113301ba0d0a8f034993a56f40e09edd25b2","src/backend/aggregate_device.rs":"ae21129aa6b3c7bd3376751b6a94d1ebe6c9f7afcd1db3107fb4d703d04da6b3","src/backend/auto_release.rs":"050fdcee74cf46b9a8a85a877e166d72a853d33220f59cf734cbb6ea09daa441","src/backend/buffer_manager.rs":"9c10a261792e32e75833b5f976b18547c338ca6beb2330eeab1ad203cc8c32bf","src/backend/device_property.rs":"d43642ea6e5f40e29c2a59ec7d81b42c154134685e417585045785359aa31686","src/backend/mixer.rs":"14e2156a8c1aeabcd4adb3336c3c9401b9c8526ec82a8c78942af7a79648f0f8","src/backend/mod.rs":"34ca5d506f7da10edf4df74155b8abc5f7815e7d67e52b9d4ecbbc00d569985e","src/backend/resampler.rs":"fd1281d28a4db1659d2f75e43b8457651745e1b6eb5a53a77f04d752135f6dc7","src/backend/tests/aggregate_device.rs":"107f5c637844cd5ae43d2b42cec4ef3369bb702751586078c0a9d50f039161cd","src/backend/tests/api.rs":"9ce44a867519d7b7a2b43c7f833327c35be38af7ba6fcc3d277ed1d7d8e7c8c2","src/backend/tests/backlog.rs":"3b189a7e036543c467cc242af0ed3332721179ee2b1c8847a6db563546f1ac52","src/backend/tests/device_change.rs":"8261f561f69dabd374ac47d69aa484812b65070a9e9581dfb2605e8404eaad6d","src/backend/tests/device_property.rs":"373f76d3bee83b263db3f02be3b94b408bdf852d84e4b5153273fda34b11a374","src/backend/tests/interfaces.rs":"14943e84a79976a7ef52882edeb9330350705d5190db6647f98b4ffa851a8396","src/backend/tests/manual.rs":"87210af9527feece99ad2b7b85651fbc8f02ec306ba8082da0c0705070e882af","src/backend/tests/mod.rs":"8dba770023d7f9c4228f0e11915347f0e07da5fd818e3ee4478c4b197af9aa2a","src/backend/tests/parallel.rs":"f9e1883660d6146b6e5075806561f5f689810e25c5e7764dfd28c9b939821a49","src/backend/tests/tone.rs":"fadf78543950249cccd64d6d42ca41cd58c68d4a4c2dc656d474da6a44eb550e","src/backend/tests/utils.rs":"1bb99ef71d3c18545bca49767e7b6bfffbe11896246994f41be7ed372772fd48","src/backend/utils.rs":"5ce1b753af0ffb654b6b2038d649aea88eebd27390a607a6d5988a9e40a4a717","src/capi.rs":"21b66b70545bf04ec719928004d1d9adb45b24ced51288f5b2993d79aaf78f5f","src/lib.rs":"5e586d45cd6b3722f0a6736d9252593299269817a153eef1930a5fb9bfbb56f5","todo.md":"b1700be0fd28f8f226ddcbff59f844ca57e1d225f21ca1d6aa613d6269da6f2c"},"package":null}

1
third_party/rust/cubeb-coreaudio/Cargo.toml поставляемый
Просмотреть файл

@ -18,3 +18,4 @@ lazy_static = "1.2"
mach = "0.3"
audio-mixer = "0.1"
ringbuf = "0.2"
triple_buffer = "5.0.5"

Просмотреть файл

@ -43,7 +43,7 @@ use std::mem;
use std::os::raw::c_void;
use std::ptr;
use std::slice;
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;
const NO_ERR: OSStatus = 0;
@ -345,13 +345,14 @@ extern "C" fn audiounit_input_callback(
let stm = unsafe { &mut *(user_ptr as *mut AudioUnitStream) };
if unsafe { *flags | kAudioTimeStampHostTimeValid } != 0 {
let input_latency_frames = compute_input_latency(&stm, unsafe { (*tstamp).mHostTime });
let now = unsafe { mach_absolute_time() };
let input_latency_frames = compute_input_latency(&stm, unsafe { (*tstamp).mHostTime }, now);
stm.total_input_latency_frames
.store(input_latency_frames, Ordering::SeqCst);
}
if stm.shutdown.load(Ordering::SeqCst) {
cubeb_log!("({:p}) input shutdown", stm as *const AudioUnitStream);
if stm.stopped.load(Ordering::SeqCst) {
cubeb_log!("({:p}) input stopped", stm as *const AudioUnitStream);
return NO_ERR;
}
@ -493,42 +494,30 @@ fn host_time_to_ns(host_time: u64) -> u64 {
rv as u64
}
fn compute_output_latency(stm: &AudioUnitStream, host_time: u64) -> u32 {
fn compute_output_latency(stm: &AudioUnitStream, host_time: u64, now: u64) -> u32 {
const NS2S: u64 = 1_000_000_000;
let now = host_time_to_ns(unsafe { mach_absolute_time() });
let audio_output_time = host_time_to_ns(host_time);
let output_hw_rate = stm.core_stream_data.output_hw_rate as u64;
let fixed_latency_ns =
(stm.current_output_latency_frames.load(Ordering::SeqCst) as u64 * NS2S) / output_hw_rate;
let total_output_latency_ns = if audio_output_time < now {
0
} else {
(stm.output_device_latency_frames.load(Ordering::SeqCst) as u64 * NS2S) / output_hw_rate;
// The total output latency is the timestamp difference + the stream latency + the hardware
// latency.
(audio_output_time - now) + fixed_latency_ns
};
let total_output_latency_ns = fixed_latency_ns + audio_output_time.saturating_sub(now);
((total_output_latency_ns * output_hw_rate) / NS2S) as u32
(total_output_latency_ns * output_hw_rate / NS2S) as u32
}
fn compute_input_latency(stm: &AudioUnitStream, host_time: u64) -> u32 {
fn compute_input_latency(stm: &AudioUnitStream, host_time: u64, now: u64) -> u32 {
const NS2S: u64 = 1_000_000_000;
let now = host_time_to_ns(unsafe { mach_absolute_time() });
let audio_input_time = host_time_to_ns(host_time);
let input_hw_rate = stm.core_stream_data.input_hw_rate as u64;
let fixed_latency_ns =
(stm.current_input_latency_frames.load(Ordering::SeqCst) as u64 * NS2S) / input_hw_rate;
let total_input_latency_ns = if audio_input_time > now {
0
} else {
(stm.input_device_latency_frames.load(Ordering::SeqCst) as u64 * NS2S) / input_hw_rate;
// The total input latency is the timestamp difference + the stream latency +
// the hardware latency.
(now - audio_input_time) + fixed_latency_ns
};
let total_input_latency_ns = now.saturating_sub(audio_input_time) + fixed_latency_ns;
((total_input_latency_ns * input_hw_rate) / NS2S) as u32
(total_input_latency_ns * input_hw_rate / NS2S) as u32
}
extern "C" fn audiounit_output_callback(
@ -547,29 +536,14 @@ extern "C" fn audiounit_output_callback(
let out_buffer_list_ref = unsafe { &mut (*out_buffer_list) };
assert_eq!(out_buffer_list_ref.mNumberBuffers, 1);
let mut buffers = unsafe {
let buffers = unsafe {
let ptr = out_buffer_list_ref.mBuffers.as_mut_ptr();
let len = out_buffer_list_ref.mNumberBuffers as usize;
slice::from_raw_parts_mut(ptr, len)
};
if unsafe { *flags | kAudioTimeStampHostTimeValid } != 0 {
let output_latency_frames = compute_output_latency(&stm, unsafe { (*tstamp).mHostTime });
stm.total_output_latency_frames
.store(output_latency_frames, Ordering::SeqCst);
}
cubeb_logv!(
"({:p}) output: buffers {}, size {}, channels {}, frames {}.",
stm as *const AudioUnitStream,
buffers.len(),
buffers[0].mDataByteSize,
buffers[0].mNumberChannels,
output_frames
);
if stm.shutdown.load(Ordering::SeqCst) {
cubeb_log!("({:p}) output shutdown.", stm as *const AudioUnitStream);
if stm.stopped.load(Ordering::SeqCst) {
cubeb_log!("({:p}) output stopped.", stm as *const AudioUnitStream);
audiounit_make_silent(&mut buffers[0]);
return NO_ERR;
}
@ -584,10 +558,24 @@ extern "C" fn audiounit_output_callback(
return NO_ERR;
}
let handler = |stm: &mut AudioUnitStream,
output_frames: u32,
buffers: &mut [AudioBuffer]|
-> (OSStatus, Option<State>) {
let now = unsafe { mach_absolute_time() };
if unsafe { *flags | kAudioTimeStampHostTimeValid } != 0 {
let output_latency_frames =
compute_output_latency(&stm, unsafe { (*tstamp).mHostTime }, now);
stm.total_output_latency_frames
.store(output_latency_frames, Ordering::SeqCst);
}
cubeb_logv!(
"({:p}) output: buffers {}, size {}, channels {}, frames {}.",
stm as *const AudioUnitStream,
buffers.len(),
buffers[0].mDataByteSize,
buffers[0].mNumberChannels,
output_frames
);
// Get output buffer
let output_buffer = match stm.core_stream_data.mixer.as_mut() {
None => buffers[0].mData,
@ -636,8 +624,6 @@ extern "C" fn audiounit_output_callback(
{
// The silent frames will be inserted in `get_linear_data` below.
let silent_frames_to_push = input_frames_needed - buffered_input_frames;
stm.frames_read
.fetch_add(input_frames_needed, Ordering::SeqCst);
cubeb_log!(
"({:p}) Missing Frames: {} will append {} frames of input silence.",
stm.core_stream_data.stm_ptr,
@ -649,6 +635,8 @@ extern "C" fn audiounit_output_callback(
},
silent_frames_to_push
);
stm.frames_read
.fetch_add(input_frames_needed, Ordering::SeqCst);
input_frames_needed
} else {
buffered_input_frames
@ -675,24 +663,29 @@ extern "C" fn audiounit_output_callback(
);
if outframes < 0 || outframes > i64::from(output_frames) {
stm.shutdown.store(true, Ordering::SeqCst);
stm.stopped.store(true, Ordering::SeqCst);
stm.core_stream_data.stop_audiounits();
audiounit_make_silent(&mut buffers[0]);
return (NO_ERR, Some(State::Error));
stm.notify_state_changed(State::Error);
return NO_ERR;
}
stm.draining
.store(outframes < i64::from(output_frames), Ordering::SeqCst);
stm.frames_played
.store(stm.frames_queued, atomic::Ordering::SeqCst);
stm.output_callback_timing_data_write
.write(OutputCallbackTimingData {
frames_queued: stm.frames_queued,
timestamp: now,
buffer_size: outframes as u64,
});
stm.frames_queued += outframes as u64;
// Post process output samples.
if stm.draining.load(Ordering::SeqCst) {
// Clear missing frames (silence)
let frames_to_bytes = |frames: usize| -> usize {
let sample_size =
cubeb_sample_size(stm.core_stream_data.output_stream_params.format());
let sample_size = cubeb_sample_size(stm.core_stream_data.output_stream_params.format());
let channel_count = stm.core_stream_data.output_stream_params.channels() as usize;
frames * sample_size * channel_count
};
@ -720,15 +713,7 @@ extern "C" fn audiounit_output_callback(
buffers[0].mDataByteSize as usize,
);
}
(NO_ERR, None)
};
let (status, notification) = handler(stm, output_frames, &mut buffers);
if let Some(state) = notification {
stm.notify_state_changed(state);
}
status
NO_ERR
}
#[allow(clippy::cognitive_complexity)]
@ -2843,7 +2828,7 @@ impl<'ctx> CoreStreamData<'ctx> {
return Err(Error::error());
}
stream.current_input_latency_frames.store(
stream.input_device_latency_frames.store(
get_fixed_latency(self.input_device.id, DeviceType::INPUT),
Ordering::SeqCst,
);
@ -2856,7 +2841,7 @@ impl<'ctx> CoreStreamData<'ctx> {
return Err(Error::error());
}
stream.current_output_latency_frames.store(
stream.output_device_latency_frames.store(
get_fixed_latency(self.output_device.id, DeviceType::OUTPUT),
Ordering::SeqCst,
);
@ -2872,7 +2857,7 @@ impl<'ctx> CoreStreamData<'ctx> {
&mut size,
) == NO_ERR
{
stream.current_output_latency_frames.fetch_add(
stream.output_device_latency_frames.fetch_add(
(unit_s * self.output_desc.mSampleRate) as u32,
Ordering::SeqCst,
);
@ -3119,6 +3104,13 @@ impl<'ctx> Drop for CoreStreamData<'ctx> {
}
}
#[derive(Debug, Clone)]
struct OutputCallbackTimingData {
frames_queued: u64,
timestamp: u64,
buffer_size: u64,
}
// The fisrt two members of the Cubeb stream must be a pointer to its Cubeb context and a void user
// defined pointer. The Cubeb interface use this assumption to operate the Cubeb APIs.
// #[repr(C)] is used to prevent any padding from being added in the beginning of the AudioUnitStream.
@ -3136,23 +3128,27 @@ struct AudioUnitStream<'ctx> {
state_callback: ffi::cubeb_state_callback,
device_changed_callback: Mutex<ffi::cubeb_device_changed_callback>,
// Frame counters
frames_played: AtomicU64,
frames_queued: u64,
// How many frames got read from the input since the stream started (includes
// padded silence)
frames_read: AtomicUsize,
// How many frames got written to the output device since the stream started
frames_written: AtomicUsize,
shutdown: AtomicBool,
stopped: AtomicBool,
draining: AtomicBool,
reinit_pending: AtomicBool,
destroy_pending: AtomicBool,
// Latency requested by the user.
latency_frames: u32,
current_output_latency_frames: AtomicU32,
current_input_latency_frames: AtomicU32,
// Fixed latency, characteristic of the device.
output_device_latency_frames: AtomicU32,
input_device_latency_frames: AtomicU32,
// Total latency: the latency of the device + the OS latency
total_output_latency_frames: AtomicU32,
total_input_latency_frames: AtomicU32,
output_callback_timing_data_read: triple_buffer::Output<OutputCallbackTimingData>,
output_callback_timing_data_write: triple_buffer::Input<OutputCallbackTimingData>,
prev_position: u64,
// This is true if a device change callback is currently running.
switching_device: AtomicBool,
core_stream_data: CoreStreamData<'ctx>,
@ -3166,6 +3162,14 @@ impl<'ctx> AudioUnitStream<'ctx> {
state_callback: ffi::cubeb_state_callback,
latency_frames: u32,
) -> Self {
let output_callback_timing_data =
triple_buffer::TripleBuffer::new(OutputCallbackTimingData {
frames_queued: 0,
timestamp: 0,
buffer_size: 0,
});
let (output_callback_timing_data_write, output_callback_timing_data_read) =
output_callback_timing_data.split();
AudioUnitStream {
context,
user_ptr,
@ -3173,19 +3177,21 @@ impl<'ctx> AudioUnitStream<'ctx> {
data_callback,
state_callback,
device_changed_callback: Mutex::new(None),
frames_played: AtomicU64::new(0),
frames_queued: 0,
frames_read: AtomicUsize::new(0),
frames_written: AtomicUsize::new(0),
shutdown: AtomicBool::new(true),
stopped: AtomicBool::new(true),
draining: AtomicBool::new(false),
reinit_pending: AtomicBool::new(false),
destroy_pending: AtomicBool::new(false),
latency_frames,
current_output_latency_frames: AtomicU32::new(0),
current_input_latency_frames: AtomicU32::new(0),
output_device_latency_frames: AtomicU32::new(0),
input_device_latency_frames: AtomicU32::new(0),
total_output_latency_frames: AtomicU32::new(0),
total_input_latency_frames: AtomicU32::new(0),
output_callback_timing_data_write,
output_callback_timing_data_read,
prev_position: 0,
switching_device: AtomicBool::new(false),
core_stream_data: CoreStreamData::default(),
}
@ -3228,7 +3234,7 @@ impl<'ctx> AudioUnitStream<'ctx> {
// which locks a mutex inside CoreAudio framework, then this call will block the current
// thread until the callback is finished since this call asks to lock a mutex inside
// CoreAudio framework that is used by the data callback.
if !self.shutdown.load(Ordering::SeqCst) {
if !self.stopped.load(Ordering::SeqCst) {
self.core_stream_data.stop_audiounits();
}
@ -3308,7 +3314,7 @@ impl<'ctx> AudioUnitStream<'ctx> {
}
// If the stream was running, start it again.
if !self.shutdown.load(Ordering::SeqCst) {
if !self.stopped.load(Ordering::SeqCst) {
self.core_stream_data.start_audiounits().map_err(|e| {
cubeb_log!(
"({:p}) Start audiounit failed.",
@ -3402,9 +3408,9 @@ impl<'ctx> AudioUnitStream<'ctx> {
// which locks a mutex inside CoreAudio framework, then this call will block the current
// thread until the callback is finished since this call asks to lock a mutex inside
// CoreAudio framework that is used by the data callback.
if !self.shutdown.load(Ordering::SeqCst) {
if !self.stopped.load(Ordering::SeqCst) {
self.core_stream_data.stop_audiounits();
self.shutdown.store(true, Ordering::SeqCst);
self.stopped.store(true, Ordering::SeqCst);
}
self.destroy_internal();
@ -3422,7 +3428,7 @@ impl<'ctx> Drop for AudioUnitStream<'ctx> {
impl<'ctx> StreamOps for AudioUnitStream<'ctx> {
fn start(&mut self) -> Result<()> {
self.shutdown.store(false, Ordering::SeqCst);
self.stopped.store(false, Ordering::SeqCst);
self.draining.store(false, Ordering::SeqCst);
// Execute start in serial queue to avoid racing with destroy or reinit.
@ -3446,7 +3452,7 @@ impl<'ctx> StreamOps for AudioUnitStream<'ctx> {
Ok(())
}
fn stop(&mut self) -> Result<()> {
self.shutdown.store(true, Ordering::SeqCst);
self.stopped.store(true, Ordering::SeqCst);
// Execute stop in serial queue to avoid racing with destroy or reinit.
let stream = &self;
@ -3466,18 +3472,42 @@ impl<'ctx> StreamOps for AudioUnitStream<'ctx> {
Err(Error::not_supported())
}
fn position(&mut self) -> Result<u64> {
let current_output_latency_frames =
u64::from(self.current_output_latency_frames.load(Ordering::SeqCst));
let frames_played = self.frames_played.load(Ordering::SeqCst);
if current_output_latency_frames != 0 {
let position = if current_output_latency_frames > frames_played {
let OutputCallbackTimingData {
frames_queued,
timestamp,
buffer_size,
} = self.output_callback_timing_data_read.read().clone();
let total_output_latency_frames =
u64::from(self.total_output_latency_frames.load(Ordering::SeqCst));
// If output latency is available, take it into account. Otherwise, use the number of
// frames played.
let position = if total_output_latency_frames != 0 {
if total_output_latency_frames > frames_queued {
0
} else {
frames_played - current_output_latency_frames
};
return Ok(position);
// Interpolate here to match other cubeb backends. Only return an interpolated time
// if we've played enough frames. If the stream is paused, clamp the interpolated
// number of frames to the buffer size.
const NS2S: u64 = 1_000_000_000;
let now = unsafe { mach_absolute_time() };
let diff = now - timestamp;
let interpolated_frames = cmp::min(
host_time_to_ns(diff)
* self.core_stream_data.output_stream_params.rate() as u64
/ NS2S,
buffer_size,
);
(frames_queued - total_output_latency_frames) + interpolated_frames
}
Ok(frames_played)
} else {
frames_queued
};
// Ensure mononicity of the clock even when changing output device.
if position > self.prev_position {
self.prev_position = position;
}
Ok(self.prev_position)
}
#[cfg(target_os = "ios")]
fn latency(&mut self) -> Result<u32> {

Просмотреть файл

@ -1,5 +1,6 @@
use super::utils::{test_get_default_device, test_ops_stream_operation, Scope};
use super::*;
use std::sync::atomic::{AtomicI64, Ordering};
#[test]
fn test_dial_tone() {
@ -24,8 +25,15 @@ fn test_dial_tone() {
output_params.layout = ffi::CUBEB_LAYOUT_MONO;
output_params.prefs = ffi::CUBEB_STREAM_PREF_NONE;
// Used to calculate the tone's wave.
let mut position: i64 = 0; // TODO: Use Atomic instead.
struct Closure {
buffer_size: AtomicI64,
phase: i64,
};
let mut closure = Closure {
buffer_size: AtomicI64::new(0),
phase: 0,
};
let closure_ptr = &mut closure as *mut Closure as *mut c_void;
test_ops_stream_operation(
"stream: North American dial tone",
@ -36,10 +44,125 @@ fn test_dial_tone() {
4096, // TODO: Get latency by get_min_latency instead ?
Some(data_callback),
Some(state_callback),
&mut position as *mut i64 as *mut c_void,
closure_ptr,
|stream| {
assert_eq!(unsafe { OPS.stream_start.unwrap()(stream) }, ffi::CUBEB_OK);
thread::sleep(Duration::from_millis(500));
#[derive(Debug)]
enum State {
WaitingForStart,
PositionIncreasing,
Paused,
Resumed,
End,
};
let mut state = State::WaitingForStart;
let mut position: u64 = 0;
let mut prev_position: u64 = 0;
let mut count = 0;
const CHECK_COUNT: i32 = 10;
loop {
thread::sleep(Duration::from_millis(50));
assert_eq!(
unsafe { OPS.stream_get_position.unwrap()(stream, &mut position) },
ffi::CUBEB_OK
);
println!(
"State: {:?}, position: {}, previous position: {}",
state, position, prev_position
);
match &mut state {
State::WaitingForStart => {
// It's expected to have 0 for a few iterations here: the stream can take
// some time to start.
if position != prev_position {
assert!(position > prev_position);
prev_position = position;
state = State::PositionIncreasing;
}
}
State::PositionIncreasing => {
// wait a few iterations, check monotony
if position != prev_position {
assert!(position > prev_position);
prev_position = position;
count += 1;
if count > CHECK_COUNT {
state = State::Paused;
count = 0;
assert_eq!(
unsafe { OPS.stream_stop.unwrap()(stream) },
ffi::CUBEB_OK
);
// Update the position once paused.
assert_eq!(
unsafe {
OPS.stream_get_position.unwrap()(stream, &mut position)
},
ffi::CUBEB_OK
);
prev_position = position;
}
}
}
State::Paused => {
// The cubeb_stream_stop call above should synchrously stop the callbacks,
// hence the clock, the assert below must always holds, modulo the client
// side interpolation.
assert!(
position == prev_position
|| position - prev_position
<= closure.buffer_size.load(Ordering::SeqCst) as u64
);
count += 1;
prev_position = position;
if count > CHECK_COUNT {
state = State::Resumed;
count = 0;
assert_eq!(unsafe { OPS.stream_start.unwrap()(stream) }, ffi::CUBEB_OK);
}
}
State::Resumed => {
// wait a few iterations, this can take some time to start
if position != prev_position {
assert!(position > prev_position);
prev_position = position;
count += 1;
if count > CHECK_COUNT {
state = State::End;
count = 0;
assert_eq!(
unsafe { OPS.stream_stop.unwrap()(stream) },
ffi::CUBEB_OK
);
assert_eq!(
unsafe {
OPS.stream_get_position.unwrap()(stream, &mut position)
},
ffi::CUBEB_OK
);
prev_position = position;
}
}
}
State::End => {
// The cubeb_stream_stop call above should synchrously stop the callbacks,
// hence the clock, the assert below must always holds, modulo the client
// side interpolation.
assert!(
position == prev_position
|| position - prev_position
<= closure.buffer_size.load(Ordering::SeqCst) as u64
);
if position == prev_position {
count += 1;
if count > CHECK_COUNT {
break;
}
}
}
}
}
assert_eq!(unsafe { OPS.stream_stop.unwrap()(stream) }, ffi::CUBEB_OK);
},
);
@ -71,14 +194,16 @@ fn test_dial_tone() {
slice::from_raw_parts_mut(ptr, len)
};
let position = unsafe { &mut *(user_ptr as *mut i64) };
let closure = unsafe { &mut *(user_ptr as *mut Closure) };
closure.buffer_size.store(nframes, Ordering::SeqCst);
// Generate tone on the fly.
for data in buffer.iter_mut() {
let t1 = (2.0 * PI * 350.0 * (*position) as f32 / SAMPLE_FREQUENCY as f32).sin();
let t2 = (2.0 * PI * 440.0 * (*position) as f32 / SAMPLE_FREQUENCY as f32).sin();
let t1 = (2.0 * PI * 350.0 * (closure.phase) as f32 / SAMPLE_FREQUENCY as f32).sin();
let t2 = (2.0 * PI * 440.0 * (closure.phase) as f32 / SAMPLE_FREQUENCY as f32).sin();
*data = f32_to_i16_sample(0.5 * (t1 + t2));
*position += 1;
closure.phase += 1;
}
nframes

1
third_party/rust/triple_buffer/.cargo-checksum.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1 @@
{"files":{"CHANGELOG.md":"878a0261b1281e00769cbec862fc6bd37a72280099ec1dd5504fdef12745b0b6","Cargo.toml":"0620bdb8ee9a324fa6c49c56802a2ee1a2d5a59b325b635ac314243d82507959","LICENSE":"4b89d4518bd135ab4ee154a7bce722246b57a98c3d7efc1a09409898160c2bd1","README.md":"662e345ee3319bb82a79afb6dd136c5c956007d35b9a2962cd1d743ee321c7c5","src/lib.rs":"eccdd71723a03ffb8a59e9f9740d408512f44f83d839724bfcb0c630149f140d"},"package":"06577fa2229f6eff69f06ba2e08a27458f32f87a7985abb5047d7bd2e0006512"}

254
third_party/rust/triple_buffer/CHANGELOG.md поставляемый Normal file
Просмотреть файл

@ -0,0 +1,254 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
_No unreleased changes in the pipeline at the moment._
## [5.0.5] - 2020-07-05
### Changed
- Use only cache-padded instead of the full crossbeam-utils crate
- Clean up CI config and cache Rust toolchain there
## [5.0.4] - 2020-02-10
### Added
- Add a changelog to the repository.
### Changed
- Deduplicate CI configuration some more.
### Fixed
- Drop now-unnecessary manual `rustfmt` configuration.
- Avoid false sharing of back-buffer information.
## [5.0.3] - 2020-02-07
### Changed
- Clean up and deduplicate GitHub Actions configuration.
- Tune down concurrent test speed to reduce CI false positives.
## [5.0.2] - 2020-01-29
### Changed
- Move continuous integration to GitHub Actions.
## [5.0.1] - 2019-11-07
### Fixed
- Update to current version of dependencies.
## [5.0.0] - 2019-04-12
### Changed
- Bump travis CI configuration to Ubuntu Xenial.
- Bump minimal supported Rust version to 1.34.0.
### Fixed
- Don't use an `usize` for buffer indices where an `u8` will suffice.
- Improve Rust API guidelines compliance.
## [4.0.1] - 2018-12-31
### Fixed
- Display `raw` feature documentation on docs.rs.
## [4.0.0] - 2018-12-18
### Changed
- Migrate to Rust 2018.
- Bump minimal supported Rust version to 1.31.0.
### Fixed
- Update to current version of dependencies.
- Start using Clippy and integrate it into continuous integration.
- Re-apply `rustfmt` coding style (was not in CI at the time...).
## [3.0.1] - 2018-08-27
### Fixed
- Make `testbench` a dev-dependency, as it's only used for tests and benchmarks.
## [3.0.0] - 2018-08-27
### Changed
- Buffers are now padded to the size of a cache line to reduce false sharing.
- Bump minimal supported Rust version to 1.26.0.
### Fixed
- Make `testbench` version requirement more explicit.
## [2.0.0] - 2018-02-11
### Changed
- Switch license to MPLv2, which is a better match to Rust's static linking
philosophy than LGPL.
## [1.1.1] - 2017-11-19
### Fixed
- Fix my understanding of Cargo features & make the `raw` feature actually work.
## [1.1.0] - 2017-11-18
### Added
- Allow in-place writes on the input and output side, at the cost of stronger
synchronization barriers, through use of the `raw` Cargo feature.
### Fixed
- Do not require a `Clone` bound on the inner data.
## [1.0.0] - 2017-11-10
### Changed
- Simplify component naming convention, e.g. `TripleBufferInput` -> `Input`.
## [0.3.4] - 2017-06-25
### Changed
- Use `testbench::RaceCell` as an improved form of data race detection in tests.
### Fixed
- Do not require a `PartialEq` bound on the inner data.
## [0.3.3] - 2017-06-15
### Changed
- Tune down concurrent test speed to reduce CI false positives.
## [0.3.2] - 2017-06-15
### Changed
- Tune down concurrent test speed to reduce CI false positives.
## [0.3.1] - 2017-06-15
### Changed
- Tune down concurrent test speed to reduce CI false positives.
## [0.3.0] - 2017-06-14
### Added
- Introduce Travis CI continuous integration.
### Fixed
- Use CI to clarify minimal supported Rust version (currently 1.12.0).
## [0.2.4] - 2017-04-04
### Changed
- Use `testbench` crate for concurrent testing and benchmarking.
## [0.2.3] - 2017-03-24
### Changed
- More detailed comparison with other synchronization primitives in README.
### Fixed
- Adopt `rustfmt` coding style.
## [0.2.2] - 2017-03-20
### Changed
- Reduce reliance on Acquire-Release synchronization.
## [0.2.1] - 2017-03-11
### Changed
- Make README a bit more spambot-proof.
## [0.2.0] - 2017-03-11
### Added
- First tagged release of triple-buffer.
[Unreleased]: https://github.com/HadrienG2/triple-buffer/compare/v5.0.5...HEAD
[5.0.4]: https://github.com/HadrienG2/triple-buffer/compare/v5.0.4...v5.0.5
[5.0.4]: https://github.com/HadrienG2/triple-buffer/compare/v5.0.3...v5.0.4
[5.0.3]: https://github.com/HadrienG2/triple-buffer/compare/v5.0.2...v5.0.3
[5.0.2]: https://github.com/HadrienG2/triple-buffer/compare/v5.0.1...v5.0.2
[5.0.1]: https://github.com/HadrienG2/triple-buffer/compare/v5.0.0...v5.0.1
[5.0.0]: https://github.com/HadrienG2/triple-buffer/compare/v4.0.1...v5.0.0
[4.0.1]: https://github.com/HadrienG2/triple-buffer/compare/v4.0.0...v4.0.1
[4.0.0]: https://github.com/HadrienG2/triple-buffer/compare/v3.0.1...v4.0.0
[3.0.1]: https://github.com/HadrienG2/triple-buffer/compare/v3.0.0...v3.0.1
[3.0.0]: https://github.com/HadrienG2/triple-buffer/compare/v2.0.0...v3.0.0
[2.0.0]: https://github.com/HadrienG2/triple-buffer/compare/v1.1.1...v2.0.0
[1.1.1]: https://github.com/HadrienG2/triple-buffer/compare/v1.1.0...v1.1.1
[1.1.0]: https://github.com/HadrienG2/triple-buffer/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/HadrienG2/triple-buffer/compare/v0.3.4...v1.0.0
[0.3.4]: https://github.com/HadrienG2/triple-buffer/compare/v0.3.3...v0.3.4
[0.3.3]: https://github.com/HadrienG2/triple-buffer/compare/v0.3.2...v0.3.3
[0.3.2]: https://github.com/HadrienG2/triple-buffer/compare/v0.3.1...v0.3.2
[0.3.1]: https://github.com/HadrienG2/triple-buffer/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/HadrienG2/triple-buffer/compare/v0.2.4...v0.3.0
[0.2.4]: https://github.com/HadrienG2/triple-buffer/compare/v0.2.3...v0.2.4
[0.2.3]: https://github.com/HadrienG2/triple-buffer/compare/v0.2.2...v0.2.3
[0.2.2]: https://github.com/HadrienG2/triple-buffer/compare/v0.2.1...v0.2.2
[0.2.1]: https://github.com/HadrienG2/triple-buffer/compare/v0.2.0...v0.2.1
[0.2.0]: https://github.com/HadrienG2/triple-buffer/releases/tag/v0.2.0

35
third_party/rust/triple_buffer/Cargo.toml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,35 @@
# 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 believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
edition = "2018"
name = "triple_buffer"
version = "5.0.5"
authors = ["Hadrien G. <knights_of_ni@gmx.com>"]
description = "An implementation of triple buffering, useful for sharing frequently updated data between threads"
documentation = "https://docs.rs/triple_buffer/"
readme = "README.md"
keywords = ["synchronization", "spsc", "multithreading", "non-blocking", "wait-free"]
categories = ["algorithms", "asynchronous", "concurrency", "data-structures"]
license = "MPL-2.0"
repository = "https://github.com/HadrienG2/triple-buffer"
[package.metadata.docs.rs]
all-features = true
[dependencies.cache-padded]
version = "1.1"
[dev-dependencies.testbench]
version = "0.7"
[features]
raw = []
[badges.maintenance]
status = "passively-maintained"

373
third_party/rust/triple_buffer/LICENSE поставляемый Normal file
Просмотреть файл

@ -0,0 +1,373 @@
Mozilla Public License Version 2.0
==================================
1. Definitions
--------------
1.1. "Contributor"
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software.
1.2. "Contributor Version"
means the combination of the Contributions of others (if any) used
by a Contributor and that particular Contributor's Contribution.
1.3. "Contribution"
means Covered Software of a particular Contributor.
1.4. "Covered Software"
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
1.5. "Incompatible With Secondary Licenses"
means
(a) that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
(b) that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
1.6. "Executable Form"
means any form of the work other than Source Code Form.
1.7. "Larger Work"
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
1.8. "License"
means this document.
1.9. "Licensable"
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
1.10. "Modifications"
means any of the following:
(a) any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
(b) any new file in Source Code Form that contains any Covered
Software.
1.11. "Patent Claims" of a Contributor
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
1.12. "Secondary License"
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
1.13. "Source Code Form"
means the form of the work preferred for making modifications.
1.14. "You" (or "Your")
means an individual or a legal entity exercising rights under this
License. For legal entities, "You" includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, "control" means (a) the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or (b) ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
2. License Grants and Conditions
--------------------------------
2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free,
non-exclusive license:
(a) under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
(b) under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
(a) for any code that a Contributor has removed from Covered Software;
or
(b) for infringements caused by: (i) Your and any other third party's
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor
Version); or
(c) under Patent Claims infringed by Covered Software in the absence of
its Contributions.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with
the notice requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
3. Responsibilities
-------------------
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
(b) You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
4. Inability to Comply Due to Statute or Regulation
---------------------------------------------------
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
5. Termination
--------------
5.1. The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated (a) provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and (b) on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
5.2. If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
************************************************************************
* *
* 6. Disclaimer of Warranty *
* ------------------------- *
* *
* Covered Software is provided under this License on an "as is" *
* basis, without warranty of any kind, either expressed, implied, or *
* statutory, including, without limitation, warranties that the *
* Covered Software is free of defects, merchantable, fit for a *
* particular purpose or non-infringing. The entire risk as to the *
* quality and performance of the Covered Software is with You. *
* Should any Covered Software prove defective in any respect, You *
* (not any Contributor) assume the cost of any necessary servicing, *
* repair, or correction. This disclaimer of warranty constitutes an *
* essential part of this License. No use of any Covered Software is *
* authorized under this License except under this disclaimer. *
* *
************************************************************************
************************************************************************
* *
* 7. Limitation of Liability *
* -------------------------- *
* *
* Under no circumstances and under no legal theory, whether tort *
* (including negligence), contract, or otherwise, shall any *
* Contributor, or anyone who distributes Covered Software as *
* permitted above, be liable to You for any direct, indirect, *
* special, incidental, or consequential damages of any character *
* including, without limitation, damages for lost profits, loss of *
* goodwill, work stoppage, computer failure or malfunction, or any *
* and all other commercial damages or losses, even if such party *
* shall have been informed of the possibility of such damages. This *
* limitation of liability shall not apply to liability for death or *
* personal injury resulting from such party's negligence to the *
* extent applicable law prohibits such limitation. Some *
* jurisdictions do not allow the exclusion or limitation of *
* incidental or consequential damages, so this exclusion and *
* limitation may not apply to You. *
* *
************************************************************************
8. Litigation
-------------
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
9. Miscellaneous
----------------
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
10. Versions of the License
---------------------------
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary
Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
Exhibit A - Source Code Form License Notice
-------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
---------------------------------------------------------
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.

199
third_party/rust/triple_buffer/README.md поставляемый Normal file
Просмотреть файл

@ -0,0 +1,199 @@
# Triple buffering in Rust
[![On crates.io](https://img.shields.io/crates/v/triple_buffer.svg)](https://crates.io/crates/triple_buffer)
[![On docs.rs](https://docs.rs/triple_buffer/badge.svg)](https://docs.rs/triple_buffer/)
[![Continuous Integration](https://github.com/HadrienG2/triple-buffer/workflows/Continuous%20Integration/badge.svg)](https://github.com/HadrienG2/triple-buffer/actions?query=workflow%3A%22Continuous+Integration%22)
![Requires rustc 1.34+](https://img.shields.io/badge/rustc-1.34+-red.svg)
## What is this?
This is an implementation of triple buffering written in Rust. You may find it
useful for the following class of thread synchronization problems:
- There is one producer thread and one consumer thread
- The producer wants to update a shared memory value periodically
- The consumer wants to access the latest update from the producer at any time
The simplest way to use it is as follows:
```rust
// Create a triple buffer:
let buf = TripleBuffer::new(0);
// Split it into an input and output interface, to be respectively sent to
// the producer thread and the consumer thread:
let (mut buf_input, mut buf_output) = buf.split();
// The producer can move a value into the buffer at any time
buf_input.write(42);
// The consumer can access the latest value from the producer at any time
let latest_value_ref = buf_output.read();
assert_eq!(*latest_value_ref, 42);
```
In situations where moving the original value away and being unable to
modify it after the fact is too costly, such as if creating a new value
involves dynamic memory allocation, you can opt into the lower-level "raw"
interface, which allows you to access the buffer's data in place and
precisely control when updates are propagated.
This data access method is more error-prone and comes at a small performance
cost, which is why you will need to enable it explicitly using the "raw"
[cargo feature](http://doc.crates.io/manifest.html#usage-in-end-products).
```rust
// Create and split a triple buffer
use triple_buffer::TripleBuffer;
let buf = TripleBuffer::new(String::with_capacity(42));
let (mut buf_input, mut buf_output) = buf.split();
// Mutate the input buffer in place
{
// Acquire a reference to the input buffer
let raw_input = buf_input.raw_input_buffer();
// In general, you don't know what's inside of the buffer, so you should
// always reset the value before use (this is a type-specific process).
raw_input.clear();
// Perform an in-place update
raw_input.push_str("Hello, ");
}
// Publish the input buffer update
buf_input.raw_publish();
// Manually fetch the buffer update from the consumer interface
buf_output.raw_update();
// Acquire a mutable reference to the output buffer
let raw_output = buf_output.raw_output_buffer();
// Post-process the output value before use
raw_output.push_str("world!");
```
## Give me details! How does it compare to alternatives?
Compared to a mutex:
- Only works in single-producer, single-consumer scenarios
- Is nonblocking, and more precisely bounded wait-free. Concurrent accesses will
be slowed down by cache contention, but no deadlock, livelock, or thread
scheduling induced slowdown is possible.
- Allows the producer and consumer to work simultaneously
- Uses a lot more memory (3x payload + 3x bytes vs 1x payload + 1 bool)
- Does not allow in-place updates, as the producer and consumer do not access
the same memory location
- Should be slower if updates are rare and in-place updates are much more
efficient than moves, faster otherwise.
Compared to the read-copy-update (RCU) primitive from the Linux kernel:
- Only works in single-producer, single-consumer scenarios
- Has higher dirty read overhead on relaxed-memory architectures (ARM, POWER...)
- Does not require accounting for reader "grace periods": once the reader has
gotten access to the latest value, the synchronization transaction is over
- Does not use the inefficient compare-and-swap hardware primitive on update
- Does not suffer from the ABA problem, allowing much simpler code
- Allocates memory on initialization only, rather than on every update
- May use more memory (3x payload + 3x bytes vs 1x pointer + amount of
payloads and refcounts that depends on the readout and update pattern)
- Should be slower if updates are rare, faster if updates are frequent
Compared to sending the updates on a message queue:
- Only works in single-producer, single-consumer scenarios (queues can work in
other scenarios, although the implementations are much less efficient)
- Consumer only has access to the latest state, not the previous ones
- Consumer does not *need* to get through every previous state
- Is nonblocking AND uses bounded amounts of memory (with queues, it's a choice)
- Can transmit information in a single move, rather than two
- Should be faster for any compatible use case
In short, triple buffering is what you're after in scenarios where a shared
memory location is updated frequently by a single writer, read by a single
reader who only wants the latest version, and you can spare some RAM.
- If you need multiple producers, look somewhere else
- If you need multiple consumers, you may be interested in my related "SPMC
buffer" work, which basically extends triple buffering to multiple consumers
- If you can't tolerate the RAM overhead or want to update the data in place,
try a Mutex instead (or possibly an RWLock)
- If the shared value is updated very rarely (e.g. every second), try an RCU
- If the consumer must get every update, try a message queue
## How do I know your unsafe lock-free code is working?
By running the tests, of course! Which is unfortunately currently harder than
I'd like it to be.
First of all, we have sequential tests, which are very thorough but obviously
do not check the lock-free/synchronization part. You run them as follows:
$ cargo test && cargo test --features raw
Then we have a concurrent test where a reader thread continuously observes the
values from a rate-limited writer thread, and makes sure that he can see every
single update without any incorrect value slipping in the middle.
This test is more important, but it is also harder to run because one must first
check some assumptions:
- The testing host must have at least 2 physical CPU cores to test all possible
race conditions
- No other code should be eating CPU in the background. Including other tests.
- As the proper writing rate is system-dependent, what is configured in this
test may not be appropriate for your machine.
Taking this and the relatively long run time (~10-20 s) into account, this test
is ignored by default.
Finally, we have benchmarks, which allow you to test how well the code is
performing on your machine. Because cargo bench has not yet landed in Stable
Rust, these benchmarks masquerade as tests, which make them a bit unpleasant to
run. I apologize for the inconvenience.
To run the concurrent test and the benchmarks, make sure no one is eating CPU in
the background and do:
$ cargo test --release -- --ignored --nocapture --test-threads=1
(As before, you can also test with `--features raw`)
Here is a guide to interpreting the benchmark results:
* `clean_read` measures the triple buffer readout time when the data has not
changed. It should be extremely fast (a couple of CPU clock cycles).
* `write` measures the amount of time it takes to write data in the triple
buffer when no one is reading.
* `write_and_dirty_read` performs a write as before, immediately followed by a
sequential read. To get the dirty read performance, substract the write time
from that result. Writes and dirty read should take comparable time.
* `concurrent_write` measures the write performance when a reader is
continuously reading. Expect significantly worse performance: lock-free
techniques can help against contention, but are not a panacea.
* `concurrent_read` measures the read performance when a writer is continuously
writing. Again, a significant hit is to be expected.
On an Intel Xeon E5-1620 v3 @ 3.50GHz, typical results are as follows:
* Write: 7.8 ns
* Clean read: 1.8 ns
* Dirty read: 9.3 ns
* Concurrent write: 45 ns
* Concurrent read: 126 ns
## License
This crate is distributed under the terms of the MPLv2 license. See the LICENSE
file for details.
More relaxed licensing (Apache, MIT, BSD...) may also be negociated, in
exchange of a financial contribution. Contact me for details at
knights_of_ni AT gmx DOTCOM.

1092
third_party/rust/triple_buffer/src/lib.rs поставляемый Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу