From 8c5e0cfad0b9427c459dd508ca4a407307c785dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:59:39 +0900 Subject: [PATCH] fix(item): trans destroy procedure --- item.go | 27 ++++++++++++++++++--------- pool.go | 8 +++----- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/item.go b/item.go index 7043daa..9647481 100644 --- a/item.go +++ b/item.go @@ -37,7 +37,7 @@ func (b *Item[T]) Trans() (tb *Item[T]) { (*uintptr)(&b.stat), uintptr(destroyedstatus), )) tb.stat.setintrans(true) - b.pool.put(b) + b.destroybystat(status(0)) return tb } @@ -83,20 +83,29 @@ func (b *Item[T]) Copy() (cb *Item[T]) { return } -// Destroy item and put it back to pool. -func (b *Item[T]) Destroy() { - stat := status(atomic.SwapUintptr( - (*uintptr)(&b.stat), uintptr(destroyedstatus), - )) - if stat.hasdestroyed() { +func (b *Item[T]) destroybystat(stat status) { + switch { + case stat.hasdestroyed(): panic("use after destroy") - } - if !stat.isintrans() && stat.isbuffered() { + case stat.isintrans(): + var v T + b.val = v + case stat.isbuffered(): b.pool.pooler.Reset(&b.val) + default: + var v T + b.val = v } b.pool.put(b) } +// Destroy item and put it back to pool. +func (b *Item[T]) Destroy() { + b.destroybystat(status(atomic.SwapUintptr( + (*uintptr)(&b.stat), uintptr(destroyedstatus), + ))) +} + // setautodestroy item on GC. // // Only can call once. diff --git a/pool.go b/pool.go index 66fb6d6..19c66da 100644 --- a/pool.go +++ b/pool.go @@ -63,14 +63,12 @@ func (pool *Pool[T]) newempty() *Item[T] { func (pool *Pool[T]) put(item *Item[T]) { runtime.SetFinalizer(item, nil) + item.stat.setdestroyed(true) + item.cfg = nil + if pool.isstrict { return } - - item.cfg = nil - var dt T - item.val = dt - pool.pool.Put(item) pool.decout()