epoll_arm64.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // +build linux,arm64
  2. package archutils
  3. // #include <sys/epoll.h>
  4. /*
  5. int EpollCreate1(int flag) {
  6. return epoll_create1(flag);
  7. }
  8. int EpollCtl(int efd, int op,int sfd, int events, int fd) {
  9. struct epoll_event event;
  10. event.events = events;
  11. event.data.fd = fd;
  12. return epoll_ctl(efd, op, sfd, &event);
  13. }
  14. struct event_t {
  15. uint32_t events;
  16. int fd;
  17. };
  18. struct epoll_event events[128];
  19. int run_epoll_wait(int fd, struct event_t *event) {
  20. int n, i;
  21. n = epoll_wait(fd, events, 128, -1);
  22. for (i = 0; i < n; i++) {
  23. event[i].events = events[i].events;
  24. event[i].fd = events[i].data.fd;
  25. }
  26. return n;
  27. }
  28. */
  29. import "C"
  30. import (
  31. "fmt"
  32. "syscall"
  33. "unsafe"
  34. )
  35. func EpollCreate1(flag int) (int, error) {
  36. fd := int(C.EpollCreate1(C.int(flag)))
  37. if fd < 0 {
  38. return fd, fmt.Errorf("failed to create epoll, errno is %d", fd)
  39. }
  40. return fd, nil
  41. }
  42. func EpollCtl(epfd int, op int, fd int, event *syscall.EpollEvent) error {
  43. errno := C.EpollCtl(C.int(epfd), C.int(syscall.EPOLL_CTL_ADD), C.int(fd), C.int(event.Events), C.int(event.Fd))
  44. if errno < 0 {
  45. return fmt.Errorf("Failed to ctl epoll")
  46. }
  47. return nil
  48. }
  49. func EpollWait(epfd int, events []syscall.EpollEvent, msec int) (int, error) {
  50. var c_events [128]C.struct_event_t
  51. n := int(C.run_epoll_wait(C.int(epfd), (*C.struct_event_t)(unsafe.Pointer(&c_events))))
  52. if n < 0 {
  53. return int(n), fmt.Errorf("Failed to wait epoll")
  54. }
  55. for i := 0; i < n; i++ {
  56. events[i].Fd = int32(c_events[i].fd)
  57. events[i].Events = uint32(c_events[i].events)
  58. }
  59. return int(n), nil
  60. }