从 Go v1.6 开始,cgo 改变了向 C 代码传递指针的规则golang/go#12416 https://github.com/golang/go/issues/12416。
从 wiki 中的 C 代码调用动态 Go 回调的示例不再有效。
package main
import (
"fmt"
"unsafe"
)
/*
extern void go_callback_int(void* foo, int p1);
// normally you will have to define function or variables
// in another separate C file to avoid the multiple definition
// errors, however, using "static inline" is a nice workaround
// for simple functions like this one.
static inline void CallMyFunction(void* pfoo) {
go_callback_int(pfoo, 5);
}
*/
import "C"
//export go_callback_int
func go_callback_int(pfoo unsafe.Pointer, p1 C.int) {
foo := *(*func(C.int))(pfoo)
foo(p1)
}
func MyCallback(x C.int) {
fmt.Println("callback with", x)
}
// we store it in a global variable so that the garbage collector
// doesn't clean up the memory for any temporary variables created.
var MyCallbackFunc = MyCallback
func Example() {
C.CallMyFunction(unsafe.Pointer(&MyCallbackFunc))
}
func main() {
Example()
}
输出如下所示:
panic: runtime error: cgo argument has Go pointer to Go pointer
今天执行此操作的正确方法是什么?最好不要像通过将指针转换为 uintptr_t 来隐藏语言中的指针那样的黑客行为。
从Go 1.6开始,cgo有了新的规则。
Go 代码可以将 Go 指针传递给 C,前提是它指向的 Go 内存不包含任何 Go 指针。
[source] https://github.com/golang/go/issues/12416
这些规则会在运行时进行检查,如果违反则程序崩溃。目前可以使用以下方法禁用检查GODEBUG=cgocheck=0
环境变量。但在未来,这可能会不再起作用。
因此,如果指针指向的内存存储了 Go 函数/方法指针,则不再可能将指针传递给 C 代码。有几种方法可以克服这个限制,但我想在大多数方法中你应该存储一个同步数据结构,它表示某个 id 和实际指针之间的对应关系。这样您就可以将 id 传递给 C 代码,而不是指针。
解决这个问题的代码可能如下所示:
package gocallback
import (
"fmt"
"sync"
)
/*
extern void go_callback_int(int foo, int p1);
// normally you will have to define function or variables
// in another separate C file to avoid the multiple definition
// errors, however, using "static inline" is a nice workaround
// for simple functions like this one.
static inline void CallMyFunction(int foo) {
go_callback_int(foo, 5);
}
*/
import "C"
//export go_callback_int
func go_callback_int(foo C.int, p1 C.int) {
fn := lookup(int(foo))
fn(p1)
}
func MyCallback(x C.int) {
fmt.Println("callback with", x)
}
func Example() {
i := register(MyCallback)
C.CallMyFunction(C.int(i))
unregister(i)
}
var mu sync.Mutex
var index int
var fns = make(map[int]func(C.int))
func register(fn func(C.int)) int {
mu.Lock()
defer mu.Unlock()
index++
for fns[index] != nil {
index++
}
fns[index] = fn
return index
}
func lookup(i int) func(C.int) {
mu.Lock()
defer mu.Unlock()
return fns[i]
}
func unregister(i int) {
mu.Lock()
defer mu.Unlock()
delete(fns, i)
}
此代码来自(已更新)维基页面 https://github.com/golang/go/wiki/cgo#function-variables.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)