Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Insert a compiler_fence after initialization #25

Merged
merged 2 commits into from
Nov 27, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#[cfg(test)]
mod test;

use core::sync::atomic::{compiler_fence, Ordering};
use core::{mem, ptr};

mod sealed {
Expand Down Expand Up @@ -133,6 +134,12 @@ where
sdata = sdata.offset(1);
sidata = sidata.offset(1);
}

// Ensure that any accesses of `static`s are not reordered before the `.data` section is
// initialized.
// We use `SeqCst`, because `Acquire` only prevents later accesses from being reordered before
// *reads*, but this method only *writes* to the locations.
compiler_fence(Ordering::SeqCst);
}

/// Zeroes the `.bss` section.
Expand All @@ -158,4 +165,10 @@ where
ptr::write_volatile(sbss, mem::zeroed());
sbss = sbss.offset(1);
}

// Ensure that any accesses of `static`s are not reordered before the `.data` section is
jonas-schievink marked this conversation as resolved.
Show resolved Hide resolved
// initialized.
// We use `SeqCst`, because `Acquire` only prevents later accesses from being reordered before
// *reads*, but this method only *writes* to the locations.
compiler_fence(Ordering::SeqCst);
}