Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Go channels implemented?

Tags:

go

channel

After (briefly) reviewing the Go language spec, effective Go, and the Go memory model, I'm still a little unclear as to how Go channels work under the hood.

What kind of structure are they? They act kind of like a thread-safe queue /array.

Does their implementation depend on the architecture?

like image 458
Matt Avatar asked Oct 27 '13 17:10

Matt


2 Answers

The source file for channels is (from your go source code root) in /src/pkg/runtime/chan.go.

hchan is the central data structure for a channel, with send and receive linked lists (holding a pointer to their goroutine and the data element) and a closed flag. There's a Lock embedded structure that is defined in runtime2.go and that serves as a mutex (futex) or semaphore depending on the OS. The locking implementation is in lock_futex.go (Linux/Dragonfly/Some BSD) or lock_sema.go (Windows/OSX/Plan9/Some BSD), based on the build tags.

Channel operations are all implemented in this chan.go file, so you can see the makechan, send and receive operations, as well as the select construct, close, len and cap built-ins.

For a great in-depth explanation on the inner workings of channels, you have to read Go channels on steroids by Dmitry Vyukov himself (Go core dev, goroutines, scheduler and channels among other things).

like image 60
mna Avatar answered Oct 05 '22 23:10

mna


Here is a good talk that describes roughly how channels are implemented:
https://youtu.be/KBZlN0izeiY

Talk description:

GopherCon 2017: Kavya Joshi - Understanding Channels

Channels provide a simple mechanism for goroutines to communicate, and a powerful construct to build sophisticated concurrency patterns. We will delve into the inner workings of channels and channel operations, including how they're supported by the runtime scheduler and memory management systems.

like image 38
ayke Avatar answered Oct 06 '22 00:10

ayke