|
@@ -1,19 +1,19 @@
|
|
|
package mux
|
|
|
|
|
|
import (
|
|
|
- "container/list"
|
|
|
"errors"
|
|
|
"github.com/cnlh/nps/lib/common"
|
|
|
"io"
|
|
|
- "sync"
|
|
|
+ "math"
|
|
|
+ "sync/atomic"
|
|
|
"time"
|
|
|
+ "unsafe"
|
|
|
)
|
|
|
|
|
|
type QueueOp struct {
|
|
|
readOp chan struct{}
|
|
|
cleanOp chan struct{}
|
|
|
- popWait bool
|
|
|
- mutex sync.Mutex
|
|
|
+ popWait int32
|
|
|
}
|
|
|
|
|
|
func (Self *QueueOp) New() {
|
|
@@ -22,15 +22,15 @@ func (Self *QueueOp) New() {
|
|
|
}
|
|
|
|
|
|
func (Self *QueueOp) allowPop() (closed bool) {
|
|
|
- Self.mutex.Lock()
|
|
|
- Self.popWait = false
|
|
|
- Self.mutex.Unlock()
|
|
|
- select {
|
|
|
- case Self.readOp <- struct{}{}:
|
|
|
- return false
|
|
|
- case <-Self.cleanOp:
|
|
|
- return true
|
|
|
+ if atomic.CompareAndSwapInt32(&Self.popWait, 1, 0) {
|
|
|
+ select {
|
|
|
+ case Self.readOp <- struct{}{}:
|
|
|
+ return false
|
|
|
+ case <-Self.cleanOp:
|
|
|
+ return true
|
|
|
+ }
|
|
|
}
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
func (Self *QueueOp) Clean() {
|
|
@@ -40,84 +40,72 @@ func (Self *QueueOp) Clean() {
|
|
|
}
|
|
|
|
|
|
type PriorityQueue struct {
|
|
|
- list *list.List
|
|
|
QueueOp
|
|
|
+ highestChain *bufChain
|
|
|
+ middleChain *bufChain
|
|
|
+ lowestChain *bufChain
|
|
|
+ hunger uint8
|
|
|
}
|
|
|
|
|
|
func (Self *PriorityQueue) New() {
|
|
|
- Self.list = list.New()
|
|
|
+ Self.highestChain = new(bufChain)
|
|
|
+ Self.highestChain.new(4)
|
|
|
+ Self.middleChain = new(bufChain)
|
|
|
+ Self.middleChain.new(32)
|
|
|
+ Self.lowestChain = new(bufChain)
|
|
|
+ Self.lowestChain.new(256)
|
|
|
Self.QueueOp.New()
|
|
|
}
|
|
|
|
|
|
func (Self *PriorityQueue) Push(packager *common.MuxPackager) {
|
|
|
- Self.mutex.Lock()
|
|
|
switch packager.Flag {
|
|
|
case common.MUX_PING_FLAG, common.MUX_PING_RETURN:
|
|
|
- Self.list.PushFront(packager)
|
|
|
+ Self.highestChain.pushHead(unsafe.Pointer(packager))
|
|
|
|
|
|
|
|
|
- case common.MUX_CONN_CLOSE:
|
|
|
- Self.insert(packager)
|
|
|
-
|
|
|
-
|
|
|
+ case common.MUX_NEW_CONN, common.MUX_NEW_CONN_OK, common.MUX_NEW_CONN_Fail:
|
|
|
+
|
|
|
+ Self.middleChain.pushHead(unsafe.Pointer(packager))
|
|
|
default:
|
|
|
- Self.list.PushBack(packager)
|
|
|
- }
|
|
|
- if Self.popWait {
|
|
|
- Self.mutex.Unlock()
|
|
|
- Self.allowPop()
|
|
|
- return
|
|
|
+ Self.lowestChain.pushHead(unsafe.Pointer(packager))
|
|
|
}
|
|
|
- Self.mutex.Unlock()
|
|
|
+ Self.allowPop()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func (Self *PriorityQueue) insert(packager *common.MuxPackager) {
|
|
|
- element := Self.list.Back()
|
|
|
- for {
|
|
|
- if element == nil {
|
|
|
- element = Self.list.Front()
|
|
|
- if element != nil {
|
|
|
- Self.list.InsertAfter(packager, element)
|
|
|
-
|
|
|
- } else {
|
|
|
- Self.list.PushFront(packager)
|
|
|
-
|
|
|
- }
|
|
|
- break
|
|
|
- }
|
|
|
- if element.Value.(*common.MuxPackager).Flag == common.MUX_NEW_MSG &&
|
|
|
- element.Value.(*common.MuxPackager).Id == packager.Id {
|
|
|
- Self.list.InsertAfter(packager, element)
|
|
|
-
|
|
|
- break
|
|
|
+func (Self *PriorityQueue) Pop() (packager *common.MuxPackager) {
|
|
|
+startPop:
|
|
|
+ ptr, ok := Self.highestChain.popTail()
|
|
|
+ if ok {
|
|
|
+ packager = (*common.MuxPackager)(ptr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if Self.hunger < 100 {
|
|
|
+ ptr, ok = Self.middleChain.popTail()
|
|
|
+ if ok {
|
|
|
+ packager = (*common.MuxPackager)(ptr)
|
|
|
+ Self.hunger++
|
|
|
+ return
|
|
|
}
|
|
|
- element = element.Prev()
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-func (Self *PriorityQueue) Pop() (packager *common.MuxPackager) {
|
|
|
- Self.mutex.Lock()
|
|
|
- element := Self.list.Front()
|
|
|
- if element != nil {
|
|
|
- packager = element.Value.(*common.MuxPackager)
|
|
|
- Self.list.Remove(element)
|
|
|
- Self.mutex.Unlock()
|
|
|
+ ptr, ok = Self.lowestChain.popTail()
|
|
|
+ if ok {
|
|
|
+ packager = (*common.MuxPackager)(ptr)
|
|
|
+ if Self.hunger > 0 {
|
|
|
+ Self.hunger = uint8(Self.hunger / 2)
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
- Self.popWait = true
|
|
|
- Self.mutex.Unlock()
|
|
|
- select {
|
|
|
- case <-Self.readOp:
|
|
|
- return Self.Pop()
|
|
|
- case <-Self.cleanOp:
|
|
|
- return nil
|
|
|
+
|
|
|
+ if atomic.CompareAndSwapInt32(&Self.popWait, 0, 1) {
|
|
|
+ select {
|
|
|
+ case <-Self.readOp:
|
|
|
+ goto startPop
|
|
|
+ case <-Self.cleanOp:
|
|
|
+ return nil
|
|
|
+ }
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-func (Self *PriorityQueue) Len() (n int) {
|
|
|
- n = Self.list.Len()
|
|
|
- return
|
|
|
+ goto startPop
|
|
|
}
|
|
|
|
|
|
type ListElement struct {
|
|
@@ -137,36 +125,36 @@ func (Self *ListElement) New(buf []byte, l uint16, part bool) (err error) {
|
|
|
}
|
|
|
|
|
|
type FIFOQueue struct {
|
|
|
- list []*ListElement
|
|
|
+ QueueOp
|
|
|
+ chain *bufChain
|
|
|
length uint32
|
|
|
stopOp chan struct{}
|
|
|
timeout time.Time
|
|
|
- QueueOp
|
|
|
}
|
|
|
|
|
|
func (Self *FIFOQueue) New() {
|
|
|
Self.QueueOp.New()
|
|
|
+ Self.chain = new(bufChain)
|
|
|
+ Self.chain.new(64)
|
|
|
Self.stopOp = make(chan struct{}, 1)
|
|
|
}
|
|
|
|
|
|
func (Self *FIFOQueue) Push(element *ListElement) {
|
|
|
- Self.mutex.Lock()
|
|
|
- Self.list = append(Self.list, element)
|
|
|
+ Self.chain.pushHead(unsafe.Pointer(element))
|
|
|
Self.length += uint32(element.l)
|
|
|
- if Self.popWait {
|
|
|
- Self.mutex.Unlock()
|
|
|
- Self.allowPop()
|
|
|
- return
|
|
|
- }
|
|
|
- Self.mutex.Unlock()
|
|
|
+ Self.allowPop()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func (Self *FIFOQueue) Pop() (element *ListElement, err error) {
|
|
|
- Self.mutex.Lock()
|
|
|
- if len(Self.list) == 0 {
|
|
|
- Self.popWait = true
|
|
|
- Self.mutex.Unlock()
|
|
|
+startPop:
|
|
|
+ ptr, ok := Self.chain.popTail()
|
|
|
+ if ok {
|
|
|
+ element = (*ListElement)(ptr)
|
|
|
+ Self.length -= uint32(element.l)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if atomic.CompareAndSwapInt32(&Self.popWait, 0, 1) {
|
|
|
t := Self.timeout.Sub(time.Now())
|
|
|
if t <= 0 {
|
|
|
t = time.Minute
|
|
@@ -175,7 +163,7 @@ func (Self *FIFOQueue) Pop() (element *ListElement, err error) {
|
|
|
defer timer.Stop()
|
|
|
select {
|
|
|
case <-Self.readOp:
|
|
|
- Self.mutex.Lock()
|
|
|
+ goto startPop
|
|
|
case <-Self.cleanOp:
|
|
|
return
|
|
|
case <-Self.stopOp:
|
|
@@ -186,11 +174,7 @@ func (Self *FIFOQueue) Pop() (element *ListElement, err error) {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
- element = Self.list[0]
|
|
|
- Self.list = Self.list[1:]
|
|
|
- Self.length -= uint32(element.l)
|
|
|
- Self.mutex.Unlock()
|
|
|
- return
|
|
|
+ goto startPop
|
|
|
}
|
|
|
|
|
|
func (Self *FIFOQueue) Len() (n uint32) {
|
|
@@ -204,3 +188,231 @@ func (Self *FIFOQueue) Stop() {
|
|
|
func (Self *FIFOQueue) SetTimeOut(t time.Time) {
|
|
|
Self.timeout = t
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+type bufDequeue struct {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ headTail uint64
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ vals []unsafe.Pointer
|
|
|
+}
|
|
|
+
|
|
|
+const dequeueBits = 32
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const dequeueLimit = (1 << dequeueBits) / 4
|
|
|
+
|
|
|
+func (d *bufDequeue) unpack(ptrs uint64) (head, tail uint32) {
|
|
|
+ const mask = 1<<dequeueBits - 1
|
|
|
+ head = uint32((ptrs >> dequeueBits) & mask)
|
|
|
+ tail = uint32(ptrs & mask)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func (d *bufDequeue) pack(head, tail uint32) uint64 {
|
|
|
+ const mask = 1<<dequeueBits - 1
|
|
|
+ return (uint64(head) << dequeueBits) |
|
|
|
+ uint64(tail&mask)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (d *bufDequeue) pushHead(val unsafe.Pointer) bool {
|
|
|
+ var slot *unsafe.Pointer
|
|
|
+ for {
|
|
|
+ ptrs := atomic.LoadUint64(&d.headTail)
|
|
|
+ head, tail := d.unpack(ptrs)
|
|
|
+ if (tail+uint32(len(d.vals)))&(1<<dequeueBits-1) == head {
|
|
|
+
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ ptrs2 := d.pack(head+1, tail)
|
|
|
+ if atomic.CompareAndSwapUint64(&d.headTail, ptrs, ptrs2) {
|
|
|
+ slot = &d.vals[head&uint32(len(d.vals)-1)]
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ *slot = val
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (d *bufDequeue) popTail() (unsafe.Pointer, bool) {
|
|
|
+ ptrs := atomic.LoadUint64(&d.headTail)
|
|
|
+ head, tail := d.unpack(ptrs)
|
|
|
+ if tail == head {
|
|
|
+
|
|
|
+ return nil, false
|
|
|
+ }
|
|
|
+ slot := &d.vals[tail&uint32(len(d.vals)-1)]
|
|
|
+ for {
|
|
|
+ typ := atomic.LoadPointer(slot)
|
|
|
+ if typ != nil {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ val := *slot
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ atomic.StorePointer(slot, nil)
|
|
|
+
|
|
|
+ if tail < math.MaxUint32 {
|
|
|
+ atomic.AddUint64(&d.headTail, 1)
|
|
|
+ } else {
|
|
|
+ atomic.AddUint64(&d.headTail, ^uint64(math.MaxUint32-1))
|
|
|
+ }
|
|
|
+ return val, true
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+type bufChain struct {
|
|
|
+
|
|
|
+
|
|
|
+ head *bufChainElt
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ tail *bufChainElt
|
|
|
+ chainStatus int32
|
|
|
+}
|
|
|
+
|
|
|
+type bufChainElt struct {
|
|
|
+ bufDequeue
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ next, prev *bufChainElt
|
|
|
+}
|
|
|
+
|
|
|
+func storePoolChainElt(pp **bufChainElt, v *bufChainElt) {
|
|
|
+ atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(pp)), unsafe.Pointer(v))
|
|
|
+}
|
|
|
+
|
|
|
+func loadPoolChainElt(pp **bufChainElt) *bufChainElt {
|
|
|
+ return (*bufChainElt)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(pp))))
|
|
|
+}
|
|
|
+
|
|
|
+func (c *bufChain) new(initSize int) {
|
|
|
+
|
|
|
+
|
|
|
+ d := new(bufChainElt)
|
|
|
+ d.vals = make([]unsafe.Pointer, initSize)
|
|
|
+ storePoolChainElt(&c.head, d)
|
|
|
+ storePoolChainElt(&c.tail, d)
|
|
|
+}
|
|
|
+
|
|
|
+func (c *bufChain) pushHead(val unsafe.Pointer) {
|
|
|
+ for {
|
|
|
+ d := loadPoolChainElt(&c.head)
|
|
|
+
|
|
|
+ if d.pushHead(val) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if atomic.CompareAndSwapInt32(&c.chainStatus, 0, 1) {
|
|
|
+ newSize := len(d.vals) * 2
|
|
|
+ if newSize >= dequeueLimit {
|
|
|
+
|
|
|
+ newSize = dequeueLimit
|
|
|
+ }
|
|
|
+
|
|
|
+ d2 := &bufChainElt{prev: d}
|
|
|
+ d2.vals = make([]unsafe.Pointer, newSize)
|
|
|
+ storePoolChainElt(&c.head, d2)
|
|
|
+ storePoolChainElt(&d.next, d2)
|
|
|
+ d2.pushHead(val)
|
|
|
+ atomic.SwapInt32(&c.chainStatus, 0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c *bufChain) popTail() (unsafe.Pointer, bool) {
|
|
|
+ d := loadPoolChainElt(&c.tail)
|
|
|
+ if d == nil {
|
|
|
+ return nil, false
|
|
|
+ }
|
|
|
+
|
|
|
+ for {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ d2 := loadPoolChainElt(&d.next)
|
|
|
+
|
|
|
+ if val, ok := d.popTail(); ok {
|
|
|
+ return val, ok
|
|
|
+ }
|
|
|
+
|
|
|
+ if d2 == nil {
|
|
|
+
|
|
|
+
|
|
|
+ return nil, false
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.tail)), unsafe.Pointer(d), unsafe.Pointer(d2)) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ storePoolChainElt(&d2.prev, nil)
|
|
|
+ }
|
|
|
+ d = d2
|
|
|
+ }
|
|
|
+}
|