go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luciexe/build/property_reservations.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package build
    16  
    17  import (
    18  	"fmt"
    19  	"runtime"
    20  	"sync"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  )
    24  
    25  type resLocations struct {
    26  	mu sync.Mutex
    27  	// locations maps reserved namespace to the source location where they were
    28  	// first reserved.
    29  	locations map[string]string
    30  }
    31  
    32  func (r *resLocations) snap() map[string]string {
    33  	r.mu.Lock()
    34  	defer r.mu.Unlock()
    35  	ret := make(map[string]string, len(r.locations))
    36  	for k, v := range r.locations {
    37  		ret[k] = v
    38  	}
    39  	return ret
    40  }
    41  
    42  // skip is the number of frames to skip from your callsite.
    43  //
    44  // skip=1 means "your caller"
    45  func (r *resLocations) reserve(ns string, kind string, skip int) {
    46  	if ns == "" {
    47  		panic(errors.New("empty namespace not allowed"))
    48  	}
    49  
    50  	r.mu.Lock()
    51  	defer r.mu.Unlock()
    52  
    53  	if current, ok := r.locations[ns]; ok {
    54  		panic(
    55  			errors.Reason("cannot reserve %s namespace %q: already reserved by %s",
    56  				kind, ns, current).Err())
    57  	}
    58  
    59  	reservationLocation := "<unknown>"
    60  	if _, file, line, ok := runtime.Caller(skip + 1); ok {
    61  		reservationLocation = fmt.Sprintf("\"%s:%d\"", file, line)
    62  	}
    63  
    64  	if r.locations == nil {
    65  		r.locations = map[string]string{}
    66  	}
    67  	r.locations[ns] = reservationLocation
    68  }
    69  
    70  func (r *resLocations) each(cb func(ns string)) {
    71  	r.mu.Lock()
    72  	defer r.mu.Unlock()
    73  	for ns := range r.locations {
    74  		cb(ns)
    75  	}
    76  }
    77  
    78  func (r *resLocations) clear(cb func()) {
    79  	r.mu.Lock()
    80  	defer r.mu.Unlock()
    81  	r.locations = nil
    82  	if cb != nil {
    83  		cb()
    84  	}
    85  }
    86  
    87  func (r *resLocations) get(ns string) string {
    88  	r.mu.Lock()
    89  	defer r.mu.Unlock()
    90  	return r.locations[ns]
    91  }