FAQ
I've started getting barrier warnings on -race programs for sending a
pointer to Go objects to C land. i was using map[uintptr]interface{}
to record and keep a reference to the Go object, sending
unsafe.Pointer(uintptr) to the C code.

Now instead I'm allocating a single byte with C.malloc and using that
as a key to my Go context:

map[unsafe.Pointer]interface{}

and something like this (some sanity checks elided):

func setContext(ctx interface{}) unsafe.Pointer {
     cctx := C.malloc(1)
     if cctx {
         cmap[cctx] = interface{}
         return cctx
     }
}

The warnings are gone, but I'm not sure if there isn't an easier
method for doing this...

--
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/d/optout.

Search Discussions

  • Ian Lance Taylor at Jan 25, 2016 at 9:40 pm

    On Mon, Jan 25, 2016 at 1:23 PM, andrey mirtchovski wrote:

    I've started getting barrier warnings on -race programs for sending a
    pointer to Go objects to C land. i was using map[uintptr]interface{}
    to record and keep a reference to the Go object, sending
    unsafe.Pointer(uintptr) to the C code.

    Now instead I'm allocating a single byte with C.malloc and using that
    as a key to my Go context:

    map[unsafe.Pointer]interface{}

    and something like this (some sanity checks elided):

    func setContext(ctx interface{}) unsafe.Pointer {
    cctx := C.malloc(1)
    if cctx {
    cmap[cctx] = interface{}
    return cctx
    }
    }

    The warnings are gone, but I'm not sure if there isn't an easier
    method for doing this...
    You can't let C code hang on to a Go pointer (see
    http://tip.golang.org/cmd/cgo/#hdr-Passing_pointers). If the C code
    requires a pointer in some way, then what you are doing is the best
    you can do. The C code probably doesn't really need a pointer, in
    which case perhaps you can write a little C wrapper function in your
    cgo comment that takes a uintptr_t and passes it to the C code as a
    void*. Then you can call your wrapper with a Go uintptr, which saves
    the C.malloc.

    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 25, '16 at 9:23p
activeJan 25, '16 at 9:40p
posts2
users2
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase