forked from tinygo-org/tinygo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime: implement newcoro, coroswitch to support package iter
- Loading branch information
Showing
3 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,14 @@ | |
3 | ||
2 | ||
1 | ||
10 | ||
9 | ||
8 | ||
7 | ||
6 | ||
5 | ||
4 | ||
3 | ||
2 | ||
1 | ||
go1.23 has lift-off! |