idlewatcher: refactor and fix data race

This commit is contained in:
yusing
2025-02-25 04:29:07 +08:00
parent 8074b93992
commit 767560804d
7 changed files with 71 additions and 68 deletions

View File

@@ -0,0 +1,5 @@
package atomic
import "sync/atomic"
type Bool = atomic.Bool

View File

@@ -22,11 +22,11 @@ func (a *Value[T]) Store(v T) {
}
func (a *Value[T]) Swap(v T) T {
return a.Value.Swap(v).(T)
}
func (a *Value[T]) CompareAndSwap(oldV, newV T) bool {
return a.Value.CompareAndSwap(oldV, newV)
if v := a.Value.Swap(v); v != nil {
return v.(T)
}
var zero T
return zero
}
func (a *Value[T]) MarshalJSON() ([]byte, error) {