Skip to content

Commit 550c347

Browse files
committed
rustuv: Deal with the rtio changes
1 parent 51348b0 commit 550c347

File tree

14 files changed

+319
-546
lines changed

14 files changed

+319
-546
lines changed

src/librustuv/addrinfo.rs

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ai = std::io::net::addrinfo;
1211
use libc::c_int;
1312
use libc;
1413
use std::mem;
1514
use std::ptr::null;
1615
use std::rt::task::BlockedTask;
16+
use std::rt::rtio;
1717

1818
use net;
1919
use super::{Loop, UvError, Request, wait_until_woken_after, wakeup};
@@ -33,7 +33,9 @@ pub struct GetAddrInfoRequest;
3333

3434
impl GetAddrInfoRequest {
3535
pub fn run(loop_: &Loop, node: Option<&str>, service: Option<&str>,
36-
hints: Option<ai::Hint>) -> Result<Vec<ai::Info>, UvError> {
36+
hints: Option<rtio::AddrinfoHint>)
37+
-> Result<Vec<rtio::AddrinfoInfo>, UvError>
38+
{
3739
assert!(node.is_some() || service.is_some());
3840
let (_c_node, c_node_ptr) = match node {
3941
Some(n) => {
@@ -54,20 +56,11 @@ impl GetAddrInfoRequest {
5456
};
5557

5658
let hint = hints.map(|hint| {
57-
let mut flags = 0;
58-
each_ai_flag(|cval, aival| {
59-
if hint.flags & (aival as uint) != 0 {
60-
flags |= cval as i32;
61-
}
62-
});
63-
let socktype = 0;
64-
let protocol = 0;
65-
6659
libc::addrinfo {
67-
ai_flags: flags,
60+
ai_flags: 0,
6861
ai_family: hint.family as c_int,
69-
ai_socktype: socktype,
70-
ai_protocol: protocol,
62+
ai_socktype: 0,
63+
ai_protocol: 0,
7164
ai_addrlen: 0,
7265
ai_canonname: null(),
7366
ai_addr: null(),
@@ -119,22 +112,8 @@ impl Drop for Addrinfo {
119112
}
120113
}
121114

122-
fn each_ai_flag(_f: |c_int, ai::Flag|) {
123-
/* FIXME: do we really want to support these?
124-
unsafe {
125-
f(uvll::rust_AI_ADDRCONFIG(), ai::AddrConfig);
126-
f(uvll::rust_AI_ALL(), ai::All);
127-
f(uvll::rust_AI_CANONNAME(), ai::CanonName);
128-
f(uvll::rust_AI_NUMERICHOST(), ai::NumericHost);
129-
f(uvll::rust_AI_NUMERICSERV(), ai::NumericServ);
130-
f(uvll::rust_AI_PASSIVE(), ai::Passive);
131-
f(uvll::rust_AI_V4MAPPED(), ai::V4Mapped);
132-
}
133-
*/
134-
}
135-
136115
// Traverse the addrinfo linked list, producing a vector of Rust socket addresses
137-
pub fn accum_addrinfo(addr: &Addrinfo) -> Vec<ai::Info> {
116+
pub fn accum_addrinfo(addr: &Addrinfo) -> Vec<rtio::AddrinfoInfo> {
138117
unsafe {
139118
let mut addr = addr.handle;
140119

@@ -143,35 +122,12 @@ pub fn accum_addrinfo(addr: &Addrinfo) -> Vec<ai::Info> {
143122
let rustaddr = net::sockaddr_to_addr(mem::transmute((*addr).ai_addr),
144123
(*addr).ai_addrlen as uint);
145124

146-
let mut flags = 0;
147-
each_ai_flag(|cval, aival| {
148-
if (*addr).ai_flags & cval != 0 {
149-
flags |= aival as uint;
150-
}
151-
});
152-
153-
/* FIXME: do we really want to support these
154-
let protocol = match (*addr).ai_protocol {
155-
p if p == uvll::rust_IPPROTO_UDP() => Some(ai::UDP),
156-
p if p == uvll::rust_IPPROTO_TCP() => Some(ai::TCP),
157-
_ => None,
158-
};
159-
let socktype = match (*addr).ai_socktype {
160-
p if p == uvll::rust_SOCK_STREAM() => Some(ai::Stream),
161-
p if p == uvll::rust_SOCK_DGRAM() => Some(ai::Datagram),
162-
p if p == uvll::rust_SOCK_RAW() => Some(ai::Raw),
163-
_ => None,
164-
};
165-
*/
166-
let protocol = None;
167-
let socktype = None;
168-
169-
addrs.push(ai::Info {
125+
addrs.push(rtio::AddrinfoInfo {
170126
address: rustaddr,
171127
family: (*addr).ai_family as uint,
172-
socktype: socktype,
173-
protocol: protocol,
174-
flags: flags,
128+
socktype: 0,
129+
protocol: 0,
130+
flags: 0,
175131
});
176132
if (*addr).ai_next.is_not_null() {
177133
addr = (*addr).ai_next;

src/librustuv/async.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use alloc::arc::Arc;
1112
use std::mem;
13+
use std::rt::exclusive::Exclusive;
1214
use std::rt::rtio::{Callback, RemoteCallback};
13-
use std::unstable::sync::Exclusive;
1415

1516
use uvll;
1617
use super::{Loop, UvHandle};
@@ -22,12 +23,12 @@ pub struct AsyncWatcher {
2223

2324
// A flag to tell the callback to exit, set from the dtor. This is
2425
// almost never contested - only in rare races with the dtor.
25-
exit_flag: Exclusive<bool>
26+
exit_flag: Arc<Exclusive<bool>>,
2627
}
2728

2829
struct Payload {
2930
callback: Box<Callback:Send>,
30-
exit_flag: Exclusive<bool>,
31+
exit_flag: Arc<Exclusive<bool>>,
3132
}
3233

3334
impl AsyncWatcher {
@@ -36,7 +37,7 @@ impl AsyncWatcher {
3637
assert_eq!(unsafe {
3738
uvll::uv_async_init(loop_.handle, handle, async_cb)
3839
}, 0);
39-
let flag = Exclusive::new(false);
40+
let flag = Arc::new(Exclusive::new(false));
4041
let payload = box Payload { callback: cb, exit_flag: flag.clone() };
4142
unsafe {
4243
let payload: *u8 = mem::transmute(payload);
@@ -80,9 +81,7 @@ extern fn async_cb(handle: *uvll::uv_async_t) {
8081
// could be called in the other thread, missing the final
8182
// callback while still destroying the handle.
8283

83-
let should_exit = unsafe {
84-
payload.exit_flag.with_imm(|&should_exit| should_exit)
85-
};
84+
let should_exit = unsafe { *payload.exit_flag.lock() };
8685

8786
payload.callback.call();
8887

@@ -108,16 +107,13 @@ impl RemoteCallback for AsyncWatcher {
108107

109108
impl Drop for AsyncWatcher {
110109
fn drop(&mut self) {
111-
unsafe {
112-
self.exit_flag.with(|should_exit| {
113-
// NB: These two things need to happen atomically. Otherwise
114-
// the event handler could wake up due to a *previous*
115-
// signal and see the exit flag, destroying the handle
116-
// before the final send.
117-
*should_exit = true;
118-
uvll::uv_async_send(self.handle)
119-
})
120-
}
110+
let mut should_exit = unsafe { self.exit_flag.lock() };
111+
// NB: These two things need to happen atomically. Otherwise
112+
// the event handler could wake up due to a *previous*
113+
// signal and see the exit flag, destroying the handle
114+
// before the final send.
115+
*should_exit = true;
116+
unsafe { uvll::uv_async_send(self.handle) }
121117
}
122118
}
123119

src/librustuv/file.rs

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use libc::{c_int, c_char, c_void, ssize_t};
1212
use libc;
1313
use std::c_str::CString;
1414
use std::c_str;
15-
use std::io::{FileStat, IoError};
16-
use std::io;
1715
use std::mem;
16+
use std::os;
17+
use std::rt::rtio::{IoResult, IoError};
1818
use std::rt::rtio;
1919
use std::rt::task::BlockedTask;
2020

@@ -56,21 +56,23 @@ impl FsRequest {
5656
})
5757
}
5858

59-
pub fn lstat(loop_: &Loop, path: &CString) -> Result<FileStat, UvError> {
59+
pub fn lstat(loop_: &Loop, path: &CString)
60+
-> Result<rtio::FileStat, UvError>
61+
{
6062
execute(|req, cb| unsafe {
6163
uvll::uv_fs_lstat(loop_.handle, req, path.with_ref(|p| p),
6264
cb)
6365
}).map(|req| req.mkstat())
6466
}
6567

66-
pub fn stat(loop_: &Loop, path: &CString) -> Result<FileStat, UvError> {
68+
pub fn stat(loop_: &Loop, path: &CString) -> Result<rtio::FileStat, UvError> {
6769
execute(|req, cb| unsafe {
6870
uvll::uv_fs_stat(loop_.handle, req, path.with_ref(|p| p),
6971
cb)
7072
}).map(|req| req.mkstat())
7173
}
7274

73-
pub fn fstat(loop_: &Loop, fd: c_int) -> Result<FileStat, UvError> {
75+
pub fn fstat(loop_: &Loop, fd: c_int) -> Result<rtio::FileStat, UvError> {
7476
execute(|req, cb| unsafe {
7577
uvll::uv_fs_fstat(loop_.handle, req, fd, cb)
7678
}).map(|req| req.mkstat())
@@ -269,40 +271,30 @@ impl FsRequest {
269271
unsafe { uvll::get_ptr_from_fs_req(self.req) }
270272
}
271273

272-
pub fn mkstat(&self) -> FileStat {
274+
pub fn mkstat(&self) -> rtio::FileStat {
273275
let stat = self.get_stat();
274276
fn to_msec(stat: uvll::uv_timespec_t) -> u64 {
275277
// Be sure to cast to u64 first to prevent overflowing if the tv_sec
276278
// field is a 32-bit integer.
277279
(stat.tv_sec as u64) * 1000 + (stat.tv_nsec as u64) / 1000000
278280
}
279-
let kind = match (stat.st_mode as c_int) & libc::S_IFMT {
280-
libc::S_IFREG => io::TypeFile,
281-
libc::S_IFDIR => io::TypeDirectory,
282-
libc::S_IFIFO => io::TypeNamedPipe,
283-
libc::S_IFBLK => io::TypeBlockSpecial,
284-
libc::S_IFLNK => io::TypeSymlink,
285-
_ => io::TypeUnknown,
286-
};
287-
FileStat {
281+
rtio::FileStat {
288282
size: stat.st_size as u64,
289-
kind: kind,
290-
perm: io::FilePermission::from_bits_truncate(stat.st_mode as u32),
283+
kind: stat.st_mode as u64,
284+
perm: stat.st_mode as u64,
291285
created: to_msec(stat.st_birthtim),
292286
modified: to_msec(stat.st_mtim),
293287
accessed: to_msec(stat.st_atim),
294-
unstable: io::UnstableFileStat {
295-
device: stat.st_dev as u64,
296-
inode: stat.st_ino as u64,
297-
rdev: stat.st_rdev as u64,
298-
nlink: stat.st_nlink as u64,
299-
uid: stat.st_uid as u64,
300-
gid: stat.st_gid as u64,
301-
blksize: stat.st_blksize as u64,
302-
blocks: stat.st_blocks as u64,
303-
flags: stat.st_flags as u64,
304-
gen: stat.st_gen as u64,
305-
}
288+
device: stat.st_dev as u64,
289+
inode: stat.st_ino as u64,
290+
rdev: stat.st_rdev as u64,
291+
nlink: stat.st_nlink as u64,
292+
uid: stat.st_uid as u64,
293+
gid: stat.st_gid as u64,
294+
blksize: stat.st_blksize as u64,
295+
blocks: stat.st_blocks as u64,
296+
flags: stat.st_flags as u64,
297+
gen: stat.st_gen as u64,
306298
}
307299
}
308300
}
@@ -369,29 +361,26 @@ impl FileWatcher {
369361
}
370362
}
371363

372-
fn base_read(&mut self, buf: &mut [u8], offset: i64) -> Result<int, IoError> {
364+
fn base_read(&mut self, buf: &mut [u8], offset: i64) -> IoResult<int> {
373365
let _m = self.fire_homing_missile();
374366
let r = FsRequest::read(&self.loop_, self.fd, buf, offset);
375367
r.map_err(uv_error_to_io_error)
376368
}
377-
fn base_write(&mut self, buf: &[u8], offset: i64) -> Result<(), IoError> {
369+
fn base_write(&mut self, buf: &[u8], offset: i64) -> IoResult<()> {
378370
let _m = self.fire_homing_missile();
379371
let r = FsRequest::write(&self.loop_, self.fd, buf, offset);
380372
r.map_err(uv_error_to_io_error)
381373
}
382-
fn seek_common(&self, pos: i64, whence: c_int) ->
383-
Result<u64, IoError>{
384-
unsafe {
385-
match libc::lseek(self.fd, pos as libc::off_t, whence) {
386-
-1 => {
387-
Err(IoError {
388-
kind: io::OtherIoError,
389-
desc: "Failed to lseek.",
390-
detail: None
391-
})
392-
},
393-
n => Ok(n as u64)
394-
}
374+
fn seek_common(&self, pos: i64, whence: c_int) -> IoResult<u64>{
375+
match unsafe { libc::lseek(self.fd, pos as libc::off_t, whence) } {
376+
-1 => {
377+
Err(IoError {
378+
code: os::errno() as uint,
379+
extra: 0,
380+
detail: None,
381+
})
382+
},
383+
n => Ok(n as u64)
395384
}
396385
}
397386
}
@@ -425,47 +414,47 @@ impl Drop for FileWatcher {
425414
}
426415

427416
impl rtio::RtioFileStream for FileWatcher {
428-
fn read(&mut self, buf: &mut [u8]) -> Result<int, IoError> {
417+
fn read(&mut self, buf: &mut [u8]) -> IoResult<int> {
429418
self.base_read(buf, -1)
430419
}
431-
fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
420+
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
432421
self.base_write(buf, -1)
433422
}
434-
fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError> {
423+
fn pread(&mut self, buf: &mut [u8], offset: u64) -> IoResult<int> {
435424
self.base_read(buf, offset as i64)
436425
}
437-
fn pwrite(&mut self, buf: &[u8], offset: u64) -> Result<(), IoError> {
426+
fn pwrite(&mut self, buf: &[u8], offset: u64) -> IoResult<()> {
438427
self.base_write(buf, offset as i64)
439428
}
440-
fn seek(&mut self, pos: i64, whence: io::SeekStyle) -> Result<u64, IoError> {
429+
fn seek(&mut self, pos: i64, whence: rtio::SeekStyle) -> IoResult<u64> {
441430
use libc::{SEEK_SET, SEEK_CUR, SEEK_END};
442431
let whence = match whence {
443-
io::SeekSet => SEEK_SET,
444-
io::SeekCur => SEEK_CUR,
445-
io::SeekEnd => SEEK_END
432+
rtio::SeekSet => SEEK_SET,
433+
rtio::SeekCur => SEEK_CUR,
434+
rtio::SeekEnd => SEEK_END
446435
};
447436
self.seek_common(pos, whence)
448437
}
449-
fn tell(&self) -> Result<u64, IoError> {
438+
fn tell(&self) -> IoResult<u64> {
450439
use libc::SEEK_CUR;
451440

452441
self.seek_common(0, SEEK_CUR)
453442
}
454-
fn fsync(&mut self) -> Result<(), IoError> {
443+
fn fsync(&mut self) -> IoResult<()> {
455444
let _m = self.fire_homing_missile();
456445
FsRequest::fsync(&self.loop_, self.fd).map_err(uv_error_to_io_error)
457446
}
458-
fn datasync(&mut self) -> Result<(), IoError> {
447+
fn datasync(&mut self) -> IoResult<()> {
459448
let _m = self.fire_homing_missile();
460449
FsRequest::datasync(&self.loop_, self.fd).map_err(uv_error_to_io_error)
461450
}
462-
fn truncate(&mut self, offset: i64) -> Result<(), IoError> {
451+
fn truncate(&mut self, offset: i64) -> IoResult<()> {
463452
let _m = self.fire_homing_missile();
464453
let r = FsRequest::truncate(&self.loop_, self.fd, offset);
465454
r.map_err(uv_error_to_io_error)
466455
}
467456

468-
fn fstat(&mut self) -> Result<FileStat, IoError> {
457+
fn fstat(&mut self) -> IoResult<rtio::FileStat> {
469458
let _m = self.fire_homing_missile();
470459
FsRequest::fstat(&self.loop_, self.fd).map_err(uv_error_to_io_error)
471460
}
@@ -475,7 +464,6 @@ impl rtio::RtioFileStream for FileWatcher {
475464
mod test {
476465
use libc::c_int;
477466
use libc::{O_CREAT, O_RDWR, O_RDONLY, S_IWUSR, S_IRUSR};
478-
use std::io;
479467
use std::str;
480468
use super::FsRequest;
481469
use super::super::Loop;
@@ -562,10 +550,6 @@ mod test {
562550
let result = FsRequest::mkdir(l(), path, mode);
563551
assert!(result.is_ok());
564552

565-
let result = FsRequest::stat(l(), path);
566-
assert!(result.is_ok());
567-
assert!(result.unwrap().kind == io::TypeDirectory);
568-
569553
let result = FsRequest::rmdir(l(), path);
570554
assert!(result.is_ok());
571555

0 commit comments

Comments
 (0)