2016-04-29 11:42:41 +00:00
|
|
|
// +build linux,arm64
|
2016-04-06 03:17:30 +00:00
|
|
|
|
2016-09-22 21:08:41 +00:00
|
|
|
package epoll
|
2016-04-06 03:17:30 +00:00
|
|
|
|
|
|
|
// #include <sys/epoll.h>
|
|
|
|
/*
|
|
|
|
int EpollCreate1(int flag) {
|
2016-04-13 06:08:28 +00:00
|
|
|
return epoll_create1(flag);
|
2016-04-06 03:17:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:08:28 +00:00
|
|
|
int EpollCtl(int efd, int op,int sfd, int events, int fd) {
|
|
|
|
struct epoll_event event;
|
|
|
|
event.events = events;
|
|
|
|
event.data.fd = fd;
|
2016-04-06 03:17:30 +00:00
|
|
|
|
2016-04-13 06:08:28 +00:00
|
|
|
return epoll_ctl(efd, op, sfd, &event);
|
2016-04-06 03:17:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:08:28 +00:00
|
|
|
struct event_t {
|
|
|
|
uint32_t events;
|
|
|
|
int fd;
|
2016-04-06 03:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct epoll_event events[128];
|
2016-04-13 06:08:28 +00:00
|
|
|
int run_epoll_wait(int fd, struct event_t *event) {
|
|
|
|
int n, i;
|
|
|
|
n = epoll_wait(fd, events, 128, -1);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
event[i].events = events[i].events;
|
|
|
|
event[i].fd = events[i].data.fd;
|
|
|
|
}
|
|
|
|
return n;
|
2016-04-06 03:17:30 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// EpollCreate1 calls a C implementation
|
2016-04-06 03:17:30 +00:00
|
|
|
func EpollCreate1(flag int) (int, error) {
|
2016-04-11 07:01:36 +00:00
|
|
|
fd := int(C.EpollCreate1(C.int(flag)))
|
2016-04-06 03:17:30 +00:00
|
|
|
if fd < 0 {
|
|
|
|
return fd, fmt.Errorf("failed to create epoll, errno is %d", fd)
|
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// EpollCtl calls a C implementation
|
2016-04-06 03:17:30 +00:00
|
|
|
func EpollCtl(epfd int, op int, fd int, event *syscall.EpollEvent) error {
|
|
|
|
errno := C.EpollCtl(C.int(epfd), C.int(syscall.EPOLL_CTL_ADD), C.int(fd), C.int(event.Events), C.int(event.Fd))
|
|
|
|
if errno < 0 {
|
|
|
|
return fmt.Errorf("Failed to ctl epoll")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// EpollWait calls a C implementation
|
2016-04-06 03:17:30 +00:00
|
|
|
func EpollWait(epfd int, events []syscall.EpollEvent, msec int) (int, error) {
|
2016-04-13 06:08:28 +00:00
|
|
|
var c_events [128]C.struct_event_t
|
|
|
|
n := int(C.run_epoll_wait(C.int(epfd), (*C.struct_event_t)(unsafe.Pointer(&c_events))))
|
2016-04-06 03:17:30 +00:00
|
|
|
if n < 0 {
|
|
|
|
return int(n), fmt.Errorf("Failed to wait epoll")
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
events[i].Fd = int32(c_events[i].fd)
|
|
|
|
events[i].Events = uint32(c_events[i].events)
|
|
|
|
}
|
|
|
|
return int(n), nil
|
|
|
|
}
|