coro.mx raw

   1  package runtime
   2  
   3  // A naive implementation of coroutines that supports
   4  // package iter.
   5  
   6  type coro struct {
   7  	f  func(*coro)
   8  	ch chan struct{}
   9  }
  10  
  11  //go:linkname newcoro
  12  
  13  func newcoro(f func(*coro)) *coro {
  14  	c := &coro{
  15  		ch: make(chan struct{}),
  16  		f:  f,
  17  	}
  18  	go func() {
  19  		defer close(c.ch)
  20  		<-c.ch
  21  		f(c)
  22  	}()
  23  	return c
  24  }
  25  
  26  //go:linkname coroswitch
  27  
  28  func coroswitch(c *coro) {
  29  	c.ch <- struct{}{}
  30  	<-c.ch
  31  }
  32