diff options
author | Elias Naur <[email protected]> | 2024-10-06 17:47:48 +0200 |
---|---|---|
committer | Ayke <[email protected]> | 2024-10-18 10:44:20 +0200 |
commit | 07d23c9d8338c877a7bc68299000307a539cf7bc (patch) | |
tree | 39ebf1f545901bd2d8ff35709abdb2ea4897ea89 /src | |
parent | d5f195387d14ba48f977841d7b3d38459604cc5d (diff) | |
download | tinygo-07d23c9d8338c877a7bc68299000307a539cf7bc.tar.gz tinygo-07d23c9d8338c877a7bc68299000307a539cf7bc.zip |
runtime: implement newcoro, coroswitch to support package iter
Diffstat (limited to 'src')
-rw-r--r-- | src/runtime/coro.go | 31 |
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 +} |