2017-11-02 18:08:18 +03:00
|
|
|
// Copyright 2017 Joshua J Baker. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-10-28 22:23:13 +03:00
|
|
|
// +build darwin netbsd freebsd openbsd dragonfly
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-11-09 02:45:30 +03:00
|
|
|
func AddRead(p, fd int, readon, writeon *bool) error {
|
|
|
|
if readon != nil {
|
|
|
|
if *readon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*readon = true
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
|
|
|
_, err := syscall.Kevent(p,
|
|
|
|
[]syscall.Kevent_t{{Ident: uint64(fd),
|
2017-11-09 02:45:30 +03:00
|
|
|
Flags: syscall.EV_ADD, Filter: syscall.EVFILT_READ}},
|
2017-10-28 22:23:13 +03:00
|
|
|
nil, nil)
|
|
|
|
return err
|
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
func DelRead(p, fd int, readon, writeon *bool) error {
|
|
|
|
if readon != nil {
|
|
|
|
if !*readon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*readon = false
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
|
|
|
_, err := syscall.Kevent(p,
|
|
|
|
[]syscall.Kevent_t{{Ident: uint64(fd),
|
2017-11-09 02:45:30 +03:00
|
|
|
Flags: syscall.EV_DELETE, Filter: syscall.EVFILT_READ}},
|
2017-10-28 22:23:13 +03:00
|
|
|
nil, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-09 02:45:30 +03:00
|
|
|
func AddWrite(p, fd int, readon, writeon *bool) error {
|
|
|
|
if writeon != nil {
|
|
|
|
if *writeon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*writeon = true
|
|
|
|
}
|
|
|
|
_, err := syscall.Kevent(p,
|
|
|
|
[]syscall.Kevent_t{{Ident: uint64(fd),
|
|
|
|
Flags: syscall.EV_ADD, Filter: syscall.EVFILT_WRITE}},
|
|
|
|
nil, nil)
|
|
|
|
return err
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
func DelWrite(p, fd int, readon, writeon *bool) error {
|
|
|
|
if writeon != nil {
|
|
|
|
if !*writeon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*writeon = false
|
|
|
|
}
|
2017-10-28 22:23:13 +03:00
|
|
|
_, err := syscall.Kevent(p,
|
|
|
|
[]syscall.Kevent_t{{Ident: uint64(fd),
|
2017-11-09 02:45:30 +03:00
|
|
|
Flags: syscall.EV_DELETE, Filter: syscall.EVFILT_WRITE}},
|
2017-10-28 22:23:13 +03:00
|
|
|
nil, nil)
|
|
|
|
return err
|
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
|
|
|
|
func MakePoll() (p int, err error) {
|
|
|
|
return syscall.Kqueue()
|
|
|
|
}
|
2017-10-28 22:23:13 +03:00
|
|
|
func MakeEvents(n int) interface{} {
|
|
|
|
return make([]syscall.Kevent_t, n)
|
|
|
|
}
|
|
|
|
func Wait(p int, evs interface{}, timeout time.Duration) (n int, err error) {
|
|
|
|
if timeout < 0 {
|
|
|
|
timeout = 0
|
|
|
|
}
|
|
|
|
ts := syscall.NsecToTimespec(int64(timeout))
|
|
|
|
return syscall.Kevent(p, nil, evs.([]syscall.Kevent_t), &ts)
|
|
|
|
}
|
|
|
|
func GetFD(evs interface{}, i int) int {
|
|
|
|
return int(evs.([]syscall.Kevent_t)[i].Ident)
|
|
|
|
}
|