2017-03-02 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / runtime / lfstack.go
blob2f2958c8869cee48054ea7e8a0924b24f76b3ec6
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.
5 // Lock-free stack.
6 // Initialize head to 0, compare with 0 to test for emptiness.
7 // The stack does not keep pointers to nodes,
8 // so they can be garbage collected if there are no other pointers to nodes.
9 // The following code runs only in non-preemptible contexts.
11 package runtime
13 import (
14 "runtime/internal/atomic"
15 "unsafe"
18 // Temporary for C code to call:
19 //go:linkname lfstackpush runtime.lfstackpush
20 //go:linkname lfstackpop runtime.lfstackpop
22 func lfstackpush(head *uint64, node *lfnode) {
23 node.pushcnt++
24 new := lfstackPack(node, node.pushcnt)
25 if node1 := lfstackUnpack(new); node1 != node {
26 print("runtime: lfstackpush invalid packing: node=", node, " cnt=", hex(node.pushcnt), " packed=", hex(new), " -> node=", node1, "\n")
27 throw("lfstackpush")
29 for {
30 old := atomic.Load64(head)
31 node.next = old
32 if atomic.Cas64(head, old, new) {
33 break
38 func lfstackpop(head *uint64) unsafe.Pointer {
39 for {
40 old := atomic.Load64(head)
41 if old == 0 {
42 return nil
44 node := lfstackUnpack(old)
45 next := atomic.Load64(&node.next)
46 if atomic.Cas64(head, old, next) {
47 return unsafe.Pointer(node)