FAQ
Hi,

I'm working on lightweight go wrapper over some C library... This library
creates threads on its own and accepts callbacks for notifications...

The question is should calling go callback work from non-go thread or not?
(go 1.1)

the code looks like:

*variant #1*
//export sourceCallbackFunc
func sourceCallbackFunc(source unsafe.Pointer, etype C.int, edata
unsafe.Pointer, cldata unsafe.Pointer) C.int {
log.Print("callback is working!")
return 0
}
var sourceCallback = sourceCallbackFunc

*variant #2 (w/o export)*
var sourceCallback = func(source unsafe.Pointer, etype C.int, edata
unsafe.Pointer, cldata unsafe.Pointer) C.int {
log.Print("callback is working!")
return 0
}

I'm passing callback as:
src := LbmSource{Event: make(chan string)}
if C.LBM_FAILURE == C.lbm_src_create(
&src.source,
this.ctx, topic,
* (*[0]byte)(unsafe.Pointer(&sourceCallback)),*
unsafe.Pointer(&src), nil) {

log.Printf("Failed to create source ctx:[%v] topic:[%v]", this.name,
topicName)
return nil, -1
}

everything works till time for callback comes :( nothing but crash. (tried
to debug with gdb but it fails to load symbol info on win7 64

any idea?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ian Lance Taylor at Aug 5, 2013 at 8:28 pm

    On Mon, Aug 5, 2013 at 12:23 PM, Nick Evgeniev wrote:
    I'm working on lightweight go wrapper over some C library... This library
    creates threads on its own and accepts callbacks for notifications...

    The question is should calling go callback work from non-go thread or not?
    (go 1.1)
    Calling a Go function from a non-Go thread should work in 1.1.

    Passing a Go function as a C function pointer and calling it from C
    will never work. You will need to arrange to call a named Go
    function, not an indirect call through function pointer. If necessary
    you could have pass your Go func value to a named Go function and have
    that Go function call the func. And if necessary you could have a C
    function to do this, and pass the pointer to that function to the C
    code that expects a callback.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nick Evgeniev at Aug 5, 2013 at 9:57 pm
    Thank you for clarification still there is a problem (I must be stupid :)
    -. If I add the following to my #cgo section
    /*
    extern int sourceCallbackFunc(void *src, int event, void *ed, void
    *clientd);

    *void *callbackAddr() {*
    * return &sourceCallbackFunc;*
    *}*
    */
    With intention to use C.callbackAddr() to provide callback pointer
    compilation fails because of duplicate definition of callbackAddr().

    more precisely error message is:
    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\lbm.cgo2.o: In
    function `callbackAddr':
    ./lbm.go:21: multiple definition of `callbackAddr'
    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\_cgo_export.o:D:\work\lbm2go\src\lbm/lbm.go:21:
    first defined here


    On Tuesday, August 6, 2013 12:28:51 AM UTC+4, Ian Lance Taylor wrote:
    On Mon, Aug 5, 2013 at 12:23 PM, Nick Evgeniev wrote:

    I'm working on lightweight go wrapper over some C library... This library
    creates threads on its own and accepts callbacks for notifications...

    The question is should calling go callback work from non-go thread or not?
    (go 1.1)
    Calling a Go function from a non-Go thread should work in 1.1.

    Passing a Go function as a C function pointer and calling it from C
    will never work. You will need to arrange to call a named Go
    function, not an indirect call through function pointer. If necessary
    you could have pass your Go func value to a named Go function and have
    that Go function call the func. And if necessary you could have a C
    function to do this, and pass the pointer to that function to the C
    code that expects a callback.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ian Lance Taylor at Aug 5, 2013 at 11:04 pm

    On Aug 5, 2013 2:57 PM, "Nick Evgeniev" wrote:
    Thank you for clarification still there is a problem (I must be stupid :)
    -. If I add the following to my #cgo section
    /*
    extern int sourceCallbackFunc(void *src, int event, void *ed, void
    *clientd);
    void *callbackAddr() {
    return &sourceCallbackFunc;
    }
    */
    With intention to use C.callbackAddr() to provide callback pointer
    compilation fails because of duplicate definition of callbackAddr().
    more precisely error message is:
    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\lbm.cgo2.o:
    In function `callbackAddr':
    ./lbm.go:21: multiple definition of `callbackAddr'
    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\_cgo_export.o:D:\work\lbm2go\src\lbm/lbm.go:21:
    first defined here

    Make it static.

    Ian
    On Tuesday, August 6, 2013 12:28:51 AM UTC+4, Ian Lance Taylor wrote:
    On Mon, Aug 5, 2013 at 12:23 PM, Nick Evgeniev wrote:

    I'm working on lightweight go wrapper over some C library... This
    library
    creates threads on its own and accepts callbacks for notifications...

    The question is should calling go callback work from non-go thread or
    not?
    (go 1.1)
    Calling a Go function from a non-Go thread should work in 1.1.

    Passing a Go function as a C function pointer and calling it from C
    will never work. You will need to arrange to call a named Go
    function, not an indirect call through function pointer. If necessary
    you could have pass your Go func value to a named Go function and have
    that Go function call the func. And if necessary you could have a C
    function to do this, and pass the pointer to that function to the C
    code that expects a callback.

    Ian
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nick Evgeniev at Aug 6, 2013 at 6:59 am
    Thanks!!!

    having it documented on cgo doc pages would be great! Having go performing
    such conversions automatically would be fantastic! :)


    more precisely error message is:
    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\lbm.cgo2.o:
    In function `callbackAddr':
    ./lbm.go:21: multiple definition of `callbackAddr'
    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\_cgo_export.o:D:\work\lbm2go\src\lbm/lbm.go:21:
    first defined here

    Make it static.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matthew Zimmerman at Aug 6, 2013 at 11:16 am
    Nick, I too have just started using cgo and got success on passing a
    Go function to a C callback.
    https://github.com/mzimmerman/mythgowii

    I followed what I saw in: https://code.google.com/p/go-wiki/wiki/cgo &
    an implementation of portaudio for go:
    https://code.google.com/p/portaudio-go/source/browse/portaudio

    Matt
    On Mon, Aug 5, 2013 at 7:03 PM, Ian Lance Taylor wrote:
    On Aug 5, 2013 2:57 PM, "Nick Evgeniev" wrote:

    Thank you for clarification still there is a problem (I must be stupid :)
    -. If I add the following to my #cgo section
    /*
    extern int sourceCallbackFunc(void *src, int event, void *ed, void
    *clientd);

    void *callbackAddr() {
    return &sourceCallbackFunc;
    }
    */
    With intention to use C.callbackAddr() to provide callback pointer
    compilation fails because of duplicate definition of callbackAddr().

    more precisely error message is:
    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\lbm.cgo2.o: In
    function `callbackAddr':
    ./lbm.go:21: multiple definition of `callbackAddr'

    C:\Users\nick\AppData\Local\Temp\go-build509875587\lbm\_obj\_cgo_export.o:D:\work\lbm2go\src\lbm/lbm.go:21:
    first defined here
    Make it static.

    Ian
    On Tuesday, August 6, 2013 12:28:51 AM UTC+4, Ian Lance Taylor wrote:
    On Mon, Aug 5, 2013 at 12:23 PM, Nick Evgeniev wrote:

    I'm working on lightweight go wrapper over some C library... This
    library
    creates threads on its own and accepts callbacks for notifications...

    The question is should calling go callback work from non-go thread or
    not?
    (go 1.1)
    Calling a Go function from a non-Go thread should work in 1.1.

    Passing a Go function as a C function pointer and calling it from C
    will never work. You will need to arrange to call a named Go
    function, not an indirect call through function pointer. If necessary
    you could have pass your Go func value to a named Go function and have
    that Go function call the func. And if necessary you could have a C
    function to do this, and pass the pointer to that function to the C
    code that expects a callback.

    Ian
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 5, '13 at 7:23p
activeAug 6, '13 at 11:16a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase