Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing function pointer to the C code using cgo

Tags:

go

cgo

Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore.

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()
}

The output looks like this:

panic: runtime error: cgo argument has Go pointer to Go pointer

What is the proper way to do this today? Preferably without hacks like hiding pointer from the language by converting it into uintptr_t.

like image 463
Iakov Davydov Avatar asked May 11 '16 08:05

Iakov Davydov


2 Answers

The solution has three parts.

  1. Use a //export comment on the Go function to tell the cgo tool to produce a C wrapper for it (https://pkg.go.dev/cmd/cgo#hdr-C_references_to_Go).

  2. Forward-declare that C identifier in the cgo C preamble, so that you can refer to the C wrapper within the Go code (https://golang.org/issue/19837).

  3. Use a C typedef to convert that pointer to the correct C type, working around a cgo bug (https://golang.org/issue/19835).

Putting it all together:

package main

/*
static void invoke(void (*f)()) {
    f();
}

void go_print_hello();  // https://golang.org/issue/19837

typedef void (*closure)();  // https://golang.org/issue/19835
*/
import "C"

import "fmt"

//export go_print_hello
func go_print_hello() {
    fmt.Println("Hello, !")
}

func main() {
    C.invoke(C.closure(C.go_print_hello))
}
like image 186
bcmills Avatar answered Oct 21 '22 20:10

bcmills


It depends exactly what you need to do with the callback function - but a trick that might work is to not pass the Go function, but a pointer to a struct with the function on it that you want.

For example:

package main

import (
    "fmt"
    "unsafe"
)

/*
extern void go_callback_int(void*, int);
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 := (*Test)(pfoo)
    foo.cb(p1)
}

type Test struct {
}

func (s *Test) cb(x C.int) {
    fmt.Println("callback with", x)
}

func main() {
    data := &Test{}
    C.CallMyFunction(unsafe.Pointer(&data))
}

Another option might be to use a global variable as you had in your example, and then forgo passing anything useful in the pointer to C.

Like this:

package main

import (
    "fmt"
    "unsafe"
)

/*
extern void go_callback_int(void*, int);
static inline void CallMyFunction(void* pfoo) {
    go_callback_int(pfoo, 5);
}
*/
import "C"

//export go_callback_int
func go_callback_int(_ unsafe.Pointer, p1 C.int) {
    MyCallbackFunc(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 main() {
    C.CallMyFunction(nil)
}

It possibly depends on what your C callback needs to do.

If neither of these work it might be possible to do some tricks with channels to send the value you need (and keep it in scope).

like image 24
Ash Berlin-Taylor Avatar answered Oct 21 '22 20:10

Ash Berlin-Taylor