Repository navigation
Expand file tree
/
Copy pathnative_callbacks.go
More file actions
105 lines (90 loc) · 2.66 KB
/
Copy pathnative_callbacks.go
File metadata and controls
105 lines (90 loc) · 2.66 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//go:build darwin
package vz
import (
"runtime"
"sync"
"sync/atomic"
"unsafe"
"github.com/Code-Hex/vz/v4/internal/vzbridge"
"github.com/ebitengine/purego"
)
type nativeCallback func(uint32, unsafe.Pointer, unsafe.Pointer, uint64) uint64
type nativeCallbackEntry struct {
callback nativeCallback
once bool
}
type nativeCallbackRegistry struct {
next atomic.Uint64
mu sync.Mutex
entries map[uint64]nativeCallbackEntry
}
func (r *nativeCallbackRegistry) add(callback nativeCallback, once bool) uint64 {
id := r.next.Add(1)
if id == 0 {
panic("native callback identifiers exhausted")
}
r.mu.Lock()
defer r.mu.Unlock()
if r.entries == nil {
r.entries = make(map[uint64]nativeCallbackEntry)
}
r.entries[id] = nativeCallbackEntry{
callback: callback,
once: once,
}
return id
}
func (r *nativeCallbackRegistry) take(id uint64, terminal bool) (nativeCallback, bool) {
r.mu.Lock()
defer r.mu.Unlock()
entry, ok := r.entries[id]
if ok && (terminal || entry.once) {
delete(r.entries, id)
}
return entry.callback, ok
}
func (r *nativeCallbackRegistry) remove(id uint64) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.entries, id)
}
var nativeCallbacks nativeCallbackRegistry
var nativeCallbackInit sync.Once
func initializeNativeCallbacks() {
nativeCallbackInit.Do(func() {
vzbridge.SetCallback(uint64(purego.NewCallback(dispatchNativeCallback)))
})
}
func registerNativeCallback(callback nativeCallback) uint64 {
initializeNativeCallbacks()
return nativeCallbacks.add(callback, false)
}
func registerNativeRequest(callback nativeCallback) uint64 {
initializeNativeCallbacks()
return nativeCallbacks.add(callback, true)
}
func unregisterNativeCallback(id uint64) { nativeCallbacks.remove(id) }
func dispatchNativeCallback(kind uint32, context uint64, first, second unsafe.Pointer, value uint64) uint64 {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if callback, ok := nativeCallbacks.take(context, kind == vzbridge.CallbackContextReleased); ok {
return callback(kind, first, second, value)
}
switch kind {
case vzbridge.CallbackSocketConnected, vzbridge.CallbackRestoreImageLoaded, vzbridge.CallbackAccessoryRegistered:
vzbridge.ReleaseObject(first)
vzbridge.ReleaseObject(second)
case vzbridge.CallbackAccessoryConnected,
vzbridge.CallbackAccessoryDisconnected,
vzbridge.CallbackUSBPassthroughDisconnected,
vzbridge.CallbackMachineStoppedWithError,
vzbridge.CallbackCompletion,
vzbridge.CallbackNetworkDisconnected,
vzbridge.CallbackSocketAccepted,
vzbridge.CallbackNetworkBlockDeviceError,
vzbridge.CallbackRosettaInstallCompleted,
vzbridge.CallbackMacOSInstallCompleted:
vzbridge.ReleaseObject(first)
}
return 0
}