本文整理匯總了Golang中syscall.EpollWait函數的典型用法代碼示例。如果您正苦於以下問題:Golang EpollWait函數的具體用法?Golang EpollWait怎麽用?Golang EpollWait使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了EpollWait函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: AddEdgeDetect
func (gpio *GPIO) AddEdgeDetect(edge GPIOEdge) (chan bool, error) {
gpio.RemoveEdgeDetect()
err := gpio.SetDirection(GPIO_INPUT)
if err != nil {
return nil, err
}
err = gpio.SetEdge(edge)
if err != nil {
return nil, err
}
err = gpio.openValueFile()
if err != nil {
return nil, err
}
epfd, err := syscall.EpollCreate(1)
if err != nil {
return nil, err
}
event := &syscall.EpollEvent{
Events: syscall.EPOLLIN | _EPOLLET | syscall.EPOLLPRI,
Fd: int32(gpio.value.Fd()),
}
err = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, int(gpio.value.Fd()), event)
if err != nil {
syscall.Close(epfd)
return nil, err
}
// / first time triggers with current state, so ignore
_, err = syscall.EpollWait(epfd, make([]syscall.EpollEvent, 1), -1)
if err != nil {
syscall.Close(epfd)
return nil, err
}
gpio.epfd.Set(epfd)
valueChan := make(chan bool)
go func() {
for gpio.epfd.Get() != 0 {
n, _ := syscall.EpollWait(epfd, make([]syscall.EpollEvent, 1), -1)
if n > 0 {
value, err := gpio.Value()
if err == nil {
valueChan <- value
}
}
}
}()
return valueChan, nil
}
示例2: setupEpoll
// setupEpoll sets up epoll for use
func setupEpoll() {
var err error
epollFD, err = syscall.EpollCreate1(0)
if err != nil {
fmt.Println("Unable to create epoll FD: ", err.Error())
os.Exit(1)
}
go func() {
var epollEvents [GPIOCount]syscall.EpollEvent
for {
numEvents, err := syscall.EpollWait(epollFD, epollEvents[:], -1)
if err != nil {
panic(fmt.Sprintf("EpollWait error: %s", err.Error()))
}
for i := 0; i < numEvents; i++ {
if eventPin, exists := watchEventCallbacks[int(epollEvents[i].Fd)]; exists {
if eventPin.initial {
eventPin.initial = false
} else {
eventPin.callback()
}
}
}
}
}()
}
示例3: listen
/*-----------------------------------------------------------------------------
-- FUNCTION: listen
--
-- DATE: February 6, 2016
--
-- REVISIONS: February 10, 2016 - Modified for EPoll
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
-- INTERFACE: func listen(srvInfo serverInfo)
-- srvInfo: information about the overall application, IPC and networking
--
-- RETURNS: void doesn't actually return.
--
-- NOTES: This function handles Epoll in a foreverloop. This function never
-- exits
------------------------------------------------------------------------------*/
func listen(srvInfo serverInfo) {
client := make(map[int]connectionInfo)
epollFd, _ := syscall.EpollCreate1(0)
events := make([]syscall.EpollEvent, epollQueueLen)
addConnectionToEPoll(epollFd, srvInfo.listener)
for {
n, _ := syscall.EpollWait(epollFd, events[:], -1)
for i := 0; i < n; i++ {
if events[i].Events&(syscall.EPOLLHUP|syscall.EPOLLERR) != 0 {
fmt.Println("Error on epoll")
endConnection(srvInfo, client[int(events[i].Fd)])
}
if events[i].Fd == int32(srvInfo.listener) { // new connection
newClient, err := newConnection(srvInfo.listener)
if err == nil {
srvInfo.serverConnection <- newConnectionConst
client[newClient.FileDescriptor] = newClient
addConnectionToEPoll(epollFd, newClient.FileDescriptor)
}
} else { // data to read from connection
// client[int(ev.Fd)] can not be used as an argument in handleData
conn := client[int(events[i].Fd)]
err := handleData(&conn)
client[int(events[i].Fd)] = conn
if err != nil {
endConnection(srvInfo, client[int(events[i].Fd)])
}
}
}
}
}
示例4: initEpollListener
func initEpollListener() *epollListener {
fd, err := syscall.EpollCreate1(0)
if err != nil {
panic(fmt.Sprintf("Unable to create epoll: %v", err))
}
listener := &epollListener{fd: fd, interruptablePins: make(map[int]*interrupt)}
go func() {
var epollEvents [MaxGPIOInterrupt]syscall.EpollEvent
for {
n, err := syscall.EpollWait(listener.fd, epollEvents[:], -1)
if err != nil {
panic(fmt.Sprintf("EpollWait error: %v", err))
}
listener.mu.Lock()
for i := 0; i < n; i++ {
if irq, ok := listener.interruptablePins[int(epollEvents[i].Fd)]; ok {
irq.Signal()
}
}
listener.mu.Unlock()
}
}()
return listener
}
示例5: loop
// loop blocks until either inotify or pipe file descriptor is ready for I/O.
// All read operations triggered by filesystem notifications are forwarded to
// one of the event's consumers. If pipe fd became ready, loop function closes
// all file descriptors opened by lazyinit method and returns afterwards.
func (i *inotify) loop(esch chan<- []*event) {
epes := make([]syscall.EpollEvent, 1)
fd := atomic.LoadInt32(&i.fd)
for {
switch _, err := syscall.EpollWait(i.epfd, epes, -1); err {
case nil:
switch epes[0].Fd {
case fd:
esch <- i.read()
epes[0].Fd = 0
case int32(i.pipefd[0]):
i.Lock()
defer i.Unlock()
if err = syscall.Close(int(fd)); err != nil && err != syscall.EINTR {
panic("notify: close(2) error " + err.Error())
}
atomic.StoreInt32(&i.fd, invalidDescriptor)
if err = i.epollclose(); err != nil && err != syscall.EINTR {
panic("notify: epollclose error " + err.Error())
}
close(esch)
return
}
case syscall.EINTR:
continue
default: // We should never reach this line.
panic("notify: epoll_wait(2) error " + err.Error())
}
}
}
示例6: listenPort
func listenPort(cReq chan<- []byte, epollFd int, reader *bufio.Reader) {
events := make([]syscall.EpollEvent, 32)
var buf []byte
var err error
for {
if _, err := syscall.EpollWait(epollFd, events, -1); err != nil {
log.Fatalln("Error receiving epoll events:", err)
}
buf, err = reader.ReadBytes('\x0a')
switch err {
case nil:
case io.EOF:
time.Sleep(time.Second * 1)
continue
default:
log.Fatalln(err)
}
break
}
cReq <- buf
}
示例7: Poll
func (ep *EPoll) Poll(timeout int) (int, error) {
nfds, err := syscall.EpollWait(ep.fd, ep.events[0:], timeout)
if err != nil {
return 0, err
}
ep.nfds = nfds
return nfds, nil
}
示例8: WaitForEdge
func (gpio *GPIO) WaitForEdge(edge Edge) (value Value, err error) {
if err = gpio.setEdge(edge); err != nil {
return 0, err
}
if err = gpio.ensureValueFileIsOpen(); err != nil {
return 0, err
}
epollFd := gpio.epollFd.Get()
if epollFd == 0 {
epollFd, err = syscall.EpollCreate(1)
if err != nil {
return 0, err
}
event := &syscall.EpollEvent{
Events: syscall.EPOLLIN | syscall.EPOLLPRI | _EPOLLET,
Fd: int32(gpio.valueFile.Fd()),
}
err = syscall.EpollCtl(epollFd, syscall.EPOLL_CTL_ADD, int(gpio.valueFile.Fd()), event)
if err != nil {
syscall.Close(epollFd)
return 0, err
}
// first time triggers with current state, so ignore
_, err = syscall.EpollWait(epollFd, dummyEpollEvents, -1)
if err != nil {
syscall.Close(epollFd)
return 0, err
}
gpio.epollFd.Set(epollFd)
}
_, err = syscall.EpollWait(epollFd, dummyEpollEvents, -1)
if err != nil {
return 0, err
}
return gpio.Value()
}
示例9: WaitFD
func (p *pollster) WaitFD(nsec int64) (fd int, mode int, err os.Error) {
// Get an event.
var evarray [1]syscall.EpollEvent
ev := &evarray[0]
var msec int = -1
if nsec > 0 {
msec = int((nsec + 1e6 - 1) / 1e6)
}
n, e := syscall.EpollWait(p.epfd, evarray[0:], msec)
for e == syscall.EAGAIN || e == syscall.EINTR {
n, e = syscall.EpollWait(p.epfd, evarray[0:], msec)
}
if e != 0 {
return -1, 0, os.NewSyscallError("epoll_wait", e)
}
if n == 0 {
return -1, 0, nil
}
fd = int(ev.Fd)
if ev.Events&writeFlags != 0 {
p.StopWaiting(fd, writeFlags)
return fd, 'w', nil
}
if ev.Events&readFlags != 0 {
p.StopWaiting(fd, readFlags)
return fd, 'r', nil
}
// Other events are error conditions - wake whoever is waiting.
events, _ := p.events[fd]
if events&writeFlags != 0 {
p.StopWaiting(fd, writeFlags)
return fd, 'w', nil
}
p.StopWaiting(fd, readFlags)
return fd, 'r', nil
}
示例10: WaitWrite
func (p *Poller) WaitWrite(deadline time.Time) error {
msec := -1
if !deadline.IsZero() {
d := deadline.Sub(time.Now())
msec = int(d.Nanoseconds() / 1000000) // ms!? omg...
}
n, err := syscall.EpollWait(p.epfd, p.events[:], msec)
if err != nil {
return err
}
if n < 1 {
return errTimeout
}
return nil
}
示例11: WaitFD
func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err error) {
for len(p.waitEvents) == 0 {
var msec int = -1
if nsec > 0 {
msec = int((nsec + 1e6 - 1) / 1e6)
}
s.Unlock()
n, e := syscall.EpollWait(p.epfd, p.waitEventBuf[0:], msec)
s.Lock()
if e != nil {
if e == syscall.EAGAIN || e == syscall.EINTR {
continue
}
return -1, 0, os.NewSyscallError("epoll_wait", e)
}
if n == 0 {
return -1, 0, nil
}
p.waitEvents = p.waitEventBuf[0:n]
}
ev := &p.waitEvents[0]
p.waitEvents = p.waitEvents[1:]
fd = int(ev.Fd)
if ev.Events&writeFlags != 0 {
p.StopWaiting(fd, writeFlags)
return fd, 'w', nil
}
if ev.Events&readFlags != 0 {
p.StopWaiting(fd, readFlags)
return fd, 'r', nil
}
// Other events are error conditions - wake whoever is waiting.
events, _ := p.events[fd]
if events&writeFlags != 0 {
p.StopWaiting(fd, writeFlags)
return fd, 'w', nil
}
p.StopWaiting(fd, readFlags)
return fd, 'r', nil
}
示例12: Run
func Run(t *Tunnel) {
var err error
epollFd, err = syscall.EpollCreate(1024)
if err != nil {
logger.Log(logger.ERR, "Create epoll fd error [%s]", err)
os.Exit(-2)
}
for _, step := range initStep {
err = step.Action(t)
if err != nil {
fmt.Fprintf(os.Stderr, step.ErrFmt, err)
os.Exit(-2)
}
}
runTunnel = t
events := make([]syscall.EpollEvent, 10, 10)
for {
en, err := syscall.EpollWait(epollFd, events, 1000)
if err != nil {
logger.Log(logger.ERR, "Wail epoll fd error [%s]", err)
os.Exit(-2)
}
for i := 0; i < en; i++ {
ee := events[i]
if runTunnel.LFd == int(ee.Fd) {
runTunnel.newConn()
continue
}
tc, ok := fdTunnelConn[int(ee.Fd)]
if !ok {
continue
}
if ee.Events&syscall.EPOLLIN != 0 {
tc.handleIn(int(ee.Fd))
}
if ee.Events&syscall.EPOLLOUT != 0 {
tc.handleOut(int(ee.Fd))
}
if ee.Events&syscall.EPOLLHUP != 0 {
tc.shutdown()
}
}
}
}
示例13: inotify_observe_collect
func inotify_observe_collect(buf []byte) map[string]int {
epoll_buf := make([]syscall.EpollEvent, 1)
collect := make(map[string]int)
for {
nread, _ := syscall.Read(inotify_fd, buf)
for offset := 0; offset < nread; offset += event_size {
event := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
if syscall.IN_IGNORED == event.Mask {
continue
}
collect[name_by_wd[event.Wd]] = 1
}
nevents, err := syscall.EpollWait(epoll_fd, epoll_buf, 500)
if 0 >= nevents {
if -1 == nevents {
tabby_log("inotify_observe_collect: " + err.Error())
}
break
}
}
return collect
}
示例14: poller
func poller() {
debugf("Started.")
events := make([]syscall.EpollEvent, 128)
for {
n, err := syscall.EpollWait(epfd, events, -1)
if err != nil {
if err == syscall.EINTR {
continue
}
log.Panicf("poller: EpollWait: %s", err.Error())
}
for i := 0; i < n; i++ {
ev := &events[i]
if isReadEvent(ev) {
epollEvent(ev, false)
}
if isWriteEvent(ev) {
epollEvent(ev, true)
}
}
}
}
示例15: epollEvents
func (w *Watcher) epollEvents(epfd int, donePipe *os.File) {
w.isRunning = true
defer func() {
syscall.Close(epfd)
w.isRunning = false
w.closed <- true
}()
events := make([]syscall.EpollEvent, EPOLL_MAX_EVENTS)
doneFd := int32(donePipe.Fd())
for {
nevents, err := syscall.EpollWait(epfd, events, -1)
if err != nil {
w.Errors <- os.NewSyscallError("epoll_wait", err)
continue
}
if nevents == 0 {
continue
}
for i := 0; i < nevents; i++ {
if events[i].Fd == doneFd {
if err = donePipe.Close(); err != nil {
w.Errors <- err
}
syscall.Close(w.fd)
close(w.Events)
close(w.Errors)
return
} else if events[i].Fd != int32(w.fd) {
continue
}
if err = w.readEvents(); err != nil {
w.Errors <- err
}
}
}
}