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"
|
|
|
|
)
|
|
|
|
|
2018-05-24 02:49:45 +03:00
|
|
|
// Poll ...
|
|
|
|
type Poll struct {
|
|
|
|
fd int
|
|
|
|
changes []syscall.Kevent_t
|
|
|
|
notes noteQueue
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2018-05-24 02:49:45 +03:00
|
|
|
|
|
|
|
// OpenPoll ...
|
|
|
|
func OpenPoll() *Poll {
|
|
|
|
l := new(Poll)
|
|
|
|
p, err := syscall.Kqueue()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2018-05-24 02:49:45 +03:00
|
|
|
l.fd = p
|
|
|
|
_, err = syscall.Kevent(l.fd, []syscall.Kevent_t{{
|
|
|
|
Ident: 0,
|
|
|
|
Filter: syscall.EVFILT_USER,
|
|
|
|
Flags: syscall.EV_ADD | syscall.EV_CLEAR,
|
|
|
|
}}, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-24 02:49:45 +03:00
|
|
|
// Close ...
|
|
|
|
func (p *Poll) Close() error {
|
|
|
|
return syscall.Close(p.fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger ...
|
|
|
|
func (p *Poll) Trigger(note interface{}) error {
|
|
|
|
p.notes.Add(note)
|
|
|
|
_, err := syscall.Kevent(p.fd, []syscall.Kevent_t{{
|
|
|
|
Ident: 0,
|
|
|
|
Filter: syscall.EVFILT_USER,
|
|
|
|
Fflags: syscall.NOTE_TRIGGER,
|
|
|
|
}}, nil, nil)
|
2017-11-09 02:45:30 +03:00
|
|
|
return err
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2018-05-24 02:49:45 +03:00
|
|
|
|
|
|
|
// Wait ...
|
|
|
|
func (p *Poll) Wait(iter func(fd int, note interface{}) error) error {
|
|
|
|
events := make([]syscall.Kevent_t, 128)
|
|
|
|
for {
|
|
|
|
n, err := syscall.Kevent(p.fd, p.changes, events, nil)
|
|
|
|
if err != nil && err != syscall.EINTR {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.changes = p.changes[:0]
|
|
|
|
if err := p.notes.ForEach(func(note interface{}) error {
|
|
|
|
return iter(0, note)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if fd := int(events[i].Ident); fd != 0 {
|
|
|
|
if err := iter(fd, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
|
2018-05-24 02:49:45 +03:00
|
|
|
// AddRead ...
|
|
|
|
func (p *Poll) AddRead(fd int) {
|
|
|
|
p.changes = append(p.changes,
|
|
|
|
syscall.Kevent_t{
|
|
|
|
Ident: uint64(fd), Flags: syscall.EV_ADD, Filter: syscall.EVFILT_READ,
|
|
|
|
},
|
|
|
|
)
|
2017-11-09 02:45:30 +03:00
|
|
|
}
|
2018-05-24 02:49:45 +03:00
|
|
|
|
|
|
|
// AddReadWrite ...
|
|
|
|
func (p *Poll) AddReadWrite(fd int) {
|
|
|
|
p.changes = append(p.changes,
|
|
|
|
syscall.Kevent_t{
|
|
|
|
Ident: uint64(fd), Flags: syscall.EV_ADD, Filter: syscall.EVFILT_READ,
|
|
|
|
},
|
|
|
|
syscall.Kevent_t{
|
|
|
|
Ident: uint64(fd), Flags: syscall.EV_ADD, Filter: syscall.EVFILT_WRITE,
|
|
|
|
},
|
|
|
|
)
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2018-05-24 02:49:45 +03:00
|
|
|
|
|
|
|
// ModRead ...
|
|
|
|
func (p *Poll) ModRead(fd int) {
|
|
|
|
p.changes = append(p.changes, syscall.Kevent_t{
|
|
|
|
Ident: uint64(fd), Flags: syscall.EV_DELETE, Filter: syscall.EVFILT_WRITE,
|
|
|
|
})
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2018-05-24 02:49:45 +03:00
|
|
|
|
|
|
|
// ModReadWrite ...
|
|
|
|
func (p *Poll) ModReadWrite(fd int) {
|
|
|
|
p.changes = append(p.changes, syscall.Kevent_t{
|
|
|
|
Ident: uint64(fd), Flags: syscall.EV_ADD, Filter: syscall.EVFILT_WRITE,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModDetach ...
|
|
|
|
func (p *Poll) ModDetach(fd int) {
|
|
|
|
p.changes = append(p.changes,
|
|
|
|
syscall.Kevent_t{
|
|
|
|
Ident: uint64(fd), Flags: syscall.EV_DELETE, Filter: syscall.EVFILT_READ,
|
|
|
|
},
|
|
|
|
syscall.Kevent_t{
|
|
|
|
Ident: uint64(fd), Flags: syscall.EV_DELETE, Filter: syscall.EVFILT_WRITE,
|
|
|
|
},
|
|
|
|
)
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|