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.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "arch.h"
|
|
|
|
|
|
|
|
#if __SIZEOF_POINTER__ == 8
|
|
|
|
// 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
|
|
|
|
#else
|
|
|
|
# define PTR_BITS 32
|
|
|
|
#endif
|
|
|
|
#define PTR_MASK ((1ull<<PTR_BITS)-1)
|
2013-01-26 00:43:23 +01:00
|
|
|
#define CNT_MASK (0ull-1)
|
|
|
|
|
2013-02-20 20:45:10 +01:00
|
|
|
#if __SIZEOF_POINTER__ == 8 && (defined(__sparc__) || (defined(__sun__) && defined(__amd64__)))
|
|
|
|
// 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
|
2013-01-29 14:58:18 +01:00
|
|
|
#undef PTR_BITS
|
|
|
|
#undef CNT_MASK
|
|
|
|
#undef PTR_MASK
|
2013-01-26 00:43:23 +01:00
|
|
|
#define PTR_BITS 0
|
|
|
|
#define CNT_MASK 7
|
|
|
|
#define PTR_MASK ((0ull-1)<<3)
|
|
|
|
#endif
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime_lfstackpush(uint64 *head, LFNode *node)
|
|
|
|
{
|
|
|
|
uint64 old, new;
|
|
|
|
|
|
|
|
if((uintptr)node != ((uintptr)node&PTR_MASK)) {
|
|
|
|
runtime_printf("p=%p\n", node);
|
|
|
|
runtime_throw("runtime_lfstackpush: invalid pointer");
|
|
|
|
}
|
|
|
|
|
|
|
|
node->pushcnt++;
|
2013-01-26 00:43:23 +01:00
|
|
|
new = (uint64)(uintptr)node|(((uint64)node->pushcnt&CNT_MASK)<<PTR_BITS);
|
2012-10-23 06:31:11 +02:00
|
|
|
old = runtime_atomicload64(head);
|
|
|
|
for(;;) {
|
|
|
|
node->next = (LFNode*)(uintptr)(old&PTR_MASK);
|
|
|
|
if(runtime_cas64(head, &old, new))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LFNode*
|
|
|
|
runtime_lfstackpop(uint64 *head)
|
|
|
|
{
|
|
|
|
LFNode *node, *node2;
|
|
|
|
uint64 old, new;
|
|
|
|
|
|
|
|
old = runtime_atomicload64(head);
|
|
|
|
for(;;) {
|
|
|
|
if(old == 0)
|
|
|
|
return nil;
|
|
|
|
node = (LFNode*)(uintptr)(old&PTR_MASK);
|
|
|
|
node2 = runtime_atomicloadp(&node->next);
|
|
|
|
new = 0;
|
|
|
|
if(node2 != nil)
|
2013-01-26 00:43:23 +01:00
|
|
|
new = (uint64)(uintptr)node2|(((uint64)node2->pushcnt&CNT_MASK)<<PTR_BITS);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(runtime_cas64(head, &old, new))
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LFNode* runtime_lfstackpop2(uint64*)
|
2013-01-24 20:44:23 +01:00
|
|
|
__asm__ (GOSYM_PREFIX "runtime.lfstackpop2");
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
LFNode*
|
|
|
|
runtime_lfstackpop2(uint64 *head)
|
|
|
|
{
|
|
|
|
return runtime_lfstackpop(head);
|
|
|
|
}
|