This commit is contained in:
Andrew Kelley 2018-07-09 01:23:47 -04:00
parent 3ba451778f
commit a0c564d762
4 changed files with 233 additions and 102 deletions

View File

@ -7,11 +7,24 @@ pub extern "c" fn mach_absolute_time() u64;
pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void; pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void;
pub extern "c" fn kqueue() c_int; pub extern "c" fn kqueue() c_int;
pub extern "c" fn kevent(kq: c_int, changelist: [*]const Kevent, nchanges: c_int, pub extern "c" fn kevent(
eventlist: [*]Kevent, nevents: c_int, timeout: ?*const timespec) c_int; kq: c_int,
changelist: [*]const Kevent,
nchanges: c_int,
eventlist: [*]Kevent,
nevents: c_int,
timeout: ?*const timespec,
) c_int;
pub extern "c" fn kevent64(kq: c_int, changelist: [*]const kevent64_s, nchanges: c_int, pub extern "c" fn kevent64(
eventlist: [*]kevent64_s, nevents: c_int, flags: c_uint, timeout: ?*const timespec) c_int; kq: c_int,
changelist: [*]const kevent64_s,
nchanges: c_int,
eventlist: [*]kevent64_s,
nevents: c_int,
flags: c_uint,
timeout: ?*const timespec,
) c_int;
pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
@ -145,4 +158,3 @@ comptime {
assert(@offsetOf(kevent64_s, "udata") == 24); assert(@offsetOf(kevent64_s, "udata") == 24);
assert(@offsetOf(kevent64_s, "ext") == 32); assert(@offsetOf(kevent64_s, "ext") == 32);
} }

View File

@ -264,17 +264,32 @@ pub const SIGUSR1 = 30;
/// user defined signal 2 /// user defined signal 2
pub const SIGUSR2 = 31; pub const SIGUSR2 = 31;
pub const KEVENT_FLAG_NONE = 0x000; /// no flag value /// no flag value
pub const KEVENT_FLAG_IMMEDIATE = 0x001; /// immediate timeout pub const KEVENT_FLAG_NONE = 0x000;
pub const KEVENT_FLAG_ERROR_EVENTS = 0x002; /// output events only include change
pub const EV_ADD = 0x0001; /// add event to kq (implies enable) /// immediate timeout
pub const EV_DELETE = 0x0002; /// delete event from kq pub const KEVENT_FLAG_IMMEDIATE = 0x001;
pub const EV_ENABLE = 0x0004; /// enable event
pub const EV_DISABLE = 0x0008; /// disable event (not reported)
pub const EV_ONESHOT = 0x0010; /// only report one occurrence /// output events only include change
pub const EV_CLEAR = 0x0020; /// clear event state after reporting pub const KEVENT_FLAG_ERROR_EVENTS = 0x002;
/// add event to kq (implies enable)
pub const EV_ADD = 0x0001;
/// delete event from kq
pub const EV_DELETE = 0x0002;
/// enable event
pub const EV_ENABLE = 0x0004;
/// disable event (not reported)
pub const EV_DISABLE = 0x0008;
/// only report one occurrence
pub const EV_ONESHOT = 0x0010;
/// clear event state after reporting
pub const EV_CLEAR = 0x0020;
/// force immediate event output /// force immediate event output
/// ... with or without EV_ERROR /// ... with or without EV_ERROR
@ -282,8 +297,11 @@ pub const EV_CLEAR = 0x0020; /// clear event state after reporting
/// on syscalls supporting flags /// on syscalls supporting flags
pub const EV_RECEIPT = 0x0040; pub const EV_RECEIPT = 0x0040;
pub const EV_DISPATCH = 0x0080; /// disable event after reporting /// disable event after reporting
pub const EV_UDATA_SPECIFIC = 0x0100; /// unique kevent per udata value pub const EV_DISPATCH = 0x0080;
/// unique kevent per udata value
pub const EV_UDATA_SPECIFIC = 0x0100;
/// ... in combination with EV_DELETE /// ... in combination with EV_DELETE
/// will defer delete until udata-specific /// will defer delete until udata-specific
@ -291,91 +309,178 @@ pub const EV_UDATA_SPECIFIC = 0x0100; /// unique kevent per udata value
/// returned to indicate the deferral /// returned to indicate the deferral
pub const EV_DISPATCH2 = EV_DISPATCH | EV_UDATA_SPECIFIC; pub const EV_DISPATCH2 = EV_DISPATCH | EV_UDATA_SPECIFIC;
/// report that source has vanished /// report that source has vanished
/// ... only valid with EV_DISPATCH2 /// ... only valid with EV_DISPATCH2
pub const EV_VANISHED = 0x0200; pub const EV_VANISHED = 0x0200;
pub const EV_SYSFLAGS = 0xF000; /// reserved by system /// reserved by system
pub const EV_FLAG0 = 0x1000; /// filter-specific flag pub const EV_SYSFLAGS = 0xF000;
pub const EV_FLAG1 = 0x2000; /// filter-specific flag
pub const EV_EOF = 0x8000; /// EOF detected /// filter-specific flag
pub const EV_ERROR = 0x4000; /// error, data contains errno pub const EV_FLAG0 = 0x1000;
/// filter-specific flag
pub const EV_FLAG1 = 0x2000;
/// EOF detected
pub const EV_EOF = 0x8000;
/// error, data contains errno
pub const EV_ERROR = 0x4000;
pub const EV_POLL = EV_FLAG0; pub const EV_POLL = EV_FLAG0;
pub const EV_OOBAND = EV_FLAG1; pub const EV_OOBAND = EV_FLAG1;
pub const EVFILT_READ = -1; pub const EVFILT_READ = -1;
pub const EVFILT_WRITE = -2; pub const EVFILT_WRITE = -2;
pub const EVFILT_AIO = -3; /// attached to aio requests
pub const EVFILT_VNODE = -4; /// attached to vnodes
pub const EVFILT_PROC = -5; /// attached to struct proc
pub const EVFILT_SIGNAL = -6; /// attached to struct proc
pub const EVFILT_TIMER = -7; /// timers
pub const EVFILT_MACHPORT = -8; /// Mach portsets
pub const EVFILT_FS = -9; /// Filesystem events
pub const EVFILT_USER = -10; /// User events
pub const EVFILT_VM = -12; /// Virtual memory events
pub const EVFILT_EXCEPT = -15; /// Exception events /// attached to aio requests
pub const EVFILT_AIO = -3;
/// attached to vnodes
pub const EVFILT_VNODE = -4;
/// attached to struct proc
pub const EVFILT_PROC = -5;
/// attached to struct proc
pub const EVFILT_SIGNAL = -6;
/// timers
pub const EVFILT_TIMER = -7;
/// Mach portsets
pub const EVFILT_MACHPORT = -8;
/// Filesystem events
pub const EVFILT_FS = -9;
/// User events
pub const EVFILT_USER = -10;
/// Virtual memory events
pub const EVFILT_VM = -12;
/// Exception events
pub const EVFILT_EXCEPT = -15;
pub const EVFILT_SYSCOUNT = 17; pub const EVFILT_SYSCOUNT = 17;
/// On input, NOTE_TRIGGER causes the event to be triggered for output. /// On input, NOTE_TRIGGER causes the event to be triggered for output.
pub const NOTE_TRIGGER = 0x01000000; pub const NOTE_TRIGGER = 0x01000000;
pub const NOTE_FFNOP = 0x00000000; /// ignore input fflags /// ignore input fflags
pub const NOTE_FFAND = 0x40000000; /// and fflags pub const NOTE_FFNOP = 0x00000000;
pub const NOTE_FFOR = 0x80000000; /// or fflags
pub const NOTE_FFCOPY = 0xc0000000; /// copy fflags /// and fflags
pub const NOTE_FFCTRLMASK = 0xc0000000; /// mask for operations pub const NOTE_FFAND = 0x40000000;
/// or fflags
pub const NOTE_FFOR = 0x80000000;
/// copy fflags
pub const NOTE_FFCOPY = 0xc0000000;
/// mask for operations
pub const NOTE_FFCTRLMASK = 0xc0000000;
pub const NOTE_FFLAGSMASK = 0x00ffffff; pub const NOTE_FFLAGSMASK = 0x00ffffff;
pub const NOTE_LOWAT = 0x00000001; /// low water mark /// low water mark
pub const NOTE_LOWAT = 0x00000001;
pub const NOTE_OOB = 0x00000002; /// OOB data /// OOB data
pub const NOTE_OOB = 0x00000002;
pub const NOTE_DELETE = 0x00000001; /// vnode was removed /// vnode was removed
pub const NOTE_WRITE = 0x00000002; /// data contents changed pub const NOTE_DELETE = 0x00000001;
pub const NOTE_EXTEND = 0x00000004; /// size increased
pub const NOTE_ATTRIB = 0x00000008; /// attributes changed
pub const NOTE_LINK = 0x00000010; /// link count changed
pub const NOTE_RENAME = 0x00000020; /// vnode was renamed
pub const NOTE_REVOKE = 0x00000040; /// vnode access was revoked
pub const NOTE_NONE = 0x00000080; /// No specific vnode event: to test for EVFILT_READ activation
pub const NOTE_FUNLOCK = 0x00000100; /// vnode was unlocked by flock(2)
pub const NOTE_EXIT = 0x80000000; /// process exited /// data contents changed
pub const NOTE_FORK = 0x40000000; /// process forked pub const NOTE_WRITE = 0x00000002;
pub const NOTE_EXEC = 0x20000000; /// process exec'd
pub const NOTE_SIGNAL = 0x08000000; /// shared with EVFILT_SIGNAL
pub const NOTE_EXITSTATUS = 0x04000000; /// exit status to be returned, valid for child process only
pub const NOTE_EXIT_DETAIL = 0x02000000; /// provide details on reasons for exit
pub const NOTE_PDATAMASK = 0x000fffff; /// mask for signal & exit status /// size increased
pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK); pub const NOTE_EXTEND = 0x00000004;
pub const NOTE_EXIT_DETAIL_MASK = 0x00070000; /// attributes changed
pub const NOTE_EXIT_DECRYPTFAIL = 0x00010000; pub const NOTE_ATTRIB = 0x00000008;
pub const NOTE_EXIT_MEMORY = 0x00020000;
pub const NOTE_EXIT_CSERROR = 0x00040000;
/// link count changed
pub const NOTE_LINK = 0x00000010;
pub const NOTE_VM_PRESSURE = 0x80000000; /// will react on memory pressure /// vnode was renamed
pub const NOTE_VM_PRESSURE_TERMINATE = 0x40000000; /// will quit on memory pressure, possibly after cleaning up dirty state pub const NOTE_RENAME = 0x00000020;
pub const NOTE_VM_PRESSURE_SUDDEN_TERMINATE = 0x20000000; /// will quit immediately on memory pressure
pub const NOTE_VM_ERROR = 0x10000000; /// there was an error
pub const NOTE_SECONDS = 0x00000001; /// data is seconds /// vnode access was revoked
pub const NOTE_USECONDS = 0x00000002; /// data is microseconds pub const NOTE_REVOKE = 0x00000040;
pub const NOTE_NSECONDS = 0x00000004; /// data is nanoseconds
pub const NOTE_ABSOLUTE = 0x00000008; /// absolute timeout
pub const NOTE_LEEWAY = 0x00000010; /// ext[1] holds leeway for power aware timers /// No specific vnode event: to test for EVFILT_READ activation
pub const NOTE_CRITICAL = 0x00000020; /// system does minimal timer coalescing pub const NOTE_NONE = 0x00000080;
pub const NOTE_BACKGROUND = 0x00000040; /// system does maximum timer coalescing
pub const NOTE_MACH_CONTINUOUS_TIME = 0x00000080;
pub const NOTE_MACHTIME = 0x00000100; /// data is mach absolute time units
/// vnode was unlocked by flock(2)
pub const NOTE_FUNLOCK = 0x00000100;
/// process exited
pub const NOTE_EXIT = 0x80000000;
/// process forked
pub const NOTE_FORK = 0x40000000;
/// process exec'd
pub const NOTE_EXEC = 0x20000000;
/// shared with EVFILT_SIGNAL
pub const NOTE_SIGNAL = 0x08000000;
/// exit status to be returned, valid for child process only
pub const NOTE_EXITSTATUS = 0x04000000;
/// provide details on reasons for exit
pub const NOTE_EXIT_DETAIL = 0x02000000;
/// mask for signal & exit status
pub const NOTE_PDATAMASK = 0x000fffff;
pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK);
pub const NOTE_EXIT_DETAIL_MASK = 0x00070000;
pub const NOTE_EXIT_DECRYPTFAIL = 0x00010000;
pub const NOTE_EXIT_MEMORY = 0x00020000;
pub const NOTE_EXIT_CSERROR = 0x00040000;
/// will react on memory pressure
pub const NOTE_VM_PRESSURE = 0x80000000;
/// will quit on memory pressure, possibly after cleaning up dirty state
pub const NOTE_VM_PRESSURE_TERMINATE = 0x40000000;
/// will quit immediately on memory pressure
pub const NOTE_VM_PRESSURE_SUDDEN_TERMINATE = 0x20000000;
/// there was an error
pub const NOTE_VM_ERROR = 0x10000000;
/// data is seconds
pub const NOTE_SECONDS = 0x00000001;
/// data is microseconds
pub const NOTE_USECONDS = 0x00000002;
/// data is nanoseconds
pub const NOTE_NSECONDS = 0x00000004;
/// absolute timeout
pub const NOTE_ABSOLUTE = 0x00000008;
/// ext[1] holds leeway for power aware timers
pub const NOTE_LEEWAY = 0x00000010;
/// system does minimal timer coalescing
pub const NOTE_CRITICAL = 0x00000020;
/// system does maximum timer coalescing
pub const NOTE_BACKGROUND = 0x00000040;
pub const NOTE_MACH_CONTINUOUS_TIME = 0x00000080;
/// data is mach absolute time units
pub const NOTE_MACHTIME = 0x00000100;
fn wstatus(x: i32) i32 { fn wstatus(x: i32) i32 {
return x & 0o177; return x & 0o177;
@ -503,12 +608,23 @@ pub fn kqueue() usize {
} }
pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize { pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize {
return errnoWrap(c.kevent(kq, changelist.ptr, @intCast(c_int, changelist.len), eventlist.ptr, @intCast(c_int, eventlist.len), timeout,)); return errnoWrap(c.kevent(
kq,
changelist.ptr,
@intCast(c_int, changelist.len),
eventlist.ptr,
@intCast(c_int, eventlist.len),
timeout,
));
} }
pub fn kevent64(kq: i32, changelist: []const kevent64_s, eventlist: []kevent64_s, flags: u32, pub fn kevent64(
timeout: ?*const timespec) usize kq: i32,
{ changelist: []const kevent64_s,
eventlist: []kevent64_s,
flags: u32,
timeout: ?*const timespec,
) usize {
return errnoWrap(c.kevent64(kq, changelist.ptr, changelist.len, eventlist.ptr, eventlist.len, flags, timeout)); return errnoWrap(c.kevent64(kq, changelist.ptr, changelist.len, eventlist.ptr, eventlist.len, flags, timeout));
} }

View File

@ -2810,7 +2810,7 @@ pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize {
} }
} }
pub const BsdKQueueError = error { pub const BsdKQueueError = error{
/// The per-process limit on the number of open file descriptors has been reached. /// The per-process limit on the number of open file descriptors has been reached.
ProcessFdQuotaExceeded, ProcessFdQuotaExceeded,
@ -2831,7 +2831,7 @@ pub fn bsdKQueue() BsdKQueueError!i32 {
} }
} }
pub const BsdKEventError = error { pub const BsdKEventError = error{
/// The process does not have permission to register a filter. /// The process does not have permission to register a filter.
AccessDenied, AccessDenied,
@ -2845,9 +2845,12 @@ pub const BsdKEventError = error {
ProcessNotFound, ProcessNotFound,
}; };
pub fn bsdKEvent(kq: i32, changelist: []const posix.Kevent, eventlist: []posix.Kevent, pub fn bsdKEvent(
timeout: ?*const posix.timespec) BsdKEventError!usize kq: i32,
{ changelist: []const posix.Kevent,
eventlist: []posix.Kevent,
timeout: ?*const posix.timespec,
) BsdKEventError!usize {
while (true) { while (true) {
const rc = posix.kevent(kq, changelist, eventlist, timeout); const rc = posix.kevent(kq, changelist, eventlist, timeout);
const err = posix.getErrno(rc); const err = posix.getErrno(rc);

View File

@ -31,7 +31,7 @@ fn test__extendhfsf2(a: u16, expected: u32) void {
if (rep == expected) { if (rep == expected) {
if (rep & 0x7fffffff > 0x7f800000) { if (rep & 0x7fffffff > 0x7f800000) {
return; // NaN is always unequal. return; // NaN is always unequal.
} }
if (x == @bitCast(f32, expected)) { if (x == @bitCast(f32, expected)) {
return; return;
@ -86,33 +86,33 @@ test "extenddftf2" {
} }
test "extendhfsf2" { test "extendhfsf2" {
test__extendhfsf2(0x7e00, 0x7fc00000); // qNaN test__extendhfsf2(0x7e00, 0x7fc00000); // qNaN
test__extendhfsf2(0x7f00, 0x7fe00000); // sNaN test__extendhfsf2(0x7f00, 0x7fe00000); // sNaN
test__extendhfsf2(0x7c01, 0x7f802000); // sNaN test__extendhfsf2(0x7c01, 0x7f802000); // sNaN
test__extendhfsf2(0, 0); // 0 test__extendhfsf2(0, 0); // 0
test__extendhfsf2(0x8000, 0x80000000); // -0 test__extendhfsf2(0x8000, 0x80000000); // -0
test__extendhfsf2(0x7c00, 0x7f800000); // inf test__extendhfsf2(0x7c00, 0x7f800000); // inf
test__extendhfsf2(0xfc00, 0xff800000); // -inf test__extendhfsf2(0xfc00, 0xff800000); // -inf
test__extendhfsf2(0x0001, 0x33800000); // denormal (min), 2**-24 test__extendhfsf2(0x0001, 0x33800000); // denormal (min), 2**-24
test__extendhfsf2(0x8001, 0xb3800000); // denormal (min), -2**-24 test__extendhfsf2(0x8001, 0xb3800000); // denormal (min), -2**-24
test__extendhfsf2(0x03ff, 0x387fc000); // denormal (max), 2**-14 - 2**-24 test__extendhfsf2(0x03ff, 0x387fc000); // denormal (max), 2**-14 - 2**-24
test__extendhfsf2(0x83ff, 0xb87fc000); // denormal (max), -2**-14 + 2**-24 test__extendhfsf2(0x83ff, 0xb87fc000); // denormal (max), -2**-14 + 2**-24
test__extendhfsf2(0x0400, 0x38800000); // normal (min), 2**-14 test__extendhfsf2(0x0400, 0x38800000); // normal (min), 2**-14
test__extendhfsf2(0x8400, 0xb8800000); // normal (min), -2**-14 test__extendhfsf2(0x8400, 0xb8800000); // normal (min), -2**-14
test__extendhfsf2(0x7bff, 0x477fe000); // normal (max), 65504 test__extendhfsf2(0x7bff, 0x477fe000); // normal (max), 65504
test__extendhfsf2(0xfbff, 0xc77fe000); // normal (max), -65504 test__extendhfsf2(0xfbff, 0xc77fe000); // normal (max), -65504
test__extendhfsf2(0x3c01, 0x3f802000); // normal, 1 + 2**-10 test__extendhfsf2(0x3c01, 0x3f802000); // normal, 1 + 2**-10
test__extendhfsf2(0xbc01, 0xbf802000); // normal, -1 - 2**-10 test__extendhfsf2(0xbc01, 0xbf802000); // normal, -1 - 2**-10
test__extendhfsf2(0x3555, 0x3eaaa000); // normal, approx. 1/3 test__extendhfsf2(0x3555, 0x3eaaa000); // normal, approx. 1/3
test__extendhfsf2(0xb555, 0xbeaaa000); // normal, approx. -1/3 test__extendhfsf2(0xb555, 0xbeaaa000); // normal, approx. -1/3
} }
test "extendsftf2" { test "extendsftf2" {