method-serial.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright (C) 2008-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libitm_i.h"
  20. // Avoid a dependency on libstdc++ for the pure virtuals in abi_dispatch.
  21. extern "C" void HIDDEN
  22. __cxa_pure_virtual ()
  23. {
  24. abort ();
  25. }
  26. using namespace GTM;
  27. namespace {
  28. // This group consists of the serial, serialirr, and serialirr_onwrite
  29. // methods, which all need no global state (except what is already provided
  30. // by the serial mode implementation).
  31. struct serial_mg : public method_group
  32. {
  33. virtual void init() { }
  34. virtual void fini() { }
  35. };
  36. static serial_mg o_serial_mg;
  37. class serialirr_dispatch : public abi_dispatch
  38. {
  39. public:
  40. serialirr_dispatch() : abi_dispatch(false, true, true, false,
  41. gtm_thread::STATE_SERIAL | gtm_thread::STATE_IRREVOCABLE, &o_serial_mg)
  42. { }
  43. protected:
  44. serialirr_dispatch(bool ro, bool wt, bool uninstrumented,
  45. bool closed_nesting, uint32_t requires_serial, method_group* mg) :
  46. abi_dispatch(ro, wt, uninstrumented, closed_nesting, requires_serial, mg)
  47. { }
  48. // Transactional loads and stores simply access memory directly.
  49. // These methods are static to avoid indirect calls, and will be used by the
  50. // virtual ABI dispatch methods or by static direct-access methods created
  51. // below.
  52. template <typename V> static V load(const V* addr, ls_modifier mod)
  53. {
  54. return *addr;
  55. }
  56. template <typename V> static void store(V* addr, const V value,
  57. ls_modifier mod)
  58. {
  59. *addr = value;
  60. }
  61. public:
  62. static void memtransfer_static(void *dst, const void* src, size_t size,
  63. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod)
  64. {
  65. if (!may_overlap)
  66. ::memcpy(dst, src, size);
  67. else
  68. ::memmove(dst, src, size);
  69. }
  70. static void memset_static(void *dst, int c, size_t size, ls_modifier mod)
  71. {
  72. ::memset(dst, c, size);
  73. }
  74. CREATE_DISPATCH_METHODS(virtual, )
  75. CREATE_DISPATCH_METHODS_MEM()
  76. virtual gtm_restart_reason begin_or_restart() { return NO_RESTART; }
  77. virtual bool trycommit(gtm_word& priv_time) { return true; }
  78. virtual void rollback(gtm_transaction_cp *cp) { abort(); }
  79. virtual abi_dispatch* closed_nesting_alternative()
  80. {
  81. // For nested transactions with an instrumented code path, we can do
  82. // undo logging.
  83. return GTM::dispatch_serial();
  84. }
  85. };
  86. class serial_dispatch : public abi_dispatch
  87. {
  88. protected:
  89. static void log(const void *addr, size_t len)
  90. {
  91. gtm_thread *tx = gtm_thr();
  92. tx->undolog.log(addr, len);
  93. }
  94. template <typename V> static V load(const V* addr, ls_modifier mod)
  95. {
  96. return *addr;
  97. }
  98. template <typename V> static void store(V* addr, const V value,
  99. ls_modifier mod)
  100. {
  101. if (mod != WaW)
  102. log(addr, sizeof(V));
  103. *addr = value;
  104. }
  105. public:
  106. static void memtransfer_static(void *dst, const void* src, size_t size,
  107. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod)
  108. {
  109. if (dst_mod != WaW && dst_mod != NONTXNAL)
  110. log(dst, size);
  111. if (!may_overlap)
  112. ::memcpy(dst, src, size);
  113. else
  114. ::memmove(dst, src, size);
  115. }
  116. static void memset_static(void *dst, int c, size_t size, ls_modifier mod)
  117. {
  118. if (mod != WaW)
  119. log(dst, size);
  120. ::memset(dst, c, size);
  121. }
  122. virtual gtm_restart_reason begin_or_restart() { return NO_RESTART; }
  123. virtual bool trycommit(gtm_word& priv_time) { return true; }
  124. // Local undo will handle this.
  125. // trydropreference() need not be changed either.
  126. virtual void rollback(gtm_transaction_cp *cp) { }
  127. CREATE_DISPATCH_METHODS(virtual, )
  128. CREATE_DISPATCH_METHODS_MEM()
  129. serial_dispatch() : abi_dispatch(false, true, false, true,
  130. gtm_thread::STATE_SERIAL, &o_serial_mg)
  131. { }
  132. };
  133. // Like serialirr_dispatch but does not requests serial-irrevocable mode until
  134. // the first write in the transaction. Can be useful for read-mostly workloads
  135. // and testing, but is likely too simple to be of general purpose.
  136. class serialirr_onwrite_dispatch : public serialirr_dispatch
  137. {
  138. public:
  139. serialirr_onwrite_dispatch() :
  140. serialirr_dispatch(false, true, false, false, 0, &o_serial_mg) { }
  141. protected:
  142. static void pre_write()
  143. {
  144. gtm_thread *tx = gtm_thr();
  145. if (!(tx->state & (gtm_thread::STATE_SERIAL
  146. | gtm_thread::STATE_IRREVOCABLE)))
  147. tx->serialirr_mode();
  148. }
  149. // Transactional loads access memory directly.
  150. // Transactional stores switch to serial mode first.
  151. template <typename V> static void store(V* addr, const V value,
  152. ls_modifier mod)
  153. {
  154. pre_write();
  155. serialirr_dispatch::store(addr, value, mod);
  156. }
  157. public:
  158. static void memtransfer_static(void *dst, const void* src, size_t size,
  159. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod)
  160. {
  161. pre_write();
  162. serialirr_dispatch::memtransfer_static(dst, src, size, may_overlap,
  163. dst_mod, src_mod);
  164. }
  165. static void memset_static(void *dst, int c, size_t size, ls_modifier mod)
  166. {
  167. pre_write();
  168. serialirr_dispatch::memset_static(dst, c, size, mod);
  169. }
  170. CREATE_DISPATCH_METHODS(virtual, )
  171. CREATE_DISPATCH_METHODS_MEM()
  172. virtual void rollback(gtm_transaction_cp *cp)
  173. {
  174. gtm_thread *tx = gtm_thr();
  175. if (tx->state & gtm_thread::STATE_IRREVOCABLE)
  176. abort();
  177. }
  178. };
  179. // This group is pure HTM with serial mode as a fallback. There is no
  180. // difference to serial_mg except that we need to enable or disable the HTM
  181. // fastpath. See gtm_thread::begin_transaction.
  182. struct htm_mg : public method_group
  183. {
  184. virtual void init()
  185. {
  186. // Enable the HTM fastpath if the HW is available. The fastpath is
  187. // initially disabled.
  188. #ifdef USE_HTM_FASTPATH
  189. htm_fastpath = htm_init();
  190. #endif
  191. }
  192. virtual void fini()
  193. {
  194. // Disable the HTM fastpath.
  195. htm_fastpath = 0;
  196. }
  197. };
  198. static htm_mg o_htm_mg;
  199. // We just need the subclass to associate it with the HTM method group that
  200. // sets up the HTM fast path. This will use serial_dispatch as fallback for
  201. // transactions that might get canceled; it has a different method group, but
  202. // this is harmless for serial dispatchs because they never abort.
  203. class htm_dispatch : public serialirr_dispatch
  204. {
  205. public:
  206. htm_dispatch() : serialirr_dispatch(false, true, false, false,
  207. gtm_thread::STATE_SERIAL | gtm_thread::STATE_IRREVOCABLE, &o_htm_mg)
  208. { }
  209. };
  210. } // anon namespace
  211. static const serialirr_dispatch o_serialirr_dispatch;
  212. static const serial_dispatch o_serial_dispatch;
  213. static const serialirr_onwrite_dispatch o_serialirr_onwrite_dispatch;
  214. static const htm_dispatch o_htm_dispatch;
  215. abi_dispatch *
  216. GTM::dispatch_serialirr ()
  217. {
  218. return const_cast<serialirr_dispatch *>(&o_serialirr_dispatch);
  219. }
  220. abi_dispatch *
  221. GTM::dispatch_serial ()
  222. {
  223. return const_cast<serial_dispatch *>(&o_serial_dispatch);
  224. }
  225. abi_dispatch *
  226. GTM::dispatch_serialirr_onwrite ()
  227. {
  228. return
  229. const_cast<serialirr_onwrite_dispatch *>(&o_serialirr_onwrite_dispatch);
  230. }
  231. abi_dispatch *
  232. GTM::dispatch_htm ()
  233. {
  234. return const_cast<htm_dispatch *>(&o_htm_dispatch);
  235. }
  236. // Put the transaction into serial-irrevocable mode.
  237. void
  238. GTM::gtm_thread::serialirr_mode ()
  239. {
  240. struct abi_dispatch *disp = abi_disp ();
  241. #if defined(USE_HTM_FASTPATH)
  242. // HTM fastpath. If we are executing a HW transaction, don't go serial but
  243. // continue. See gtm_thread::begin_transaction.
  244. if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
  245. return;
  246. #endif
  247. if (this->state & STATE_SERIAL)
  248. {
  249. if (this->state & STATE_IRREVOCABLE)
  250. return;
  251. // Try to commit the dispatch-specific part of the transaction, as we
  252. // would do for an outermost commit.
  253. // We're already serial, so we don't need to ensure privatization safety
  254. // for other transactions here.
  255. gtm_word priv_time = 0;
  256. bool ok = disp->trycommit (priv_time);
  257. // Given that we're already serial, the trycommit better work.
  258. assert (ok);
  259. }
  260. else if (serial_lock.write_upgrade (this))
  261. {
  262. this->state |= STATE_SERIAL;
  263. // Try to commit the dispatch-specific part of the transaction, as we
  264. // would do for an outermost commit.
  265. // We have successfully upgraded to serial mode, so we don't need to
  266. // ensure privatization safety for other transactions here.
  267. // However, we are still a reader (wrt. privatization safety) until we
  268. // have either committed or restarted, so finish the upgrade after that.
  269. gtm_word priv_time = 0;
  270. if (!disp->trycommit (priv_time))
  271. restart (RESTART_SERIAL_IRR, true);
  272. gtm_thread::serial_lock.write_upgrade_finish(this);
  273. }
  274. else
  275. restart (RESTART_SERIAL_IRR, false);
  276. this->state |= (STATE_SERIAL | STATE_IRREVOCABLE);
  277. set_abi_disp (dispatch_serialirr ());
  278. }
  279. void ITM_REGPARM
  280. _ITM_changeTransactionMode (_ITM_transactionState state)
  281. {
  282. assert (state == modeSerialIrrevocable);
  283. gtm_thr()->serialirr_mode ();
  284. }