annotate libitm/libitm_i.h @ 145:1830386684a0

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