Browse Source

syncslice添加注释

yjp 1 year ago
parent
commit
6b2e318cee
2 changed files with 27 additions and 3 deletions
  1. 17 0
      syncutils/sync_slice.go
  2. 10 3
      syncutils/sync_slice_test.go

+ 17 - 0
syncutils/sync_slice.go

@@ -2,14 +2,17 @@ package syncutils
 
 import "sync"
 
+// RangeFunc Range方法回调函数
 type RangeFunc[T any] func(index int, e T) bool
 
+// SyncSlice 并发安全的slice
 type SyncSlice[T any] struct {
 	writeLocker sync.Locker
 	readLocker  sync.Locker
 	s           []T
 }
 
+// NewSyncSlice 创建SyncSlice
 func NewSyncSlice[T any](s []T, isRWMutex bool) *SyncSlice[T] {
 	var writeLocker sync.Locker
 	var readLocker sync.Locker
@@ -31,22 +34,27 @@ func NewSyncSlice[T any](s []T, isRWMutex bool) *SyncSlice[T] {
 	}
 }
 
+// Lock 手动上写锁
 func (syncSlice *SyncSlice[T]) Lock() {
 	syncSlice.writeLocker.Lock()
 }
 
+// Unlock 手动解写锁
 func (syncSlice *SyncSlice[T]) Unlock() {
 	syncSlice.writeLocker.Unlock()
 }
 
+// RLock 手动上读锁
 func (syncSlice *SyncSlice[T]) RLock() {
 	syncSlice.readLocker.Lock()
 }
 
+// RUnlock 手动解读锁
 func (syncSlice *SyncSlice[T]) RUnlock() {
 	syncSlice.readLocker.Unlock()
 }
 
+// AppendNoLock 不上锁添加元素
 func (syncSlice *SyncSlice[T]) AppendNoLock(elements ...T) {
 	if elements == nil || len(elements) == 0 {
 		return
@@ -55,6 +63,7 @@ func (syncSlice *SyncSlice[T]) AppendNoLock(elements ...T) {
 	syncSlice.appendNoLock(elements...)
 }
 
+// DeleteAtNoLock 不上锁删除元素
 func (syncSlice *SyncSlice[T]) DeleteAtNoLock(index int) T {
 	if index < 0 {
 		var zero T
@@ -64,6 +73,7 @@ func (syncSlice *SyncSlice[T]) DeleteAtNoLock(index int) T {
 	return syncSlice.deleteAtNoLock(index)
 }
 
+// RangeNoLock 不上锁遍历slice
 func (syncSlice *SyncSlice[T]) RangeNoLock(rangeFunc RangeFunc[T]) {
 	if rangeFunc == nil {
 		return
@@ -72,14 +82,17 @@ func (syncSlice *SyncSlice[T]) RangeNoLock(rangeFunc RangeFunc[T]) {
 	syncSlice.rangeNoLock(rangeFunc)
 }
 
+// LenNoLock 不上锁获取slice长度
 func (syncSlice *SyncSlice[T]) LenNoLock() int {
 	return syncSlice.lenNoLock()
 }
 
+// CapNoLock 不上锁获取slice容量
 func (syncSlice *SyncSlice[T]) CapNoLock() int {
 	return syncSlice.capNoLock()
 }
 
+// Append 上锁添加元素
 func (syncSlice *SyncSlice[T]) Append(elements ...T) {
 	if elements == nil || len(elements) == 0 {
 		return
@@ -91,6 +104,7 @@ func (syncSlice *SyncSlice[T]) Append(elements ...T) {
 	syncSlice.appendNoLock(elements...)
 }
 
+// DeleteAt 上锁删除元素
 func (syncSlice *SyncSlice[T]) DeleteAt(index int) T {
 	if index < 0 {
 		var zero T
@@ -103,6 +117,7 @@ func (syncSlice *SyncSlice[T]) DeleteAt(index int) T {
 	return syncSlice.deleteAtNoLock(index)
 }
 
+// Range 上锁遍历slice
 func (syncSlice *SyncSlice[T]) Range(rangeFunc RangeFunc[T]) {
 	if rangeFunc == nil {
 		return
@@ -114,6 +129,7 @@ func (syncSlice *SyncSlice[T]) Range(rangeFunc RangeFunc[T]) {
 	syncSlice.rangeNoLock(rangeFunc)
 }
 
+// Len 上锁获取slice长度
 func (syncSlice *SyncSlice[T]) Len() int {
 	syncSlice.readLocker.Lock()
 	defer syncSlice.readLocker.Unlock()
@@ -121,6 +137,7 @@ func (syncSlice *SyncSlice[T]) Len() int {
 	return syncSlice.lenNoLock()
 }
 
+// Cap 上锁获取slice容量
 func (syncSlice *SyncSlice[T]) Cap() int {
 	syncSlice.readLocker.Lock()
 	defer syncSlice.readLocker.Unlock()

+ 10 - 3
syncutils/sync_slice_test.go

@@ -79,8 +79,10 @@ func TestSyncSlice(t *testing.T) {
 	fmt.Println()
 
 	rwSyncSlice.Lock()
-
 	rwSyncSlice.AppendNoLock("ddd")
+	rwSyncSlice.Unlock()
+
+	rwSyncSlice.RLock()
 	fmt.Println("Len:", rwSyncSlice.LenNoLock())
 	fmt.Println("Cap:", rwSyncSlice.CapNoLock())
 	fmt.Println()
@@ -89,17 +91,22 @@ func TestSyncSlice(t *testing.T) {
 		fmt.Println("Index", strconv.Itoa(index)+":", e)
 		return false
 	})
+	rwSyncSlice.RUnlock()
+
 	fmt.Println()
 
+	rwSyncSlice.Lock()
 	deleted = rwSyncSlice.DeleteAtNoLock(3)
+	rwSyncSlice.Unlock()
 	fmt.Println("Deleted:", deleted)
 	fmt.Println()
 
+	rwSyncSlice.RLock()
 	rwSyncSlice.RangeNoLock(func(index int, e string) bool {
 		fmt.Println("Index", strconv.Itoa(index)+":", e)
 		return false
 	})
-	fmt.Println()
+	rwSyncSlice.RUnlock()
 
-	rwSyncSlice.Unlock()
+	fmt.Println()
 }