Source file src/runtime/lfstack.go

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Lock-free stack.
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/runtime/atomic"
    11  	"unsafe"
    12  )
    13  
    14  // lfstack is the head of a lock-free stack.
    15  //
    16  // The zero value of lfstack is an empty list.
    17  //
    18  // This stack is intrusive. Nodes must embed lfnode as the first field.
    19  //
    20  // The stack does not keep GC-visible pointers to nodes, so the caller
    21  // must ensure the nodes are allocated outside the Go heap.
    22  type lfstack uint64
    23  
    24  func (head *lfstack) push(node *lfnode) {
    25  	node.pushcnt++
    26  	new := lfstackPack(node, node.pushcnt)
    27  	for {
    28  		old := atomic.Load64((*uint64)(head))
    29  		node.next = old
    30  		if atomic.Cas64((*uint64)(head), old, new) {
    31  			break
    32  		}
    33  	}
    34  }
    35  
    36  func (head *lfstack) pop() unsafe.Pointer {
    37  	for {
    38  		old := atomic.Load64((*uint64)(head))
    39  		if old == 0 {
    40  			return nil
    41  		}
    42  		node := lfstackUnpack(old)
    43  		next := atomic.Load64(&node.next)
    44  		if atomic.Cas64((*uint64)(head), old, next) {
    45  			return unsafe.Pointer(node)
    46  		}
    47  	}
    48  }
    49  
    50  func (head *lfstack) empty() bool {
    51  	return atomic.Load64((*uint64)(head)) == 0
    52  }
    53  
    54  // lfnodeValidate panics if node is not a valid address for use with
    55  // lfstack.push. This only needs to be called when node is allocated.
    56  func lfnodeValidate(node *lfnode) {
    57  	if base, _, _ := findObject(uintptr(unsafe.Pointer(node)), 0, 0); base != 0 {
    58  		throw("lfstack node allocated from the heap")
    59  	}
    60  	lfstackPack(node, ^uintptr(0))
    61  }
    62  
    63  func lfstackPack(node *lfnode, cnt uintptr) uint64 {
    64  	return uint64(taggedPointerPack(unsafe.Pointer(node), cnt&(1<<tagBits-1)))
    65  }
    66  
    67  func lfstackUnpack(val uint64) *lfnode {
    68  	return (*lfnode)(taggedPointer(val).pointer())
    69  }
    70  

View as plain text