Skip to content

Commit 9e4e0c0

Browse files
committed
Wrap SigEvent in a module
1 parent 5ab4e26 commit 9e4e0c0

File tree

1 file changed

+101
-95
lines changed

1 file changed

+101
-95
lines changed

src/sys/signal.rs

+101-95
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
use libc;
55
use {Errno, Error, Result};
6-
#[cfg(not(target_os = "openbsd"))]
7-
use std::fmt;
8-
#[cfg(not(target_os = "openbsd"))]
9-
use std::fmt::Debug;
106
use std::mem;
117
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
128
use std::os::unix::io::RawFd;
139
use std::ptr;
1410

11+
#[cfg(not(target_os = "openbsd"))]
12+
pub use self::sigevent::*;
13+
1514
// Currently there is only one definition of c_int in libc, as well as only one
1615
// type for signal constants.
1716
// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
@@ -490,106 +489,113 @@ pub enum SigevNotify {
490489
si_value: libc::intptr_t },
491490
}
492491

493-
/// Used to request asynchronous notification of the completion of certain
494-
/// events, such as POSIX AIO and timers.
495492
#[cfg(not(target_os = "openbsd"))]
496-
#[repr(C)]
497-
pub struct SigEvent {
498-
sigevent: libc::sigevent
499-
}
493+
mod sigevent {
494+
use libc;
495+
use std::mem;
496+
use std::ptr;
497+
use std::fmt::{self, Debug};
498+
use super::SigevNotify;
499+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
500+
use super::type_of_thread_id;
501+
502+
/// Used to request asynchronous notification of the completion of certain
503+
/// events, such as POSIX AIO and timers.
504+
#[repr(C)]
505+
pub struct SigEvent {
506+
sigevent: libc::sigevent
507+
}
508+
509+
impl SigEvent {
510+
// Note: this constructor does not allow the user to set the
511+
// sigev_notify_kevent_flags field. That's considered ok because on FreeBSD
512+
// at least those flags don't do anything useful. That field is part of a
513+
// union that shares space with the more genuinely useful
514+
// Note: This constructor also doesn't allow the caller to set the
515+
// sigev_notify_function or sigev_notify_attributes fields, which are
516+
// required for SIGEV_THREAD. That's considered ok because on no operating
517+
// system is SIGEV_THREAD the most efficient way to deliver AIO
518+
// notification. FreeBSD and Dragonfly programs should prefer SIGEV_KEVENT.
519+
// Linux, Solaris, and portable programs should prefer SIGEV_THREAD_ID or
520+
// SIGEV_SIGNAL. That field is part of a union that shares space with the
521+
// more genuinely useful sigev_notify_thread_id
522+
pub fn new(sigev_notify: SigevNotify) -> SigEvent {
523+
let mut sev = unsafe { mem::zeroed::<libc::sigevent>()};
524+
sev.sigev_notify = match sigev_notify {
525+
SigevNotify::SigevNone => libc::SIGEV_NONE,
526+
SigevNotify::SigevSignal{..} => libc::SIGEV_SIGNAL,
527+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
528+
SigevNotify::SigevKevent{..} => libc::SIGEV_KEVENT,
529+
#[cfg(target_os = "freebsd")]
530+
SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID,
531+
#[cfg(all(target_os = "linux", target_env = "gnu", not(target_arch = "mips")))]
532+
SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID,
533+
#[cfg(any(all(target_os = "linux", target_env = "musl"), target_arch = "mips"))]
534+
SigevNotify::SigevThreadId{..} => 4 // No SIGEV_THREAD_ID defined
535+
};
536+
sev.sigev_signo = match sigev_notify {
537+
SigevNotify::SigevSignal{ signal, .. } => signal as libc::c_int,
538+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
539+
SigevNotify::SigevKevent{ kq, ..} => kq,
540+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
541+
SigevNotify::SigevThreadId{ signal, .. } => signal as libc::c_int,
542+
_ => 0
543+
};
544+
sev.sigev_value.sival_ptr = match sigev_notify {
545+
SigevNotify::SigevNone => ptr::null_mut::<libc::c_void>(),
546+
SigevNotify::SigevSignal{ si_value, .. } => si_value as *mut libc::c_void,
547+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
548+
SigevNotify::SigevKevent{ udata, .. } => udata as *mut libc::c_void,
549+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
550+
SigevNotify::SigevThreadId{ si_value, .. } => si_value as *mut libc::c_void,
551+
};
552+
SigEvent::set_tid(&mut sev, &sigev_notify);
553+
SigEvent{sigevent: sev}
554+
}
500555

501-
#[cfg(not(target_os = "openbsd"))]
502-
impl SigEvent {
503-
// Note: this constructor does not allow the user to set the
504-
// sigev_notify_kevent_flags field. That's considered ok because on FreeBSD
505-
// at least those flags don't do anything useful. That field is part of a
506-
// union that shares space with the more genuinely useful
507-
// Note: This constructor also doesn't allow the caller to set the
508-
// sigev_notify_function or sigev_notify_attributes fields, which are
509-
// required for SIGEV_THREAD. That's considered ok because on no operating
510-
// system is SIGEV_THREAD the most efficient way to deliver AIO
511-
// notification. FreeBSD and Dragonfly programs should prefer SIGEV_KEVENT.
512-
// Linux, Solaris, and portable programs should prefer SIGEV_THREAD_ID or
513-
// SIGEV_SIGNAL. That field is part of a union that shares space with the
514-
// more genuinely useful sigev_notify_thread_id
515-
pub fn new(sigev_notify: SigevNotify) -> SigEvent {
516-
let mut sev = unsafe { mem::zeroed::<libc::sigevent>()};
517-
sev.sigev_notify = match sigev_notify {
518-
SigevNotify::SigevNone => libc::SIGEV_NONE,
519-
SigevNotify::SigevSignal{..} => libc::SIGEV_SIGNAL,
520-
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
521-
SigevNotify::SigevKevent{..} => libc::SIGEV_KEVENT,
522-
#[cfg(target_os = "freebsd")]
523-
SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID,
524-
#[cfg(all(target_os = "linux", target_env = "gnu", not(target_arch = "mips")))]
525-
SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID,
526-
#[cfg(any(all(target_os = "linux", target_env = "musl"), target_arch = "mips"))]
527-
SigevNotify::SigevThreadId{..} => 4 // No SIGEV_THREAD_ID defined
528-
};
529-
sev.sigev_signo = match sigev_notify {
530-
SigevNotify::SigevSignal{ signal, .. } => signal as libc::c_int,
531-
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
532-
SigevNotify::SigevKevent{ kq, ..} => kq,
533-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
534-
SigevNotify::SigevThreadId{ signal, .. } => signal as libc::c_int,
535-
_ => 0
536-
};
537-
sev.sigev_value.sival_ptr = match sigev_notify {
538-
SigevNotify::SigevNone => ptr::null_mut::<libc::c_void>(),
539-
SigevNotify::SigevSignal{ si_value, .. } => si_value as *mut libc::c_void,
540-
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
541-
SigevNotify::SigevKevent{ udata, .. } => udata as *mut libc::c_void,
542-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
543-
SigevNotify::SigevThreadId{ si_value, .. } => si_value as *mut libc::c_void,
544-
};
545-
SigEvent::set_tid(&mut sev, &sigev_notify);
546-
SigEvent{sigevent: sev}
547-
}
556+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
557+
fn set_tid(sev: &mut libc::sigevent, sigev_notify: &SigevNotify) {
558+
sev.sigev_notify_thread_id = match sigev_notify {
559+
&SigevNotify::SigevThreadId { thread_id, .. } => thread_id,
560+
_ => 0 as type_of_thread_id
561+
};
562+
}
548563

549-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
550-
fn set_tid(sev: &mut libc::sigevent, sigev_notify: &SigevNotify) {
551-
sev.sigev_notify_thread_id = match sigev_notify {
552-
&SigevNotify::SigevThreadId { thread_id, .. } => thread_id,
553-
_ => 0 as type_of_thread_id
554-
};
555-
}
564+
#[cfg(not(any(target_os = "freebsd", target_os = "linux")))]
565+
fn set_tid(_sev: &mut libc::sigevent, _sigev_notify: &SigevNotify) {
566+
}
556567

557-
#[cfg(not(any(target_os = "freebsd", target_os = "linux")))]
558-
fn set_tid(_sev: &mut libc::sigevent, _sigev_notify: &SigevNotify) {
568+
pub fn sigevent(&self) -> libc::sigevent {
569+
self.sigevent
570+
}
559571
}
560572

561-
pub fn sigevent(&self) -> libc::sigevent {
562-
self.sigevent
563-
}
564-
}
573+
impl Debug for SigEvent {
574+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
575+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
576+
fmt.debug_struct("SigEvent")
577+
.field("sigev_notify", &self.sigevent.sigev_notify)
578+
.field("sigev_signo", &self.sigevent.sigev_signo)
579+
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
580+
.field("sigev_notify_thread_id",
581+
&self.sigevent.sigev_notify_thread_id)
582+
.finish()
583+
}
565584

566-
#[cfg(not(target_os = "openbsd"))]
567-
impl Debug for SigEvent {
568-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
569-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
570-
fmt.debug_struct("SigEvent")
571-
.field("sigev_notify", &self.sigevent.sigev_notify)
572-
.field("sigev_signo", &self.sigevent.sigev_signo)
573-
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
574-
.field("sigev_notify_thread_id",
575-
&self.sigevent.sigev_notify_thread_id)
576-
.finish()
577-
}
578-
579-
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
580-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
581-
fmt.debug_struct("SigEvent")
582-
.field("sigev_notify", &self.sigevent.sigev_notify)
583-
.field("sigev_signo", &self.sigevent.sigev_signo)
584-
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
585-
.finish()
585+
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
586+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
587+
fmt.debug_struct("SigEvent")
588+
.field("sigev_notify", &self.sigevent.sigev_notify)
589+
.field("sigev_signo", &self.sigevent.sigev_signo)
590+
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
591+
.finish()
592+
}
586593
}
587-
}
588594

589-
#[cfg(not(target_os = "openbsd"))]
590-
impl<'a> From<&'a libc::sigevent> for SigEvent {
591-
fn from(sigevent: &libc::sigevent) -> Self {
592-
SigEvent{ sigevent: sigevent.clone() }
595+
impl<'a> From<&'a libc::sigevent> for SigEvent {
596+
fn from(sigevent: &libc::sigevent) -> Self {
597+
SigEvent{ sigevent: sigevent.clone() }
598+
}
593599
}
594600
}
595601

0 commit comments

Comments
 (0)