Repository navigation
Expand file tree
/
Copy pathexecutor_impl_cgo.go
More file actions
66 lines (57 loc) · 1.61 KB
/
Copy pathexecutor_impl_cgo.go
File metadata and controls
66 lines (57 loc) · 1.61 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
//go:build !with_purego
package cronet
// #include <stdlib.h>
// #include <stdbool.h>
// #include <cronet_c.h>
// extern CRONET_EXPORT void cronetExecutorExecute(Cronet_ExecutorPtr self,Cronet_RunnablePtr command);
import "C"
import (
"sync"
"sync/atomic"
"unsafe"
)
type executorEntry struct {
executeFunc ExecutorExecuteFunc
destroyed atomic.Bool
}
var (
executorAccess sync.RWMutex
executors map[uintptr]*executorEntry
)
func init() {
executors = make(map[uintptr]*executorEntry)
}
func NewExecutor(executeFunc ExecutorExecuteFunc) Executor {
if executeFunc == nil {
panic("nil executor execute function")
}
ptr := C.Cronet_Executor_CreateWith((*[0]byte)(C.cronetExecutorExecute))
ptrVal := uintptr(unsafe.Pointer(ptr))
executorAccess.Lock()
executors[ptrVal] = &executorEntry{executeFunc: executeFunc}
executorAccess.Unlock()
return Executor{ptrVal}
}
func (e Executor) Destroy() {
executorAccess.Lock()
entry := executors[e.ptr]
if entry != nil {
entry.destroyed.Store(true)
}
executorAccess.Unlock()
C.Cronet_Executor_Destroy(C.Cronet_ExecutorPtr(unsafe.Pointer(e.ptr)))
// Cleanup after C destroy
executorAccess.Lock()
delete(executors, e.ptr)
executorAccess.Unlock()
}
//export cronetExecutorExecute
func cronetExecutorExecute(self C.Cronet_ExecutorPtr, command C.Cronet_RunnablePtr) {
executorAccess.RLock()
entry := executors[uintptr(unsafe.Pointer(self))]
executorAccess.RUnlock()
if entry == nil || entry.destroyed.Load() {
return // Post-destroy callback, silently ignore
}
entry.executeFunc(Executor{uintptr(unsafe.Pointer(self))}, Runnable{uintptr(unsafe.Pointer(command))})
}