// get get a free timer data. func(t *Timer)get()(td *TimerData) { if td = t.free; td == nil { t.grow() td = t.free } t.free = td.next return }
// put put back a timer data. func(t *Timer)put(td *TimerData) { td.fn = nil td.next = t.free t.free = td }
// Push pushes the element x onto the heap. The complexity is // O(log(n)) where n = h.Len(). func(t *Timer)add(td *TimerData) { var d itime.Duration td.index = len(t.timers) // add to the minheap last node t.timers = append(t.timers, td) t.up(td.index) if td.index == 0 { // if first node, signal start goroutine d = td.Delay() t.signal.Reset(d) if Debug { log.Debug("timer: add reset delay %d ms", int64(d)/int64(itime.Millisecond)) } } if Debug { log.Debug("timer: push item key: %s, expire: %s, index: %d", td.Key, td.ExpireString(), td.index) } return }
func(t *Timer)del(td *TimerData) { var ( i = td.index last = len(t.timers) - 1 ) if i < 0 || i > last || t.timers[i] != td { // already remove, usually by expire if Debug { log.Debug("timer del i: %d, last: %d, %p", i, last, td) } return } if i != last { t.swap(i, last) t.down(i, last) t.up(i) } // remove item is the last node t.timers[last].index = -1// for safety t.timers = t.timers[:last] if Debug { log.Debug("timer: remove item key: %s, expire: %s, index: %d", td.Key, td.ExpireString(), td.index) } return }
// Get get a free memory buffer. func(p *Pool)Get()(b *Buffer) { p.lock.Lock() if b = p.free; b == nil { p.grow() b = p.free } p.free = b.next p.lock.Unlock() return }
// Put put back a memory buffer to free. func(p *Pool)Put(b *Buffer) { p.lock.Lock() b.next = p.free p.free = b p.lock.Unlock() return }
type Cleaner struct { cLock sync.Mutex size int root CleanData maps map[int64]*CleanData }
func (c *Cleaner) PushFront(key int64, expire time.Duration) { c.cLock.Lock() if e, ok := c.maps[key]; ok { // update time e.expireTime = time.Now().Add(expire) c.moveToFront(e) } else { e = new(CleanData) e.Key = key e.expireTime = time.Now().Add(expire) c.maps[key] = e at := &c.root n := at.next at.next = e e.prev = at e.next = n n.prev = e c.size++ } c.cLock.Unlock() }
type Ring struct { // read rp uint64 num uint64 mask uint64 // TODO split cacheline, many cpu cache line size is 64 // pad [40]byte // write wp uint64 data []proto.Proto }
funcNewRing(num int) *Ring { r := new(Ring) r.init(uint64(num)) return r }