spawn_orphan.mx raw

   1  // A spawns B and exits. B keeps logging - its port goes to the page's hub,
   2  // never through A - and exits cleanly when the root has finished with it. This
   3  // is the orphan invariant of the wasm hub (docs/wasm-hub.md): a clean exit
   4  // adopts the subtree instead of killing it. Native has no hub; the same program
   5  // must still produce the same output there.
   6  package main
   7  
   8  func b(in chan int32, done chan int32) (closed chan struct{}) {
   9  	select {
  10  	case <-closed:
  11  		return closed
  12  	default:
  13  	}
  14  	for i := int32(0); i < 3; i++ {
  15  		v := <-in
  16  		println("b got", v)
  17  		done <- v
  18  	}
  19  	return closed
  20  }
  21  
  22  func a(toB chan int32, fromB chan int32) (closed chan struct{}) {
  23  	select {
  24  	case <-closed:
  25  		return closed
  26  	default:
  27  	}
  28  	spawn(b, toB, fromB)
  29  	println("a spawned b")
  30  	return closed
  31  }
  32  
  33  func main() {
  34  	select {
  35  	case <-shutdown:
  36  		return
  37  	default:
  38  	}
  39  	toB := chan int32{:1}
  40  	fromB := chan int32{:1}
  41  	aDone := spawn(a, toB, fromB)
  42  	<-aDone
  43  	println("a is gone")
  44  	for i := int32(0); i < 3; i++ {
  45  		toB <- i + 10
  46  		v := <-fromB
  47  		println("root got", v)
  48  	}
  49  	println("root done")
  50  }
  51