tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
WBMMPolicy.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 /**
12  * In order to get allocation and deallocation to work correctly inside of a
13  * speculative transactional region, we need to be sure that a doomed
14  * transaction cannot access memory that has been returned to the OS.
15  *
16  * WBMMPolicy is RSTM's variant of epoch-based reclamation. It is similar
17  * to proposals by [Fraser PhD 2003] and [Hudson ISMM 2006].
18  *
19  * Note that this file has real code in it, and that code gets inlined into
20  * many places. It's not pretty, and we may eventually want to reduce the
21  * footprint of this file on the rest of the project.
22  */
23 
24 #ifndef WBMMPOLICY_HPP__
25 #define WBMMPOLICY_HPP__
26 
27 #include <stdlib.h>
28 #include <stm/config.h>
29 #include "stm/MiniVector.hpp"
30 #include "stm/metadata.hpp"
31 
32 namespace stm
33 {
34  /*** forward declare the threadcount used by TxThread */
36 
37  /*** store every thread's counter */
39 
40  /*** Node type for a list of timestamped void*s */
41  struct limbo_t
42  {
43  /*** Number of void*s held in a limbo_t */
44  static const uint32_t POOL_SIZE = 32;
45 
46  /*** Set of void*s */
47  void* pool[POOL_SIZE];
48 
49  /*** Timestamp when last void* was added */
50  uint32_t ts[MAX_THREADS];
51 
52  /*** # valid timestamps in ts, or # elements in pool */
53  uint32_t length;
54 
55  /*** NehelperMin pointer for the limbo list */
57 
58  /*** The constructor for the limbo_t just zeroes out everything */
59  limbo_t() : length(0), older(NULL) { }
60  };
61 
62  /**
63  * WBMMPolicy
64  * - log allocs and frees from within a transaction
65  * - on abort, free any allocs
66  * - on commit, replay any frees
67  * - use epochs to prevent reclamation during a doomed transaction's
68  * execution
69  */
70  class WBMMPolicy
71  {
72  /*** location of my timestamp value */
73  volatile uintptr_t* my_ts;
74 
75  /*** As we mark things for deletion, we accumulate them here */
77 
78  /*** sorted list of timestamped reclaimables */
80 
81  /*** List of objects to delete if the current transaction commits */
83 
84  /*** List of objects to delete if the current transaction aborts */
86 
87  /**
88  * Schedule a pointer for reclamation. Reclamation will not happen
89  * until enough time has passed.
90  */
91  void schedForReclaim(void* ptr)
92  {
93  // insert /ptr/ into the prelimbo pool and increment the pool size
95  // if prelimbo is not full, we're done
97  return;
98  // if prelimbo is full, we have a lot more work to do
100  }
101 
102  /**
103  * This code is the cornerstone of the WBMMPolicy. We buffer lots of
104  * frees onto a prelimbo list, and then, at some point, we must give
105  * that list a timestamp and tuck it away until the timestamp expires.
106  * This is how we do it.
107  */
109 
110  public:
111 
112  /**
113  * Constructing the DeferredReclamationMMPolicy is very easy
114  * Null out the timestamp for a particular thread. We only call this
115  * at initialization.
116  */
118  : prelimbo(new limbo_t()), limbo(NULL), frees(128), allocs(128)
119  { }
120 
121  /**
122  * Since a TxThread constructs its allocator before it gets its id, we
123  * need the TxThread to inform the allocator of its id from within the
124  * constructor, via this method.
125  */
126  void setID(uint32_t id) { my_ts = &trans_nums[id].val; }
127 
128  /*** Wrapper to thread-specific allocator for allocating memory */
129  void* txAlloc(size_t const &size)
130  {
131  void* ptr = malloc(size);
132  if ((*my_ts)&1)
133  allocs.insert(ptr);
134  return ptr;
135  }
136 
137  /*** Wrapper to thread-specific allocator for freeing memory */
138  void txFree(void* ptr)
139  {
140  if ((*my_ts)&1)
141  frees.insert(ptr);
142  else
143  free(ptr);
144  }
145 
146  /*** On begin, move to an odd epoch and start logging */
147  void onTxBegin() { *my_ts = 1 + *my_ts; }
148 
149  /*** On abort, unroll allocs, clear lists, exit epoch */
150  void onTxAbort()
151  {
153  for (i = allocs.begin(), e = allocs.end(); i != e; ++i)
154  free(*i);
155  frees.reset();
156  allocs.reset();
157  *my_ts = 1+*my_ts;
158  }
159 
160  /*** On commit, perform frees, clear lists, exit epoch */
161  void onTxCommit()
162  {
164  for (i = frees.begin(), e = frees.end(); i != e; ++i)
165  schedForReclaim(*i);
166  frees.reset();
167  allocs.reset();
168  *my_ts = 1+*my_ts;
169  }
170  }; // class stm::WBMMPolicy
171 
172 } // namespace stm
173 
174 #endif // WBMMPOLICY_HPP__
void *volatile ptr
Definition: counted_ptr.hpp:57
#define NOINLINE
Definition: platform.hpp:48
limbo_t * prelimbo
Definition: WBMMPolicy.hpp:76
pad_word_t trans_nums[MAX_THREADS]
Definition: WBMMPolicy.cpp:27
void onTxAbort()
Definition: WBMMPolicy.hpp:150
void onTxBegin()
Definition: WBMMPolicy.hpp:147
Definition: metadata.hpp:115
Definition: stm_fraser.c:61
limbo_t()
Definition: WBMMPolicy.hpp:59
TM_INLINE void insert(T data)
Definition: MiniVector.hpp:55
Definition: WBMMPolicy.hpp:41
TM_INLINE void reset()
Definition: MiniVector.hpp:52
AddressList frees
Definition: WBMMPolicy.hpp:82
volatile uintptr_t val
Definition: metadata.hpp:117
TM_INLINE iterator begin() const
Definition: MiniVector.hpp:82
AddressList allocs
Definition: WBMMPolicy.hpp:85
void schedForReclaim(void *ptr)
Definition: WBMMPolicy.hpp:91
pad_word_t threadcount
Definition: WBMMPolicy.hpp:35
WBMMPolicy()
Definition: WBMMPolicy.hpp:117
Definition: memory.c:97
volatile uintptr_t * my_ts
Definition: WBMMPolicy.hpp:73
static const uint32_t POOL_SIZE
Definition: WBMMPolicy.hpp:44
limbo_t * limbo
Definition: WBMMPolicy.hpp:79
Definition: WBMMPolicy.hpp:70
void * pool[POOL_SIZE]
Definition: WBMMPolicy.hpp:47
NOINLINE void handle_full_prelimbo()
Definition: WBMMPolicy.cpp:30
void txFree(void *ptr)
Definition: WBMMPolicy.hpp:138
static const unsigned MAX_THREADS
Definition: metadata.hpp:33
void ** iterator
Definition: MiniVector.hpp:79
void onTxCommit()
Definition: WBMMPolicy.hpp:161
void * txAlloc(size_t const &size)
Definition: WBMMPolicy.hpp:129
uint32_t length
Definition: WBMMPolicy.hpp:53
void setID(uint32_t id)
Definition: WBMMPolicy.hpp:126
TM_INLINE iterator end() const
Definition: MiniVector.hpp:85
uint32_t ts[MAX_THREADS]
Definition: WBMMPolicy.hpp:50
limbo_t * older
Definition: WBMMPolicy.hpp:56