edge_bare_end.mx raw

   1  // Phase 0c: a bare end crosses a spawn boundary.
   2  //
   3  // The pair's two ends need not travel together: the parent can keep its send
   4  // end and hand the child's receive end to a worker. A Sender/Receiver is a
   5  // handle, so the word IS the message - spawnDomain must not size a ring pair
   6  // for it, and the child's breaker is bound through the handle's pair record
   7  // (which reaches the parent's end, the one that has to wake).
   8  //
   9  //:wasmtest skip
  10  package main
  11  
  12  import "fmt"
  13  
  14  type Req struct {
  15  	Body []byte `cap:"1024"`
  16  }
  17  
  18  func sink(in Receiver[Req], out chan int32) (closed chan struct{}) {
  19  	for {
  20  		select {
  21  		case <-closed:
  22  			return closed
  23  		case r, ok := <-in:
  24  			if !ok {
  25  				return closed
  26  			}
  27  			out <- int32(len(r.Body))
  28  		}
  29  	}
  30  }
  31  
  32  func main() {
  33  	select {
  34  	case <-shutdown:
  35  		return
  36  	default:
  37  	}
  38  	parent, child := edge[int32, Req]{2}
  39  	out := chan int32{:4}
  40  	done := spawn(sink, child.Recv, out)
  41  	for _, n := range []int32{5, 300, 1024} {
  42  		parent.Send <- Req{Body: []byte{:n}}
  43  		if got := <-out; got != n {
  44  			panic("bare end lost a request")
  45  		}
  46  	}
  47  	close(parent.Send)
  48  	<-done
  49  	fmt.Println("edge bare end ok")
  50  }
  51