2012-10-23 06:31:11 +02:00
|
|
|
// 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.
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
package runtime
|
2012-10-23 06:31:11 +02:00
|
|
|
#include "runtime.h"
|
|
|
|
#include "arch.h"
|
|
|
|
|
|
|
|
#if __SIZEOF_POINTER__ == 8
|
2013-02-20 20:45:10 +01:00
|
|
|
// SPARC64 and Solaris on AMD64 uses all 64 bits of virtual addresses.
|
|
|
|
// Use low-order three bits as ABA counter.
|
|
|
|
// http://docs.oracle.com/cd/E19120-01/open.solaris/816-5138/6mba6ua5p/index.html
|
2015-09-23 19:07:15 +02:00
|
|
|
# if defined(__sparc__) || (defined(__sun__) && defined(__amd64__))
|
|
|
|
static inline uint64 lfPack(LFNode *node, uintptr cnt) {
|
|
|
|
return ((uint64)(node)) | ((cnt)&7);
|
|
|
|
}
|
|
|
|
static inline LFNode* lfUnpack(uint64 val) {
|
|
|
|
return (LFNode*)(val&~7);
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
# if defined(__aarch64__)
|
|
|
|
// Depending on the kernel options, pointers on arm64 can have up to 48 significant
|
|
|
|
// bits (see https://www.kernel.org/doc/Documentation/arm64/memory.txt).
|
|
|
|
# define PTR_BITS 48
|
|
|
|
# else
|
|
|
|
// Amd64 uses 48-bit virtual addresses, 47-th bit is used as kernel/user flag.
|
|
|
|
// So we use 17msb of pointers as ABA counter.
|
|
|
|
# define PTR_BITS 47
|
|
|
|
# endif
|
|
|
|
# define CNT_BITS (64 - PTR_BITS + 3)
|
|
|
|
static inline uint64 lfPack(LFNode *node, uintptr cnt) {
|
|
|
|
return ((uint64)(node)<<(64-PTR_BITS)) | (cnt&(((1<<CNT_BITS)-1)));
|
|
|
|
}
|
|
|
|
static inline LFNode* lfUnpack(uint64 val) {
|
|
|
|
return (LFNode*)((val >> CNT_BITS) << 3);
|
|
|
|
}
|
2015-09-24 15:33:20 +02:00
|
|
|
# endif
|
2015-09-23 19:07:15 +02:00
|
|
|
#else
|
|
|
|
static inline uint64 lfPack(LFNode *node, uintptr cnt) {
|
|
|
|
return ((uint64)(uintptr)(node)<<32) | cnt;
|
|
|
|
}
|
|
|
|
static inline LFNode* lfUnpack(uint64 val) {
|
|
|
|
return (LFNode*)(uintptr)(val >> 32);
|
|
|
|
}
|
2013-01-26 00:43:23 +01:00
|
|
|
#endif
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime_lfstackpush(uint64 *head, LFNode *node)
|
|
|
|
{
|
|
|
|
uint64 old, new;
|
|
|
|
|
2015-09-23 19:07:15 +02:00
|
|
|
if(node != lfUnpack(lfPack(node, 0))) {
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_printf("p=%p\n", node);
|
|
|
|
runtime_throw("runtime_lfstackpush: invalid pointer");
|
|
|
|
}
|
|
|
|
|
|
|
|
node->pushcnt++;
|
2015-09-23 19:07:15 +02:00
|
|
|
new = lfPack(node, node->pushcnt);
|
2012-10-23 06:31:11 +02:00
|
|
|
for(;;) {
|
2013-11-06 20:49:01 +01:00
|
|
|
old = runtime_atomicload64(head);
|
2015-09-23 19:07:15 +02:00
|
|
|
node->next = lfUnpack(old);
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_cas64(head, old, new))
|
2012-10-23 06:31:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LFNode*
|
|
|
|
runtime_lfstackpop(uint64 *head)
|
|
|
|
{
|
|
|
|
LFNode *node, *node2;
|
|
|
|
uint64 old, new;
|
|
|
|
|
|
|
|
for(;;) {
|
2013-11-06 20:49:01 +01:00
|
|
|
old = runtime_atomicload64(head);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(old == 0)
|
|
|
|
return nil;
|
2015-09-23 19:07:15 +02:00
|
|
|
node = lfUnpack(old);
|
2012-10-23 06:31:11 +02:00
|
|
|
node2 = runtime_atomicloadp(&node->next);
|
|
|
|
new = 0;
|
|
|
|
if(node2 != nil)
|
2015-09-23 19:07:15 +02:00
|
|
|
new = lfPack(node2, node2->pushcnt);
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_cas64(head, old, new))
|
2012-10-23 06:31:11 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func lfstackpush_go(head *uint64, node *LFNode) {
|
|
|
|
runtime_lfstackpush(head, node);
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func lfstackpop_go(head *uint64) (node *LFNode) {
|
|
|
|
node = runtime_lfstackpop(head);
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|