github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/nosync/mutex.go (about)

     1  package nosync
     2  
     3  // Mutex is a dummy which is non-blocking.
     4  type Mutex struct {
     5  	locked bool
     6  }
     7  
     8  // Lock locks m. It is a run-time error if m is already locked.
     9  func (m *Mutex) Lock() {
    10  	if m.locked {
    11  		panic("nosync: mutex is already locked")
    12  	}
    13  	m.locked = true
    14  }
    15  
    16  // Unlock unlocks m. It is a run-time error if m is not locked.
    17  func (m *Mutex) Unlock() {
    18  	if !m.locked {
    19  		panic("nosync: unlock of unlocked mutex")
    20  	}
    21  	m.locked = false
    22  }
    23  
    24  // RWMutex is a dummy which is non-blocking.
    25  type RWMutex struct {
    26  	writeLocked     bool
    27  	readLockCounter int
    28  }
    29  
    30  // Lock locks m for writing. It is a run-time error if rw is already locked for reading or writing.
    31  func (rw *RWMutex) Lock() {
    32  	if rw.readLockCounter != 0 || rw.writeLocked {
    33  		panic("nosync: mutex is already locked")
    34  	}
    35  	rw.writeLocked = true
    36  }
    37  
    38  // Unlock unlocks rw for writing. It is a run-time error if rw is not locked for writing.
    39  func (rw *RWMutex) Unlock() {
    40  	if !rw.writeLocked {
    41  		panic("nosync: unlock of unlocked mutex")
    42  	}
    43  	rw.writeLocked = false
    44  }
    45  
    46  // RLock locks m for reading. It is a run-time error if rw is already locked for reading or writing.
    47  func (rw *RWMutex) RLock() {
    48  	if rw.writeLocked {
    49  		panic("nosync: mutex is already locked")
    50  	}
    51  	rw.readLockCounter++
    52  }
    53  
    54  // RUnlock undoes a single RLock call; it does not affect other simultaneous readers. It is a run-time error if rw is not locked for reading.
    55  func (rw *RWMutex) RUnlock() {
    56  	if rw.readLockCounter == 0 {
    57  		panic("nosync: unlock of unlocked mutex")
    58  	}
    59  	rw.readLockCounter--
    60  }
    61  
    62  // WaitGroup is a dummy which is non-blocking.
    63  type WaitGroup struct {
    64  	counter int
    65  }
    66  
    67  // Add adds delta, which may be negative, to the WaitGroup If the counter goes negative, Add panics.
    68  func (wg *WaitGroup) Add(delta int) {
    69  	wg.counter += delta
    70  	if wg.counter < 0 {
    71  		panic("sync: negative WaitGroup counter")
    72  	}
    73  }
    74  
    75  // Done decrements the WaitGroup counter.
    76  func (wg *WaitGroup) Done() {
    77  	wg.Add(-1)
    78  }
    79  
    80  // Wait panics if the WaitGroup counter is not zero.
    81  func (wg *WaitGroup) Wait() {
    82  	if wg.counter != 0 {
    83  		panic("sync: WaitGroup counter not zero")
    84  	}
    85  }