tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
stamp.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 API_STAMP_HPP__
12 #define API_STAMP_HPP__
13 
14 #include <setjmp.h>
15 #include <cstdlib>
16 #include <cassert>
17 #include <api/library.hpp>
18 
19 /**
20  * We are gradually moving to a state where STAMP will be in pure C++.
21  * Clearly we are not there yet, but currently we have STAMP set to build with
22  * g++ instead of gcc.
23  *
24  * That being the case, we're going to avoid some cruft by mapping MACROs
25  * directly to c++ functions, instead of hiding code within an 'extern C'
26  * block with C-style wrapper functions.
27  */
28 
29 #define STM_THREAD_T stm::TxThread
30 #define STM_SELF tm_descriptor
31 #define STM_STARTUP(numThread) tm_main_startup()
32 #define STM_SHUTDOWN() stm::sys_shutdown()
33 #define STM_NEW_THREAD() 0
34 #define STM_INIT_THREAD(t, id) tm_start(&t, thread_getId())
35 #define STM_FREE_THREAD(t)
36 #define STM_RESTART() stm::restart()
37 
38 #define STM_LOCAL_WRITE_I(var, val) ({var = val; var;})
39 #define STM_LOCAL_WRITE_L(var, val) ({var = val; var;})
40 #define STM_LOCAL_WRITE_F(var, val) ({var = val; var;})
41 #define STM_LOCAL_WRITE_P(var, val) ({var = val; var;})
42 
43 /**
44  * Special alloc for STAMP, used to detect nontransactional mallocs from
45  * within transactions
46  */
47 inline void* tx_safe_non_tx_alloc(size_t size)
48 {
49  return malloc(size);
50 }
51 
52 /**
53  * Special free for STAMP, used to detect nontransactional frees from within
54  * transactions
55  */
56 inline void tx_safe_non_tx_free(void * ptr)
57 {
58  free(ptr);
59 }
60 
61 /**
62  * The begin and commit instrumentation are straightforward
63  */
64 #define STM_BEGIN_WR() \
65  { \
66  jmp_buf jmpbuf_; \
67  uint32_t abort_flags = setjmp(jmpbuf_); \
68  begin(static_cast<stm::TxThread*>(STM_SELF), &jmpbuf_, abort_flags);\
69  CFENCE; \
70  {
71 
72 #define STM_END() \
73  } \
74  commit(static_cast<stm::TxThread*>(STM_SELF)); \
75  }
76 
77 /*** read-only begin == read/write begin */
78 #define STM_BEGIN_RD() STM_BEGIN_WR()
79 
80 /**
81  * tm_main_startup()
82  *
83  * call before any threads try to run transactions, in order to ensure
84  * that the TM library is properly configured.
85  *
86  * multiple calls are safe, since the library protects itself
87  */
88 inline void tm_main_startup()
89 {
90  // start the STM runtime
91  stm::sys_init(NULL);
92 
93  // create a descriptor for this thread
95 }
96 
97 /**
98  * tm_start(desc, id)
99  *
100  * STAMP uses this during its main processing loop, once all of the
101  * threads have been created and are sitting at a barrier waiting to
102  * start. We expect it to be only called once per thread, but it is ok if
103  * it is called more than once.
104  *
105  * The thread that called tm_main_startup(...) /can/, but does not have
106  * to, call this routine.
107  */
108 inline void tm_start(stm::TxThread** desc, int id)
109 {
111  // The desc parameter is an "out" parameter, so return its address
112  *desc = stm::Self;
113 }
114 
115 #endif // API_STAMP_HPP__
void *volatile ptr
Definition: counted_ptr.hpp:57
void * tx_safe_non_tx_alloc(size_t size)
Definition: stamp.hpp:47
void sys_init(void(*abort_handler)(TxThread *)=NULL)
void tm_main_startup()
Definition: stamp.hpp:88
void tx_safe_non_tx_free(void *ptr)
Definition: stamp.hpp:56
void tm_start(stm::TxThread **desc, int id)
Definition: stamp.hpp:108
void thread_init()
Definition: library.hpp:196
Definition: txthread.hpp:47