-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add methods for atomic access to Bytes #115
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"coverage_score": 84.8, | ||
"coverage_score": 85.4, | ||
"exclude_path": "mmap_windows.rs", | ||
"crate_features": "backend-mmap,backend-atomic" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
use std::sync::atomic::Ordering; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please help to add a license header? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
/// Objects that implement this trait must consist exclusively of atomic types | ||
/// from [`std::sync::atomic`](https://doc.rust-lang.org/std/sync/atomic/), except for | ||
/// [`AtomicPtr<T>`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicPtr.html) and | ||
/// [`AtomicBool`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html). | ||
pub unsafe trait AtomicInteger: Sync + Send { | ||
/// The raw value type associated with the atomic integer (i.e. `u16` for `AtomicU16`). | ||
type V; | ||
|
||
/// Create a new instance of `Self`. | ||
fn new(v: Self::V) -> Self; | ||
|
||
/// Loads a value from the atomic integer. | ||
fn load(&self, order: Ordering) -> Self::V; | ||
|
||
/// Stores a value into the atomic integer. | ||
fn store(&self, val: Self::V, order: Ordering); | ||
} | ||
|
||
macro_rules! impl_atomic_integer_ops { | ||
($T:path, $V:ty) => { | ||
unsafe impl AtomicInteger for $T { | ||
type V = $V; | ||
|
||
fn new(v: Self::V) -> Self { | ||
Self::new(v) | ||
} | ||
|
||
fn load(&self, order: Ordering) -> Self::V { | ||
self.load(order) | ||
} | ||
|
||
fn store(&self, val: Self::V, order: Ordering) { | ||
self.store(val, order) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
// TODO: Detect availability using #[cfg(target_has_atomic) when it is stabilized. | ||
// Right now we essentially assume we're running on either x86 or Arm (32 or 64 bit). AFAIK, | ||
// Rust starts using additional synchronization primitives to implement atomics when they're | ||
// not natively available, and that doesn't interact safely with how we cast pointers to | ||
// atomic value references. We should be wary of this when looking at a broader range of | ||
// platforms. | ||
|
||
impl_atomic_integer_ops!(std::sync::atomic::AtomicI8, i8); | ||
impl_atomic_integer_ops!(std::sync::atomic::AtomicI16, i16); | ||
impl_atomic_integer_ops!(std::sync::atomic::AtomicI32, i32); | ||
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] | ||
impl_atomic_integer_ops!(std::sync::atomic::AtomicI64, i64); | ||
|
||
impl_atomic_integer_ops!(std::sync::atomic::AtomicU8, u8); | ||
impl_atomic_integer_ops!(std::sync::atomic::AtomicU16, u16); | ||
impl_atomic_integer_ops!(std::sync::atomic::AtomicU32, u32); | ||
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] | ||
impl_atomic_integer_ops!(std::sync::atomic::AtomicU64, u64); | ||
|
||
impl_atomic_integer_ops!(std::sync::atomic::AtomicIsize, isize); | ||
impl_atomic_integer_ops!(std::sync::atomic::AtomicUsize, usize); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use std::fmt::Debug; | ||
use std::sync::atomic::AtomicU32; | ||
|
||
fn check_atomic_integer_ops<A: AtomicInteger>() | ||
where | ||
A::V: Copy + Debug + From<u8> + PartialEq, | ||
{ | ||
let v = A::V::from(0); | ||
let a = A::new(v); | ||
assert_eq!(a.load(Ordering::Relaxed), v); | ||
|
||
let v2 = A::V::from(100); | ||
a.store(v2, Ordering::Relaxed); | ||
assert_eq!(a.load(Ordering::Relaxed), v2); | ||
} | ||
|
||
#[test] | ||
fn test_atomic_integer_ops() { | ||
check_atomic_integer_ops::<AtomicU32>() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a breaking change, should we bump the minor version number for next release?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. This also reminds me I have to update the changelog.