Skip to content

Commit

Permalink
impl Debug for AioCb and SigevNotify
Browse files Browse the repository at this point in the history
Also, fix style bug in AIO tests
  • Loading branch information
asomers committed Feb 25, 2017
1 parent 3c25561 commit e29c0ef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/sys/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use {Error, Errno, Result};
use std::os::unix::io::RawFd;
use libc::{c_void, off_t, size_t};
use libc;
use std::fmt;
use std::fmt::Debug;
use std::io::Write;
use std::io::stderr;
use std::marker::PhantomData;
Expand Down Expand Up @@ -270,6 +272,23 @@ pub fn lio_listio(mode: LioMode, list: &[&mut AioCb],
}).map(drop)
}

impl<'a> Debug for AioCb<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("AioCb")
.field("aio_fildes", &self.aiocb.aio_fildes)
.field("aio_offset", &self.aiocb.aio_offset)
.field("aio_buf", &self.aiocb.aio_buf)
.field("aio_nbytes", &self.aiocb.aio_nbytes)
.field("aio_lio_opcode", &self.aiocb.aio_lio_opcode)
.field("aio_reqprio", &self.aiocb.aio_reqprio)
.field("aio_sigevent", &SigEvent::from(&self.aiocb.aio_sigevent))
.field("mutable", &self.mutable)
.field("in_progress", &self.in_progress)
.field("phantom", &self.phantom)
.finish()
}
}

impl<'a> Drop for AioCb<'a> {
/// If the `AioCb` has no remaining state in the kernel, just drop it.
/// Otherwise, collect its error and return values, so as not to leak
Expand Down
29 changes: 29 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use libc;
use {Errno, Error, Result};
use std::fmt;
use std::fmt::Debug;
use std::mem;
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
use std::os::unix::io::RawFd;
Expand Down Expand Up @@ -505,6 +507,33 @@ impl SigEvent {
}
}

impl Debug for SigEvent {
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("SigEvent")
.field("sigev_notify", &self.sigevent.sigev_notify)
.field("sigev_signo", &self.sigevent.sigev_signo)
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
.field("sigev_notify_thread_id",
&self.sigevent.sigev_notify_thread_id)
.finish()
}

#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("SigEvent")
.field("sigev_notify", &self.sigevent.sigev_notify)
.field("sigev_signo", &self.sigevent.sigev_signo)
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
.finish()
}
}

impl<'a> From<&'a libc::sigevent> for SigEvent {
fn from(sigevent: &libc::sigevent) -> Self {
SigEvent{ sigevent: sigevent.clone() }
}
}

#[cfg(test)]
mod tests {
Expand Down
12 changes: 6 additions & 6 deletions test/sys/test_aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ fn test_write() {
}

// XXX: should be sig_atomic_t, but rust's libc doesn't define that yet
static mut signaled: i32 = 0;
static mut SIGNALED: i32 = 0;

extern fn sigfunc(_: c_int) {
// It's a pity that Rust can't understand that static mutable sig_atomic_t
// variables can be safely accessed
unsafe { signaled = 1 };
unsafe { SIGNALED = 1 };
}

// Test an aio operation with completion delivered by a signal
Expand All @@ -207,7 +207,7 @@ fn test_write_sigev_signal() {
let sa = SigAction::new(SigHandler::Handler(sigfunc),
SA_RESETHAND,
SigSet::empty());
unsafe {signaled = 0 };
unsafe {SIGNALED = 0 };
unsafe { sigaction(Signal::SIGUSR2, &sa) }.unwrap();

const INITIAL: &'static [u8] = b"abcdef123456";
Expand All @@ -227,7 +227,7 @@ fn test_write_sigev_signal() {
},
LioOpcode::LIO_NOP);
aiocb.write().unwrap();
while unsafe { signaled == 0 } {
while unsafe { SIGNALED == 0 } {
thread::sleep(time::Duration::from_millis(10));
}

Expand Down Expand Up @@ -357,11 +357,11 @@ fn test_lio_listio_signal() {
0, //priority
SigevNotify::SigevNone,
LioOpcode::LIO_READ);
unsafe {signaled = 0 };
unsafe {SIGNALED = 0 };
unsafe { sigaction(Signal::SIGUSR2, &sa) }.unwrap();
let err = lio_listio(LioMode::LIO_NOWAIT, &[&mut wcb, &mut rcb], sigev_notify);
err.expect("lio_listio failed");
while unsafe { signaled == 0 } {
while unsafe { SIGNALED == 0 } {
thread::sleep(time::Duration::from_millis(10));
}

Expand Down

0 comments on commit e29c0ef

Please sign in to comment.