github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/spanlatch/list.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package spanlatch
    12  
    13  // latchList is a double-linked circular list of *latch elements.
    14  type latchList struct {
    15  	root latch
    16  	len  int
    17  }
    18  
    19  func (ll *latchList) front() *latch {
    20  	if ll.len == 0 {
    21  		return nil
    22  	}
    23  	return ll.root.next
    24  }
    25  
    26  func (ll *latchList) lazyInit() {
    27  	if ll.root.next == nil {
    28  		ll.root.next = &ll.root
    29  		ll.root.prev = &ll.root
    30  	}
    31  }
    32  
    33  func (ll *latchList) pushBack(la *latch) {
    34  	ll.lazyInit()
    35  	at := ll.root.prev
    36  	n := at.next
    37  	at.next = la
    38  	la.prev = at
    39  	la.next = n
    40  	n.prev = la
    41  	ll.len++
    42  }
    43  
    44  func (ll *latchList) remove(la *latch) {
    45  	la.prev.next = la.next
    46  	la.next.prev = la.prev
    47  	la.next = nil // avoid memory leaks
    48  	la.prev = nil // avoid memory leaks
    49  	ll.len--
    50  }