github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dsess/mutexmap/mutexmap.go (about)

     1  // Copyright 2024 Dolthub, Inc.
     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 mutexmap
    16  
    17  import (
    18  	"sync"
    19  )
    20  
    21  // MutexMap holds a dynamic number of mutexes identified by keys. When a mutex is no longer needed, it's removed from
    22  // the map.
    23  type MutexMap struct {
    24  	mu           sync.Mutex // Access to the map itself must be synchronized.
    25  	keyedMutexes map[interface{}]*mapMutex
    26  }
    27  
    28  type mapMutex struct {
    29  	key      interface{}
    30  	mu       sync.Mutex
    31  	parent   *MutexMap
    32  	refcount int
    33  }
    34  
    35  func NewMutexMap() *MutexMap {
    36  	return &MutexMap{keyedMutexes: make(map[interface{}]*mapMutex)}
    37  }
    38  
    39  func (mm *MutexMap) Lock(key interface{}) func() {
    40  	mm.mu.Lock()
    41  
    42  	var keyedMutex *mapMutex
    43  	func() {
    44  		// We must release the parent lock before attempting to acquire the child lock, otherwise if the child lock
    45  		// is currently held it will never be released.
    46  		defer mm.mu.Unlock()
    47  		var hasKey bool
    48  		keyedMutex, hasKey = mm.keyedMutexes[key]
    49  		if !hasKey {
    50  			keyedMutex = &mapMutex{parent: mm, key: key}
    51  			mm.keyedMutexes[key] = keyedMutex
    52  		}
    53  		keyedMutex.refcount++
    54  	}()
    55  
    56  	keyedMutex.mu.Lock()
    57  
    58  	return func() {
    59  		keyedMutex.Unlock()
    60  	}
    61  }
    62  
    63  func (mm *mapMutex) Unlock() {
    64  	mutexMap := mm.parent
    65  	mutexMap.mu.Lock()
    66  	defer mutexMap.mu.Unlock()
    67  
    68  	mm.refcount--
    69  	if mm.refcount < 1 {
    70  		delete(mutexMap.keyedMutexes, mm.key)
    71  	}
    72  	mm.mu.Unlock()
    73  }