change func(xxx, safe...bool) to func(xxx, unsafe...bool); add new internal package mutex

This commit is contained in:
John
2019-01-12 23:36:22 +08:00
parent 0a422e9a89
commit f4644ce685
35 changed files with 271 additions and 283 deletions

View File

@ -4,10 +4,9 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// Package garray provides kinds of concurrent-safe(alternative) arrays.
// 并发安全的数组.
// Package garray provides kinds of concurrent-safe(alternative) arrays/并发安全的数组.
package garray
func New(size int, cap int, safe...bool) *Array {
return NewArray(size, cap, safe...)
func New(size int, cap int, unsafe...bool) *Array {
return NewArray(size, cap, unsafe...)
}

View File

@ -7,7 +7,7 @@
package garray
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type IntArray struct {
@ -17,9 +17,9 @@ type IntArray struct {
array []int // 底层数组
}
func NewIntArray(size int, cap int, safe...bool) *IntArray {
func NewIntArray(size int, cap int, unsafe...bool) *IntArray {
a := &IntArray{
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
a.size = size
if cap > 0 {

View File

@ -7,7 +7,7 @@
package garray
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type Array struct {
@ -17,9 +17,9 @@ type Array struct {
array []interface{} // 底层数组
}
func NewArray(size int, cap int, safe...bool) *Array {
func NewArray(size int, cap int, unsafe...bool) *Array {
a := &Array{
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
a.size = size
if cap > 0 {

View File

@ -8,7 +8,7 @@ package garray
import (
"gitee.com/johng/gf/g/container/gtype"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
// 默认按照从低到高进行排序
@ -21,9 +21,9 @@ type SortedIntArray struct {
}
// 创建一个排序的int数组
func NewSortedIntArray(cap int, safe...bool) *SortedIntArray {
func NewSortedIntArray(cap int, unsafe...bool) *SortedIntArray {
return &SortedIntArray {
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
array : make([]int, 0, cap),
unique : gtype.NewBool(),
compareFunc : func(v1, v2 int) int {

View File

@ -8,7 +8,7 @@ package garray
import (
"gitee.com/johng/gf/g/container/gtype"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
// 默认按照从低到高进行排序
@ -20,9 +20,9 @@ type SortedArray struct {
compareFunc func(v1, v2 interface{}) int // 比较函数,返回值 -1: v1 < v20: v1 == v21: v1 > v2
}
func NewSortedArray(cap int, compareFunc func(v1, v2 interface{}) int, safe...bool) *SortedArray {
func NewSortedArray(cap int, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray {
return &SortedArray{
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
unique : gtype.NewBool(),
array : make([]interface{}, 0, cap),
compareFunc : compareFunc,

View File

@ -9,7 +9,7 @@ package garray
import (
"gitee.com/johng/gf/g/container/gtype"
"strings"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
// 默认按照从低到高进行排序
@ -21,9 +21,9 @@ type SortedStringArray struct {
compareFunc func(v1, v2 string) int // 比较函数,返回值 -1: v1 < v20: v1 == v21: v1 > v2
}
func NewSortedStringArray(cap int, safe...bool) *SortedStringArray {
func NewSortedStringArray(cap int, unsafe...bool) *SortedStringArray {
return &SortedStringArray {
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
array : make([]string, 0, cap),
unique : gtype.NewBool(),
compareFunc : func(v1, v2 string) int {

View File

@ -8,7 +8,7 @@ package garray
import (
"strings"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type StringArray struct {
@ -18,9 +18,9 @@ type StringArray struct {
array []string // 底层数组
}
func NewStringArray(size int, cap int, safe...bool) *StringArray {
func NewStringArray(size int, cap int, unsafe...bool) *StringArray {
a := &StringArray{
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
a.size = size
if cap > 0 {

View File

@ -4,8 +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 gchan provides graceful operations for channel.
// 优雅的Channel操作.
// Package gchan provides graceful operations for channel/优雅的Channel操作.
package gchan
import (

View File

@ -10,7 +10,7 @@ package glist
import (
"container/list"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
// 变长双向链表
@ -23,9 +23,9 @@ type Element = list.Element
// 获得一个变长链表指针
func New(safe...bool) *List {
func New(unsafe...bool) *List {
return &List {
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
list : list.New(),
}
}

View File

@ -4,8 +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 gmap provides kinds of concurrent-safe(alternative) maps.
// 并发安全的哈希MAP.
// Package gmap provides kinds of concurrent-safe(alternative) maps/并发安全的哈希MAP.
package gmap
// 默认的Map对象其实就是InterfaceInterfaceMap的别名。

View File

@ -8,7 +8,7 @@
package gmap
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type IntBoolMap struct {
@ -16,10 +16,10 @@ type IntBoolMap struct {
mu *rwmutex.RWMutex
}
func NewIntBoolMap(safe...bool) *IntBoolMap {
func NewIntBoolMap(unsafe...bool) *IntBoolMap {
return &IntBoolMap{
m : make(map[int]bool),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -8,7 +8,7 @@
package gmap
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type IntIntMap struct {
@ -16,10 +16,10 @@ type IntIntMap struct {
m map[int]int
}
func NewIntIntMap(safe...bool) *IntIntMap {
func NewIntIntMap(unsafe...bool) *IntIntMap {
return &IntIntMap{
m : make(map[int]int),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -7,17 +7,17 @@
package gmap
import "gitee.com/johng/gf/g/container/internal/rwmutex"
import "gitee.com/johng/gf/g/internal/rwmutex"
type IntInterfaceMap struct {
mu *rwmutex.RWMutex
m map[int]interface{}
}
func NewIntInterfaceMap(safe...bool) *IntInterfaceMap {
func NewIntInterfaceMap(unsafe...bool) *IntInterfaceMap {
return &IntInterfaceMap{
m : make(map[int]interface{}),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -8,7 +8,7 @@
package gmap
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type IntStringMap struct {
@ -16,10 +16,10 @@ type IntStringMap struct {
m map[int]string
}
func NewIntStringMap(safe...bool) *IntStringMap {
func NewIntStringMap(unsafe...bool) *IntStringMap {
return &IntStringMap{
m : make(map[int]string),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -8,7 +8,7 @@
package gmap
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type InterfaceInterfaceMap struct {
@ -16,10 +16,10 @@ type InterfaceInterfaceMap struct {
m map[interface{}]interface{}
}
func NewInterfaceInterfaceMap(safe...bool) *InterfaceInterfaceMap {
func NewInterfaceInterfaceMap(unsafe...bool) *InterfaceInterfaceMap {
return &InterfaceInterfaceMap{
m : make(map[interface{}]interface{}),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -8,7 +8,7 @@
package gmap
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type StringBoolMap struct {
@ -16,10 +16,10 @@ type StringBoolMap struct {
m map[string]bool
}
func NewStringBoolMap(safe...bool) *StringBoolMap {
func NewStringBoolMap(unsafe...bool) *StringBoolMap {
return &StringBoolMap{
m : make(map[string]bool),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -7,17 +7,17 @@
package gmap
import "gitee.com/johng/gf/g/container/internal/rwmutex"
import "gitee.com/johng/gf/g/internal/rwmutex"
type StringIntMap struct {
mu *rwmutex.RWMutex
m map[string]int
}
func NewStringIntMap(safe...bool) *StringIntMap {
func NewStringIntMap(unsafe...bool) *StringIntMap {
return &StringIntMap{
m : make(map[string]int),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -8,7 +8,7 @@
package gmap
import (
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type StringInterfaceMap struct {
@ -16,10 +16,10 @@ type StringInterfaceMap struct {
m map[string]interface{}
}
func NewStringInterfaceMap(safe...bool) *StringInterfaceMap {
func NewStringInterfaceMap(unsafe...bool) *StringInterfaceMap {
return &StringInterfaceMap{
m : make(map[string]interface{}),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -7,17 +7,17 @@
package gmap
import "gitee.com/johng/gf/g/container/internal/rwmutex"
import "gitee.com/johng/gf/g/internal/rwmutex"
type StringStringMap struct {
mu *rwmutex.RWMutex
m map[string]string
}
func NewStringStringMap(safe...bool) *StringStringMap {
func NewStringStringMap(unsafe...bool) *StringStringMap {
return &StringStringMap{
m : make(map[string]string),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}

View File

@ -4,13 +4,13 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// Package gring provides a concurrent-safe(alternative) ring(circular lists)/并发安全环.
// Package gring provides a concurrent-safe(alternative) ring(circular lists)/并发安全环.
package gring
import (
"container/ring"
"gitee.com/johng/gf/g/container/gtype"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type Ring struct {
@ -21,9 +21,9 @@ type Ring struct {
dirty *gtype.Bool // 标记环是否脏了(需要重新计算大小,当环大小发生改变时做标记)
}
func New(cap int, safe...bool) *Ring {
func New(cap int, unsafe...bool) *Ring {
return &Ring {
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
ring : ring.New(cap),
len : gtype.NewInt(),
cap : gtype.NewInt(cap),

View File

@ -4,13 +4,12 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// Package gset provides kinds of concurrent-safe(alternative) sets.
// 并发安全的集合SET.
// Package gset provides kinds of concurrent-safe(alternative) sets/并发安全集合.
package gset
type Set = InterfaceSet
// 默认Set类型
func New(safe...bool) *Set {
return NewInterfaceSet(safe...)
func New(unsafe...bool) *Set {
return NewInterfaceSet(unsafe...)
}

View File

@ -10,7 +10,7 @@ package gset
import (
"fmt"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type IntSet struct {
@ -18,18 +18,18 @@ type IntSet struct {
m map[int]struct{}
}
func NewIntSet(safe...bool) *IntSet {
func NewIntSet(unsafe...bool) *IntSet {
return &IntSet{
m : make(map[int]struct{}),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *IntSet) Iterator(f func (v int) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, _ := range this.m {
func (set *IntSet) Iterator(f func (v int) bool) {
set.mu.RLock()
defer set.mu.RUnlock()
for k, _ := range set.m {
if !f(k) {
break
}
@ -37,80 +37,80 @@ func (this *IntSet) Iterator(f func (v int) bool) {
}
// 设置键
func (this *IntSet) Add(item int) *IntSet {
this.mu.Lock()
this.m[item] = struct{}{}
this.mu.Unlock()
return this
func (set *IntSet) Add(item int) *IntSet {
set.mu.Lock()
set.m[item] = struct{}{}
set.mu.Unlock()
return set
}
// 批量添加设置键
func (this *IntSet) BatchAdd(items []int) *IntSet {
this.mu.Lock()
func (set *IntSet) BatchAdd(items []int) *IntSet {
set.mu.Lock()
for _, item := range items {
this.m[item] = struct{}{}
set.m[item] = struct{}{}
}
this.mu.Unlock()
return this
set.mu.Unlock()
return set
}
// 键是否存在
func (this *IntSet) Contains(item int) bool {
this.mu.RLock()
_, exists := this.m[item]
this.mu.RUnlock()
func (set *IntSet) Contains(item int) bool {
set.mu.RLock()
_, exists := set.m[item]
set.mu.RUnlock()
return exists
}
// 删除键值对
func (this *IntSet) Remove(key int) {
this.mu.Lock()
delete(this.m, key)
this.mu.Unlock()
func (set *IntSet) Remove(key int) {
set.mu.Lock()
delete(set.m, key)
set.mu.Unlock()
}
// 大小
func (this *IntSet) Size() int {
this.mu.RLock()
l := len(this.m)
this.mu.RUnlock()
func (set *IntSet) Size() int {
set.mu.RLock()
l := len(set.m)
set.mu.RUnlock()
return l
}
// 清空set
func (this *IntSet) Clear() {
this.mu.Lock()
this.m = make(map[int]struct{})
this.mu.Unlock()
func (set *IntSet) Clear() {
set.mu.Lock()
set.m = make(map[int]struct{})
set.mu.Unlock()
}
// 转换为数组
func (this *IntSet) Slice() []int {
this.mu.RLock()
ret := make([]int, len(this.m))
func (set *IntSet) Slice() []int {
set.mu.RLock()
ret := make([]int, len(set.m))
i := 0
for item := range this.m {
for item := range set.m {
ret[i] = item
i++
}
this.mu.RUnlock()
set.mu.RUnlock()
return ret
}
// 转换为字符串
func (this *IntSet) String() string {
return fmt.Sprint(this.Slice())
func (set *IntSet) String() string {
return fmt.Sprint(set.Slice())
}
func (this *IntSet) LockFunc(f func(m map[int]struct{})) {
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
func (set *IntSet) LockFunc(f func(m map[int]struct{})) {
set.mu.Lock(true)
defer set.mu.Unlock(true)
f(set.m)
}
func (this *IntSet) RLockFunc(f func(m map[int]struct{})) {
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
func (set *IntSet) RLockFunc(f func(m map[int]struct{})) {
set.mu.RLock(true)
defer set.mu.RUnlock(true)
f(set.m)
}

View File

@ -9,7 +9,7 @@ package gset
import (
"fmt"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type InterfaceSet struct {
@ -17,18 +17,18 @@ type InterfaceSet struct {
m map[interface{}]struct{}
}
func NewInterfaceSet(safe...bool) *InterfaceSet {
func NewInterfaceSet(unsafe...bool) *InterfaceSet {
return &InterfaceSet{
m : make(map[interface{}]struct{}),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *InterfaceSet) Iterator(f func (v interface{}) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, _ := range this.m {
func (set *InterfaceSet) Iterator(f func (v interface{}) bool) {
set.mu.RLock()
defer set.mu.RUnlock()
for k, _ := range set.m {
if !f(k) {
break
}
@ -36,79 +36,79 @@ func (this *InterfaceSet) Iterator(f func (v interface{}) bool) {
}
// 添加
func (this *InterfaceSet) Add(item interface{}) *InterfaceSet {
this.mu.Lock()
this.m[item] = struct{}{}
this.mu.Unlock()
return this
func (set *InterfaceSet) Add(item interface{}) *InterfaceSet {
set.mu.Lock()
set.m[item] = struct{}{}
set.mu.Unlock()
return set
}
// 批量添加
func (this *InterfaceSet) BatchAdd(items []interface{}) *InterfaceSet {
this.mu.Lock()
func (set *InterfaceSet) BatchAdd(items []interface{}) *InterfaceSet {
set.mu.Lock()
for _, item := range items {
this.m[item] = struct{}{}
set.m[item] = struct{}{}
}
this.mu.Unlock()
return this
set.mu.Unlock()
return set
}
// 键是否存在
func (this *InterfaceSet) Contains(item interface{}) bool {
this.mu.RLock()
_, exists := this.m[item]
this.mu.RUnlock()
func (set *InterfaceSet) Contains(item interface{}) bool {
set.mu.RLock()
_, exists := set.m[item]
set.mu.RUnlock()
return exists
}
// 删除键值对
func (this *InterfaceSet) Remove(key interface{}) {
this.mu.Lock()
delete(this.m, key)
this.mu.Unlock()
func (set *InterfaceSet) Remove(key interface{}) {
set.mu.Lock()
delete(set.m, key)
set.mu.Unlock()
}
// 大小
func (this *InterfaceSet) Size() int {
this.mu.RLock()
l := len(this.m)
this.mu.RUnlock()
func (set *InterfaceSet) Size() int {
set.mu.RLock()
l := len(set.m)
set.mu.RUnlock()
return l
}
// 清空set
func (this *InterfaceSet) Clear() {
this.mu.Lock()
this.m = make(map[interface{}]struct{})
this.mu.Unlock()
func (set *InterfaceSet) Clear() {
set.mu.Lock()
set.m = make(map[interface{}]struct{})
set.mu.Unlock()
}
// 转换为数组
func (this *InterfaceSet) Slice() []interface{} {
this.mu.RLock()
func (set *InterfaceSet) Slice() []interface{} {
set.mu.RLock()
i := 0
ret := make([]interface{}, len(this.m))
for item := range this.m {
ret := make([]interface{}, len(set.m))
for item := range set.m {
ret[i] = item
i++
}
this.mu.RUnlock()
set.mu.RUnlock()
return ret
}
// 转换为字符串
func (this *InterfaceSet) String() string {
return fmt.Sprint(this.Slice())
func (set *InterfaceSet) String() string {
return fmt.Sprint(set.Slice())
}
func (this *InterfaceSet) LockFunc(f func(m map[interface{}]struct{})) {
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
func (set *InterfaceSet) LockFunc(f func(m map[interface{}]struct{})) {
set.mu.Lock(true)
defer set.mu.Unlock(true)
f(set.m)
}
func (this *InterfaceSet) RLockFunc(f func(m map[interface{}]struct{})) {
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
func (set *InterfaceSet) RLockFunc(f func(m map[interface{}]struct{})) {
set.mu.RLock(true)
defer set.mu.RUnlock(true)
f(set.m)
}

View File

@ -9,7 +9,7 @@ package gset
import (
"fmt"
"gitee.com/johng/gf/g/container/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
)
type StringSet struct {
@ -17,18 +17,18 @@ type StringSet struct {
m map[string]struct{}
}
func NewStringSet(safe...bool) *StringSet {
return &StringSet{
func NewStringSet(unsafe...bool) *StringSet {
return &StringSet {
m : make(map[string]struct{}),
mu : rwmutex.New(safe...),
mu : rwmutex.New(unsafe...),
}
}
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *StringSet) Iterator(f func (v string) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, _ := range this.m {
func (set *StringSet) Iterator(f func (v string) bool) {
set.mu.RLock()
defer set.mu.RUnlock()
for k, _ := range set.m {
if !f(k) {
break
}
@ -36,80 +36,80 @@ func (this *StringSet) Iterator(f func (v string) bool) {
}
// 设置键
func (this *StringSet) Add(item string) *StringSet {
this.mu.Lock()
this.m[item] = struct{}{}
this.mu.Unlock()
return this
func (set *StringSet) Add(item string) *StringSet {
set.mu.Lock()
set.m[item] = struct{}{}
set.mu.Unlock()
return set
}
// 批量添加设置键
func (this *StringSet) BatchAdd(items []string) *StringSet {
this.mu.Lock()
func (set *StringSet) BatchAdd(items []string) *StringSet {
set.mu.Lock()
for _, item := range items {
this.m[item] = struct{}{}
set.m[item] = struct{}{}
}
this.mu.Unlock()
return this
set.mu.Unlock()
return set
}
// 键是否存在
func (this *StringSet) Contains(item string) bool {
this.mu.RLock()
_, exists := this.m[item]
this.mu.RUnlock()
func (set *StringSet) Contains(item string) bool {
set.mu.RLock()
_, exists := set.m[item]
set.mu.RUnlock()
return exists
}
// 删除键值对
func (this *StringSet) Remove(key string) {
this.mu.Lock()
delete(this.m, key)
this.mu.Unlock()
func (set *StringSet) Remove(key string) {
set.mu.Lock()
delete(set.m, key)
set.mu.Unlock()
}
// 大小
func (this *StringSet) Size() int {
this.mu.RLock()
l := len(this.m)
this.mu.RUnlock()
func (set *StringSet) Size() int {
set.mu.RLock()
l := len(set.m)
set.mu.RUnlock()
return l
}
// 清空set
func (this *StringSet) Clear() {
this.mu.Lock()
this.m = make(map[string]struct{})
this.mu.Unlock()
func (set *StringSet) Clear() {
set.mu.Lock()
set.m = make(map[string]struct{})
set.mu.Unlock()
}
// 转换为数组
func (this *StringSet) Slice() []string {
this.mu.RLock()
ret := make([]string, len(this.m))
func (set *StringSet) Slice() []string {
set.mu.RLock()
ret := make([]string, len(set.m))
i := 0
for item := range this.m {
for item := range set.m {
ret[i] = item
i++
}
this.mu.RUnlock()
set.mu.RUnlock()
return ret
}
// 转换为字符串
func (this *StringSet) String() string {
return fmt.Sprint(this.Slice())
func (set *StringSet) String() string {
return fmt.Sprint(set.Slice())
}
func (this *StringSet) LockFunc(f func(m map[string]struct{})) {
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
func (set *StringSet) LockFunc(f func(m map[string]struct{})) {
set.mu.Lock(true)
defer set.mu.Unlock(true)
f(set.m)
}
func (this *StringSet) RLockFunc(f func(m map[string]struct{})) {
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)
func (set *StringSet) RLockFunc(f func(m map[string]struct{})) {
set.mu.RLock(true)
defer set.mu.RUnlock(true)
f(set.m)
}

View File

@ -14,9 +14,9 @@ import (
"gitee.com/johng/gf/g/container/gset"
)
var intsUnsafe = gset.NewIntSet(false)
var itfsUnsafe = gset.NewInterfaceSet(false)
var strsUnsafe = gset.NewStringSet(false)
var intsUnsafe = gset.NewIntSet(true)
var itfsUnsafe = gset.NewInterfaceSet(true)
var strsUnsafe = gset.NewStringSet(true)
func Benchmark_Unsafe_IntSet_Add(b *testing.B) {
for i := 0; i < b.N; i++ {

View File

@ -4,8 +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 gtype provides kinds of concurrent-safe basic-types.
// 并发安全的基本类型.
// Package gtype provides kinds of concurrent-safe basic-types/并发安全基本类型.
package gtype
type Type = Interface

View File

@ -4,8 +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 gvar provides a universal variable type.
// 通用动态变量.
// Package gvar provides a universal variable type/通用动态变量.
package gvar
import (
@ -21,10 +20,10 @@ type Var struct {
}
// 创建一个动态变量value参数可以为nil
func New(value interface{}, safe...bool) *Var {
func New(value interface{}, unsafe...bool) *Var {
v := &Var{}
if len(safe) > 0 && safe[0] {
v.safe = safe[0]
if len(unsafe) == 0 || !unsafe[0] {
v.safe = true
v.value = gtype.NewInterface(value)
} else {
v.value = value
@ -33,8 +32,8 @@ func New(value interface{}, safe...bool) *Var {
}
// 创建一个只读动态变量value参数可以为nil
func NewRead(value interface{}, safe...bool) VarRead {
return VarRead(New(value, safe...))
func NewRead(value interface{}, unsafe...bool) VarRead {
return VarRead(New(value, unsafe...))
}
// 返回动态变量的只读接口
@ -90,10 +89,16 @@ func (v *Var) Floats() []float64 { return gconv.Floats(v.Val()) }
func (v *Var) Strings() []string { return gconv.Strings(v.Val()) }
func (v *Var) Interfaces() []interface{} { return gconv.Interfaces(v.Val()) }
func (v *Var) Time(format...string) time.Time { return gconv.Time(v.Val(), format...) }
func (v *Var) TimeDuration() time.Duration { return gconv.TimeDuration(v.Val()) }
func (v *Var) Time(format...string) time.Time {
return gconv.Time(v.Val(), format...)
}
func (v *Var) TimeDuration() time.Duration {
return gconv.TimeDuration(v.Val())
}
func (v *Var) GTime(format...string) *gtime.Time { return gconv.GTime(v.Val(), format...) }
func (v *Var) GTime(format...string) *gtime.Time {
return gconv.GTime(v.Val(), format...)
}
// 将变量转换为对象,注意 objPointer 参数必须为struct指针
func (v *Var) Struct(objPointer interface{}, attrMapping...map[string]string) error {

View File

@ -21,7 +21,7 @@ import (
"gitee.com/johng/gf/g/encoding/gtoml"
"gitee.com/johng/gf/g/util/gstr"
"time"
"gitee.com/johng/gf/g/encoding/gjson/internal/rwmutex"
"gitee.com/johng/gf/g/internal/rwmutex"
"fmt"
)
@ -38,7 +38,7 @@ type Json struct {
}
// 将变量转换为Json对象进行处理该变量至少应当是一个map或者slice否者转换没有意义
func New(value interface{}, safe...bool) *Json {
func New(value interface{}, unsafe...bool) *Json {
j := (*Json)(nil)
switch value.(type) {
case map[string]interface{}, []interface{}, nil:
@ -67,7 +67,7 @@ func New(value interface{}, safe...bool) *Json {
}
}
}
j.mu = rwmutex.New(safe...)
j.mu = rwmutex.New(unsafe...)
return j
}

View File

@ -1,47 +0,0 @@
package rwmutex
import "sync"
// RWMutex的封装支持对并发安全开启/关闭的控制。
type RWMutex struct {
sync.RWMutex
safe bool
}
func New(safe...bool) *RWMutex {
mu := new(RWMutex)
if len(safe) > 0 {
mu.safe = safe[0]
} else {
mu.safe = true
}
return mu
}
func (mu *RWMutex) IsSafe() bool {
return mu.safe
}
func (mu *RWMutex) Lock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.Lock()
}
}
func (mu *RWMutex) Unlock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.Unlock()
}
}
func (mu *RWMutex) RLock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.RLock()
}
}
func (mu *RWMutex) RUnlock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.RWMutex.RUnlock()
}
}

View File

@ -18,8 +18,8 @@ type Parser struct {
// 将变量转换为Parser对象进行处理该变量至少应当是一个map或者array否者转换没有意义
// value可以传递nil, 表示创建一个空的Parser对象
func New (value interface{}, safe...bool) *Parser {
return &Parser{gjson.New(value, safe...)}
func New (value interface{}, unsafe...bool) *Parser {
return &Parser{gjson.New(value, unsafe...)}
}
// 非并发安全Parser对象

View File

@ -24,8 +24,8 @@ const (
)
// 动态变量
func NewVar(i interface{}, safe...bool) *Var {
return gvar.New(i, safe...)
func NewVar(i interface{}, unsafe...bool) *Var {
return gvar.New(i, unsafe...)
}
// 阻塞等待HTTPServer执行完成(同一进程多HTTPServer情况下)

35
g/internal/mutex/mutex.go Normal file
View File

@ -0,0 +1,35 @@
package mutex
import "sync"
// Mutex的封装支持对并发安全开启/关闭的控制。
type Mutex struct {
sync.Mutex
safe bool
}
func New(unsafe...bool) *Mutex {
mu := new(Mutex)
if len(unsafe) > 0 {
mu.safe = !unsafe[0]
} else {
mu.safe = true
}
return mu
}
func (mu *Mutex) IsSafe() bool {
return mu.safe
}
func (mu *Mutex) Lock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.Mutex.Lock()
}
}
func (mu *Mutex) Unlock(force...bool) {
if mu.safe || (len(force) > 0 && force[0]) {
mu.Mutex.Unlock()
}
}

View File

@ -8,10 +8,10 @@ type RWMutex struct {
safe bool
}
func New(safe...bool) *RWMutex {
func New(unsafe...bool) *RWMutex {
mu := new(RWMutex)
if len(safe) > 0 {
mu.safe = safe[0]
if len(unsafe) > 0 {
mu.safe = !unsafe[0]
} else {
mu.safe = true
}

View File

@ -150,7 +150,7 @@ func (s *Server) searchHookHandler(method, path, domain, hook string) []*handler
}
// 多层链表遍历检索,从数组末尾的链表开始遍历,末尾的深度高优先级也高
pushedSet := gset.NewStringSet(false)
pushedSet := gset.NewStringSet(true)
for i := len(lists) - 1; i >= 0; i-- {
for e := lists[i].Front(); e != nil; e = e.Next() {
handler := e.Value.(*handlerItem)

View File

@ -84,7 +84,7 @@ func Struct(params interface{}, objPointer interface{}, attrMapping...map[string
}
}
// 最后按照默认规则进行匹配
attrset := gset.NewStringSet(false)
attrset := gset.NewStringSet(true)
elemtype := elem.Type()
for i := 0; i < elem.NumField(); i++ {
attrset.Add(elemtype.Field(i).Name)