diff options
Diffstat (limited to 'src/runtime/lfstack.go')
| -rw-r--r-- | src/runtime/lfstack.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/runtime/lfstack.go b/src/runtime/lfstack.go new file mode 100644 index 0000000000..a4ad8a10c6 --- /dev/null +++ b/src/runtime/lfstack.go @@ -0,0 +1,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) + } + } +} |
