aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/runtime
diff options
context:
space:
mode:
authorElias Naur <[email protected]>2024-10-06 17:47:48 +0200
committerAyke <[email protected]>2024-10-18 10:44:20 +0200
commit07d23c9d8338c877a7bc68299000307a539cf7bc (patch)
tree39ebf1f545901bd2d8ff35709abdb2ea4897ea89 /src/runtime
parentd5f195387d14ba48f977841d7b3d38459604cc5d (diff)
downloadtinygo-07d23c9d8338c877a7bc68299000307a539cf7bc.tar.gz
tinygo-07d23c9d8338c877a7bc68299000307a539cf7bc.zip
runtime: implement newcoro, coroswitch to support package iter
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/coro.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/runtime/coro.go b/src/runtime/coro.go
new file mode 100644
index 000000000..204a5c2be
--- /dev/null
+++ b/src/runtime/coro.go
@@ -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
+}