Russ Cox | ece0979 | 2014-11-11 17:07:54 -0500 | [diff] [blame] | 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. |
| 4 | |
| 5 | // Lock-free stack. |
| 6 | // The following code runs only on g0 stack. |
| 7 | |
| 8 | package runtime |
| 9 | |
| 10 | import "unsafe" |
| 11 | |
Russ Cox | ece0979 | 2014-11-11 17:07:54 -0500 | [diff] [blame] | 12 | func lfstackpush(head *uint64, node *lfnode) { |
Russ Cox | ece0979 | 2014-11-11 17:07:54 -0500 | [diff] [blame] | 13 | node.pushcnt++ |
Russ Cox | 5fce15a | 2014-11-14 12:55:23 -0500 | [diff] [blame] | 14 | new := lfstackPack(node, node.pushcnt) |
Austin Clements | b76e836 | 2014-11-19 11:30:58 -0500 | [diff] [blame] | 15 | if node1, _ := lfstackUnpack(new); node1 != node { |
| 16 | println("runtime: lfstackpush invalid packing: node=", node, " cnt=", hex(node.pushcnt), " packed=", hex(new), " -> node=", node1, "\n") |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 17 | throw("lfstackpush") |
Austin Clements | b76e836 | 2014-11-19 11:30:58 -0500 | [diff] [blame] | 18 | } |
Russ Cox | ece0979 | 2014-11-11 17:07:54 -0500 | [diff] [blame] | 19 | for { |
| 20 | old := atomicload64(head) |
Russ Cox | 0fcf54b | 2014-11-15 08:00:38 -0500 | [diff] [blame] | 21 | node.next = old |
Russ Cox | ece0979 | 2014-11-11 17:07:54 -0500 | [diff] [blame] | 22 | if cas64(head, old, new) { |
| 23 | break |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func lfstackpop(head *uint64) unsafe.Pointer { |
| 29 | for { |
| 30 | old := atomicload64(head) |
| 31 | if old == 0 { |
| 32 | return nil |
| 33 | } |
Russ Cox | 5fce15a | 2014-11-14 12:55:23 -0500 | [diff] [blame] | 34 | node, _ := lfstackUnpack(old) |
Russ Cox | 0fcf54b | 2014-11-15 08:00:38 -0500 | [diff] [blame] | 35 | next := atomicload64(&node.next) |
| 36 | if cas64(head, old, next) { |
Russ Cox | ece0979 | 2014-11-11 17:07:54 -0500 | [diff] [blame] | 37 | return unsafe.Pointer(node) |
| 38 | } |
| 39 | } |
| 40 | } |