Skip to content

Commit

Permalink
Merge #585
Browse files Browse the repository at this point in the history
585: Get rid of the hack for epoch::unprotected r=taiki-e a=coolreader18

Noticed that this hack isn't necessary (anymore?); originally had a newtype over `Guard` that I implemented `Sync` for, and then I realized that a `static mut` works as well.


Co-authored-by: Noah <[email protected]>
  • Loading branch information
bors[bot] and coolreader18 authored Oct 17, 2020
2 parents e8a2e83 + a248d31 commit 220e690
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions crossbeam-epoch/src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,13 @@ impl fmt::Debug for Guard {
/// [`defer`]: Guard::defer
#[inline]
pub unsafe fn unprotected() -> &'static Guard {
// HACK(stjepang): An unprotected guard is just a `Guard` with its field `local` set to null.
// Since this function returns a `'static` reference to a `Guard`, we must return a reference
// to a global guard. However, it's not possible to create a `static` `Guard` because it does
// not implement `Sync`. To get around the problem, we create a static `usize` initialized to
// zero and then transmute it into a `Guard`. This is safe because `usize` and `Guard`
// (consisting of a single pointer) have the same representation in memory.
static UNPROTECTED: usize = 0;
&*(&UNPROTECTED as *const _ as *const Guard)
// An unprotected guard is just a `Guard` with its field `local` set to null.
// We make a newtype over `Guard` because `Guard` isn't `Sync`, so can't be directly stored in
// a `static`
struct GuardWrapper(Guard);
unsafe impl Sync for GuardWrapper {}
static UNPROTECTED: GuardWrapper = GuardWrapper(Guard {
local: core::ptr::null(),
});
&UNPROTECTED.0
}

0 comments on commit 220e690

Please sign in to comment.