libitm_i.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. /* The following are internal implementation functions and definitions.
  20. To distinguish them from those defined by the Intel ABI, they all
  21. begin with GTM/gtm. */
  22. #ifndef LIBITM_I_H
  23. #define LIBITM_I_H 1
  24. #include "libitm.h"
  25. #include "config.h"
  26. #include <assert.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unwind.h>
  30. #include "local_type_traits"
  31. #include "local_atomic"
  32. /* Don't require libgcc_s.so for exceptions. */
  33. extern void _Unwind_DeleteException (_Unwind_Exception*) __attribute__((weak));
  34. #include "common.h"
  35. namespace GTM HIDDEN {
  36. using namespace std;
  37. // A helper template for accessing an unsigned integral of SIZE bytes.
  38. template<size_t SIZE> struct sized_integral { };
  39. template<> struct sized_integral<1> { typedef uint8_t type; };
  40. template<> struct sized_integral<2> { typedef uint16_t type; };
  41. template<> struct sized_integral<4> { typedef uint32_t type; };
  42. template<> struct sized_integral<8> { typedef uint64_t type; };
  43. typedef unsigned int gtm_word __attribute__((mode (word)));
  44. // These values are given to GTM_restart_transaction and indicate the
  45. // reason for the restart. The reason is used to decide what STM
  46. // implementation should be used during the next iteration.
  47. enum gtm_restart_reason
  48. {
  49. RESTART_REALLOCATE,
  50. RESTART_LOCKED_READ,
  51. RESTART_LOCKED_WRITE,
  52. RESTART_VALIDATE_READ,
  53. RESTART_VALIDATE_WRITE,
  54. RESTART_VALIDATE_COMMIT,
  55. RESTART_SERIAL_IRR,
  56. RESTART_NOT_READONLY,
  57. RESTART_CLOSED_NESTING,
  58. RESTART_INIT_METHOD_GROUP,
  59. NUM_RESTARTS,
  60. NO_RESTART = NUM_RESTARTS
  61. };
  62. } // namespace GTM
  63. #include "target.h"
  64. #include "rwlock.h"
  65. #include "aatree.h"
  66. #include "cacheline.h"
  67. #include "stmlock.h"
  68. #include "dispatch.h"
  69. #include "containers.h"
  70. #ifdef __USER_LABEL_PREFIX__
  71. # define UPFX UPFX1(__USER_LABEL_PREFIX__)
  72. # define UPFX1(t) UPFX2(t)
  73. # define UPFX2(t) #t
  74. #else
  75. # define UPFX
  76. #endif
  77. namespace GTM HIDDEN {
  78. // This type is private to alloc.c, but needs to be defined so that
  79. // the template used inside gtm_thread can instantiate.
  80. struct gtm_alloc_action
  81. {
  82. void (*free_fn)(void *);
  83. bool allocated;
  84. };
  85. struct gtm_thread;
  86. // A transaction checkpoint: data that has to saved and restored when doing
  87. // closed nesting.
  88. struct gtm_transaction_cp
  89. {
  90. gtm_jmpbuf jb;
  91. size_t undolog_size;
  92. aa_tree<uintptr_t, gtm_alloc_action> alloc_actions;
  93. size_t user_actions_size;
  94. _ITM_transactionId_t id;
  95. uint32_t prop;
  96. uint32_t cxa_catch_count;
  97. void *cxa_unthrown;
  98. // We might want to use a different but compatible dispatch method for
  99. // a nested transaction.
  100. abi_dispatch *disp;
  101. // Nesting level of this checkpoint (1 means that this is a checkpoint of
  102. // the outermost transaction).
  103. uint32_t nesting;
  104. void save(gtm_thread* tx);
  105. void commit(gtm_thread* tx);
  106. };
  107. // An undo log for writes.
  108. struct gtm_undolog
  109. {
  110. vector<gtm_word> undolog;
  111. // Log the previous value at a certain address.
  112. // The easiest way to inline this is to just define this here.
  113. void log(const void *ptr, size_t len)
  114. {
  115. size_t words = (len + sizeof(gtm_word) - 1) / sizeof(gtm_word);
  116. gtm_word *undo = undolog.push(words + 2);
  117. memcpy(undo, ptr, len);
  118. undo[words] = len;
  119. undo[words + 1] = (gtm_word) ptr;
  120. }
  121. void commit () { undolog.clear(); }
  122. size_t size() const { return undolog.size(); }
  123. // In local.cc
  124. void rollback (gtm_thread* tx, size_t until_size = 0);
  125. };
  126. // An entry of a read or write log. Used by multi-lock TM methods.
  127. struct gtm_rwlog_entry
  128. {
  129. atomic<gtm_word> *orec;
  130. gtm_word value;
  131. };
  132. // Contains all thread-specific data required by the entire library.
  133. // This includes all data relevant to a single transaction. Because most
  134. // thread-specific data is about the current transaction, we also refer to
  135. // the transaction-specific parts of gtm_thread as "the transaction" (the
  136. // same applies to names of variables and arguments).
  137. // All but the shared part of this data structure are thread-local data.
  138. // gtm_thread could be split into transaction-specific structures and other
  139. // per-thread data (with those parts then nested in gtm_thread), but this
  140. // would make it harder to later rearrange individual members to optimize data
  141. // accesses. Thus, for now we keep one flat object, and will only split it if
  142. // the code gets too messy.
  143. struct gtm_thread
  144. {
  145. struct user_action
  146. {
  147. _ITM_userCommitFunction fn;
  148. void *arg;
  149. bool on_commit;
  150. _ITM_transactionId_t resuming_id;
  151. };
  152. // The jump buffer by which GTM_longjmp restarts the transaction.
  153. // This field *must* be at the beginning of the transaction.
  154. gtm_jmpbuf jb;
  155. // Data used by local.c for the undo log for both local and shared memory.
  156. gtm_undolog undolog;
  157. // Read and write logs. Used by multi-lock TM methods.
  158. vector<gtm_rwlog_entry> readlog;
  159. vector<gtm_rwlog_entry> writelog;
  160. // Data used by alloc.c for the malloc/free undo log.
  161. aa_tree<uintptr_t, gtm_alloc_action> alloc_actions;
  162. // Data used by useraction.c for the user-defined commit/abort handlers.
  163. vector<user_action> user_actions;
  164. // A numerical identifier for this transaction.
  165. _ITM_transactionId_t id;
  166. // The _ITM_codeProperties of this transaction as given by the compiler.
  167. uint32_t prop;
  168. // The nesting depth for subsequently started transactions. This variable
  169. // will be set to 1 when starting an outermost transaction.
  170. uint32_t nesting;
  171. // Set if this transaction owns the serial write lock.
  172. // Can be reset only when restarting the outermost transaction.
  173. static const uint32_t STATE_SERIAL = 0x0001;
  174. // Set if the serial-irrevocable dispatch table is installed.
  175. // Implies that no logging is being done, and abort is not possible.
  176. // Can be reset only when restarting the outermost transaction.
  177. static const uint32_t STATE_IRREVOCABLE = 0x0002;
  178. // A bitmask of the above.
  179. uint32_t state;
  180. // In order to reduce cacheline contention on global_tid during
  181. // beginTransaction, we allocate a block of 2**N ids to the thread
  182. // all at once. This number is the next value to be allocated from
  183. // the block, or 0 % 2**N if no such block is allocated.
  184. _ITM_transactionId_t local_tid;
  185. // Data used by eh_cpp.c for managing exceptions within the transaction.
  186. uint32_t cxa_catch_count;
  187. void *cxa_unthrown;
  188. void *eh_in_flight;
  189. // Checkpoints for closed nesting.
  190. vector<gtm_transaction_cp> parent_txns;
  191. // Data used by retry.c for deciding what STM implementation should
  192. // be used for the next iteration of the transaction.
  193. // Only restart_total is reset to zero when the transaction commits, the
  194. // other counters are total values for all previously executed transactions.
  195. // restart_total is also used by the HTM fastpath in a different way.
  196. uint32_t restart_reason[NUM_RESTARTS];
  197. uint32_t restart_total;
  198. // *** The shared part of gtm_thread starts here. ***
  199. // Shared state is on separate cachelines to avoid false sharing with
  200. // thread-local parts of gtm_thread.
  201. // Points to the next thread in the list of all threads.
  202. gtm_thread *next_thread __attribute__((__aligned__(HW_CACHELINE_SIZE)));
  203. // If this transaction is inactive, shared_state is ~0. Otherwise, this is
  204. // an active or serial transaction.
  205. atomic<gtm_word> shared_state;
  206. // The lock that provides access to serial mode. Non-serialized
  207. // transactions acquire read locks; a serialized transaction aquires
  208. // a write lock.
  209. // Accessed from assembly language, thus the "asm" specifier on
  210. // the name, avoiding complex name mangling.
  211. static gtm_rwlock serial_lock __asm__(UPFX "gtm_serial_lock");
  212. // The head of the list of all threads' transactions.
  213. static gtm_thread *list_of_threads;
  214. // The number of all registered threads.
  215. static unsigned number_of_threads;
  216. // In alloc.cc
  217. void commit_allocations (bool, aa_tree<uintptr_t, gtm_alloc_action>*);
  218. void record_allocation (void *, void (*)(void *));
  219. void forget_allocation (void *, void (*)(void *));
  220. void drop_references_allocations (const void *ptr)
  221. {
  222. this->alloc_actions.erase((uintptr_t) ptr);
  223. }
  224. // In beginend.cc
  225. void rollback (gtm_transaction_cp *cp = 0, bool aborting = false);
  226. bool trycommit ();
  227. void restart (gtm_restart_reason, bool finish_serial_upgrade = false)
  228. ITM_NORETURN;
  229. gtm_thread();
  230. ~gtm_thread();
  231. static void *operator new(size_t);
  232. static void operator delete(void *);
  233. // Invoked from assembly language, thus the "asm" specifier on
  234. // the name, avoiding complex name mangling.
  235. static uint32_t begin_transaction(uint32_t, const gtm_jmpbuf *)
  236. __asm__(UPFX "GTM_begin_transaction") ITM_REGPARM;
  237. // In eh_cpp.cc
  238. void revert_cpp_exceptions (gtm_transaction_cp *cp = 0);
  239. // In retry.cc
  240. // Must be called outside of transactions (i.e., after rollback).
  241. void decide_retry_strategy (gtm_restart_reason);
  242. abi_dispatch* decide_begin_dispatch (uint32_t prop);
  243. void number_of_threads_changed(unsigned previous, unsigned now);
  244. // Must be called from serial mode. Does not call set_abi_disp().
  245. void set_default_dispatch(abi_dispatch* disp);
  246. // In method-serial.cc
  247. void serialirr_mode ();
  248. // In useraction.cc
  249. void rollback_user_actions (size_t until_size = 0);
  250. void commit_user_actions ();
  251. };
  252. } // namespace GTM
  253. #include "tls.h"
  254. namespace GTM HIDDEN {
  255. // An unscaled count of the number of times we should spin attempting to
  256. // acquire locks before we block the current thread and defer to the OS.
  257. // This variable isn't used when the standard POSIX lock implementations
  258. // are used.
  259. extern uint64_t gtm_spin_count_var;
  260. extern "C" uint32_t GTM_longjmp (uint32_t, const gtm_jmpbuf *, uint32_t)
  261. ITM_NORETURN ITM_REGPARM;
  262. extern "C" void GTM_LB (const void *, size_t) ITM_REGPARM;
  263. extern void GTM_error (const char *fmt, ...)
  264. __attribute__((format (printf, 1, 2)));
  265. extern void GTM_fatal (const char *fmt, ...)
  266. __attribute__((noreturn, format (printf, 1, 2)));
  267. extern abi_dispatch *dispatch_serial();
  268. extern abi_dispatch *dispatch_serialirr();
  269. extern abi_dispatch *dispatch_serialirr_onwrite();
  270. extern abi_dispatch *dispatch_gl_wt();
  271. extern abi_dispatch *dispatch_ml_wt();
  272. extern abi_dispatch *dispatch_htm();
  273. extern gtm_cacheline_mask gtm_mask_stack(gtm_cacheline *, gtm_cacheline_mask);
  274. // Control variable for the HTM fastpath that uses serial mode as fallback.
  275. // Non-zero if the HTM fastpath is enabled. See gtm_thread::begin_transaction.
  276. // Accessed from assembly language, thus the "asm" specifier on
  277. // the name, avoiding complex name mangling.
  278. extern uint32_t htm_fastpath __asm__(UPFX "gtm_htm_fastpath");
  279. } // namespace GTM
  280. #endif // LIBITM_I_H