2018-03-22 15:24:52 +00:00
|
|
|
package h2mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AtomicCounter struct {
|
|
|
|
count uint64
|
|
|
|
}
|
|
|
|
|
2018-07-05 19:06:27 +00:00
|
|
|
func NewAtomicCounter(initCount uint64) *AtomicCounter {
|
2018-04-06 22:47:13 +00:00
|
|
|
return &AtomicCounter{count: initCount}
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:24:52 +00:00
|
|
|
func (c *AtomicCounter) IncrementBy(number uint64) {
|
|
|
|
atomic.AddUint64(&c.count, number)
|
|
|
|
}
|
|
|
|
|
2018-07-05 19:06:27 +00:00
|
|
|
// Count returns the current value of counter and reset it to 0
|
2018-03-22 15:24:52 +00:00
|
|
|
func (c *AtomicCounter) Count() uint64 {
|
|
|
|
return atomic.SwapUint64(&c.count, 0)
|
|
|
|
}
|
2018-07-05 19:06:27 +00:00
|
|
|
|
|
|
|
// Value returns the current value of counter
|
|
|
|
func (c *AtomicCounter) Value() uint64 {
|
|
|
|
return atomic.LoadUint64(&c.count)
|
|
|
|
}
|