CGO and C Threads

581 views
Skip to first unread message

Patrick

unread,
Dec 28, 2011, 4:58:15 AM12/28/11
to golang-nuts
I am trying to create a go package for a c messaging library. The
messaging library creates a thread internally to handle messaging.
After going through this discussion
http://groups.google.com/group/golang-nuts/browse_thread/thread/536f03f12837307b/1c80493c3c1ad13e?lnk=gst&q=go+c+thread#1c80493c3c1ad13e
it appears that this is not possible with cgo. Are there plans to add
this functionality?
Thanks,
Patrick

Ian Lance Taylor

unread,
Dec 28, 2011, 10:20:45 AM12/28/11
to Patrick, golang-nuts
Patrick <doherty...@gmail.com> writes:

Assuming you mean the ability to call Go code from a thread started by
C: there are plans to add this functionality, but there is no schedule.
Of course this is something that anybody can work on, you don't have to
wait for us.

Ian

brainman

unread,
Dec 28, 2011, 6:09:15 PM12/28/11
to golan...@googlegroups.com
Ian,

Do you have a suggestion about the way runtime interface for this facility should look like? I am trying to call some windows api that requires similar approach. Perhaps, I could put my efforts into doing something that is general enough to be used by cgo too. Thank you.

Alex

Ian Lance Taylor

unread,
Dec 28, 2011, 6:31:16 PM12/28/11
to golan...@googlegroups.com
brainman <alex.b...@gmail.com> writes:

On the Unix side, I would say that if we get a call back to Go and the
thread-local m variable is nil, then the thread should create a new
goroutine calling the function, add the goroutine to the scheduling
list, and add some magic so that the thread can wait until the goroutine
terminates. It's conceptually simple, it's just has annoying details.
The first two steps are just runtime·newproc. The last step might be
implemented by, say, adding an optional note to a G structure and having
schedule call notewakeup when it finds a goroutine in Gmoribund state.
Note that before calling any runtime code the C thread needs to set up
the stack guard such that the Go functions won't try to ask for more
stack space.

Ian

Gustavo Niemeyer

unread,
Dec 28, 2011, 6:36:01 PM12/28/11
to golan...@googlegroups.com, Gustavo Sverzut Barbieri

Coincidently, Gustavo Barbieri (CCd) was asking me about this _today_
for implementing the EFL [1] support for Go.

Something I did before for solving that problem was to sustain a
blocked goroutine waiting for input from the C side for running logic
requested. The C side would then block holding for the handling of the
call from the Go side.

[1] http://www.enlightenment.org/

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

brainman

unread,
Dec 28, 2011, 6:56:37 PM12/28/11
to golan...@googlegroups.com
Sounds like a plan. Thank you. I'll have a go.

Alex

Gustavo Niemeyer

unread,
Dec 28, 2011, 8:19:19 PM12/28/11
to golan...@googlegroups.com
> Sounds like a plan. Thank you. I'll have a go.

Nice, thanks Alex.

Reply all
Reply to author
Forward
0 new messages