github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/sync/cond.go (about)

     1  //go:build js
     2  // +build js
     3  
     4  package sync
     5  
     6  type Cond struct {
     7  	// fields used by vanilla implementation
     8  	noCopy  noCopy
     9  	L       Locker
    10  	notify  notifyList
    11  	checker copyChecker
    12  
    13  	// fields used by new implementation
    14  	n  int
    15  	ch chan bool
    16  }
    17  
    18  func (c *Cond) Wait() {
    19  	c.n++
    20  	if c.ch == nil {
    21  		c.ch = make(chan bool)
    22  	}
    23  	c.L.Unlock()
    24  	<-c.ch
    25  	c.L.Lock()
    26  }
    27  
    28  func (c *Cond) Signal() {
    29  	if c.n == 0 {
    30  		return
    31  	}
    32  	c.n--
    33  	c.ch <- true
    34  }
    35  
    36  func (c *Cond) Broadcast() {
    37  	n := c.n
    38  	c.n = 0
    39  	for i := 0; i < n; i++ {
    40  		c.ch <- true
    41  	}
    42  }