dispatch.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* Copyright (C) 2011-2022 Free Software Foundation, Inc.
  2. Contributed by Torvald Riegel <triegel@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. #ifndef DISPATCH_H
  20. #define DISPATCH_H 1
  21. #include "libitm.h"
  22. #include "common.h"
  23. // Creates ABI load/store methods (can be made virtual or static using M,
  24. // use M2 to create separate methods names for virtual and static)
  25. // The _PV variants are for the pure-virtual methods in the base class.
  26. #define ITM_READ_M(T, LSMOD, M, M2) \
  27. M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
  28. { \
  29. return load(ptr, abi_dispatch::LSMOD); \
  30. }
  31. #define ITM_READ_M_PV(T, LSMOD, M, M2) \
  32. M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
  33. = 0;
  34. #define ITM_WRITE_M(T, LSMOD, M, M2) \
  35. M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
  36. _ITM_TYPE_##T val) \
  37. { \
  38. store(ptr, val, abi_dispatch::LSMOD); \
  39. }
  40. #define ITM_WRITE_M_PV(T, LSMOD, M, M2) \
  41. M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
  42. _ITM_TYPE_##T val) \
  43. = 0;
  44. // Creates ABI load/store methods for all load/store modifiers for a particular
  45. // type.
  46. #define CREATE_DISPATCH_METHODS_T(T, M, M2) \
  47. ITM_READ_M(T, R, M, M2) \
  48. ITM_READ_M(T, RaR, M, M2) \
  49. ITM_READ_M(T, RaW, M, M2) \
  50. ITM_READ_M(T, RfW, M, M2) \
  51. ITM_WRITE_M(T, W, M, M2) \
  52. ITM_WRITE_M(T, WaR, M, M2) \
  53. ITM_WRITE_M(T, WaW, M, M2)
  54. #define CREATE_DISPATCH_METHODS_T_PV(T, M, M2) \
  55. ITM_READ_M_PV(T, R, M, M2) \
  56. ITM_READ_M_PV(T, RaR, M, M2) \
  57. ITM_READ_M_PV(T, RaW, M, M2) \
  58. ITM_READ_M_PV(T, RfW, M, M2) \
  59. ITM_WRITE_M_PV(T, W, M, M2) \
  60. ITM_WRITE_M_PV(T, WaR, M, M2) \
  61. ITM_WRITE_M_PV(T, WaW, M, M2)
  62. // Creates ABI load/store methods for all types.
  63. // See CREATE_DISPATCH_FUNCTIONS for comments.
  64. #define CREATE_DISPATCH_METHODS(M, M2) \
  65. CREATE_DISPATCH_METHODS_T (U1, M, M2) \
  66. CREATE_DISPATCH_METHODS_T (U2, M, M2) \
  67. CREATE_DISPATCH_METHODS_T (U4, M, M2) \
  68. CREATE_DISPATCH_METHODS_T (U8, M, M2) \
  69. CREATE_DISPATCH_METHODS_T (F, M, M2) \
  70. CREATE_DISPATCH_METHODS_T (D, M, M2) \
  71. CREATE_DISPATCH_METHODS_T (E, M, M2) \
  72. CREATE_DISPATCH_METHODS_T (CF, M, M2) \
  73. CREATE_DISPATCH_METHODS_T (CD, M, M2) \
  74. CREATE_DISPATCH_METHODS_T (CE, M, M2)
  75. #define CREATE_DISPATCH_METHODS_PV(M, M2) \
  76. CREATE_DISPATCH_METHODS_T_PV (U1, M, M2) \
  77. CREATE_DISPATCH_METHODS_T_PV (U2, M, M2) \
  78. CREATE_DISPATCH_METHODS_T_PV (U4, M, M2) \
  79. CREATE_DISPATCH_METHODS_T_PV (U8, M, M2) \
  80. CREATE_DISPATCH_METHODS_T_PV (F, M, M2) \
  81. CREATE_DISPATCH_METHODS_T_PV (D, M, M2) \
  82. CREATE_DISPATCH_METHODS_T_PV (E, M, M2) \
  83. CREATE_DISPATCH_METHODS_T_PV (CF, M, M2) \
  84. CREATE_DISPATCH_METHODS_T_PV (CD, M, M2) \
  85. CREATE_DISPATCH_METHODS_T_PV (CE, M, M2)
  86. // Creates memcpy/memmove/memset methods.
  87. #define CREATE_DISPATCH_METHODS_MEM() \
  88. virtual void memtransfer(void *dst, const void* src, size_t size, \
  89. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) \
  90. { \
  91. if (size > 0) \
  92. memtransfer_static(dst, src, size, may_overlap, dst_mod, src_mod); \
  93. } \
  94. virtual void memset(void *dst, int c, size_t size, ls_modifier mod) \
  95. { \
  96. if (size > 0) \
  97. memset_static(dst, c, size, mod); \
  98. }
  99. #define CREATE_DISPATCH_METHODS_MEM_PV() \
  100. virtual void memtransfer(void *dst, const void* src, size_t size, \
  101. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) = 0; \
  102. virtual void memset(void *dst, int c, size_t size, ls_modifier mod) = 0;
  103. // Creates ABI load/store functions that can target either a class or an
  104. // object.
  105. #define ITM_READ(T, LSMOD, TARGET, M2) \
  106. _ITM_TYPE_##T ITM_REGPARM _ITM_##LSMOD##T (const _ITM_TYPE_##T *ptr) \
  107. { \
  108. return TARGET ITM_##LSMOD##T##M2(ptr); \
  109. }
  110. #define ITM_WRITE(T, LSMOD, TARGET, M2) \
  111. void ITM_REGPARM _ITM_##LSMOD##T (_ITM_TYPE_##T *ptr, _ITM_TYPE_##T val) \
  112. { \
  113. TARGET ITM_##LSMOD##T##M2(ptr, val); \
  114. }
  115. // Creates ABI load/store functions for all load/store modifiers for a
  116. // particular type.
  117. #define CREATE_DISPATCH_FUNCTIONS_T(T, TARGET, M2) \
  118. ITM_READ(T, R, TARGET, M2) \
  119. ITM_READ(T, RaR, TARGET, M2) \
  120. ITM_READ(T, RaW, TARGET, M2) \
  121. ITM_READ(T, RfW, TARGET, M2) \
  122. ITM_WRITE(T, W, TARGET, M2) \
  123. ITM_WRITE(T, WaR, TARGET, M2) \
  124. ITM_WRITE(T, WaW, TARGET, M2)
  125. // Creates ABI memcpy/memmove/memset functions.
  126. #define ITM_MEMTRANSFER_DEF(TARGET, M2, NAME, READ, WRITE) \
  127. void ITM_REGPARM _ITM_memcpy##NAME(void *dst, const void *src, size_t size) \
  128. { \
  129. TARGET memtransfer##M2 (dst, src, size, \
  130. false, GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ); \
  131. } \
  132. void ITM_REGPARM _ITM_memmove##NAME(void *dst, const void *src, size_t size) \
  133. { \
  134. TARGET memtransfer##M2 (dst, src, size, \
  135. GTM::abi_dispatch::memmove_overlap_check(dst, src, size, \
  136. GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ), \
  137. GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ); \
  138. }
  139. #define ITM_MEMSET_DEF(TARGET, M2, WRITE) \
  140. void ITM_REGPARM _ITM_memset##WRITE(void *dst, int c, size_t size) \
  141. { \
  142. TARGET memset##M2 (dst, c, size, GTM::abi_dispatch::WRITE); \
  143. } \
  144. // ??? The number of virtual methods is large (7*4 for integers, 7*6 for FP,
  145. // 7*3 for vectors). Is the cache footprint so costly that we should go for
  146. // a small table instead (i.e., only have two virtual load/store methods for
  147. // each supported type)? Note that this doesn't affect custom code paths at
  148. // all because these use only direct calls.
  149. // A large cache footprint could especially decrease HTM performance (due
  150. // to HTM capacity). We could add the modifier (RaR etc.) as parameter, which
  151. // would give us just 4*2+6*2+3*2 functions (so we'd just need one line for
  152. // the integer loads/stores), but then the modifier can be checked only at
  153. // runtime.
  154. // For memcpy/memmove/memset, we just have two virtual methods (memtransfer
  155. // and memset).
  156. #define CREATE_DISPATCH_FUNCTIONS(TARGET, M2) \
  157. CREATE_DISPATCH_FUNCTIONS_T (U1, TARGET, M2) \
  158. CREATE_DISPATCH_FUNCTIONS_T (U2, TARGET, M2) \
  159. CREATE_DISPATCH_FUNCTIONS_T (U4, TARGET, M2) \
  160. CREATE_DISPATCH_FUNCTIONS_T (U8, TARGET, M2) \
  161. CREATE_DISPATCH_FUNCTIONS_T (F, TARGET, M2) \
  162. CREATE_DISPATCH_FUNCTIONS_T (D, TARGET, M2) \
  163. CREATE_DISPATCH_FUNCTIONS_T (E, TARGET, M2) \
  164. CREATE_DISPATCH_FUNCTIONS_T (CF, TARGET, M2) \
  165. CREATE_DISPATCH_FUNCTIONS_T (CD, TARGET, M2) \
  166. CREATE_DISPATCH_FUNCTIONS_T (CE, TARGET, M2) \
  167. ITM_MEMTRANSFER_DEF(TARGET, M2, RnWt, NONTXNAL, W) \
  168. ITM_MEMTRANSFER_DEF(TARGET, M2, RnWtaR, NONTXNAL, WaR) \
  169. ITM_MEMTRANSFER_DEF(TARGET, M2, RnWtaW, NONTXNAL, WaW) \
  170. ITM_MEMTRANSFER_DEF(TARGET, M2, RtWn, R, NONTXNAL) \
  171. ITM_MEMTRANSFER_DEF(TARGET, M2, RtWt, R, W) \
  172. ITM_MEMTRANSFER_DEF(TARGET, M2, RtWtaR, R, WaR) \
  173. ITM_MEMTRANSFER_DEF(TARGET, M2, RtWtaW, R, WaW) \
  174. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWn, RaR, NONTXNAL) \
  175. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWt, RaR, W) \
  176. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWtaR, RaR, WaR) \
  177. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWtaW, RaR, WaW) \
  178. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWn, RaW, NONTXNAL) \
  179. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWt, RaW, W) \
  180. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWtaR, RaW, WaR) \
  181. ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWtaW, RaW, WaW) \
  182. ITM_MEMSET_DEF(TARGET, M2, W) \
  183. ITM_MEMSET_DEF(TARGET, M2, WaR) \
  184. ITM_MEMSET_DEF(TARGET, M2, WaW)
  185. // Creates ABI load/store functions that delegate to a transactional memcpy.
  186. #define ITM_READ_MEMCPY(T, LSMOD, TARGET, M2) \
  187. _ITM_TYPE_##T ITM_REGPARM _ITM_##LSMOD##T (const _ITM_TYPE_##T *ptr)\
  188. { \
  189. _ITM_TYPE_##T v; \
  190. TARGET memtransfer##M2(&v, ptr, sizeof(_ITM_TYPE_##T), false, \
  191. GTM::abi_dispatch::NONTXNAL, GTM::abi_dispatch::LSMOD); \
  192. return v; \
  193. }
  194. #define ITM_WRITE_MEMCPY(T, LSMOD, TARGET, M2) \
  195. void ITM_REGPARM _ITM_##LSMOD##T (_ITM_TYPE_##T *ptr, _ITM_TYPE_##T val)\
  196. { \
  197. TARGET memtransfer##M2(ptr, &val, sizeof(_ITM_TYPE_##T), false, \
  198. GTM::abi_dispatch::LSMOD, GTM::abi_dispatch::NONTXNAL); \
  199. }
  200. #define CREATE_DISPATCH_FUNCTIONS_T_MEMCPY(T, TARGET, M2) \
  201. ITM_READ_MEMCPY(T, R, TARGET, M2) \
  202. ITM_READ_MEMCPY(T, RaR, TARGET, M2) \
  203. ITM_READ_MEMCPY(T, RaW, TARGET, M2) \
  204. ITM_READ_MEMCPY(T, RfW, TARGET, M2) \
  205. ITM_WRITE_MEMCPY(T, W, TARGET, M2) \
  206. ITM_WRITE_MEMCPY(T, WaR, TARGET, M2) \
  207. ITM_WRITE_MEMCPY(T, WaW, TARGET, M2)
  208. namespace GTM HIDDEN {
  209. struct gtm_transaction_cp;
  210. struct method_group
  211. {
  212. // Start using a TM method from this group. This constructs required meta
  213. // data on demand when this method group is actually used. Will be called
  214. // either on first use or after a previous call to fini().
  215. virtual void init() = 0;
  216. // Stop using any method from this group for now. This can be used to
  217. // destruct meta data as soon as this method group is not used anymore.
  218. virtual void fini() = 0;
  219. // This can be overriden to implement more light-weight re-initialization.
  220. virtual void reinit()
  221. {
  222. fini();
  223. init();
  224. }
  225. };
  226. // This is the base interface that all TM methods have to implement.
  227. struct abi_dispatch
  228. {
  229. public:
  230. enum ls_modifier { NONTXNAL, R, RaR, RaW, RfW, W, WaR, WaW };
  231. private:
  232. // Disallow copies
  233. abi_dispatch(const abi_dispatch &) = delete;
  234. abi_dispatch& operator=(const abi_dispatch &) = delete;
  235. public:
  236. // Starts or restarts a transaction. Is called right before executing the
  237. // transactional application code (by either returning from
  238. // gtm_thread::begin_transaction or doing the longjmp when restarting).
  239. // Returns NO_RESTART if the transaction started successfully. Returns
  240. // a real restart reason if it couldn't start and does need to abort. This
  241. // allows TM methods to just give up and delegate ensuring progress to the
  242. // restart mechanism. If it returns a restart reason, this call must be
  243. // idempotent because it will trigger the restart mechanism, which could
  244. // switch to a different TM method.
  245. virtual gtm_restart_reason begin_or_restart() = 0;
  246. // Tries to commit the transaction. Iff this returns true, the transaction
  247. // got committed and all per-transaction data will have been reset.
  248. // Currently, this is called only for the commit of the outermost
  249. // transaction, or when switching to serial mode (which can happen in a
  250. // nested transaction).
  251. // If privatization safety must be ensured in a quiescence-based way, set
  252. // priv_time to a value different to 0. Nontransactional code will not be
  253. // executed after this commit until all registered threads' shared_state is
  254. // larger than or equal to this value.
  255. virtual bool trycommit(gtm_word& priv_time) = 0;
  256. // Rolls back a transaction. Called on abort or after trycommit() returned
  257. // false.
  258. virtual void rollback(gtm_transaction_cp *cp = 0) = 0;
  259. // Returns true iff the snapshot is most recent, which will be the case if
  260. // this transaction cannot be the reason why other transactions cannot
  261. // ensure privatization safety.
  262. virtual bool snapshot_most_recent() = 0;
  263. // Return an alternative method that is compatible with the current
  264. // method but supports closed nesting. Return zero if there is none.
  265. // Note that too be compatible, it must be possible to switch to this other
  266. // method on begin of a nested transaction without committing or restarting
  267. // the parent method.
  268. virtual abi_dispatch* closed_nesting_alternative() { return 0; }
  269. // Returns true iff this method group supports the current situation.
  270. // NUMBER_OF_THREADS is the current number of threads that might execute
  271. // transactions.
  272. virtual bool supports(unsigned number_of_threads) { return true; }
  273. bool read_only () const { return m_read_only; }
  274. bool write_through() const { return m_write_through; }
  275. bool can_run_uninstrumented_code() const
  276. {
  277. return m_can_run_uninstrumented_code;
  278. }
  279. // Returns true iff this TM method supports closed nesting.
  280. bool closed_nesting() const { return m_closed_nesting; }
  281. // Returns STATE_SERIAL or STATE_SERIAL | STATE_IRREVOCABLE iff the TM
  282. // method only works for serial-mode transactions.
  283. uint32_t requires_serial() const { return m_requires_serial; }
  284. method_group* get_method_group() const { return m_method_group; }
  285. static void *operator new(size_t s) { return xmalloc (s); }
  286. static void operator delete(void *p) { free (p); }
  287. public:
  288. static bool memmove_overlap_check(void *dst, const void *src, size_t size,
  289. ls_modifier dst_mod, ls_modifier src_mod);
  290. // Creates the ABI dispatch methods for loads and stores.
  291. // ??? Should the dispatch table instead be embedded in the dispatch object
  292. // to avoid the indirect lookup in the vtable?
  293. CREATE_DISPATCH_METHODS_PV(virtual, )
  294. // Creates the ABI dispatch methods for memcpy/memmove/memset.
  295. CREATE_DISPATCH_METHODS_MEM_PV()
  296. protected:
  297. const bool m_read_only;
  298. const bool m_write_through;
  299. const bool m_can_run_uninstrumented_code;
  300. const bool m_closed_nesting;
  301. const uint32_t m_requires_serial;
  302. method_group* const m_method_group;
  303. abi_dispatch(bool ro, bool wt, bool uninstrumented, bool closed_nesting,
  304. uint32_t requires_serial, method_group* mg) :
  305. m_read_only(ro), m_write_through(wt),
  306. m_can_run_uninstrumented_code(uninstrumented),
  307. m_closed_nesting(closed_nesting), m_requires_serial(requires_serial),
  308. m_method_group(mg)
  309. { }
  310. };
  311. }
  312. #endif // DISPATCH_H