mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-11 21:51:18 +08:00
fix: let trans not reset val
This commit is contained in:
10
item.go
10
item.go
@@ -22,9 +22,11 @@ type Item[T any] struct {
|
|||||||
// Trans ownership to a new item and
|
// Trans ownership to a new item and
|
||||||
// destroy original item immediately.
|
// destroy original item immediately.
|
||||||
//
|
//
|
||||||
|
// The value in new item will not be Reset().
|
||||||
|
//
|
||||||
// Call this function to drop your ownership
|
// Call this function to drop your ownership
|
||||||
// before passing it to another function
|
// 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]) {
|
func (b *Item[T]) Trans() (tb *Item[T]) {
|
||||||
if b.stat.hasdestroyed() {
|
if b.stat.hasdestroyed() {
|
||||||
panic("use after destroy")
|
panic("use after destroy")
|
||||||
@@ -34,6 +36,7 @@ func (b *Item[T]) Trans() (tb *Item[T]) {
|
|||||||
tb.stat = status(atomic.SwapUintptr(
|
tb.stat = status(atomic.SwapUintptr(
|
||||||
(*uintptr)(&b.stat), uintptr(destroyedstatus),
|
(*uintptr)(&b.stat), uintptr(destroyedstatus),
|
||||||
))
|
))
|
||||||
|
tb.stat.setintrans(true)
|
||||||
b.pool.put(b)
|
b.pool.put(b)
|
||||||
return tb
|
return tb
|
||||||
}
|
}
|
||||||
@@ -66,6 +69,7 @@ func (b *Item[T]) Ref() (rb *Item[T]) {
|
|||||||
rb = b.pool.newempty()
|
rb = b.pool.newempty()
|
||||||
*rb = *b
|
*rb = *b
|
||||||
rb.stat.setbuffered(false)
|
rb.stat.setbuffered(false)
|
||||||
|
rb.stat.setintrans(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +91,7 @@ func (b *Item[T]) Destroy() {
|
|||||||
if stat.hasdestroyed() {
|
if stat.hasdestroyed() {
|
||||||
panic("use after destroy")
|
panic("use after destroy")
|
||||||
}
|
}
|
||||||
if b.stat.isbuffered() {
|
if !stat.isintrans() && stat.isbuffered() {
|
||||||
b.pool.pooler.Reset(&b.val)
|
b.pool.pooler.Reset(&b.val)
|
||||||
}
|
}
|
||||||
b.pool.put(b)
|
b.pool.put(b)
|
||||||
@@ -102,7 +106,7 @@ func (b *Item[T]) setautodestroy() *Item[T] {
|
|||||||
if item.stat.hasdestroyed() {
|
if item.stat.hasdestroyed() {
|
||||||
panic("unexpected hasdestroyed")
|
panic("unexpected hasdestroyed")
|
||||||
}
|
}
|
||||||
if item.stat.isbuffered() {
|
if !item.stat.isintrans() && item.stat.isbuffered() {
|
||||||
item.pool.pooler.Reset(&item.val)
|
item.pool.pooler.Reset(&item.val)
|
||||||
}
|
}
|
||||||
item.stat.setdestroyed(true)
|
item.stat.setdestroyed(true)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import "sync/atomic"
|
|||||||
const (
|
const (
|
||||||
statusisbuffered = 1 << iota
|
statusisbuffered = 1 << iota
|
||||||
statusdestroyed
|
statusdestroyed
|
||||||
|
statusisintrans
|
||||||
)
|
)
|
||||||
|
|
||||||
type status uintptr
|
type status uintptr
|
||||||
@@ -57,3 +58,11 @@ func (c *status) hasdestroyed() bool {
|
|||||||
func (c *status) setdestroyed(v bool) {
|
func (c *status) setdestroyed(v bool) {
|
||||||
c.setbool(v, statusdestroyed)
|
c.setbool(v, statusdestroyed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *status) isintrans() bool {
|
||||||
|
return c.loadbool(statusisintrans)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *status) setintrans(v bool) {
|
||||||
|
c.setbool(v, statusisintrans)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user