1 /* Copyright (C) 2008, 2009, 2011, 2012 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@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
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/>. */
27 // Avoid a dependency on libstdc++ for the pure virtuals in abi_dispatch.
28 extern "C" void HIDDEN
38 // This group consists of the serial, serialirr, and serialirr_onwrite
39 // methods, which all need no global state (except what is already provided
40 // by the serial mode implementation).
41 struct serial_mg
: public method_group
43 virtual void init() { }
44 virtual void fini() { }
47 static serial_mg o_serial_mg
;
50 class serialirr_dispatch
: public abi_dispatch
53 serialirr_dispatch() : abi_dispatch(false, true, true, false,
54 gtm_thread::STATE_SERIAL
| gtm_thread::STATE_IRREVOCABLE
, &o_serial_mg
)
58 serialirr_dispatch(bool ro
, bool wt
, bool uninstrumented
,
59 bool closed_nesting
, uint32_t requires_serial
, method_group
* mg
) :
60 abi_dispatch(ro
, wt
, uninstrumented
, closed_nesting
, requires_serial
, mg
)
63 // Transactional loads and stores simply access memory directly.
64 // These methods are static to avoid indirect calls, and will be used by the
65 // virtual ABI dispatch methods or by static direct-access methods created
67 template <typename V
> static V
load(const V
* addr
, ls_modifier mod
)
71 template <typename V
> static void store(V
* addr
, const V value
,
78 static void memtransfer_static(void *dst
, const void* src
, size_t size
,
79 bool may_overlap
, ls_modifier dst_mod
, ls_modifier src_mod
)
82 ::memcpy(dst
, src
, size
);
84 ::memmove(dst
, src
, size
);
87 static void memset_static(void *dst
, int c
, size_t size
, ls_modifier mod
)
89 ::memset(dst
, c
, size
);
92 CREATE_DISPATCH_METHODS(virtual, )
93 CREATE_DISPATCH_METHODS_MEM()
95 virtual gtm_restart_reason
begin_or_restart() { return NO_RESTART
; }
96 virtual bool trycommit(gtm_word
& priv_time
) { return true; }
97 virtual void rollback(gtm_transaction_cp
*cp
) { abort(); }
99 virtual abi_dispatch
* closed_nesting_alternative()
101 // For nested transactions with an instrumented code path, we can do
103 return GTM::dispatch_serial();
107 class serial_dispatch
: public abi_dispatch
110 static void log(const void *addr
, size_t len
)
112 gtm_thread
*tx
= gtm_thr();
113 tx
->undolog
.log(addr
, len
);
116 template <typename V
> static V
load(const V
* addr
, ls_modifier mod
)
120 template <typename V
> static void store(V
* addr
, const V value
,
124 log(addr
, sizeof(V
));
129 static void memtransfer_static(void *dst
, const void* src
, size_t size
,
130 bool may_overlap
, ls_modifier dst_mod
, ls_modifier src_mod
)
132 if (dst_mod
!= WaW
&& dst_mod
!= NONTXNAL
)
135 ::memcpy(dst
, src
, size
);
137 ::memmove(dst
, src
, size
);
140 static void memset_static(void *dst
, int c
, size_t size
, ls_modifier mod
)
144 ::memset(dst
, c
, size
);
147 virtual gtm_restart_reason
begin_or_restart() { return NO_RESTART
; }
148 virtual bool trycommit(gtm_word
& priv_time
) { return true; }
149 // Local undo will handle this.
150 // trydropreference() need not be changed either.
151 virtual void rollback(gtm_transaction_cp
*cp
) { }
153 CREATE_DISPATCH_METHODS(virtual, )
154 CREATE_DISPATCH_METHODS_MEM()
156 serial_dispatch() : abi_dispatch(false, true, false, true,
157 gtm_thread::STATE_SERIAL
, &o_serial_mg
)
162 // Like serialirr_dispatch but does not requests serial-irrevocable mode until
163 // the first write in the transaction. Can be useful for read-mostly workloads
164 // and testing, but is likely too simple to be of general purpose.
165 class serialirr_onwrite_dispatch
: public serialirr_dispatch
168 serialirr_onwrite_dispatch() :
169 serialirr_dispatch(false, true, false, false, 0, &o_serial_mg
) { }
172 static void pre_write()
174 gtm_thread
*tx
= gtm_thr();
175 if (!(tx
->state
& (gtm_thread::STATE_SERIAL
176 | gtm_thread::STATE_IRREVOCABLE
)))
177 tx
->serialirr_mode();
180 // Transactional loads access memory directly.
181 // Transactional stores switch to serial mode first.
182 template <typename V
> static void store(V
* addr
, const V value
,
186 serialirr_dispatch::store(addr
, value
, mod
);
190 static void memtransfer_static(void *dst
, const void* src
, size_t size
,
191 bool may_overlap
, ls_modifier dst_mod
, ls_modifier src_mod
)
194 serialirr_dispatch::memtransfer_static(dst
, src
, size
, may_overlap
,
198 static void memset_static(void *dst
, int c
, size_t size
, ls_modifier mod
)
201 serialirr_dispatch::memset_static(dst
, c
, size
, mod
);
204 CREATE_DISPATCH_METHODS(virtual, )
205 CREATE_DISPATCH_METHODS_MEM()
207 virtual void rollback(gtm_transaction_cp
*cp
)
209 gtm_thread
*tx
= gtm_thr();
210 if (tx
->state
& gtm_thread::STATE_IRREVOCABLE
)
217 static const serialirr_dispatch o_serialirr_dispatch
;
218 static const serial_dispatch o_serial_dispatch
;
219 static const serialirr_onwrite_dispatch o_serialirr_onwrite_dispatch
;
222 GTM::dispatch_serialirr ()
224 return const_cast<serialirr_dispatch
*>(&o_serialirr_dispatch
);
228 GTM::dispatch_serial ()
230 return const_cast<serial_dispatch
*>(&o_serial_dispatch
);
234 GTM::dispatch_serialirr_onwrite ()
237 const_cast<serialirr_onwrite_dispatch
*>(&o_serialirr_onwrite_dispatch
);
240 // Put the transaction into serial-irrevocable mode.
243 GTM::gtm_thread::serialirr_mode ()
245 struct abi_dispatch
*disp
= abi_disp ();
247 if (this->state
& STATE_SERIAL
)
249 if (this->state
& STATE_IRREVOCABLE
)
252 // Try to commit the dispatch-specific part of the transaction, as we
253 // would do for an outermost commit.
254 // We're already serial, so we don't need to ensure privatization safety
255 // for other transactions here.
256 gtm_word priv_time
= 0;
257 bool ok
= disp
->trycommit (priv_time
);
258 // Given that we're already serial, the trycommit better work.
261 else if (serial_lock
.write_upgrade (this))
263 this->state
|= STATE_SERIAL
;
264 // Try to commit the dispatch-specific part of the transaction, as we
265 // would do for an outermost commit.
266 // We have successfully upgraded to serial mode, so we don't need to
267 // ensure privatization safety for other transactions here.
268 // However, we are still a reader (wrt. privatization safety) until we
269 // have either committed or restarted, so finish the upgrade after that.
270 gtm_word priv_time
= 0;
271 if (!disp
->trycommit (priv_time
))
272 restart (RESTART_SERIAL_IRR
, true);
273 gtm_thread::serial_lock
.write_upgrade_finish(this);
276 restart (RESTART_SERIAL_IRR
, false);
278 this->state
|= (STATE_SERIAL
| STATE_IRREVOCABLE
);
279 set_abi_disp (dispatch_serialirr ());
283 _ITM_changeTransactionMode (_ITM_transactionState state
)
285 assert (state
== modeSerialIrrevocable
);
286 gtm_thr()->serialirr_mode ();