Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
heapless
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
heapless
Commits
978f0ee2
Commit
978f0ee2
authored
7 years ago
by
Jorge Aparicio
Browse files
Options
Downloads
Patches
Plain Diff
add a compiler barrier
parent
158d19b4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/lib.rs
+1
-0
1 addition, 0 deletions
src/lib.rs
src/ring_buffer/spsc.rs
+18
-0
18 additions, 0 deletions
src/ring_buffer/spsc.rs
with
19 additions
and
0 deletions
src/lib.rs
+
1
−
0
View file @
978f0ee2
...
...
@@ -144,6 +144,7 @@
//! is_send::<Vec<NotSend, [NotSend; 4]>>();
//! ```
#![cfg_attr(not(target_has_atomic
=
"ptr"
),
feature(asm))]
#![cfg_attr(target_has_atomic
=
"ptr"
,
feature(const_atomic_usize_new))]
#![deny(missing_docs)]
#![feature(cfg_target_has_atomic)]
...
...
This diff is collapsed.
Click to expand it.
src/ring_buffer/spsc.rs
+
18
−
0
View file @
978f0ee2
...
...
@@ -6,6 +6,14 @@ use core::sync::atomic::Ordering;
use
BufferFullError
;
use
ring_buffer
::
RingBuffer
;
// Compiler barrier
#[cfg(not(target_has_atomic
=
"ptr"
))]
macro_rules!
barrier
{
()
=>
{
unsafe
{
asm!
(
""
:::
"memory"
)
}
}
}
impl
<
T
,
A
>
RingBuffer
<
T
,
A
>
where
A
:
Unsize
<
[
T
]
>
,
...
...
@@ -75,6 +83,12 @@ where
// consumer so we inform this to the compiler using a volatile load
if
rb
.head
!=
unsafe
{
ptr
::
read_volatile
(
&
rb
.tail
)
}
{
let
item
=
unsafe
{
ptr
::
read
(
buffer
.get_unchecked
(
rb
.head
))
};
// NOTE(barrier!) this ensures that the compiler won't place the instructions to read
// the data *before* the instructions to increment the `head` pointer -- note that this
// won't be enough on architectures that allow out of order execution
barrier!
();
rb
.head
=
(
rb
.head
+
1
)
%
n
;
Some
(
item
)
}
else
{
...
...
@@ -146,6 +160,10 @@ where
if
next_tail
!=
unsafe
{
ptr
::
read_volatile
(
&
rb
.head
)
}
{
// NOTE(ptr::write) see the other `enqueue` implementation above for details
unsafe
{
ptr
::
write
(
buffer
.get_unchecked_mut
(
rb
.tail
),
item
)
}
// NOTE(barrier!) see the NOTE(barrier!) above
barrier!
();
rb
.tail
=
next_tail
;
Ok
(())
}
else
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment