Port resources to classes in libcore
This commit is contained in:
parent
b8710de5ff
commit
fee78d296c
@ -224,7 +224,11 @@ impl <T: reader, C> of reader for {base: T, cleanup: C} {
|
||||
fn tell() -> uint { self.base.tell() }
|
||||
}
|
||||
|
||||
resource FILE_res(f: *libc::FILE) { libc::fclose(f); }
|
||||
class FILE_res {
|
||||
let f: *libc::FILE;
|
||||
new(f: *libc::FILE) { self.f = f; }
|
||||
drop { libc::fclose(self.f); }
|
||||
}
|
||||
|
||||
fn FILE_reader(f: *libc::FILE, cleanup: bool) -> reader {
|
||||
if cleanup {
|
||||
@ -383,7 +387,11 @@ impl of writer for fd_t {
|
||||
fn flush() -> int { 0 }
|
||||
}
|
||||
|
||||
resource fd_res(fd: fd_t) { libc::close(fd); }
|
||||
class fd_res {
|
||||
let fd: fd_t;
|
||||
new(fd: fd_t) { self.fd = fd; }
|
||||
drop { libc::close(self.fd); }
|
||||
}
|
||||
|
||||
fn fd_writer(fd: fd_t, cleanup: bool) -> writer {
|
||||
if cleanup {
|
||||
@ -695,13 +703,17 @@ mod fsync {
|
||||
}
|
||||
|
||||
|
||||
// Resource of artifacts that need to fsync on destruction
|
||||
resource res<t>(arg: arg<t>) {
|
||||
alt arg.opt_level {
|
||||
option::none { }
|
||||
option::some(level) {
|
||||
// fail hard if not succesful
|
||||
assert(arg.fsync_fn(arg.val, level) != -1);
|
||||
// Artifacts that need to fsync on destruction
|
||||
class res<t> {
|
||||
let arg: arg<t>;
|
||||
new(-arg: arg<t>) { self.arg <- arg; }
|
||||
drop {
|
||||
alt self.arg.opt_level {
|
||||
option::none { }
|
||||
option::some(level) {
|
||||
// fail hard if not succesful
|
||||
assert(self.arg.fsync_fn(self.arg.val, level) != -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -718,7 +730,7 @@ mod fsync {
|
||||
fn FILE_res_sync(&&file: FILE_res, opt_level: option<level>,
|
||||
blk: fn(&&res<*libc::FILE>)) {
|
||||
blk(res({
|
||||
val: *file, opt_level: opt_level,
|
||||
val: file.f, opt_level: opt_level,
|
||||
fsync_fn: fn@(&&file: *libc::FILE, l: level) -> int {
|
||||
ret os::fsync_fd(libc::fileno(file), l) as int;
|
||||
}
|
||||
@ -729,7 +741,7 @@ mod fsync {
|
||||
fn fd_res_sync(&&fd: fd_res, opt_level: option<level>,
|
||||
blk: fn(&&res<fd_t>)) {
|
||||
blk(res({
|
||||
val: *fd, opt_level: opt_level,
|
||||
val: fd.fd, opt_level: opt_level,
|
||||
fsync_fn: fn@(&&fd: fd_t, l: level) -> int {
|
||||
ret os::fsync_fd(fd, l) as int;
|
||||
}
|
||||
|
@ -140,8 +140,10 @@ fn test_unwrap_str() {
|
||||
|
||||
#[test]
|
||||
fn test_unwrap_resource() {
|
||||
resource r(i: @mut int) {
|
||||
*i += 1;
|
||||
class r {
|
||||
let i: @mut int;
|
||||
new(i: @mut int) { self.i = i; }
|
||||
drop { *(self.i) += 1; }
|
||||
}
|
||||
let i = @mut 0;
|
||||
{
|
||||
|
@ -85,16 +85,20 @@ pure fn log_str<T>(t: T) -> str {
|
||||
}
|
||||
}
|
||||
|
||||
resource lock_and_signal(lock: rust_cond_lock) {
|
||||
rustrt::rust_destroy_cond_lock(lock);
|
||||
class lock_and_signal {
|
||||
let lock: rust_cond_lock;
|
||||
new(lock: rust_cond_lock) { self.lock = lock; }
|
||||
drop { rustrt::rust_destroy_cond_lock(self.lock); }
|
||||
}
|
||||
|
||||
enum condition {
|
||||
condition_(rust_cond_lock)
|
||||
}
|
||||
|
||||
resource unlock(lock: rust_cond_lock) {
|
||||
rustrt::rust_unlock_cond_lock(lock);
|
||||
class unlock {
|
||||
let lock: rust_cond_lock;
|
||||
new(lock: rust_cond_lock) { self.lock = lock; }
|
||||
drop { rustrt::rust_unlock_cond_lock(self.lock); }
|
||||
}
|
||||
|
||||
fn create_lock() -> lock_and_signal {
|
||||
@ -103,15 +107,15 @@ fn create_lock() -> lock_and_signal {
|
||||
|
||||
impl methods for lock_and_signal {
|
||||
fn lock<T>(f: fn() -> T) -> T {
|
||||
rustrt::rust_lock_cond_lock(*self);
|
||||
let _r = unlock(*self);
|
||||
rustrt::rust_lock_cond_lock(self.lock);
|
||||
let _r = unlock(self.lock);
|
||||
f()
|
||||
}
|
||||
|
||||
fn lock_cond<T>(f: fn(condition) -> T) -> T {
|
||||
rustrt::rust_lock_cond_lock(*self);
|
||||
let _r = unlock(*self);
|
||||
f(condition_(*self))
|
||||
rustrt::rust_lock_cond_lock(self.lock);
|
||||
let _r = unlock(self.lock);
|
||||
f(condition_(self.lock))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -482,9 +482,12 @@ Temporarily make the task unkillable
|
||||
|
||||
"]
|
||||
unsafe fn unkillable(f: fn()) {
|
||||
resource allow_failure(_i: ()) {
|
||||
rustrt::rust_task_allow_kill();
|
||||
class allow_failure {
|
||||
let i: (); // since a class must have at least one field
|
||||
new(_i: ()) { self.i = (); }
|
||||
drop { rustrt::rust_task_allow_kill(); }
|
||||
}
|
||||
|
||||
let _allow_failure = allow_failure(());
|
||||
rustrt::rust_task_inhibit_kill();
|
||||
f();
|
||||
|
Loading…
x
Reference in New Issue
Block a user