-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcapi_handle.go
54 lines (48 loc) · 1.45 KB
/
capi_handle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//go:build go1.24
package tiledb
import (
"runtime"
"sync/atomic"
"unsafe"
)
// capiHandle encapsulates and manages the lifetime of a resource, usually a TileDB C API handle.
// Do not use directly; use one of the wrapper types for specific handle kinds.
type capiHandle struct {
ptr unsafe.Pointer
freeFunc func(unsafe.Pointer)
cleanup runtime.Cleanup
}
// Free releases the resource held by the capiHandle.
// This method is safe to call from multiple goroutines concurrently.
// However, freeing the handle while it is being used by another goroutine is not safe and
// will result in crashes.
func (x *capiHandle) Free() {
x.cleanup.Stop()
p := atomic.SwapPointer(&x.ptr, nil)
// Do not fail if a handle is freed multiple times.
if p != nil {
x.freeFunc(p)
}
}
// Get returns the pointer contained in the capiHandle.
// This function will panic if it is called after calling Free.
func (x *capiHandle) Get() (ptr unsafe.Pointer) {
ptr = atomic.LoadPointer(&x.ptr)
if ptr == nil {
panic("capiHandle.Get: handle is freed")
}
return
}
// newCapiHandle creates a capiHandle. It accepts a pointer and a function that will
// release the resources held by the pointer.
func newCapiHandle(p unsafe.Pointer, freeFunc func(unsafe.Pointer)) *capiHandle {
if p == nil {
return nil
}
handle := &capiHandle{
freeFunc: freeFunc,
}
atomic.StorePointer(&handle.ptr, unsafe.Pointer(p))
handle.cleanup = runtime.AddCleanup(handle, freeFunc, p)
return handle
}