Skip to content

Commit

Permalink
opt: reduce the potential system calls for waking pollers up
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Sep 9, 2021
1 parent e2ad774 commit 9ce41f3
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/netpoll/epoll_default_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (p *Poller) Polling(callback func(fd int, ev uint32) error) error {
queue.PutTask(task)
}
atomic.StoreInt32(&p.netpollWakeSig, 0)
if !p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty() {
if (!p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty()) && atomic.CompareAndSwapInt32(&p.netpollWakeSig, 0, 1) {
for _, err = unix.Write(p.wfd, b); err == unix.EINTR || err == unix.EAGAIN; _, err = unix.Write(p.wfd, b) {
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/netpoll/epoll_optimized_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (p *Poller) Polling() error {
queue.PutTask(task)
}
atomic.StoreInt32(&p.netpollWakeSig, 0)
if !p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty() {
if (!p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty()) && atomic.CompareAndSwapInt32(&p.netpollWakeSig, 0, 1) {
for _, err = unix.Write(p.wpa.FD, b); err == unix.EINTR || err == unix.EAGAIN; _, err = unix.Write(p.wpa.FD, b) {
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/netpoll/kqueue_default_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (p *Poller) Polling(callback func(fd int, filter int16) error) error {
queue.PutTask(task)
}
atomic.StoreInt32(&p.netpollWakeSig, 0)
if !p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty() {
if (!p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty()) && atomic.CompareAndSwapInt32(&p.netpollWakeSig, 0, 1) {
for _, err = unix.Kevent(p.fd, wakeChanges, nil, nil); err == unix.EINTR || err == unix.EAGAIN; _, err = unix.Kevent(p.fd, wakeChanges, nil, nil) {
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/netpoll/kqueue_optimized_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (p *Poller) Polling() error {
queue.PutTask(task)
}
atomic.StoreInt32(&p.netpollWakeSig, 0)
if !p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty() {
if (!p.asyncTaskQueue.Empty() || !p.priorAsyncTaskQueue.Empty()) && atomic.CompareAndSwapInt32(&p.netpollWakeSig, 0, 1) {
for _, err = unix.Kevent(p.fd, wakeChanges, nil, nil); err == unix.EINTR || err == unix.EAGAIN; _, err = unix.Kevent(p.fd, wakeChanges, nil, nil) {
}
}
Expand Down

0 comments on commit 9ce41f3

Please sign in to comment.