]> 127.0.0.1 Git - part/.git/commitdiff
Add v0.23.11
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 4 Mar 2023 15:41:45 +0000 (23:41 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 4 Mar 2023 15:41:45 +0000 (23:41 +0800)
pool/Pool.go
pool/Pool_test.go

index c131c0eb8fb139c20d1e1b4fb5dbbf1d5a40e556..25e3c9db47da86e8a5d2a7bc232bb4d65007c8ba 100644 (file)
@@ -11,7 +11,7 @@ type Buf[T any] struct {
        reuseF  func(*T) *T
        poolF   func(*T) *T
        buf     []*T
-       sync.RWMutex
+       l       sync.RWMutex
 }
 
 // 创建池
@@ -35,9 +35,29 @@ func New[T any](NewF func() *T, InUse func(*T) bool, ReuseF func(*T) *T, PoolF f
        return t
 }
 
+func (t *Buf[T]) PoolInUse() (inUse int) {
+       t.l.RLock()
+       defer t.l.RUnlock()
+
+       for i := 0; i < len(t.buf); i++ {
+               if t.inUse(t.buf[i]) {
+                       inUse++
+               }
+       }
+
+       return
+}
+
+func (t *Buf[T]) PoolSum() int {
+       t.l.RLock()
+       defer t.l.RUnlock()
+
+       return len(t.buf)
+}
+
 func (t *Buf[T]) Trim() {
-       t.Lock()
-       defer t.Unlock()
+       t.l.Lock()
+       defer t.l.Unlock()
 
        for i := 0; i < len(t.buf); i++ {
                if !t.inUse(t.buf[i]) {
@@ -49,8 +69,8 @@ func (t *Buf[T]) Trim() {
 }
 
 func (t *Buf[T]) Get() *T {
-       t.Lock()
-       defer t.Unlock()
+       t.l.Lock()
+       defer t.l.Unlock()
 
        for i := 0; i < len(t.buf); i++ {
                if !t.inUse(t.buf[i]) {
@@ -66,8 +86,8 @@ func (t *Buf[T]) Put(item ...*T) {
                return
        }
 
-       t.Lock()
-       defer t.Unlock()
+       t.l.Lock()
+       defer t.l.Unlock()
 
        var cu = 0
        for i := 0; i < len(t.buf); i++ {
index da6965a5c776f79f0500b3a05ccc394d10d92383..55cbb006b368a4452b23bfb3caec6718db33d3e5 100644 (file)
@@ -52,5 +52,4 @@ func TestXxx(t *testing.T) {
        var c3 = b.Get()
 
        t.Log(unsafe.Pointer(c3), c3)
-
 }