From ad060ad98a3e42a5fcf5699ef462c1cbbb786fb9 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:44:48 +0900 Subject: [PATCH] fix: let trans not reset val --- item.go | 10 +++++++--- status.go | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/item.go b/item.go index 2d0f3db..7043daa 100644 --- a/item.go +++ b/item.go @@ -22,9 +22,11 @@ type Item[T any] struct { // Trans ownership to a new item and // destroy original item immediately. // +// The value in new item will not be Reset(). +// // Call this function to drop your ownership // before passing it to another function -// that do not return to you. +// that is not controlled by you. func (b *Item[T]) Trans() (tb *Item[T]) { if b.stat.hasdestroyed() { panic("use after destroy") @@ -34,6 +36,7 @@ func (b *Item[T]) Trans() (tb *Item[T]) { tb.stat = status(atomic.SwapUintptr( (*uintptr)(&b.stat), uintptr(destroyedstatus), )) + tb.stat.setintrans(true) b.pool.put(b) return tb } @@ -66,6 +69,7 @@ func (b *Item[T]) Ref() (rb *Item[T]) { rb = b.pool.newempty() *rb = *b rb.stat.setbuffered(false) + rb.stat.setintrans(false) return } @@ -87,7 +91,7 @@ func (b *Item[T]) Destroy() { if stat.hasdestroyed() { panic("use after destroy") } - if b.stat.isbuffered() { + if !stat.isintrans() && stat.isbuffered() { b.pool.pooler.Reset(&b.val) } b.pool.put(b) @@ -102,7 +106,7 @@ func (b *Item[T]) setautodestroy() *Item[T] { if item.stat.hasdestroyed() { panic("unexpected hasdestroyed") } - if item.stat.isbuffered() { + if !item.stat.isintrans() && item.stat.isbuffered() { item.pool.pooler.Reset(&item.val) } item.stat.setdestroyed(true) diff --git a/status.go b/status.go index 920a1ec..e2e9707 100644 --- a/status.go +++ b/status.go @@ -5,6 +5,7 @@ import "sync/atomic" const ( statusisbuffered = 1 << iota statusdestroyed + statusisintrans ) type status uintptr @@ -57,3 +58,11 @@ func (c *status) hasdestroyed() bool { func (c *status) setdestroyed(v bool) { c.setbool(v, statusdestroyed) } + +func (c *status) isintrans() bool { + return c.loadbool(statusisintrans) +} + +func (c *status) setintrans(v bool) { + c.setbool(v, statusisintrans) +}