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