Skip to content

Commit

Permalink
Rollup merge of #80236 - tmiasko:atomic-swap, r=oli-obk
Browse files Browse the repository at this point in the history
Use pointer type in AtomicPtr::swap implementation

Closes #80234.
  • Loading branch information
Dylan-DPC authored Dec 21, 2020
2 parents 2528acb + 4ad53dc commit 328e89c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 13 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
};

let ty = substs.type_at(0);
if int_type_width_signed(ty, bx.tcx()).is_some() {
bx.atomic_rmw(atom_op, args[0].immediate(), args[1].immediate(), order)
if int_type_width_signed(ty, bx.tcx()).is_some()
|| (ty.is_unsafe_ptr() && op == "xchg")
{
let mut ptr = args[0].immediate();
let mut val = args[1].immediate();
if ty.is_unsafe_ptr() {
// Some platforms do not support atomic operations on pointers,
// so we cast to integer first.
let ptr_llty = bx.type_ptr_to(bx.type_isize());
ptr = bx.pointercast(ptr, ptr_llty);
val = bx.ptrtoint(val, bx.type_isize());
}
bx.atomic_rmw(atom_op, ptr, val, order)
} else {
return invalid_monomorphization(ty);
}
Expand Down
10 changes: 9 additions & 1 deletion library/core/src/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,8 +1040,16 @@ impl<T> AtomicPtr<T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(target_has_atomic = "ptr")]
pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T {
#[cfg(bootstrap)]
// SAFETY: data races are prevented by atomic intrinsics.
unsafe { atomic_swap(self.p.get() as *mut usize, ptr as usize, order) as *mut T }
unsafe {
atomic_swap(self.p.get() as *mut usize, ptr as usize, order) as *mut T
}
#[cfg(not(bootstrap))]
// SAFETY: data races are prevented by atomic intrinsics.
unsafe {
atomic_swap(self.p.get(), ptr, order)
}
}

/// Stores a value into the pointer if the current value is the same as the `current` value.
Expand Down

0 comments on commit 328e89c

Please sign in to comment.