package ecs

import (
	
)

// Manages locks by mask bits.
//
// The number of simultaneous locks at a given time is limited to 64.
type lock struct {
	bitPool bitPool   // The bit pool for getting and recycling bits.
	locks   bitMask64 // The actual locks.
	mu      sync.Mutex
}

func newLock() lock {
	return lock{
		bitPool: newBitPool(),
	}
}

// Lock the world and get the Lock bit for later unlocking.
// This is not concurrency-safe.
func ( *lock) () uint8 {
	 := .bitPool.Get()
	.locks.Set()
	return 
}

// Unlock unlocks the given lock bit.
// This is not concurrency-safe.
func ( *lock) ( uint8) {
	if !.locks.Get() {
		panic("unbalanced unlock. Did you close a query that was already iterated?")
	}
	.locks.Clear()
	.bitPool.Recycle()
}

// LockSafe locks the world and get the Lock bit for later unlocking.
// This is concurrency-safe.
func ( *lock) () uint8 {
	.mu.Lock()
	 := .bitPool.Get()
	.locks.Set()
	.mu.Unlock()
	return 
}

// UnlockSafe unlocks the given lock bit.
// This is concurrency-safe.
func ( *lock) ( uint8) {
	.mu.Lock()
	if !.locks.Get() {
		.mu.Unlock()
		panic("unbalanced unlock. Did you close a query that was already iterated?")
	}
	.locks.Clear()
	.bitPool.Recycle()
	.mu.Unlock()
}

// IsLocked returns whether the world is locked by any queries.
func ( *lock) () bool {
	return !.locks.IsZero()
}

// Reset the locks and the pool.
func ( *lock) () {
	.locks = bitMask64{}
	.bitPool.Reset()
}