package daemon

import "sync"

// vmLockSet maps VM IDs to per-VM mutexes. Concurrent operations on different
// VMs run in parallel; concurrent operations on the same VM serialise.
type vmLockSet struct {
	byID sync.Map // map[string]*sync.Mutex
}

// lock acquires the mutex for the given VM ID and returns its unlock func.
// LoadOrStore is atomic — exactly one *sync.Mutex wins for each ID, so there
// is no release-then-reacquire TOCTOU window.
func (s *vmLockSet) lock(id string) func() {
	val, _ := s.byID.LoadOrStore(id, &sync.Mutex{})
	mu := val.(*sync.Mutex)
	mu.Lock()
	return mu.Unlock
}
