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.
|
2016-04-13 03:16:21 -06:00
|
|
|
// 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.
|
2014-11-11 15:07:54 -07:00
|
|
|
// The following code runs only on g0 stack.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
2015-11-02 12:09:24 -07:00
|
|
|
import (
|
|
|
|
"runtime/internal/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2014-11-11 15:07:54 -07:00
|
|
|
|
|
|
|
func lfstackpush(head *uint64, node *lfnode) {
|
|
|
|
node.pushcnt++
|
2014-11-14 10:55:23 -07:00
|
|
|
new := lfstackPack(node, node.pushcnt)
|
2016-04-06 02:43:23 -06:00
|
|
|
if node1 := lfstackUnpack(new); node1 != node {
|
2015-11-11 21:49:37 -07:00
|
|
|
print("runtime: lfstackpush invalid packing: node=", node, " cnt=", hex(node.pushcnt), " packed=", hex(new), " -> node=", node1, "\n")
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("lfstackpush")
|
2014-11-19 09:30:58 -07:00
|
|
|
}
|
2014-11-11 15:07:54 -07:00
|
|
|
for {
|
2015-11-02 12:09:24 -07:00
|
|
|
old := atomic.Load64(head)
|
2014-11-15 06:00:38 -07:00
|
|
|
node.next = old
|
2015-11-02 12:09:24 -07:00
|
|
|
if atomic.Cas64(head, old, new) {
|
2014-11-11 15:07:54 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lfstackpop(head *uint64) unsafe.Pointer {
|
|
|
|
for {
|
2015-11-02 12:09:24 -07:00
|
|
|
old := atomic.Load64(head)
|
2014-11-11 15:07:54 -07:00
|
|
|
if old == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-06 02:43:23 -06:00
|
|
|
node := lfstackUnpack(old)
|
2015-11-02 12:09:24 -07:00
|
|
|
next := atomic.Load64(&node.next)
|
|
|
|
if atomic.Cas64(head, old, next) {
|
2014-11-11 15:07:54 -07:00
|
|
|
return unsafe.Pointer(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|