Debian Codegraph @

generic/register.go

raw
1 // Copyright 2022 Huan Du. All rights reserved.
2 // Licensed under the MIT license that can be found in the LICENSE file.
3
4 package clone
5
6 import (
7 "reflect"
8 "sync/atomic"
9 )
10
11 // Record the count of cloning atomic.Pointer[T] for test purpose only.
12 var registerAtomicPointerCalled int32
13
14 // RegisterAtomicPointer registers a custom clone function for atomic.Pointer[T].
15 func RegisterAtomicPointer[T any]() {
16 SetCustomFunc(reflect.TypeOf(atomic.Pointer[T]{}), func(allocator *Allocator, old, new reflect.Value) {
17 if !old.CanAddr() {
18 return
19 }
20
21 // Clone value inside atomic.Pointer[T].
22 oldValue := old.Addr().Interface().(*atomic.Pointer[T])
23 newValue := new.Addr().Interface().(*atomic.Pointer[T])
24 v := oldValue.Load()
25 newValue.Store(v)
26
27 atomic.AddInt32(&registerAtomicPointerCalled, 1)
28 })
29 }