// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sync

import (
	
	
	
)

// There is a modified copy of this file in runtime/rwmutex.go.
// If you make any changes here, see if you should make them there.

// A RWMutex is a reader/writer mutual exclusion lock.
// The lock can be held by an arbitrary number of readers or a single writer.
// The zero value for a RWMutex is an unlocked mutex.
//
// A RWMutex must not be copied after first use.
//
// If a goroutine holds a RWMutex for reading and another goroutine might
// call Lock, no goroutine should expect to be able to acquire a read lock
// until the initial read lock is released. In particular, this prohibits
// recursive read locking. This is to ensure that the lock eventually becomes
// available; a blocked Lock call excludes new readers from acquiring the
// lock.
type RWMutex struct {
	w           Mutex  // held if there are pending writers
	writerSem   uint32 // semaphore for writers to wait for completing readers
	readerSem   uint32 // semaphore for readers to wait for completing writers
	readerCount int32  // number of pending readers
	readerWait  int32  // number of departing readers
}

const rwmutexMaxReaders = 1 << 30

// Happens-before relationships are indicated to the race detector via:
// - Unlock  -> Lock:  readerSem
// - Unlock  -> RLock: readerSem
// - RUnlock -> Lock:  writerSem
//
// The methods below temporarily disable handling of race synchronization
// events in order to provide the more precise model above to the race
// detector.
//
// For example, atomic.AddInt32 in RLock should not appear to provide
// acquire-release semantics, which would incorrectly synchronize racing
// readers, thus potentially missing races.

// RLock locks rw for reading.
//
// It should not be used for recursive read locking; a blocked Lock
// call excludes new readers from acquiring the lock. See the
// documentation on the RWMutex type.
func ( *RWMutex) () {
	if race.Enabled {
		_ = .w.state
		race.Disable()
	}
	if atomic.AddInt32(&.readerCount, 1) < 0 {
		// A writer is pending, wait for it.
		runtime_SemacquireMutex(&.readerSem, false, 0)
	}
	if race.Enabled {
		race.Enable()
		race.Acquire(unsafe.Pointer(&.readerSem))
	}
}

// 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
// on entry to RUnlock.
func ( *RWMutex) () {
	if race.Enabled {
		_ = .w.state
		race.ReleaseMerge(unsafe.Pointer(&.writerSem))
		race.Disable()
	}
	if  := atomic.AddInt32(&.readerCount, -1);  < 0 {
		// Outlined slow-path to allow the fast-path to be inlined
		.rUnlockSlow()
	}
	if race.Enabled {
		race.Enable()
	}
}

func ( *RWMutex) ( int32) {
	if +1 == 0 || +1 == -rwmutexMaxReaders {
		race.Enable()
		throw("sync: RUnlock of unlocked RWMutex")
	}
	// A writer is pending.
	if atomic.AddInt32(&.readerWait, -1) == 0 {
		// The last reader unblocks the writer.
		runtime_Semrelease(&.writerSem, false, 1)
	}
}

// Lock locks rw for writing.
// If the lock is already locked for reading or writing,
// Lock blocks until the lock is available.
func ( *RWMutex) () {
	if race.Enabled {
		_ = .w.state
		race.Disable()
	}
	// First, resolve competition with other writers.
	.w.Lock()
	// Announce to readers there is a pending writer.
	 := atomic.AddInt32(&.readerCount, -rwmutexMaxReaders) + rwmutexMaxReaders
	// Wait for active readers.
	if  != 0 && atomic.AddInt32(&.readerWait, ) != 0 {
		runtime_SemacquireMutex(&.writerSem, false, 0)
	}
	if race.Enabled {
		race.Enable()
		race.Acquire(unsafe.Pointer(&.readerSem))
		race.Acquire(unsafe.Pointer(&.writerSem))
	}
}

// Unlock unlocks rw for writing. It is a run-time error if rw is
// not locked for writing on entry to Unlock.
//
// As with Mutexes, a locked RWMutex is not associated with a particular
// goroutine. One goroutine may RLock (Lock) a RWMutex and then
// arrange for another goroutine to RUnlock (Unlock) it.
func ( *RWMutex) () {
	if race.Enabled {
		_ = .w.state
		race.Release(unsafe.Pointer(&.readerSem))
		race.Disable()
	}

	// Announce to readers there is no active writer.
	 := atomic.AddInt32(&.readerCount, rwmutexMaxReaders)
	if  >= rwmutexMaxReaders {
		race.Enable()
		throw("sync: Unlock of unlocked RWMutex")
	}
	// Unblock blocked readers, if any.
	for  := 0;  < int(); ++ {
		runtime_Semrelease(&.readerSem, false, 0)
	}
	// Allow other writers to proceed.
	.w.Unlock()
	if race.Enabled {
		race.Enable()
	}
}

// RLocker returns a Locker interface that implements
// the Lock and Unlock methods by calling rw.RLock and rw.RUnlock.
func ( *RWMutex) () Locker {
	return (*rlocker)()
}

type rlocker RWMutex

func ( *rlocker) ()   { (*RWMutex)().RLock() }
func ( *rlocker) () { (*RWMutex)().RUnlock() }