tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
macros.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 MACROS_HPP__
12 #define MACROS_HPP__
13 
14 #include <stm/config.h>
15 
16 /**
17  * This file establishes a few helpful macros. Some of these are obvious.
18  * Others are used to simplify some very redundant programming, particularly
19  * with regard to declaring STM functions and abort codes.
20  */
21 
22 /*** Helper Macros for concatenating tokens ***/
23 #define CAT2(a,b) a ## b
24 #define CAT3(a,b,c) a ## b ## c
25 
26 /*** Helper Macros for turning a macro token into a string ***/
27 #define TO_STRING_LITERAL(arg) #arg
28 #define MAKE_STR(arg) TO_STRING_LITERAL(arg)
29 
30 /*** max of two vals */
31 #define MAXIMUM(x,y) (((x)>(y))?(x):(y))
32 
33 /**
34  * C++ iterators can get so ugly without c++0x 'auto'. These macros are not
35  * a good idea, but it makes it much easier to write 79-column code
36  */
37 #define foreach(TYPE, VAR, COLLECTION) \
38  for (TYPE::iterator VAR = COLLECTION.begin(), \
39  CEND = COLLECTION.end(); \
40  VAR != CEND; ++VAR)
41 
42 #define FOREACH_REVERSE(TYPE, VAR, COLLECTION) \
43  for (TYPE::iterator VAR = COLLECTION.end() - 1, \
44  CAT2(end, __LINE__) = COLLECTION.begin(); \
45  VAR >= CAT2(end, __LINE__); --VAR)
46 
47 /**
48  * When we use compiler-based instrumentation, support for
49  * sub-word-granularity accesses requires the individual STM read/write
50  * functions to take a mask as the third parameter. The following macros let
51  * us inject a parameter into the function signature as needed for this
52  * purpose.
53  */
54 #if defined(STM_WS_BYTELOG)
55 #define STM_MASK(x) , x
56 #elif defined(STM_WS_WORDLOG)
57 #define STM_MASK(x)
58 #else
59 #error "Either STM_WS_BYTELOG or STM_WS_WORDLOG must be defined"
60 #endif
61 
62 /**
63  * We need to protect the stack from "stale" writes during redo-writeback and
64  * undo-undo. This requires some platform-specific functionality to get some
65  * stack information.
66  */
67 #ifdef STM_PROTECT_STACK
68 # define STM_WHEN_PROTECT_STACK(S) S
69 # define TOP_OF_ARGS(N) ((void**)__builtin_frame_address(0) + 2 + N)
70 #else
71 # define STM_WHEN_PROTECT_STACK(S)
72 #endif
73 
74 #ifdef STM_ABORT_ON_THROW
75 # define STM_WHEN_ABORT_ON_THROW(S) S
76 #else
77 # define STM_WHEN_ABORT_ON_THROW(S)
78 #endif
79 
80 #if defined(STM_WS_BYTELOG)
81 # define STM_READ_SIG(tx, addr, mask) TxThread* tx, void** addr, uintptr_t mask
82 # define STM_WRITE_SIG(tx, addr, val, mask) TxThread* tx, void** addr, void* val, uintptr_t mask
83 #else
84 # define STM_READ_SIG(tx, addr, mask) TxThread* tx, void** addr
85 # define STM_WRITE_SIG(tx, addr, val, mask) TxThread* tx, void** addr, void* val
86 #endif
87 
88 #if defined(STM_PROTECT_STACK)
89 # define STM_COMMIT_SIG(tx, stack) TxThread* tx, void** stack
90 # define STM_IRREVOC_SIG(tx, stack) TxThread* tx, void** stack
91 #else
92 # define STM_COMMIT_SIG(tx, stack) TxThread* tx
93 # define STM_IRREVOC_SIG(tx, stack) TxThread* tx
94 #endif
95 
96 #if defined(STM_PROTECT_STACK) && defined(STM_ABORT_ON_THROW)
97 # define STM_ROLLBACK_SIG(tx, stack, exception, len) \
98  TxThread* tx, void** stack, void** exception, size_t len
99 #elif defined(STM_PROTECT_STACK) && !defined(STM_ABORT_ON_THROW)
100 # define STM_ROLLBACK_SIG(tx, stack, exception, len) \
101  TxThread* tx, void** stack
102 #elif !defined(STM_PROTECT_STACK) && defined(STM_ABORT_ON_THROW)
103 # define STM_ROLLBACK_SIG(tx, stack, exception, len) \
104  TxThread* tx, void** exception, size_t len
105 #elif !defined(STM_PROTECT_STACK) && !defined(STM_ABORT_ON_THROW)
106 # define STM_ROLLBACK_SIG(tx, stack, exception, len) TxThread* tx
107 #else
108 # error Preprocessor if/else logic error
109 #endif
110 
111 #endif // MACROS_HPP__