mirror of
https://github.com/golang/go
synced 2024-10-02 20:41:21 -06:00
02b8e6978a
These comments were left behind after runtime.h was converted from C to Go. I examined the original code and tried to move these to the places that the most sense. Change-Id: I8769d60234c0113d682f9de3bd8d6c34c450c188 Reviewed-on: https://go-review.googlesource.com/21969 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
// Copyright 2012 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Lock-free stack.
|
|
// Initialize head to 0, compare with 0 to test for emptiness.
|
|
// The stack does not keep pointers to nodes,
|
|
// so they can be garbage collected if there are no other pointers to nodes.
|
|
// The following code runs only on g0 stack.
|
|
|
|
package runtime
|
|
|
|
import (
|
|
"runtime/internal/atomic"
|
|
"unsafe"
|
|
)
|
|
|
|
func lfstackpush(head *uint64, node *lfnode) {
|
|
node.pushcnt++
|
|
new := lfstackPack(node, node.pushcnt)
|
|
if node1 := lfstackUnpack(new); node1 != node {
|
|
print("runtime: lfstackpush invalid packing: node=", node, " cnt=", hex(node.pushcnt), " packed=", hex(new), " -> node=", node1, "\n")
|
|
throw("lfstackpush")
|
|
}
|
|
for {
|
|
old := atomic.Load64(head)
|
|
node.next = old
|
|
if atomic.Cas64(head, old, new) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func lfstackpop(head *uint64) unsafe.Pointer {
|
|
for {
|
|
old := atomic.Load64(head)
|
|
if old == 0 {
|
|
return nil
|
|
}
|
|
node := lfstackUnpack(old)
|
|
next := atomic.Load64(&node.next)
|
|
if atomic.Cas64(head, old, next) {
|
|
return unsafe.Pointer(node)
|
|
}
|
|
}
|
|
}
|