Skip to content
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

librustc: Make C functions unsafe #4599

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
12 changes: 9 additions & 3 deletions src/libcore/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,23 @@ extern mod rusti {
// I get link errors. This is a bug that needs investigated more.
#[doc(hidden)]
pub fn atomic_xchng_rel(dst: &mut int, src: int) -> int {
rusti::atomic_xchg_rel(dst, src)
unsafe {
rusti::atomic_xchg_rel(dst, src)
}
}

#[doc(hidden)]
pub fn atomic_add_acq(dst: &mut int, src: int) -> int {
rusti::atomic_xadd_acq(dst, src)
unsafe {
rusti::atomic_xadd_acq(dst, src)
}
}

#[doc(hidden)]
pub fn atomic_sub_rel(dst: &mut int, src: int) -> int {
rusti::atomic_xsub_rel(dst, src)
unsafe {
rusti::atomic_xsub_rel(dst, src)
}
}

#[doc(hidden)]
Expand Down
6 changes: 4 additions & 2 deletions src/libcore/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ type rust_port_id = uint;
type GlobalPtr = *libc::uintptr_t;

fn compare_and_swap(address: &mut int, oldval: int, newval: int) -> bool {
let old = rusti::atomic_cxchg(address, oldval, newval);
old == oldval
unsafe {
let old = rusti::atomic_cxchg(address, oldval, newval);
old == oldval
}
}

/**
Expand Down
22 changes: 13 additions & 9 deletions src/libcore/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ impl ReprVisitor {

#[inline(always)]
fn visit_ptr_inner(ptr: *c_void, inner: *TyDesc) -> bool {
let mut u = ReprVisitor(ptr, self.writer);
let v = reflect::MovePtrAdaptor(move u);
visit_tydesc(inner, (move v) as @TyVisitor);
true
unsafe {
let mut u = ReprVisitor(ptr, self.writer);
let v = reflect::MovePtrAdaptor(move u);
visit_tydesc(inner, (move v) as @TyVisitor);
true
}
}

#[inline(always)]
Expand Down Expand Up @@ -558,11 +560,13 @@ impl ReprVisitor : TyVisitor {
}

pub fn write_repr<T>(writer: @Writer, object: &T) {
let ptr = ptr::to_unsafe_ptr(object) as *c_void;
let tydesc = intrinsic::get_tydesc::<T>();
let mut u = ReprVisitor(ptr, writer);
let v = reflect::MovePtrAdaptor(move u);
visit_tydesc(tydesc, (move v) as @TyVisitor)
unsafe {
let ptr = ptr::to_unsafe_ptr(object) as *c_void;
let tydesc = intrinsic::get_tydesc::<T>();
let mut u = ReprVisitor(ptr, writer);
let v = reflect::MovePtrAdaptor(move u);
visit_tydesc(tydesc, (move v) as @TyVisitor)
}
}

#[test]
Expand Down
68 changes: 45 additions & 23 deletions src/libcore/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,23 +571,29 @@ pub fn try<T:Owned>(f: fn~() -> T) -> Result<T,()> {
pub fn yield() {
//! Yield control to the task scheduler

let task_ = rt::rust_get_task();
let killed = rt::rust_task_yield(task_);
if killed && !failing() {
fail ~"killed";
unsafe {
let task_ = rt::rust_get_task();
let killed = rt::rust_task_yield(task_);
if killed && !failing() {
fail ~"killed";
}
}
}

pub fn failing() -> bool {
//! True if the running task has failed

rt::rust_task_is_unwinding(rt::rust_get_task())
unsafe {
rt::rust_task_is_unwinding(rt::rust_get_task())
}
}

pub fn get_task() -> Task {
//! Get a handle to the running task

TaskHandle(rt::get_task_id())
unsafe {
TaskHandle(rt::get_task_id())
}
}

/**
Expand All @@ -608,7 +614,11 @@ pub fn get_task() -> Task {
pub unsafe fn unkillable<U>(f: fn() -> U) -> U {
struct AllowFailure {
t: *rust_task,
drop { rt::rust_task_allow_kill(self.t); }
drop {
unsafe {
rt::rust_task_allow_kill(self.t);
}
}
}

fn AllowFailure(t: *rust_task) -> AllowFailure{
Expand All @@ -617,17 +627,23 @@ pub unsafe fn unkillable<U>(f: fn() -> U) -> U {
}
}

let t = rt::rust_get_task();
let _allow_failure = AllowFailure(t);
rt::rust_task_inhibit_kill(t);
f()
unsafe {
let t = rt::rust_get_task();
let _allow_failure = AllowFailure(t);
rt::rust_task_inhibit_kill(t);
f()
}
}

/// The inverse of unkillable. Only ever to be used nested in unkillable().
pub unsafe fn rekillable<U>(f: fn() -> U) -> U {
struct DisallowFailure {
t: *rust_task,
drop { rt::rust_task_inhibit_kill(self.t); }
drop {
unsafe {
rt::rust_task_inhibit_kill(self.t);
}
}
}

fn DisallowFailure(t: *rust_task) -> DisallowFailure {
Expand All @@ -636,10 +652,12 @@ pub unsafe fn rekillable<U>(f: fn() -> U) -> U {
}
}

let t = rt::rust_get_task();
let _allow_failure = DisallowFailure(t);
rt::rust_task_allow_kill(t);
f()
unsafe {
let t = rt::rust_get_task();
let _allow_failure = DisallowFailure(t);
rt::rust_task_allow_kill(t);
f()
}
}

/**
Expand All @@ -650,8 +668,10 @@ pub unsafe fn atomically<U>(f: fn() -> U) -> U {
struct DeferInterrupts {
t: *rust_task,
drop {
rt::rust_task_allow_yield(self.t);
rt::rust_task_allow_kill(self.t);
unsafe {
rt::rust_task_allow_yield(self.t);
rt::rust_task_allow_kill(self.t);
}
}
}

Expand All @@ -661,11 +681,13 @@ pub unsafe fn atomically<U>(f: fn() -> U) -> U {
}
}

let t = rt::rust_get_task();
let _interrupts = DeferInterrupts(t);
rt::rust_task_inhibit_kill(t);
rt::rust_task_inhibit_yield(t);
f()
unsafe {
let t = rt::rust_get_task();
let _interrupts = DeferInterrupts(t);
rt::rust_task_inhibit_kill(t);
rt::rust_task_inhibit_yield(t);
f()
}
}

#[test] #[should_fail] #[ignore(cfg(windows))]
Expand Down
Loading