mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 04:23:41 +00:00
runtime: implement newcoro, coroswitch to support package iter
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user