// Copyright 2011 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.

// +build dragonfly freebsd linux

package runtime

import (
	
	
)

// This implementation depends on OS-specific implementations of
//
//	futexsleep(addr *uint32, val uint32, ns int64)
//		Atomically,
//			if *addr == val { sleep }
//		Might be woken up spuriously; that's allowed.
//		Don't sleep longer than ns; ns < 0 means forever.
//
//	futexwakeup(addr *uint32, cnt uint32)
//		If any procs are sleeping on addr, wake up at most cnt.

const (
	mutex_unlocked = 0
	mutex_locked   = 1
	mutex_sleeping = 2

	active_spin     = 4
	active_spin_cnt = 30
	passive_spin    = 1
)

// Possible lock states are mutex_unlocked, mutex_locked and mutex_sleeping.
// mutex_sleeping means that there is presumably at least one sleeping thread.
// Note that there can be spinning threads during all states - they do not
// affect mutex's state.

// We use the uintptr mutex.key and note.key as a uint32.
//go:nosplit
func key32( *uintptr) *uint32 {
	return (*uint32)(unsafe.Pointer())
}

func lock( *mutex) {
	lockWithRank(, getLockRank())
}

func lock2( *mutex) {
	 := getg()

	if .m.locks < 0 {
		throw("runtime·lock: lock count")
	}
	.m.locks++

	// Speculative grab for lock.
	 := atomic.Xchg(key32(&.key), mutex_locked)
	if  == mutex_unlocked {
		return
	}

	// wait is either MUTEX_LOCKED or MUTEX_SLEEPING
	// depending on whether there is a thread sleeping
	// on this mutex. If we ever change l->key from
	// MUTEX_SLEEPING to some other value, we must be
	// careful to change it back to MUTEX_SLEEPING before
	// returning, to ensure that the sleeping thread gets
	// its wakeup call.
	 := 

	// On uniprocessors, no point spinning.
	// On multiprocessors, spin for ACTIVE_SPIN attempts.
	 := 0
	if ncpu > 1 {
		 = active_spin
	}
	for {
		// Try for lock, spinning.
		for  := 0;  < ; ++ {
			for .key == mutex_unlocked {
				if atomic.Cas(key32(&.key), mutex_unlocked, ) {
					return
				}
			}
			procyield(active_spin_cnt)
		}

		// Try for lock, rescheduling.
		for  := 0;  < passive_spin; ++ {
			for .key == mutex_unlocked {
				if atomic.Cas(key32(&.key), mutex_unlocked, ) {
					return
				}
			}
			osyield()
		}

		// Sleep.
		 = atomic.Xchg(key32(&.key), mutex_sleeping)
		if  == mutex_unlocked {
			return
		}
		 = mutex_sleeping
		futexsleep(key32(&.key), mutex_sleeping, -1)
	}
}

func unlock( *mutex) {
	unlockWithRank()
}

func unlock2( *mutex) {
	 := atomic.Xchg(key32(&.key), mutex_unlocked)
	if  == mutex_unlocked {
		throw("unlock of unlocked lock")
	}
	if  == mutex_sleeping {
		futexwakeup(key32(&.key), 1)
	}

	 := getg()
	.m.locks--
	if .m.locks < 0 {
		throw("runtime·unlock: lock count")
	}
	if .m.locks == 0 && .preempt { // restore the preemption request in case we've cleared it in newstack
		.stackguard0 = stackPreempt
	}
}

// One-time notifications.
func noteclear( *note) {
	.key = 0
}

func notewakeup( *note) {
	 := atomic.Xchg(key32(&.key), 1)
	if  != 0 {
		print("notewakeup - double wakeup (", , ")\n")
		throw("notewakeup - double wakeup")
	}
	futexwakeup(key32(&.key), 1)
}

func notesleep( *note) {
	 := getg()
	if  != .m.g0 {
		throw("notesleep not on g0")
	}
	 := int64(-1)
	if *cgo_yield != nil {
		// Sleep for an arbitrary-but-moderate interval to poll libc interceptors.
		 = 10e6
	}
	for atomic.Load(key32(&.key)) == 0 {
		.m.blocked = true
		futexsleep(key32(&.key), 0, )
		if *cgo_yield != nil {
			asmcgocall(*cgo_yield, nil)
		}
		.m.blocked = false
	}
}

// May run with m.p==nil if called from notetsleep, so write barriers
// are not allowed.
//
//go:nosplit
//go:nowritebarrier
func notetsleep_internal( *note,  int64) bool {
	 := getg()

	if  < 0 {
		if *cgo_yield != nil {
			// Sleep for an arbitrary-but-moderate interval to poll libc interceptors.
			 = 10e6
		}
		for atomic.Load(key32(&.key)) == 0 {
			.m.blocked = true
			futexsleep(key32(&.key), 0, )
			if *cgo_yield != nil {
				asmcgocall(*cgo_yield, nil)
			}
			.m.blocked = false
		}
		return true
	}

	if atomic.Load(key32(&.key)) != 0 {
		return true
	}

	 := nanotime() + 
	for {
		if *cgo_yield != nil &&  > 10e6 {
			 = 10e6
		}
		.m.blocked = true
		futexsleep(key32(&.key), 0, )
		if *cgo_yield != nil {
			asmcgocall(*cgo_yield, nil)
		}
		.m.blocked = false
		if atomic.Load(key32(&.key)) != 0 {
			break
		}
		 := nanotime()
		if  >=  {
			break
		}
		 =  - 
	}
	return atomic.Load(key32(&.key)) != 0
}

func notetsleep( *note,  int64) bool {
	 := getg()
	if  != .m.g0 && .m.preemptoff != "" {
		throw("notetsleep not on g0")
	}

	return notetsleep_internal(, )
}

// same as runtime·notetsleep, but called on user g (not g0)
// calls only nosplit functions between entersyscallblock/exitsyscall
func notetsleepg( *note,  int64) bool {
	 := getg()
	if  == .m.g0 {
		throw("notetsleepg on g0")
	}

	entersyscallblock()
	 := notetsleep_internal(, )
	exitsyscall()
	return 
}

func beforeIdle(int64) (*g, bool) {
	return nil, false
}

func checkTimeouts() {}