github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/fsimpl/lock/lock_set_functions.go (about)

     1  // Copyright 2018 The gVisor 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 lock
    16  
    17  import (
    18  	"math"
    19  )
    20  
    21  // LockSet maps a set of Locks into a file.  The key is the file offset.
    22  
    23  type lockSetFunctions struct{}
    24  
    25  func (lockSetFunctions) MinKey() uint64 {
    26  	return 0
    27  }
    28  
    29  func (lockSetFunctions) MaxKey() uint64 {
    30  	return math.MaxUint64
    31  }
    32  
    33  func (lockSetFunctions) ClearValue(l *Lock) {
    34  	*l = Lock{}
    35  }
    36  
    37  func (lockSetFunctions) Merge(r1 LockRange, val1 Lock, r2 LockRange, val2 Lock) (Lock, bool) {
    38  	// Merge only if the Readers/Writers are identical.
    39  	if len(val1.Readers) != len(val2.Readers) {
    40  		return Lock{}, false
    41  	}
    42  	for k := range val1.Readers {
    43  		if _, ok := val2.Readers[k]; !ok {
    44  			return Lock{}, false
    45  		}
    46  	}
    47  	if val1.Writer != val2.Writer {
    48  		return Lock{}, false
    49  	}
    50  	return val1, true
    51  }
    52  
    53  func (lockSetFunctions) Split(r LockRange, val Lock, split uint64) (Lock, Lock) {
    54  	// Copy the segment so that split segments don't contain map references
    55  	// to other segments.
    56  	val0 := Lock{Readers: make(map[UniqueID]OwnerInfo)}
    57  	for k, v := range val.Readers {
    58  		val0.Readers[k] = v
    59  	}
    60  	val0.Writer = val.Writer
    61  	val0.WriterInfo = val.WriterInfo
    62  
    63  	return val, val0
    64  }