github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/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  	_      bool
     7  	_      bool
     8  	_      bool
     9  	_      uint32
    10  }
    11  
    12  // Lock locks m. It is a run-time error if m is already locked.
    13  func (m *Mutex) Lock() {
    14  	if m.locked {
    15  		panic("nosync: mutex is already locked")
    16  	}
    17  	m.locked = true
    18  }
    19  
    20  // Unlock unlocks m. It is a run-time error if m is not locked.
    21  func (m *Mutex) Unlock() {
    22  	if !m.locked {
    23  		panic("nosync: unlock of unlocked mutex")
    24  	}
    25  	m.locked = false
    26  }
    27  
    28  // RWMutex is a dummy which is non-blocking.
    29  type RWMutex struct {
    30  	_               Mutex
    31  	writeLocked     bool
    32  	_               bool
    33  	_               bool
    34  	_               bool
    35  	readLockCounter int32
    36  	_               int32
    37  	_               int32
    38  }
    39  
    40  // Lock locks m for writing. It is a run-time error if rw is already locked for reading or writing.
    41  func (rw *RWMutex) Lock() {
    42  	if rw.readLockCounter != 0 || rw.writeLocked {
    43  		panic("nosync: mutex is already locked")
    44  	}
    45  	rw.writeLocked = true
    46  }
    47  
    48  // Unlock unlocks rw for writing. It is a run-time error if rw is not locked for writing.
    49  func (rw *RWMutex) Unlock() {
    50  	if !rw.writeLocked {
    51  		panic("nosync: unlock of unlocked mutex")
    52  	}
    53  	rw.writeLocked = false
    54  }
    55  
    56  // RLock locks m for reading. It is a run-time error if rw is already locked for reading or writing.
    57  func (rw *RWMutex) RLock() {
    58  	if rw.writeLocked {
    59  		panic("nosync: mutex is already locked")
    60  	}
    61  	rw.readLockCounter++
    62  }
    63  
    64  // 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.
    65  func (rw *RWMutex) RUnlock() {
    66  	if rw.readLockCounter == 0 {
    67  		panic("nosync: unlock of unlocked mutex")
    68  	}
    69  	rw.readLockCounter--
    70  }
    71  
    72  // WaitGroup is a dummy which is non-blocking.
    73  type WaitGroup struct {
    74  	counter int
    75  }
    76  
    77  // Add adds delta, which may be negative, to the WaitGroup If the counter goes negative, Add panics.
    78  func (wg *WaitGroup) Add(delta int) {
    79  	wg.counter += delta
    80  	if wg.counter < 0 {
    81  		panic("sync: negative WaitGroup counter")
    82  	}
    83  }
    84  
    85  // Done decrements the WaitGroup counter.
    86  func (wg *WaitGroup) Done() {
    87  	wg.Add(-1)
    88  }
    89  
    90  // Wait panics if the WaitGroup counter is not zero.
    91  func (wg *WaitGroup) Wait() {
    92  	if wg.counter != 0 {
    93  		panic("sync: WaitGroup counter not zero")
    94  	}
    95  }