tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
counted_ptr.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 #ifndef COUNTED_PTR_HPP__
12 #define COUNTED_PTR_HPP__
13 
14 #include "common/platform.hpp"
15 
16 // Counted pointers incorporate a serial number to avoid the A-B-A problem
17 // in shared data structures. They are needed for algorithms based on
18 // compare-and-swap. We could get by without them if we had LL/SC.
19 //
21 {
22  struct {
23  void* volatile ptr;
24  volatile unsigned long sn; // serial number
25  } p;
26  volatile unsigned long long all;
27  // For when we need to read the whole thing at once.
28  // (We're assuming that doubles are loaded and stored with a single
29  // atomic instruction)
30 } __attribute__ ((aligned(8)));
31 
32 static inline bool
33 cp_cas(volatile unsigned long long* addr,
34  void* expected_ptr, unsigned long expected_sn,
35  void* new_ptr)
36 {
37  unsigned long new_sn = expected_sn + 1;
38  counted_ptr old_cp;
39  old_cp.p.ptr = expected_ptr; old_cp.p.sn = expected_sn;
40  counted_ptr new_cp;
41  new_cp.p.ptr = new_ptr; new_cp.p.sn = new_sn;;
42  return __sync_bool_compare_and_swap(addr, old_cp.all, new_cp.all);
43 }
44 
45 #endif // COUNTED_PTR_HPP__
#define __sync_bool_compare_and_swap(p, f, t)
Definition: icc-sync.hpp:48
union counted_ptr __attribute__((aligned(8)))
Definition: counted_ptr.hpp:20
void *volatile ptr
Definition: counted_ptr.hpp:23
volatile unsigned long long all
Definition: counted_ptr.hpp:26
volatile unsigned long sn
Definition: counted_ptr.hpp:24
struct counted_ptr::@17 p
static bool cp_cas(volatile unsigned long long *addr, void *expected_ptr, unsigned long expected_sn, void *new_ptr)
Definition: counted_ptr.hpp:33