1
# DP: libgo: Fix PR go/67508, rewrite lfstack packing/unpacking
2
# DP: to look more like that in Go
4
From 95eb3733500feffc2eeeec5bcfac7e34583514e2 Mon Sep 17 00:00:00 2001
5
From: Michael Hudson-Doyle <michael.hudson@canonical.com>
6
Date: Fri, 31 Jul 2015 11:45:45 +1200
7
Subject: [PATCH] runtime: rewrite lfstack packing/unpacking to look more like that in Go
9
Change-Id: I4ca61240c3f69c6dce1fde8d859f8507dfab80fc
12
diff --git a/libgo/runtime/lfstack.goc b/libgo/runtime/lfstack.goc
13
index 060a0cc..9eb80d9 100644
14
--- a/src/libgo/runtime/lfstack.goc
15
+++ b/src/libgo/runtime/lfstack.goc
19
#if __SIZEOF_POINTER__ == 8
20
-// Amd64 uses 48-bit virtual addresses, 47-th bit is used as kernel/user flag.
21
-// So we use 17msb of pointers as ABA counter.
26
-#define PTR_MASK ((1ull<<PTR_BITS)-1)
27
-#define CNT_MASK (0ull-1)
29
-#if __SIZEOF_POINTER__ == 8 && (defined(__sparc__) || (defined(__sun__) && defined(__amd64__)))
30
// SPARC64 and Solaris on AMD64 uses all 64 bits of virtual addresses.
31
// Use low-order three bits as ABA counter.
32
// http://docs.oracle.com/cd/E19120-01/open.solaris/816-5138/6mba6ua5p/index.html
38
-#define PTR_MASK ((0ull-1)<<3)
39
+# if defined(__sparc__) || (defined(__sun__) && defined(__amd64__))
40
+static inline uint64 lfPack(LFNode *node, uintptr cnt) {
41
+ return ((uint64)(node)) | ((cnt)&7);
43
+static inline LFNode* lfUnpack(uint64 val) {
44
+ return (LFNode*)(val&~7);
47
+# if defined(__aarch64__)
48
+// Depending on the kernel options, pointers on arm64 can have up to 48 significant
49
+// bits (see https://www.kernel.org/doc/Documentation/arm64/memory.txt).
52
+// Amd64 uses 48-bit virtual addresses, 47-th bit is used as kernel/user flag.
53
+// So we use 17msb of pointers as ABA counter.
57
+# define CNT_BITS (64 - PTR_BITS + 3)
58
+static inline uint64 lfPack(LFNode *node, uintptr cnt) {
59
+ return ((uint64)(node)<<(64-PTR_BITS)) | (cnt&(((1<<CNT_BITS)-1)));
61
+static inline LFNode* lfUnpack(uint64 val) {
62
+ return (LFNode*)((val >> CNT_BITS) << 3);
65
+static inline uint64 lfPack(LFNode *node, uintptr cnt) {
66
+ return ((uint64)(uintptr)(node)<<32) | cnt;
68
+static inline LFNode* lfUnpack(uint64 val) {
69
+ return (LFNode*)(uintptr)(val >> 32);
78
- if((uintptr)node != ((uintptr)node&PTR_MASK)) {
79
+ if(node != lfUnpack(lfPack(node, 0))) {
80
runtime_printf("p=%p\n", node);
81
runtime_throw("runtime_lfstackpush: invalid pointer");
85
- new = (uint64)(uintptr)node|(((uint64)node->pushcnt&CNT_MASK)<<PTR_BITS);
86
+ new = lfPack(node, node->pushcnt);
88
old = runtime_atomicload64(head);
89
- node->next = (LFNode*)(uintptr)(old&PTR_MASK);
90
+ node->next = lfUnpack(old);
91
if(runtime_cas64(head, old, new))
95
old = runtime_atomicload64(head);
98
- node = (LFNode*)(uintptr)(old&PTR_MASK);
99
+ node = lfUnpack(old);
100
node2 = runtime_atomicloadp(&node->next);
103
- new = (uint64)(uintptr)node2|(((uint64)node2->pushcnt&CNT_MASK)<<PTR_BITS);
104
+ new = lfPack(node2, node2->pushcnt);
105
if(runtime_cas64(head, old, new))