Skip to content

Added relaxed atomics and used them in the global heap #6614

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 17 additions & 3 deletions src/libcore/rt/global_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@ use c_malloc = libc::malloc;
use c_free = libc::free;
use managed::raw::{BoxHeaderRepr, BoxRepr};
use cast::transmute;
use unstable::intrinsics::{atomic_xadd,atomic_xsub};
use ptr::null;
use intrinsic::TyDesc;

#[cfg(stage0)]
use unstable::intrinsics::{atomic_xadd,atomic_xsub};
#[cfg(not(stage0))]
use unstable::intrinsics::{atomic_xadd_relaxed,atomic_xsub_relaxed};

#[cfg(stage0)]
unsafe fn atomic_xadd_relaxed(dst:&mut int, src:int) -> int {
atomic_xadd(dst, src)
}

#[cfg(stage0)]
unsafe fn atomic_xsub_relaxed(dst:&mut int, src:int) -> int {
atomic_xsub(dst, src)
}

pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
assert!(td.is_not_null());

Expand All @@ -35,7 +49,7 @@ pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
box.header.next = null();

let exchange_count = &mut *exchange_count_ptr();
atomic_xadd(exchange_count, 1);
atomic_xadd_relaxed(exchange_count, 1);

return transmute(box);
}
Expand All @@ -53,7 +67,7 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {

pub unsafe fn free(ptr: *c_void) {
let exchange_count = &mut *exchange_count_ptr();
atomic_xsub(exchange_count, 1);
atomic_xsub_relaxed(exchange_count, 1);

assert!(ptr.is_not_null());
c_free(ptr);
Expand Down
6 changes: 6 additions & 0 deletions src/libcore/unstable/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,19 @@ pub extern "rust-intrinsic" {
pub fn atomic_xadd_acq(dst: &mut int, src: int) -> int;
/// Atomic addition, release ordering.
pub fn atomic_xadd_rel(dst: &mut int, src: int) -> int;
/// Atomic addition, relaxed ordering
#[cfg(not(stage0))]
pub fn atomic_xadd_relaxed(dst: &mut int, src: int) -> int;

/// Atomic subtraction, sequentially consistent.
pub fn atomic_xsub(dst: &mut int, src: int) -> int;
/// Atomic subtraction, acquire ordering.
pub fn atomic_xsub_acq(dst: &mut int, src: int) -> int;
/// Atomic subtraction, release ordering.
pub fn atomic_xsub_rel(dst: &mut int, src: int) -> int;
/// Atomic subtraction, relaxed ordering
#[cfg(not(stage0))]
pub fn atomic_xsub_relaxed(dst: &mut int, src: int) -> int;

/// The size of a type in bytes.
///
Expand Down
16 changes: 15 additions & 1 deletion src/librustc/middle/trans/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use back::{link, abi};
use lib::llvm::{SequentiallyConsistent, Acquire, Release, Xchg};
use lib::llvm::{SequentiallyConsistent, Acquire, Release, Xchg, Monotonic};
use lib::llvm::{TypeRef, ValueRef};
use lib;
use middle::trans::base::*;
Expand Down Expand Up @@ -657,6 +657,13 @@ pub fn trans_intrinsic(ccx: @CrateContext,
Release);
Store(bcx, old, fcx.llretptr.get());
}
~"atomic_xadd_relaxed" => {
let old = AtomicRMW(bcx, lib::llvm::Add,
get_param(decl, first_real_arg),
get_param(decl, first_real_arg + 1u),
Monotonic);
Store(bcx, old, fcx.llretptr.get());
}
~"atomic_xsub" => {
let old = AtomicRMW(bcx, lib::llvm::Sub,
get_param(decl, first_real_arg),
Expand All @@ -678,6 +685,13 @@ pub fn trans_intrinsic(ccx: @CrateContext,
Release);
Store(bcx, old, fcx.llretptr.get());
}
~"atomic_xsub_relaxed" => {
let old = AtomicRMW(bcx, lib::llvm::Sub,
get_param(decl, first_real_arg),
get_param(decl, first_real_arg + 1u),
Monotonic);
Store(bcx, old, fcx.llretptr.get());
}
~"size_of" => {
let tp_ty = substs.tys[0];
let lltp_ty = type_of::type_of(ccx, tp_ty);
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/trans/type_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub fn type_uses_for(ccx: @CrateContext, fn_id: def_id, n_tps: uint)
~"atomic_xadd" | ~"atomic_xsub" |
~"atomic_xchg_acq" | ~"atomic_xadd_acq" |
~"atomic_xsub_acq" | ~"atomic_xchg_rel" |
~"atomic_xadd_relaxed" | ~"atomic_xsub_relaxed" |
~"atomic_xadd_rel" | ~"atomic_xsub_rel" => 0,

~"visit_tydesc" | ~"forget" | ~"frame_address" |
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,7 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
ty::mk_nil())
}
~"atomic_xchg" | ~"atomic_xadd" | ~"atomic_xsub" |
~"atomic_xadd_relaxed" | ~"atomic_xsub_relaxed" |
~"atomic_xchg_acq" | ~"atomic_xadd_acq" | ~"atomic_xsub_acq" |
~"atomic_xchg_rel" | ~"atomic_xadd_rel" | ~"atomic_xsub_rel" => {
(0,
Expand Down