[NEEDS SNAPSHOT] Port remainder of resources to classes in libcore
This commit is contained in:
parent
a141f58e9b
commit
25aa360595
@ -24,17 +24,19 @@ fn rust_atomic_decrement(p: &mut libc::intptr_t)
|
||||
data: T
|
||||
};
|
||||
|
||||
resource arc_destruct<T>(data: *libc::c_void) {
|
||||
unsafe {
|
||||
let data: ~arc_data<T> = unsafe::reinterpret_cast(data);
|
||||
let new_count = rustrt::rust_atomic_decrement(&mut data.count);
|
||||
assert new_count >= 0;
|
||||
if new_count == 0 {
|
||||
// drop glue takes over.
|
||||
} else {
|
||||
unsafe::forget(data);
|
||||
}
|
||||
}
|
||||
class arc_destruct<T> {
|
||||
let data: *libc::c_void;
|
||||
new(data: *libc::c_void) { self.data = data; }
|
||||
drop unsafe {
|
||||
let data: ~arc_data<T> = unsafe::reinterpret_cast(self.data);
|
||||
let new_count = rustrt::rust_atomic_decrement(&mut data.count);
|
||||
assert new_count >= 0;
|
||||
if new_count == 0 {
|
||||
// drop glue takes over.
|
||||
} else {
|
||||
unsafe::forget(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type arc<T: const> = arc_destruct<T>;
|
||||
@ -52,7 +54,7 @@ fn arc<T: const>(-data: T) -> arc<T> {
|
||||
wrapper."]
|
||||
fn get<T: const>(rc: &a.arc<T>) -> &a.T {
|
||||
unsafe {
|
||||
let ptr: ~arc_data<T> = unsafe::reinterpret_cast(**rc);
|
||||
let ptr: ~arc_data<T> = unsafe::reinterpret_cast((*rc).data);
|
||||
// Cast us back into the correct region
|
||||
let r = unsafe::reinterpret_cast(&ptr.data);
|
||||
unsafe::forget(ptr);
|
||||
@ -67,12 +69,12 @@ fn get<T: const>(rc: &a.arc<T>) -> &a.T {
|
||||
allowing them to share the underlying data."]
|
||||
fn clone<T: const>(rc: &arc<T>) -> arc<T> {
|
||||
unsafe {
|
||||
let ptr: ~arc_data<T> = unsafe::reinterpret_cast(**rc);
|
||||
let ptr: ~arc_data<T> = unsafe::reinterpret_cast((*rc).data);
|
||||
let new_count = rustrt::rust_atomic_increment(&mut ptr.count);
|
||||
assert new_count >= 2;
|
||||
unsafe::forget(ptr);
|
||||
}
|
||||
arc_destruct(**rc)
|
||||
arc_destruct((*rc).data)
|
||||
}
|
||||
|
||||
// An arc over mutable data that is protected by a lock.
|
||||
@ -93,17 +95,19 @@ impl methods<T: send> for exclusive<T> {
|
||||
fn clone() -> exclusive<T> {
|
||||
unsafe {
|
||||
// this makes me nervous...
|
||||
let ptr: ~arc_data<ex_data<T>> = unsafe::reinterpret_cast(*self);
|
||||
let ptr: ~arc_data<ex_data<T>> =
|
||||
unsafe::reinterpret_cast(self.data);
|
||||
let new_count = rustrt::rust_atomic_increment(&mut ptr.count);
|
||||
assert new_count > 1;
|
||||
unsafe::forget(ptr);
|
||||
}
|
||||
arc_destruct(*self)
|
||||
arc_destruct(self.data)
|
||||
}
|
||||
|
||||
fn with<U>(f: fn(sys::condition, x: &T) -> U) -> U {
|
||||
unsafe {
|
||||
let ptr: ~arc_data<ex_data<T>> = unsafe::reinterpret_cast(*self);
|
||||
let ptr: ~arc_data<ex_data<T>> =
|
||||
unsafe::reinterpret_cast(self.data);
|
||||
let r = {
|
||||
let rec: &ex_data<T> = &(*ptr).data;
|
||||
rec.lock.lock_cond() {|c|
|
||||
@ -123,8 +127,10 @@ fn with<U>(f: fn(sys::condition, x: &T) -> U) -> U {
|
||||
// (terminate, get)
|
||||
type shared_arc<T: const send> = (shared_arc_res, get_chan<T>);
|
||||
|
||||
resource shared_arc_res(c: comm::chan<()>) {
|
||||
c.send(());
|
||||
class shared_arc_res {
|
||||
let c: comm::chan<()>;
|
||||
new(c: comm::chan<()>) { self.c = c; }
|
||||
drop { self.c.send(()); }
|
||||
}
|
||||
|
||||
fn shared_arc<T: send const>(-data: T) -> shared_arc<T> {
|
||||
|
@ -181,8 +181,12 @@ unsafe fn weaken_task(f: fn(comm::port<()>)) unsafe {
|
||||
let _unweaken = unweaken(ch);
|
||||
f(po);
|
||||
|
||||
resource unweaken(ch: comm::chan<()>) unsafe {
|
||||
rustrt::rust_task_unweaken(unsafe::reinterpret_cast(ch));
|
||||
class unweaken {
|
||||
let ch: comm::chan<()>;
|
||||
new(ch: comm::chan<()>) { self.ch = ch; }
|
||||
drop unsafe {
|
||||
rustrt::rust_task_unweaken(unsafe::reinterpret_cast(self.ch));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -230,10 +230,14 @@ fn shuffle_mut<T>(&&values: [mut T]) {
|
||||
|
||||
}
|
||||
|
||||
resource rand_res(c: *rctx) { rustrt::rand_free(c); }
|
||||
class rand_res {
|
||||
let c: *rctx;
|
||||
new(c: *rctx) { self.c = c; }
|
||||
drop { rustrt::rand_free(self.c); }
|
||||
}
|
||||
|
||||
impl of rng for @rand_res {
|
||||
fn next() -> u32 { ret rustrt::rand_next(**self); }
|
||||
fn next() -> u32 { ret rustrt::rand_next((*self).c); }
|
||||
}
|
||||
|
||||
#[doc = "Create a new random seed for seeded_rng"]
|
||||
|
@ -170,9 +170,9 @@ fn run_program(prog: str, args: [str]) -> int {
|
||||
#[doc ="
|
||||
Spawns a process and returns a program
|
||||
|
||||
The returned value is a boxed resource containing a <program> object that can
|
||||
be used for sending and recieving data over the standard file descriptors.
|
||||
The resource will ensure that file descriptors are closed properly.
|
||||
The returned value is a boxed class containing a <program> object that can
|
||||
be used for sending and receiving data over the standard file descriptors.
|
||||
The class will ensure that file descriptors are closed properly.
|
||||
|
||||
# Arguments
|
||||
|
||||
@ -181,7 +181,7 @@ fn run_program(prog: str, args: [str]) -> int {
|
||||
|
||||
# Return value
|
||||
|
||||
A boxed resource of <program>
|
||||
A class with a <program> field
|
||||
"]
|
||||
fn start_program(prog: str, args: [str]) -> program {
|
||||
let pipe_input = os::pipe();
|
||||
@ -221,16 +221,20 @@ fn destroy_repr(r: prog_repr) {
|
||||
libc::fclose(r.out_file);
|
||||
libc::fclose(r.err_file);
|
||||
}
|
||||
resource prog_res(r: prog_repr) { destroy_repr(r); }
|
||||
class prog_res {
|
||||
let r: prog_repr;
|
||||
new(-r: prog_repr) { self.r = r; }
|
||||
drop { destroy_repr(self.r); }
|
||||
}
|
||||
|
||||
impl of program for prog_res {
|
||||
fn get_id() -> pid_t { ret self.pid; }
|
||||
fn input() -> io::writer { io::fd_writer(self.in_fd, false) }
|
||||
fn output() -> io::reader { io::FILE_reader(self.out_file, false) }
|
||||
fn err() -> io::reader { io::FILE_reader(self.err_file, false) }
|
||||
fn close_input() { close_repr_input(*self); }
|
||||
fn finish() -> int { finish_repr(*self) }
|
||||
fn destroy() { destroy_repr(*self); }
|
||||
fn get_id() -> pid_t { ret self.r.pid; }
|
||||
fn input() -> io::writer { io::fd_writer(self.r.in_fd, false) }
|
||||
fn output() -> io::reader { io::FILE_reader(self.r.out_file, false) }
|
||||
fn err() -> io::reader { io::FILE_reader(self.r.err_file, false) }
|
||||
fn close_input() { close_repr_input(self.r); }
|
||||
fn finish() -> int { finish_repr(self.r) }
|
||||
fn destroy() { destroy_repr(self.r); }
|
||||
}
|
||||
let repr = {pid: pid,
|
||||
mut in_fd: pipe_input.out,
|
||||
|
Loading…
Reference in New Issue
Block a user