summaryrefslogtreecommitdiff
path: root/src/runtime/lfstack.go
blob: a4ad8a10c69584aa3d4f2408757a00bbed4879c7 (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
// 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)
	for {
		old := atomicload64(head)
		node.next = 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)
		next := atomicload64(&node.next)
		if cas64(head, old, next) {
			return unsafe.Pointer(node)
		}
	}
}