Skip to content

Commit

Permalink
kqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
cheng-zhongliang committed Oct 3, 2023
1 parent 1779aa4 commit 215ca78
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package event
type eventHeap []*Event

func newEventHeap() *eventHeap {
return new(eventHeap).init()
return new(eventHeap)
}

func (eh eventHeap) less(i, j int) bool {
Expand All @@ -22,7 +22,7 @@ func (eh eventHeap) swap(i, j int) {

func (eh eventHeap) up(j int) {
for {
i := (j - 1) / 2
i := (j - 1) / 4
if i == j || !eh.less(j, i) {
break
}
Expand All @@ -34,14 +34,20 @@ func (eh eventHeap) up(j int) {
func (eh eventHeap) down(i0, n int) bool {
i := i0
for {
j1 := 2*i + 1
j1 := 4*i + 1
if j1 >= n {
break
}
j := j1
if j2 := j1 + 1; j2 < n && eh.less(j2, j1) {
j = j2
}
if j3 := j1 + 2; j3 < n && eh.less(j3, j) {
j = j3
}
if j4 := j1 + 3; j4 < n && eh.less(j4, j) {
j = j4
}
if !eh.less(j, i) {
break
}
Expand Down Expand Up @@ -79,7 +85,7 @@ func (eh *eventHeap) empty() bool {

func (eh *eventHeap) init() *eventHeap {
n := len(*eh)
for i := n/2 - 1; i >= 0; i-- {
for i := n/4 - 1; i >= 0; i-- {
eh.down(i, n)
}
return eh
Expand Down
8 changes: 4 additions & 4 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package event

// NewTimer creates a new timer event.
func NewTimer(callback func(fd int, events uint32, arg interface{}), arg interface{}) *Event {
return New(-1, EvTimeout, callback, arg)
func NewTimer(base *EventBase, callback func(fd int, events uint32, arg interface{}), arg interface{}) *Event {
return New(base, -1, EvTimeout, callback, arg)
}

// NewTicker creates a new ticker event.
func NewTicker(callback func(fd int, events uint32, arg interface{}), arg interface{}) *Event {
return New(-1, EvTimeout|EvPersist, callback, arg)
func NewTicker(base *EventBase, callback func(fd int, events uint32, arg interface{}), arg interface{}) *Event {
return New(base, -1, EvTimeout|EvPersist, callback, arg)
}

0 comments on commit 215ca78

Please sign in to comment.