blob: 92d196a580382207e95e5d3b22145da36f57f04b [file] [log] [blame]
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package maker
import (
"sort"
"sync"
)
type lock struct {
name string
mu sync.Mutex
}
var locks = map[string]*lock{}
func addLock(name string) {
_, ok := locks[name]
if !ok {
locks[name] = &lock{name: name}
}
}
func withLocks(accesses []string, do func()) {
// sort the names for consistent aquire order
sort.Strings(accesses)
// aquire all the mutexes in order
for _, name := range accesses {
locks[name].mu.Lock()
}
do()
// unlock order does not matter
for _, name := range accesses {
locks[name].mu.Unlock()
}
}