2014-11-11 15:07:54 -07:00
|
|
|
// 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.
|
|
|
|
// The following code runs only on g0 stack.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
func lfstackpush(head *uint64, node *lfnode) {
|
|
|
|
node.pushcnt++
|
2014-11-14 10:55:23 -07:00
|
|
|
new := lfstackPack(node, node.pushcnt)
|
2014-11-19 09:30:58 -07:00
|
|
|
if node1, _ := lfstackUnpack(new); node1 != node {
|
|
|
|
println("runtime: lfstackpush invalid packing: node=", node, " cnt=", hex(node.pushcnt), " packed=", hex(new), " -> node=", node1, "\n")
|
|
|
|
gothrow("lfstackpush")
|
|
|
|
}
|
2014-11-11 15:07:54 -07:00
|
|
|
for {
|
|
|
|
old := atomicload64(head)
|
2014-11-15 06:00:38 -07:00
|
|
|
node.next = old
|
2014-11-11 15:07:54 -07:00
|
|
|
if cas64(head, old, new) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lfstackpop(head *uint64) unsafe.Pointer {
|
|
|
|
for {
|
|
|
|
old := atomicload64(head)
|
|
|
|
if old == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-14 10:55:23 -07:00
|
|
|
node, _ := lfstackUnpack(old)
|
2014-11-15 06:00:38 -07:00
|
|
|
next := atomicload64(&node.next)
|
|
|
|
if cas64(head, old, next) {
|
2014-11-11 15:07:54 -07:00
|
|
|
return unsafe.Pointer(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|