summaryrefslogtreecommitdiff
path: root/src/runtime/lfstack.go
blob: 8a36a67b35706d170df6229cecf4e7562d7a4fda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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++
	new := lfstackPack(node, node.pushcnt)
	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")
	}
	for {
		old := atomicload64(head)
		node.next, _ = lfstackUnpack(old)
		if cas64(head, old, new) {
			break
		}
	}
}

func lfstackpop(head *uint64) unsafe.Pointer {
	for {
		old := atomicload64(head)
		if old == 0 {
			return nil
		}
		node, _ := lfstackUnpack(old)
		node2 := (*lfnode)(atomicloadp(unsafe.Pointer(&node.next)))
		new := uint64(0)
		if node2 != nil {
			new = lfstackPack(node2, node2.pushcnt)
		}
		if cas64(head, old, new) {
			return unsafe.Pointer(node)
		}
	}
}