Fix ChangeLog record for 171649:
[official-gcc.git] / libitm / dispatch.h
blobd059c493507df885461981581ce62e11aef83e81
1 /* Copyright (C) 2011 Free Software Foundation, Inc.
2 Contributed by Torvald Riegel <triegel@redhat.com>.
4 This file is part of the GNU Transactional Memory Library (libitm).
6 Libitm is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #ifndef DISPATCH_H
26 #define DISPATCH_H 1
28 #include "libitm.h"
29 #include "common.h"
31 // Creates ABI load/store methods (can be made virtual or static using M,
32 // use M2 to create separate methods names for virtual and static)
33 // The _PV variants are for the pure-virtual methods in the base class.
34 #define ITM_READ_M(T, LSMOD, M, M2) \
35 M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
36 { \
37 return load(ptr, abi_dispatch::LSMOD); \
40 #define ITM_READ_M_PV(T, LSMOD, M, M2) \
41 M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
42 = 0;
44 #define ITM_WRITE_M(T, LSMOD, M, M2) \
45 M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
46 _ITM_TYPE_##T val) \
47 { \
48 store(ptr, val, abi_dispatch::LSMOD); \
51 #define ITM_WRITE_M_PV(T, LSMOD, M, M2) \
52 M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
53 _ITM_TYPE_##T val) \
54 = 0;
56 // Creates ABI load/store methods for all load/store modifiers for a particular
57 // type.
58 #define CREATE_DISPATCH_METHODS_T(T, M, M2) \
59 ITM_READ_M(T, R, M, M2) \
60 ITM_READ_M(T, RaR, M, M2) \
61 ITM_READ_M(T, RaW, M, M2) \
62 ITM_READ_M(T, RfW, M, M2) \
63 ITM_WRITE_M(T, W, M, M2) \
64 ITM_WRITE_M(T, WaR, M, M2) \
65 ITM_WRITE_M(T, WaW, M, M2)
66 #define CREATE_DISPATCH_METHODS_T_PV(T, M, M2) \
67 ITM_READ_M_PV(T, R, M, M2) \
68 ITM_READ_M_PV(T, RaR, M, M2) \
69 ITM_READ_M_PV(T, RaW, M, M2) \
70 ITM_READ_M_PV(T, RfW, M, M2) \
71 ITM_WRITE_M_PV(T, W, M, M2) \
72 ITM_WRITE_M_PV(T, WaR, M, M2) \
73 ITM_WRITE_M_PV(T, WaW, M, M2)
75 // Creates ABI load/store methods for all types.
76 // See CREATE_DISPATCH_FUNCTIONS for comments.
77 #define CREATE_DISPATCH_METHODS(M, M2) \
78 CREATE_DISPATCH_METHODS_T (U1, M, M2) \
79 CREATE_DISPATCH_METHODS_T (U2, M, M2) \
80 CREATE_DISPATCH_METHODS_T (U4, M, M2) \
81 CREATE_DISPATCH_METHODS_T (U8, M, M2) \
82 CREATE_DISPATCH_METHODS_T (F, M, M2) \
83 CREATE_DISPATCH_METHODS_T (D, M, M2) \
84 CREATE_DISPATCH_METHODS_T (E, M, M2) \
85 CREATE_DISPATCH_METHODS_T (CF, M, M2) \
86 CREATE_DISPATCH_METHODS_T (CD, M, M2) \
87 CREATE_DISPATCH_METHODS_T (CE, M, M2)
88 #define CREATE_DISPATCH_METHODS_PV(M, M2) \
89 CREATE_DISPATCH_METHODS_T_PV (U1, M, M2) \
90 CREATE_DISPATCH_METHODS_T_PV (U2, M, M2) \
91 CREATE_DISPATCH_METHODS_T_PV (U4, M, M2) \
92 CREATE_DISPATCH_METHODS_T_PV (U8, M, M2) \
93 CREATE_DISPATCH_METHODS_T_PV (F, M, M2) \
94 CREATE_DISPATCH_METHODS_T_PV (D, M, M2) \
95 CREATE_DISPATCH_METHODS_T_PV (E, M, M2) \
96 CREATE_DISPATCH_METHODS_T_PV (CF, M, M2) \
97 CREATE_DISPATCH_METHODS_T_PV (CD, M, M2) \
98 CREATE_DISPATCH_METHODS_T_PV (CE, M, M2)
100 // Creates memcpy/memmove/memset methods.
101 #define CREATE_DISPATCH_METHODS_MEM() \
102 virtual void memtransfer(void *dst, const void* src, size_t size, \
103 bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) \
105 memtransfer_static(dst, src, size, may_overlap, dst_mod, src_mod); \
107 virtual void memset(void *dst, int c, size_t size, ls_modifier mod) \
109 memset_static(dst, c, size, mod); \
112 #define CREATE_DISPATCH_METHODS_MEM_PV() \
113 virtual void memtransfer(void *dst, const void* src, size_t size, \
114 bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) = 0; \
115 virtual void memset(void *dst, int c, size_t size, ls_modifier mod) = 0;
118 // Creates ABI load/store functions that can target either a class or an
119 // object.
120 #define ITM_READ(T, LSMOD, TARGET, M2) \
121 _ITM_TYPE_##T ITM_REGPARM _ITM_##LSMOD##T (const _ITM_TYPE_##T *ptr) \
123 return TARGET ITM_##LSMOD##T##M2(ptr); \
126 #define ITM_WRITE(T, LSMOD, TARGET, M2) \
127 void ITM_REGPARM _ITM_##LSMOD##T (_ITM_TYPE_##T *ptr, _ITM_TYPE_##T val) \
129 TARGET ITM_##LSMOD##T##M2(ptr, val); \
132 // Creates ABI load/store functions for all load/store modifiers for a
133 // particular type.
134 #define CREATE_DISPATCH_FUNCTIONS_T(T, TARGET, M2) \
135 ITM_READ(T, R, TARGET, M2) \
136 ITM_READ(T, RaR, TARGET, M2) \
137 ITM_READ(T, RaW, TARGET, M2) \
138 ITM_READ(T, RfW, TARGET, M2) \
139 ITM_WRITE(T, W, TARGET, M2) \
140 ITM_WRITE(T, WaR, TARGET, M2) \
141 ITM_WRITE(T, WaW, TARGET, M2)
143 // Creates ABI memcpy/memmove/memset functions.
144 #define ITM_MEMTRANSFER_DEF(TARGET, M2, NAME, READ, WRITE) \
145 void ITM_REGPARM _ITM_memcpy##NAME(void *dst, const void *src, size_t size) \
147 TARGET memtransfer##M2 (dst, src, size, \
148 false, GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ); \
150 void ITM_REGPARM _ITM_memmove##NAME(void *dst, const void *src, size_t size) \
152 TARGET memtransfer##M2 (dst, src, size, \
153 GTM::abi_dispatch::memmove_overlap_check(dst, src, size, \
154 GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ), \
155 GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ); \
158 #define ITM_MEMSET_DEF(TARGET, M2, WRITE) \
159 void ITM_REGPARM _ITM_memset##WRITE(void *dst, int c, size_t size) \
161 TARGET memset##M2 (dst, c, size, GTM::abi_dispatch::WRITE); \
165 // ??? The number of virtual methods is large (7*4 for integers, 7*6 for FP,
166 // 7*3 for vectors). Is the cache footprint so costly that we should go for
167 // a small table instead (i.e., only have two virtual load/store methods for
168 // each supported type)? Note that this doesn't affect custom code paths at
169 // all because these use only direct calls.
170 // A large cache footprint could especially decrease HTM performance (due
171 // to HTM capacity). We could add the modifier (RaR etc.) as parameter, which
172 // would give us just 4*2+6*2+3*2 functions (so we'd just need one line for
173 // the integer loads/stores), but then the modifier can be checked only at
174 // runtime.
175 // For memcpy/memmove/memset, we just have two virtual methods (memtransfer
176 // and memset).
177 #define CREATE_DISPATCH_FUNCTIONS(TARGET, M2) \
178 CREATE_DISPATCH_FUNCTIONS_T (U1, TARGET, M2) \
179 CREATE_DISPATCH_FUNCTIONS_T (U2, TARGET, M2) \
180 CREATE_DISPATCH_FUNCTIONS_T (U4, TARGET, M2) \
181 CREATE_DISPATCH_FUNCTIONS_T (U8, TARGET, M2) \
182 CREATE_DISPATCH_FUNCTIONS_T (F, TARGET, M2) \
183 CREATE_DISPATCH_FUNCTIONS_T (D, TARGET, M2) \
184 CREATE_DISPATCH_FUNCTIONS_T (E, TARGET, M2) \
185 CREATE_DISPATCH_FUNCTIONS_T (CF, TARGET, M2) \
186 CREATE_DISPATCH_FUNCTIONS_T (CD, TARGET, M2) \
187 CREATE_DISPATCH_FUNCTIONS_T (CE, TARGET, M2) \
188 ITM_MEMTRANSFER_DEF(TARGET, M2, RnWt, NONTXNAL, W) \
189 ITM_MEMTRANSFER_DEF(TARGET, M2, RnWtaR, NONTXNAL, WaR) \
190 ITM_MEMTRANSFER_DEF(TARGET, M2, RnWtaW, NONTXNAL, WaW) \
191 ITM_MEMTRANSFER_DEF(TARGET, M2, RtWn, R, NONTXNAL) \
192 ITM_MEMTRANSFER_DEF(TARGET, M2, RtWt, R, W) \
193 ITM_MEMTRANSFER_DEF(TARGET, M2, RtWtaR, R, WaR) \
194 ITM_MEMTRANSFER_DEF(TARGET, M2, RtWtaW, R, WaW) \
195 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWn, RaR, NONTXNAL) \
196 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWt, RaR, W) \
197 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWtaR, RaR, WaR) \
198 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWtaW, RaR, WaW) \
199 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWn, RaW, NONTXNAL) \
200 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWt, RaW, W) \
201 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWtaR, RaW, WaR) \
202 ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWtaW, RaW, WaW) \
203 ITM_MEMSET_DEF(TARGET, M2, W) \
204 ITM_MEMSET_DEF(TARGET, M2, WaR) \
205 ITM_MEMSET_DEF(TARGET, M2, WaW)
208 // Creates ABI load/store functions that delegate to a transactional memcpy.
209 #define ITM_READ_MEMCPY(T, LSMOD, TARGET, M2) \
210 _ITM_TYPE_##T ITM_REGPARM _ITM_##LSMOD##T (const _ITM_TYPE_##T *ptr)\
212 _ITM_TYPE_##T v; \
213 TARGET memtransfer##M2(&v, ptr, sizeof(_ITM_TYPE_##T), false, \
214 GTM::abi_dispatch::NONTXNAL, GTM::abi_dispatch::LSMOD); \
215 return v; \
218 #define ITM_WRITE_MEMCPY(T, LSMOD, TARGET, M2) \
219 void ITM_REGPARM _ITM_##LSMOD##T (_ITM_TYPE_##T *ptr, _ITM_TYPE_##T val)\
221 TARGET memtransfer##M2(ptr, &val, sizeof(_ITM_TYPE_##T), false, \
222 GTM::abi_dispatch::LSMOD, GTM::abi_dispatch::NONTXNAL); \
225 #define CREATE_DISPATCH_FUNCTIONS_T_MEMCPY(T, TARGET, M2) \
226 ITM_READ_MEMCPY(T, R, TARGET, M2) \
227 ITM_READ_MEMCPY(T, RaR, TARGET, M2) \
228 ITM_READ_MEMCPY(T, RaW, TARGET, M2) \
229 ITM_READ_MEMCPY(T, RfW, TARGET, M2) \
230 ITM_WRITE_MEMCPY(T, W, TARGET, M2) \
231 ITM_WRITE_MEMCPY(T, WaR, TARGET, M2) \
232 ITM_WRITE_MEMCPY(T, WaW, TARGET, M2)
235 namespace GTM HIDDEN {
237 struct gtm_transaction_cp;
239 struct method_group
241 // Start using a TM method from this group. This constructs required meta
242 // data on demand when this method group is actually used. Will be called
243 // either on first use or after a previous call to fini().
244 virtual void init() = 0;
245 // Stop using any method from this group for now. This can be used to
246 // destruct meta data as soon as this method group is not used anymore.
247 virtual void fini() = 0;
248 // This can be overriden to implement more light-weight re-initialization.
249 virtual void reinit()
251 fini();
252 init();
257 // This is the base interface that all TM methods have to implement.
258 struct abi_dispatch
260 public:
261 enum ls_modifier { NONTXNAL, R, RaR, RaW, RfW, W, WaR, WaW };
263 private:
264 // Disallow copies
265 abi_dispatch(const abi_dispatch &) = delete;
266 abi_dispatch& operator=(const abi_dispatch &) = delete;
268 public:
269 // Starts or restarts a transaction. Is called right before executing the
270 // transactional application code (by either returning from
271 // gtm_thread::begin_transaction or doing the longjmp when restarting).
272 // Returns NO_RESTART if the transaction started successfully. Returns
273 // a real restart reason if it couldn't start and does need to abort. This
274 // allows TM methods to just give up and delegate ensuring progress to the
275 // restart mechanism. If it returns a restart reason, this call must be
276 // idempotent because it will trigger the restart mechanism, which could
277 // switch to a different TM method.
278 virtual gtm_restart_reason begin_or_restart() = 0;
279 // Tries to commit the transaction. Iff this returns true, the transaction
280 // got committed and all per-transaction data will have been reset.
281 // Currently, this is called only for the commit of the outermost
282 // transaction, or when switching to serial mode (which can happen in a
283 // nested transaction).
284 // If privatization safety must be ensured in a quiescence-based way, set
285 // priv_time to a value different to 0. Nontransactional code will not be
286 // executed after this commit until all registered threads' shared_state is
287 // larger than or equal to this value.
288 virtual bool trycommit(gtm_word& priv_time) = 0;
289 // Rolls back a transaction. Called on abort or after trycommit() returned
290 // false.
291 virtual void rollback(gtm_transaction_cp *cp = 0) = 0;
293 // Return an alternative method that is compatible with the current
294 // method but supports closed nesting. Return zero if there is none.
295 // Note that too be compatible, it must be possible to switch to this other
296 // method on begin of a nested transaction without committing or restarting
297 // the parent method.
298 virtual abi_dispatch* closed_nesting_alternative() { return 0; }
299 // Returns true iff this method group supports the current situation.
300 // NUMBER_OF_THREADS is the current number of threads that might execute
301 // transactions.
302 virtual bool supports(unsigned number_of_threads) { return true; }
304 bool read_only () const { return m_read_only; }
305 bool write_through() const { return m_write_through; }
306 bool can_run_uninstrumented_code() const
308 return m_can_run_uninstrumented_code;
310 // Returns true iff this TM method supports closed nesting.
311 bool closed_nesting() const { return m_closed_nesting; }
312 method_group* get_method_group() const { return m_method_group; }
314 static void *operator new(size_t s) { return xmalloc (s); }
315 static void operator delete(void *p) { free (p); }
317 public:
318 static bool memmove_overlap_check(void *dst, const void *src, size_t size,
319 ls_modifier dst_mod, ls_modifier src_mod);
321 // Creates the ABI dispatch methods for loads and stores.
322 // ??? Should the dispatch table instead be embedded in the dispatch object
323 // to avoid the indirect lookup in the vtable?
324 CREATE_DISPATCH_METHODS_PV(virtual, )
325 // Creates the ABI dispatch methods for memcpy/memmove/memset.
326 CREATE_DISPATCH_METHODS_MEM_PV()
328 protected:
329 const bool m_read_only;
330 const bool m_write_through;
331 const bool m_can_run_uninstrumented_code;
332 const bool m_closed_nesting;
333 method_group* const m_method_group;
334 abi_dispatch(bool ro, bool wt, bool uninstrumented, bool closed_nesting,
335 method_group* mg) :
336 m_read_only(ro), m_write_through(wt),
337 m_can_run_uninstrumented_code(uninstrumented),
338 m_closed_nesting(closed_nesting), m_method_group(mg)
344 #endif // DISPATCH_H