github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/runtime/lfstack.goc (about)

     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  
     7  package runtime
     8  #include "runtime.h"
     9  #include "arch.h"
    10  
    11  #if __SIZEOF_POINTER__ == 8
    12  // Amd64 uses 48-bit virtual addresses, 47-th bit is used as kernel/user flag.
    13  // So we use 17msb of pointers as ABA counter.
    14  # define PTR_BITS 47
    15  #else
    16  # define PTR_BITS 32
    17  #endif
    18  #define PTR_MASK ((1ull<<PTR_BITS)-1)
    19  #define CNT_MASK (0ull-1)
    20  
    21  #if __SIZEOF_POINTER__ == 8 && (defined(__sparc__) || (defined(__sun__) && defined(__amd64__)))
    22  // SPARC64 and Solaris on AMD64 uses all 64 bits of virtual addresses.
    23  // Use low-order three bits as ABA counter.
    24  // http://docs.oracle.com/cd/E19120-01/open.solaris/816-5138/6mba6ua5p/index.html
    25  #undef PTR_BITS
    26  #undef CNT_MASK
    27  #undef PTR_MASK
    28  #define PTR_BITS 0
    29  #define CNT_MASK 7
    30  #define PTR_MASK ((0ull-1)<<3)
    31  #endif
    32  
    33  void
    34  runtime_lfstackpush(uint64 *head, LFNode *node)
    35  {
    36  	uint64 old, new;
    37  
    38  	if((uintptr)node != ((uintptr)node&PTR_MASK)) {
    39  		runtime_printf("p=%p\n", node);
    40  		runtime_throw("runtime_lfstackpush: invalid pointer");
    41  	}
    42  
    43  	node->pushcnt++;
    44  	new = (uint64)(uintptr)node|(((uint64)node->pushcnt&CNT_MASK)<<PTR_BITS);
    45  	for(;;) {
    46  		old = runtime_atomicload64(head);
    47  		node->next = (LFNode*)(uintptr)(old&PTR_MASK);
    48  		if(runtime_cas64(head, old, new))
    49  			break;
    50  	}
    51  }
    52  
    53  LFNode*
    54  runtime_lfstackpop(uint64 *head)
    55  {
    56  	LFNode *node, *node2;
    57  	uint64 old, new;
    58  
    59  	for(;;) {
    60  		old = runtime_atomicload64(head);
    61  		if(old == 0)
    62  			return nil;
    63  		node = (LFNode*)(uintptr)(old&PTR_MASK);
    64  		node2 = runtime_atomicloadp(&node->next);
    65  		new = 0;
    66  		if(node2 != nil)
    67  			new = (uint64)(uintptr)node2|(((uint64)node2->pushcnt&CNT_MASK)<<PTR_BITS);
    68  		if(runtime_cas64(head, old, new))
    69  			return node;
    70  	}
    71  }
    72  
    73  func lfstackpush_go(head *uint64, node *LFNode) {
    74  	runtime_lfstackpush(head, node);
    75  }
    76  
    77  func lfstackpop_go(head *uint64) (node *LFNode) {
    78  	node = runtime_lfstackpop(head);
    79  }