mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-12 22:40:54 +08:00
fix(item): trans destroy procedure
This commit is contained in:
27
item.go
27
item.go
@@ -37,7 +37,7 @@ func (b *Item[T]) Trans() (tb *Item[T]) {
|
|||||||
(*uintptr)(&b.stat), uintptr(destroyedstatus),
|
(*uintptr)(&b.stat), uintptr(destroyedstatus),
|
||||||
))
|
))
|
||||||
tb.stat.setintrans(true)
|
tb.stat.setintrans(true)
|
||||||
b.pool.put(b)
|
b.destroybystat(status(0))
|
||||||
return tb
|
return tb
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,20 +83,29 @@ func (b *Item[T]) Copy() (cb *Item[T]) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy item and put it back to pool.
|
func (b *Item[T]) destroybystat(stat status) {
|
||||||
func (b *Item[T]) Destroy() {
|
switch {
|
||||||
stat := status(atomic.SwapUintptr(
|
case stat.hasdestroyed():
|
||||||
(*uintptr)(&b.stat), uintptr(destroyedstatus),
|
|
||||||
))
|
|
||||||
if stat.hasdestroyed() {
|
|
||||||
panic("use after destroy")
|
panic("use after destroy")
|
||||||
}
|
case stat.isintrans():
|
||||||
if !stat.isintrans() && stat.isbuffered() {
|
var v T
|
||||||
|
b.val = v
|
||||||
|
case stat.isbuffered():
|
||||||
b.pool.pooler.Reset(&b.val)
|
b.pool.pooler.Reset(&b.val)
|
||||||
|
default:
|
||||||
|
var v T
|
||||||
|
b.val = v
|
||||||
}
|
}
|
||||||
b.pool.put(b)
|
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.
|
// setautodestroy item on GC.
|
||||||
//
|
//
|
||||||
// Only can call once.
|
// Only can call once.
|
||||||
|
|||||||
8
pool.go
8
pool.go
@@ -63,14 +63,12 @@ func (pool *Pool[T]) newempty() *Item[T] {
|
|||||||
func (pool *Pool[T]) put(item *Item[T]) {
|
func (pool *Pool[T]) put(item *Item[T]) {
|
||||||
runtime.SetFinalizer(item, nil)
|
runtime.SetFinalizer(item, nil)
|
||||||
|
|
||||||
|
item.stat.setdestroyed(true)
|
||||||
|
item.cfg = nil
|
||||||
|
|
||||||
if pool.isstrict {
|
if pool.isstrict {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
item.cfg = nil
|
|
||||||
var dt T
|
|
||||||
item.val = dt
|
|
||||||
|
|
||||||
pool.pool.Put(item)
|
pool.pool.Put(item)
|
||||||
|
|
||||||
pool.decout()
|
pool.decout()
|
||||||
|
|||||||
Reference in New Issue
Block a user