1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/* Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
Contributed by Richard Henderson <rth@redhat.com>.
This file is part of the GNU Transactional Memory Library (libitm).
Libitm is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "libitm_i.h"
// The default TM method used when starting a new transaction.
static GTM::abi_dispatch* default_dispatch = 0;
// The default TM method as requested by the user, if any.
static GTM::abi_dispatch* default_dispatch_user = 0;
void
GTM::gtm_thread::decide_retry_strategy (gtm_restart_reason r)
{
struct abi_dispatch *disp = abi_disp ();
this->restart_reason[r]++;
this->restart_total++;
if (r == RESTART_INIT_METHOD_GROUP)
{
// A re-initializations of the method group has been requested. Switch
// to serial mode, initialize, and resume normal operation.
if ((state & STATE_SERIAL) == 0)
{
// We have to eventually re-init the method group. Therefore,
// we cannot just upgrade to a write lock here because this could
// fail forever when other transactions execute in serial mode.
// However, giving up the read lock then means that a change of the
// method group could happen in-between, so check that we're not
// re-initializing without a need.
// ??? Note that we can still re-initialize too often, but avoiding
// that would increase code complexity, which seems unnecessary
// given that re-inits should be very infrequent.
serial_lock.read_unlock(this);
serial_lock.write_lock();
if (disp->get_method_group() == default_dispatch->get_method_group())
{
// Still the same method group.
disp->get_method_group()->fini();
disp->get_method_group()->init();
}
serial_lock.write_unlock();
serial_lock.read_lock(this);
if (disp->get_method_group() != default_dispatch->get_method_group())
{
disp = default_dispatch;
set_abi_disp(disp);
}
}
else
{
// We are a serial transaction already, which makes things simple.
disp->get_method_group()->fini();
disp->get_method_group()->init();
}
}
bool retry_irr = (r == RESTART_SERIAL_IRR);
bool retry_serial = (retry_irr || this->restart_total > 100);
// We assume closed nesting to be infrequently required, so just use
// dispatch_serial (with undo logging) if required.
if (r == RESTART_CLOSED_NESTING)
retry_serial = true;
if (retry_serial)
{
// In serialirr_mode we can succeed with the upgrade to
// write-lock but fail the trycommit. In any case, if the
// write lock is not yet held, grab it. Don't do this with
// an upgrade, since we've no need to preserve the state we
// acquired with the read.
// Note that we will be restarting with either dispatch_serial or
// dispatch_serialirr, which are compatible with all TM methods; if
// we would retry with a different method, we would have to first check
// whether the default dispatch or the method group have changed. Also,
// the caller must have rolled back the previous transaction, so we
// don't have to worry about things such as privatization.
if ((this->state & STATE_SERIAL) == 0)
{
this->state |= STATE_SERIAL;
serial_lock.read_unlock (this);
serial_lock.write_lock ();
}
// We can retry with dispatch_serialirr if the transaction
// doesn't contain an abort and if we don't need closed nesting.
if ((this->prop & pr_hasNoAbort) && (r != RESTART_CLOSED_NESTING))
retry_irr = true;
}
// Note that we can just use serial mode here without having to switch
// TM method sets because serial mode is compatible with all of them.
if (retry_irr)
{
this->state = (STATE_SERIAL | STATE_IRREVOCABLE);
disp = dispatch_serialirr ();
set_abi_disp (disp);
}
else if (retry_serial)
{
disp = dispatch_serial();
set_abi_disp (disp);
}
}
// Decides which TM method should be used on the first attempt to run this
// transaction.
GTM::abi_dispatch*
GTM::gtm_thread::decide_begin_dispatch (uint32_t prop)
{
// TODO Pay more attention to prop flags (eg, *omitted) when selecting
// dispatch.
if ((prop & pr_doesGoIrrevocable) || !(prop & pr_instrumentedCode))
return dispatch_serialirr();
// If we might need closed nesting and the default dispatch has an
// alternative that supports closed nesting, use it.
// ??? We could choose another TM method that we know supports closed
// nesting but isn't the default (e.g., dispatch_serial()). However, we
// assume that aborts that need closed nesting are infrequent, so don't
// choose a non-default method until we have to actually restart the
// transaction.
if (!(prop & pr_hasNoAbort) && !default_dispatch->closed_nesting()
&& default_dispatch->closed_nesting_alternative())
return default_dispatch->closed_nesting_alternative();
// No special case, just use the default dispatch.
return default_dispatch;
}
void
GTM::gtm_thread::set_default_dispatch(GTM::abi_dispatch* disp)
{
if (default_dispatch == disp)
return;
if (default_dispatch)
{
// If we are switching method groups, initialize and shut down properly.
if (default_dispatch->get_method_group() != disp->get_method_group())
{
default_dispatch->get_method_group()->fini();
disp->get_method_group()->init();
}
}
else
disp->get_method_group()->init();
default_dispatch = disp;
}
static GTM::abi_dispatch*
parse_default_method()
{
const char *env = getenv("ITM_DEFAULT_METHOD");
GTM::abi_dispatch* disp = 0;
if (env == NULL)
return 0;
while (isspace((unsigned char) *env))
++env;
if (strncmp(env, "serialirr_onwrite", 17) == 0)
{
disp = GTM::dispatch_serialirr_onwrite();
env += 17;
}
else if (strncmp(env, "serialirr", 9) == 0)
{
disp = GTM::dispatch_serialirr();
env += 9;
}
else if (strncmp(env, "serial", 6) == 0)
{
disp = GTM::dispatch_serial();
env += 6;
}
else if (strncmp(env, "gl_wt", 5) == 0)
{
disp = GTM::dispatch_gl_wt();
env += 5;
}
else
goto unknown;
while (isspace((unsigned char) *env))
++env;
if (*env == '\0')
return disp;
unknown:
GTM::GTM_error("Unknown TM method in environment variable "
"ITM_DEFAULT_METHOD\n");
return 0;
}
// Gets notifications when the number of registered threads changes. This is
// used to initialize the method set choice and trigger straightforward choice
// adaption.
// This must be called only by serial threads.
void
GTM::gtm_thread::number_of_threads_changed(unsigned previous, unsigned now)
{
if (previous == 0)
{
// No registered threads before, so initialize.
static bool initialized = false;
if (!initialized)
{
initialized = true;
// Check for user preferences here.
default_dispatch_user = parse_default_method();
}
}
else if (now == 0)
{
// No registered threads anymore. The dispatch based on serial mode do
// not have any global state, so this effectively shuts down properly.
set_default_dispatch(dispatch_serialirr());
}
if (now == 1)
{
// Only one thread, so use a serializing method.
// ??? If we don't have a fast serial mode implementation, it might be
// better to use the global lock method set here.
if (default_dispatch_user)
set_default_dispatch(default_dispatch_user);
else
set_default_dispatch(dispatch_serialirr());
}
else if (now > 1 && previous <= 1)
{
// More than one thread, use the default method.
if (default_dispatch_user)
set_default_dispatch(default_dispatch_user);
else
set_default_dispatch(dispatch_serialirr_onwrite());
}
}
|