From cbaa2386def973327e9ff64e24d57085234290c4 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 29 Mar 2018 13:46:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/gchan/gchan.go | 19 ++++------- g/container/gmap/int_bool_map.go | 4 +-- g/container/gmap/int_int_map.go | 4 +-- g/container/gmap/int_interface_map.go | 4 +-- g/container/gmap/int_string_map.go | 4 +-- g/container/gmap/interface_interface_map.go | 4 +-- g/container/gmap/string_bool_map.go | 4 +-- g/container/gmap/string_int_map.go | 4 +-- g/container/gmap/string_interface_map.go | 4 +-- g/container/gmap/string_string_map.go | 4 +-- g/container/gqueue/gqueue.go | 26 ++++++++------- g/os/gcache/gcache.go | 3 +- g/os/gcmd/gcmd.go | 2 +- g/os/gfile/gfile.go | 2 +- g/util/gidgen/gidgen.go | 36 +++++++++++++++------ geg/other/test.go | 11 ++++--- 16 files changed, 77 insertions(+), 58 deletions(-) diff --git a/g/container/gchan/gchan.go b/g/container/gchan/gchan.go index f28367cfb..8c8b1a5a1 100644 --- a/g/container/gchan/gchan.go +++ b/g/container/gchan/gchan.go @@ -8,31 +8,28 @@ package gchan import ( - "sync" "errors" + "gitee.com/johng/gf/g/container/gtype" ) type Chan struct { - mu sync.RWMutex list chan interface{} - closed bool + closed *gtype.Bool } func New(limit int) *Chan { return &Chan { - list : make(chan interface{}, limit), + list : make(chan interface{}, limit), + closed : gtype.NewBool(), } } // 将数据压入队列 func (q *Chan) Push(v interface{}) error { - q.mu.RLock() - if q.closed { - q.mu.RUnlock() + if q.closed.Val() { return errors.New("closed") } q.list <- v - q.mu.RUnlock() return nil } @@ -43,12 +40,10 @@ func (q *Chan) Pop() interface{} { // 关闭队列(通知所有通过Pop阻塞的协程退出) func (q *Chan) Close() { - q.mu.Lock() - if !q.closed { - q.closed = true + if !q.closed.Val() { + q.closed.Set(true) close(q.list) } - q.mu.Unlock() } // 获取当前队列大小 diff --git a/g/container/gmap/int_bool_map.go b/g/container/gmap/int_bool_map.go index a3d9613d3..6e6d0a200 100644 --- a/g/container/gmap/int_bool_map.go +++ b/g/container/gmap/int_bool_map.go @@ -138,9 +138,9 @@ func (this *IntBoolMap) Contains(key int) bool { // 哈希表大小 func (this *IntBoolMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/int_int_map.go b/g/container/gmap/int_int_map.go index 1563d5bcc..56bcbda14 100644 --- a/g/container/gmap/int_int_map.go +++ b/g/container/gmap/int_int_map.go @@ -138,9 +138,9 @@ func (this *IntIntMap) Contains(key int) bool { // 哈希表大小 func (this *IntIntMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/int_interface_map.go b/g/container/gmap/int_interface_map.go index eb395d80d..2182b5ba3 100644 --- a/g/container/gmap/int_interface_map.go +++ b/g/container/gmap/int_interface_map.go @@ -163,9 +163,9 @@ func (this *IntInterfaceMap) Contains(key int) bool { // 哈希表大小 func (this *IntInterfaceMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/int_string_map.go b/g/container/gmap/int_string_map.go index bab4251d4..90b8f7626 100644 --- a/g/container/gmap/int_string_map.go +++ b/g/container/gmap/int_string_map.go @@ -138,9 +138,9 @@ func (this *IntStringMap) Contains(key int) bool { // 哈希表大小 func (this *IntStringMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/interface_interface_map.go b/g/container/gmap/interface_interface_map.go index 5d6f1b2c4..fb2e21138 100644 --- a/g/container/gmap/interface_interface_map.go +++ b/g/container/gmap/interface_interface_map.go @@ -163,9 +163,9 @@ func (this *InterfaceInterfaceMap) Contains(key interface{}) bool { // 哈希表大小 func (this *InterfaceInterfaceMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/string_bool_map.go b/g/container/gmap/string_bool_map.go index 4a9051674..f05c1c200 100644 --- a/g/container/gmap/string_bool_map.go +++ b/g/container/gmap/string_bool_map.go @@ -138,9 +138,9 @@ func (this *StringBoolMap) Contains(key string) bool { // 哈希表大小 func (this *StringBoolMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/string_int_map.go b/g/container/gmap/string_int_map.go index 8a8ed2165..1badb53d2 100644 --- a/g/container/gmap/string_int_map.go +++ b/g/container/gmap/string_int_map.go @@ -138,9 +138,9 @@ func (this *StringIntMap) Contains(key string) bool { // 哈希表大小 func (this *StringIntMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/string_interface_map.go b/g/container/gmap/string_interface_map.go index 90779d42a..24130f144 100644 --- a/g/container/gmap/string_interface_map.go +++ b/g/container/gmap/string_interface_map.go @@ -163,9 +163,9 @@ func (this *StringInterfaceMap) Contains(key string) bool { // 哈希表大小 func (this *StringInterfaceMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gmap/string_string_map.go b/g/container/gmap/string_string_map.go index 6e8a07ee6..3c2f01a70 100644 --- a/g/container/gmap/string_string_map.go +++ b/g/container/gmap/string_string_map.go @@ -138,9 +138,9 @@ func (this *StringStringMap) Contains(key string) bool { // 哈希表大小 func (this *StringStringMap) Size() int { this.mu.RLock() - len := len(this.m) + length := len(this.m) this.mu.RUnlock() - return len + return length } // 哈希表是否为空 diff --git a/g/container/gqueue/gqueue.go b/g/container/gqueue/gqueue.go index 2e22551c4..544e2549e 100644 --- a/g/container/gqueue/gqueue.go +++ b/g/container/gqueue/gqueue.go @@ -15,7 +15,7 @@ import ( "sync" "errors" "container/list" - "sync/atomic" + "gitee.com/johng/gf/g/container/gtype" ) type Queue struct { @@ -24,7 +24,7 @@ type Queue struct { limit int // 队列限制大小 limits chan struct{} // 用于队列写入限制 events chan struct{} // 用于队列出列限制 - closed int32 // 队列是否关闭(0:未关闭,1:关闭) + closed *gtype.Bool // 队列是否关闭 } // 队列大小为非必须参数,默认不限制 @@ -37,13 +37,14 @@ func New(limit...int) *Queue { list : list.New(), limit : 0, limits : make(chan struct{}, size), - events : make(chan struct{}, math.MaxInt64), + events : make(chan struct{}, math.MaxInt32), + closed : gtype.NewBool(), } } // 将数据压入队列, 队尾 func (q *Queue) PushBack(v interface{}) error { - if q.isClosed() { + if q.closed.Val() { return errors.New("closed") } if q.limit > 0 { @@ -60,7 +61,7 @@ func (q *Queue) PushBack(v interface{}) error { // 将数据压入队列, 队头 func (q *Queue) PushFront(v interface{}) error { - if q.isClosed() { + if q.closed.Val() { return errors.New("closed") } // 限制队列大小,使用channel进行阻塞限制 @@ -78,6 +79,9 @@ func (q *Queue) PushFront(v interface{}) error { // 从队头先进先出地从队列取出一项数据,当没有数据可获取时,阻塞等待 func (q *Queue) PopFront() interface{} { + if q.closed.Val() { + return nil + } if q.limit > 0 { <- q.limits } else { @@ -95,6 +99,9 @@ func (q *Queue) PopFront() interface{} { // 从队尾先进先出地从队列取出一项数据,当没有数据可获取时,阻塞等待 func (q *Queue) PopBack() interface{} { + if q.closed.Val() { + return nil + } if q.limit > 0 { <- q.limits } else { @@ -112,8 +119,8 @@ func (q *Queue) PopBack() interface{} { // 关闭队列(通知所有通过Pop*阻塞的协程退出) func (q *Queue) Close() { - if !q.isClosed() { - atomic.StoreInt32(&q.closed, 1) + if !q.closed.Val() { + q.closed.Set(true) close(q.limits) close(q.events) } @@ -124,7 +131,4 @@ func (q *Queue) Size() int { return len(q.events) } -// 队列是否关闭 -func (q *Queue) isClosed() bool { - return atomic.LoadInt32(&q.closed) > 0 -} + diff --git a/g/os/gcache/gcache.go b/g/os/gcache/gcache.go index 7d05aca72..38eddad6b 100644 --- a/g/os/gcache/gcache.go +++ b/g/os/gcache/gcache.go @@ -266,9 +266,10 @@ func (c *Cache) autoSyncLoop() { c.lru.Push(item.k) } } else { - break + return } } + } // LRU缓存淘汰处理+自动清理过期键值对 diff --git a/g/os/gcmd/gcmd.go b/g/os/gcmd/gcmd.go index a86ef2add..df3a8b78d 100644 --- a/g/os/gcmd/gcmd.go +++ b/g/os/gcmd/gcmd.go @@ -5,7 +5,7 @@ // You can obtain one at https://gitee.com/johng/gf. // -// 命令行管理 +// 命令行管理. package gcmd import ( diff --git a/g/os/gfile/gfile.go b/g/os/gfile/gfile.go index aaf1a2c91..e78a4259c 100644 --- a/g/os/gfile/gfile.go +++ b/g/os/gfile/gfile.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -// 文件管理 +// 文件管理. package gfile import ( diff --git a/g/util/gidgen/gidgen.go b/g/util/gidgen/gidgen.go index 63010e853..97f99c772 100644 --- a/g/util/gidgen/gidgen.go +++ b/g/util/gidgen/gidgen.go @@ -13,24 +13,37 @@ import "math" // ID生成器管理对象 type Gen struct { - ch chan uint + ch chan uint + closeQueue chan struct{} } // 创建一个ID生成器,并给定ID池大小 func New (bufsize int) *Gen { g := &Gen { - ch : make(chan uint, bufsize), + ch : make(chan uint, bufsize), + closeQueue : make(chan struct{}), } go g.startLoop() return g } +// 关闭生成器 +func (g *Gen) Close() { + close(g.closeQueue) + close(g.ch) +} + // 内部循环,当最大值使用完之后重新从1开始获取 func (g *Gen) startLoop() { for { - // 当ch达到缓冲池大小,会阻塞,只要有线程取出值,再立即填充 - for i := uint(1); i < uint(math.MaxUint64); i++ { - g.ch <- i + select { + case <- g.closeQueue: + return + default: + // 当ch达到缓冲池大小,会阻塞,只要有线程取出值,再立即填充 + for i := uint(1); i < uint(math.MaxUint64); i++ { + g.ch <- i + } } } } @@ -42,11 +55,14 @@ func (g *Gen) Uint() uint { // 从池中获取一个ID返回(int) func (g *Gen) Int() int { - i := int(<- g.ch & 0x7FFFFFFFFFFFFFFF) - // 可能是int与uint之间的临界点 - if i == 0 { - i = int(<- g.ch & 0x7FFFFFFFFFFFFFFF) + i := <- g.ch + if i != 0 { + i = i & 0x7FFFFFFFFFFFFFFF + // 可能是int与uint之间的临界点 + if i == 0 { + return g.Int() + } } - return i + return int(i) } diff --git a/geg/other/test.go b/geg/other/test.go index e6738c575..9cdf07053 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,13 +2,16 @@ package main import ( "fmt" - "gitee.com/johng/gf/g/os/gtime" - "math" + "gitee.com/johng/gf/g/util/gidgen" ) func main() { - fmt.Println(gtime.Millisecond()) - fmt.Println(math.MaxInt64) + g := gidgen.New(2) + for i := 0; i < 11; i++ { + fmt.Println(g.Int()) + } + g.Close() + fmt.Println(g.Uint()) //events2 := make(chan int, 100) //go func() { // for{