method-serial.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* Copyright (C) 2008-2022 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 bool snapshot_most_recent() { return true; }
  80. virtual abi_dispatch* closed_nesting_alternative()
  81. {
  82. // For nested transactions with an instrumented code path, we can do
  83. // undo logging.
  84. return GTM::dispatch_serial();
  85. }
  86. };
  87. class serial_dispatch : public abi_dispatch
  88. {
  89. protected:
  90. static void log(const void *addr, size_t len)
  91. {
  92. gtm_thread *tx = gtm_thr();
  93. tx->undolog.log(addr, len);
  94. }
  95. template <typename V> static V load(const V* addr, ls_modifier mod)
  96. {
  97. return *addr;
  98. }
  99. template <typename V> static void store(V* addr, const V value,
  100. ls_modifier mod)
  101. {
  102. if (mod != WaW)
  103. log(addr, sizeof(V));
  104. *addr = value;
  105. }
  106. public:
  107. static void memtransfer_static(void *dst, const void* src, size_t size,
  108. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod)
  109. {
  110. if (dst_mod != WaW && dst_mod != NONTXNAL)
  111. log(dst, size);
  112. if (!may_overlap)
  113. ::memcpy(dst, src, size);
  114. else
  115. ::memmove(dst, src, size);
  116. }
  117. static void memset_static(void *dst, int c, size_t size, ls_modifier mod)
  118. {
  119. if (mod != WaW)
  120. log(dst, size);
  121. ::memset(dst, c, size);
  122. }
  123. virtual gtm_restart_reason begin_or_restart() { return NO_RESTART; }
  124. virtual bool trycommit(gtm_word& priv_time) { return true; }
  125. // Local undo will handle this.
  126. // trydropreference() need not be changed either.
  127. virtual void rollback(gtm_transaction_cp *cp) { }
  128. virtual bool snapshot_most_recent() { return true; }
  129. CREATE_DISPATCH_METHODS(virtual, )
  130. CREATE_DISPATCH_METHODS_MEM()
  131. serial_dispatch() : abi_dispatch(false, true, false, true,
  132. gtm_thread::STATE_SERIAL, &o_serial_mg)
  133. { }
  134. };
  135. // Like serialirr_dispatch but does not requests serial-irrevocable mode until
  136. // the first write in the transaction. Can be useful for read-mostly workloads
  137. // and testing, but is likely too simple to be of general purpose.
  138. class serialirr_onwrite_dispatch : public serialirr_dispatch
  139. {
  140. public:
  141. serialirr_onwrite_dispatch() :
  142. serialirr_dispatch(false, true, false, false, 0, &o_serial_mg) { }
  143. protected:
  144. static void pre_write()
  145. {
  146. gtm_thread *tx = gtm_thr();
  147. if (!(tx->state & (gtm_thread::STATE_SERIAL
  148. | gtm_thread::STATE_IRREVOCABLE)))
  149. tx->serialirr_mode();
  150. }
  151. // Transactional loads access memory directly.
  152. // Transactional stores switch to serial mode first.
  153. template <typename V> static void store(V* addr, const V value,
  154. ls_modifier mod)
  155. {
  156. pre_write();
  157. serialirr_dispatch::store(addr, value, mod);
  158. }
  159. public:
  160. static void memtransfer_static(void *dst, const void* src, size_t size,
  161. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod)
  162. {
  163. pre_write();
  164. serialirr_dispatch::memtransfer_static(dst, src, size, may_overlap,
  165. dst_mod, src_mod);
  166. }
  167. static void memset_static(void *dst, int c, size_t size, ls_modifier mod)
  168. {
  169. pre_write();
  170. serialirr_dispatch::memset_static(dst, c, size, mod);
  171. }
  172. CREATE_DISPATCH_METHODS(virtual, )
  173. CREATE_DISPATCH_METHODS_MEM()
  174. virtual void rollback(gtm_transaction_cp *cp)
  175. {
  176. gtm_thread *tx = gtm_thr();
  177. if (tx->state & gtm_thread::STATE_IRREVOCABLE)
  178. abort();
  179. }
  180. virtual bool snapshot_most_recent() { return true; }
  181. };
  182. // This group is pure HTM with serial mode as a fallback. There is no
  183. // difference to serial_mg except that we need to enable or disable the HTM
  184. // fastpath. See gtm_thread::begin_transaction.
  185. struct htm_mg : public method_group
  186. {
  187. virtual void init()
  188. {
  189. // Enable the HTM fastpath if the HW is available. The fastpath is
  190. // initially disabled.
  191. #ifdef USE_HTM_FASTPATH
  192. gtm_thread::serial_lock.set_htm_fastpath(htm_init());
  193. #endif
  194. }
  195. virtual void fini()
  196. {
  197. // Disable the HTM fastpath.
  198. gtm_thread::serial_lock.set_htm_fastpath(0);
  199. }
  200. };
  201. static htm_mg o_htm_mg;
  202. // We just need the subclass to associate it with the HTM method group that
  203. // sets up the HTM fast path. This will use serial_dispatch as fallback for
  204. // transactions that might get canceled; it has a different method group, but
  205. // this is harmless for serial dispatchs because they never abort.
  206. class htm_dispatch : public serialirr_dispatch
  207. {
  208. public:
  209. htm_dispatch() : serialirr_dispatch(false, true, false, false,
  210. gtm_thread::STATE_SERIAL | gtm_thread::STATE_IRREVOCABLE, &o_htm_mg)
  211. { }
  212. };
  213. } // anon namespace
  214. static const serialirr_dispatch o_serialirr_dispatch;
  215. static const serial_dispatch o_serial_dispatch;
  216. static const serialirr_onwrite_dispatch o_serialirr_onwrite_dispatch;
  217. static const htm_dispatch o_htm_dispatch;
  218. abi_dispatch *
  219. GTM::dispatch_serialirr ()
  220. {
  221. return const_cast<serialirr_dispatch *>(&o_serialirr_dispatch);
  222. }
  223. abi_dispatch *
  224. GTM::dispatch_serial ()
  225. {
  226. return const_cast<serial_dispatch *>(&o_serial_dispatch);
  227. }
  228. abi_dispatch *
  229. GTM::dispatch_serialirr_onwrite ()
  230. {
  231. return
  232. const_cast<serialirr_onwrite_dispatch *>(&o_serialirr_onwrite_dispatch);
  233. }
  234. abi_dispatch *
  235. GTM::dispatch_htm ()
  236. {
  237. return const_cast<htm_dispatch *>(&o_htm_dispatch);
  238. }
  239. // Put the transaction into serial-irrevocable mode.
  240. void
  241. GTM::gtm_thread::serialirr_mode ()
  242. {
  243. struct abi_dispatch *disp = abi_disp ();
  244. #if defined(USE_HTM_FASTPATH)
  245. // HTM fastpath. If we are executing a HW transaction, don't go serial but
  246. // continue. See gtm_thread::begin_transaction.
  247. if (likely(!gtm_thread::serial_lock.htm_fastpath_disabled()))
  248. return;
  249. #endif
  250. if (this->state & STATE_SERIAL)
  251. {
  252. if (this->state & STATE_IRREVOCABLE)
  253. return;
  254. // Try to commit the dispatch-specific part of the transaction, as we
  255. // would do for an outermost commit.
  256. // We're already serial, so we don't need to ensure privatization safety
  257. // for other transactions here.
  258. gtm_word priv_time = 0;
  259. bool ok __attribute__((unused)) = disp->trycommit (priv_time);
  260. // Given that we're already serial, the trycommit better work.
  261. assert (ok);
  262. }
  263. else if (serial_lock.write_upgrade (this))
  264. {
  265. this->state |= STATE_SERIAL;
  266. // Try to commit the dispatch-specific part of the transaction, as we
  267. // would do for an outermost commit.
  268. // We have successfully upgraded to serial mode, so we don't need to
  269. // ensure privatization safety for other transactions here.
  270. // However, we are still a reader (wrt. privatization safety) until we
  271. // have either committed or restarted, so finish the upgrade after that.
  272. gtm_word priv_time = 0;
  273. if (!disp->trycommit (priv_time))
  274. restart (RESTART_SERIAL_IRR, true);
  275. gtm_thread::serial_lock.write_upgrade_finish(this);
  276. }
  277. else
  278. restart (RESTART_SERIAL_IRR, false);
  279. this->state |= (STATE_SERIAL | STATE_IRREVOCABLE);
  280. set_abi_disp (dispatch_serialirr ());
  281. }
  282. void ITM_REGPARM
  283. _ITM_changeTransactionMode (_ITM_transactionState state)
  284. {
  285. assert (state == modeSerialIrrevocable);
  286. gtm_thr()->serialirr_mode ();
  287. }