evio/internal/internal_linux.go

54 lines
1.2 KiB
Go
Raw Normal View History

2017-11-02 18:08:18 +03:00
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2017-10-28 22:23:13 +03:00
package internal
import (
"syscall"
"time"
)
func AddWrite(p, fd int, on *bool) error {
if *on {
return nil
}
*on = true
return syscall.EpollCtl(p, syscall.EPOLL_CTL_MOD, fd,
&syscall.EpollEvent{Fd: int32(fd),
Events: syscall.EPOLLIN | syscall.EPOLLOUT,
})
}
func DelWrite(p, fd int, on *bool) error {
if !*on {
return nil
}
*on = false
return syscall.EpollCtl(p, syscall.EPOLL_CTL_MOD, fd,
&syscall.EpollEvent{Fd: int32(fd),
Events: syscall.EPOLLIN,
})
}
func MakePoll() (p int, err error) {
return syscall.EpollCreate1(0)
}
func AddRead(p, fd int) error {
return syscall.EpollCtl(p, syscall.EPOLL_CTL_ADD, fd,
&syscall.EpollEvent{Fd: int32(fd),
Events: syscall.EPOLLIN,
})
}
func MakeEvents(n int) interface{} {
return make([]syscall.EpollEvent, n)
}
func Wait(p int, evs interface{}, timeout time.Duration) (n int, err error) {
if timeout < 0 {
timeout = 0
}
ts := int(timeout / time.Millisecond)
return syscall.EpollWait(p, evs.([]syscall.EpollEvent), ts)
}
func GetFD(evs interface{}, i int) int {
return int(evs.([]syscall.EpollEvent)[i].Fd)
}