runtime: implement newcoro, coroswitch to support package iter

This commit is contained in:
Elias Naur
2024-10-06 17:47:48 +02:00
committed by Ayke
parent d5f195387d
commit 07d23c9d83
3 changed files with 59 additions and 3 deletions
+31
View File
@@ -0,0 +1,31 @@
package runtime
// A naive implementation of coroutines that supports
// package iter.
type coro struct {
f func(*coro)
ch chan struct{}
}
//go:linkname newcoro
func newcoro(f func(*coro)) *coro {
c := &coro{
ch: make(chan struct{}),
f: f,
}
go func() {
defer close(c.ch)
<-c.ch
f(c)
}()
return c
}
//go:linkname coroswitch
func coroswitch(c *coro) {
c.ch <- struct{}{}
<-c.ch
}