summaryrefslogtreecommitdiff
path: root/libitm/beginend.cc
blob: 9b16973b2a4555f730faacc63bffff977d50501a (plain)
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
266
267
268
269
270
271
272
273
274
275
/* 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 "libitm_i.h"


using namespace GTM;

__thread gtm_thread GTM::_gtm_thr;
gtm_rwlock GTM::gtm_transaction::serial_lock;

gtm_stmlock GTM::gtm_stmlock_array[LOCK_ARRAY_SIZE];
gtm_version GTM::gtm_clock;

/* ??? Move elsewhere when we figure out library initialization.  */
uint64_t GTM::gtm_spin_count_var = 1000;

static _ITM_transactionId_t global_tid;

/* Allocate a transaction structure.  Reuse an old one if possible.  */

void *
GTM::gtm_transaction::operator new (size_t s)
{
  gtm_thread *thr = gtm_thr ();
  void *tx;

  assert(s == sizeof(gtm_transaction));

  if (thr->free_tx_count == 0)
    tx = xmalloc (sizeof (gtm_transaction));
  else
    {
      thr->free_tx_count--;
      tx = thr->free_tx[thr->free_tx_idx];
      thr->free_tx_idx = (thr->free_tx_idx + 1) % gtm_thread::MAX_FREE_TX;
    }
  memset (tx, 0, sizeof (gtm_transaction));

  return tx;
}

/* Queue a transaction structure for freeing.  We never free the given
   transaction immediately -- this is a requirement of abortTransaction
   as the jmpbuf is used immediately after calling this function.  Thus
   the requirement that this queue be per-thread.  */

void
GTM::gtm_transaction::operator delete(void *tx)
{
  gtm_thread *thr = gtm_thr ();
  unsigned idx
    = (thr->free_tx_idx + thr->free_tx_count) % gtm_thread::MAX_FREE_TX;

  if (thr->free_tx_count == gtm_thread::MAX_FREE_TX)
    {
      thr->free_tx_idx = (thr->free_tx_idx + 1) % gtm_thread::MAX_FREE_TX;
      free (thr->free_tx[idx]);
    }
  else
    thr->free_tx_count++;

  thr->free_tx[idx] = tx;
}

#ifndef HAVE_64BIT_SYNC_BUILTINS
static pthread_mutex_t global_tid_lock = PTHREAD_MUTEX_INITIALIZER;
#endif

uint32_t
GTM::gtm_transaction::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
{
  static const _ITM_transactionId_t tid_block_size = 1 << 16;

  gtm_transaction *tx;
  abi_dispatch *disp;
  uint32_t ret;

  gtm_thread *thr = setup_gtm_thr ();

  tx = new gtm_transaction;

  tx->prop = prop;
  tx->prev = gtm_tx();
  if (tx->prev)
    tx->nesting = tx->prev->nesting + 1;

  // As long as we have not exhausted a previously allocated block of TIDs,
  // we can avoid an atomic operation on a shared cacheline.
  if (thr->local_tid & (tid_block_size - 1))
    tx->id = thr->local_tid++;
  else
    {
#ifdef HAVE_64BIT_SYNC_BUILTINS
      tx->id = __sync_add_and_fetch (&global_tid, tid_block_size);
      thr->local_tid = tx->id + 1;
#else
      pthread_mutex_lock (&global_tid_lock);
      global_tid += tid_block_size;
      tx->id = global_tid;
      thr->local_tid = tx->id + 1;
      pthread_mutex_unlock (&global_tid_lock);
#endif
    }

  tx->jb = *jb;

  set_gtm_tx (tx);

  // ??? pr_undoLogCode is not properly defined in the ABI. Are barriers
  // omitted because they are not necessary (e.g., a transaction on thread-
  // local data) or because the compiler thinks that some kind of global
  // synchronization might perform better?
  if (unlikely(prop & pr_undoLogCode))
    GTM_fatal("pr_undoLogCode not supported");

  if ((prop & pr_doesGoIrrevocable) || !(prop & pr_instrumentedCode))
    tx->state = (STATE_SERIAL | STATE_IRREVOCABLE);

  else
    disp = tx->decide_begin_dispatch ();

  if (tx->state & STATE_SERIAL)
    {
      serial_lock.write_lock ();

      if (tx->state & STATE_IRREVOCABLE)
        disp = dispatch_serialirr ();
      else
        disp = dispatch_serial ();

      ret = a_runUninstrumentedCode;
      if ((prop & pr_multiwayCode) == pr_instrumentedCode)
	ret = a_runInstrumentedCode;
    }
  else
    {
      serial_lock.read_lock ();
      ret = a_runInstrumentedCode | a_saveLiveVariables;
    }

  set_abi_disp (disp);

  return ret;
}

void
GTM::gtm_transaction::rollback ()
{
  abi_disp()->rollback ();
  rollback_local ();

  free_actions (&this->commit_actions);
  run_actions (&this->undo_actions);
  commit_allocations (true);
  revert_cpp_exceptions ();

  if (this->eh_in_flight)
    {
      _Unwind_DeleteException ((_Unwind_Exception *) this->eh_in_flight);
      this->eh_in_flight = NULL;
    }
}

void ITM_REGPARM
_ITM_abortTransaction (_ITM_abortReason reason)
{
  gtm_transaction *tx = gtm_tx();

  assert (reason == userAbort);
  assert ((tx->prop & pr_hasNoAbort) == 0);

  if (tx->state & gtm_transaction::STATE_IRREVOCABLE)
    abort ();

  tx->rollback ();
  abi_disp()->fini ();

  if (tx->state & gtm_transaction::STATE_SERIAL)
    gtm_transaction::serial_lock.write_unlock ();
  else
    gtm_transaction::serial_lock.read_unlock ();

  set_gtm_tx (tx->prev);
  delete tx;

  GTM_longjmp (&tx->jb, a_abortTransaction | a_restoreLiveVariables, tx->prop);
}

bool
GTM::gtm_transaction::trycommit ()
{
  if (abi_disp()->trycommit ())
    {
      commit_local ();
      free_actions (&this->undo_actions);
      run_actions (&this->commit_actions);
      commit_allocations (false);
      return true;
    }
  return false;
}

bool
GTM::gtm_transaction::trycommit_and_finalize ()
{
  if (trycommit ())
    {
      abi_disp()->fini ();
      set_gtm_tx (this->prev);
      delete this;
      if (this->state & gtm_transaction::STATE_SERIAL)
	gtm_transaction::serial_lock.write_unlock ();
      else
	gtm_transaction::serial_lock.read_unlock ();
      return true;
    }
  return false;
}

void ITM_NORETURN
GTM::gtm_transaction::restart (gtm_restart_reason r)
{
  uint32_t actions;

  rollback ();
  decide_retry_strategy (r);

  actions = a_runInstrumentedCode | a_restoreLiveVariables;
  if ((this->prop & pr_uninstrumentedCode)
      && (this->state & gtm_transaction::STATE_IRREVOCABLE))
    actions = a_runUninstrumentedCode | a_restoreLiveVariables;

  GTM_longjmp (&this->jb, actions, this->prop);
}

void ITM_REGPARM
_ITM_commitTransaction(void)
{
  gtm_transaction *tx = gtm_tx();
  if (!tx->trycommit_and_finalize ())
    tx->restart (RESTART_VALIDATE_COMMIT);
}

void ITM_REGPARM
_ITM_commitTransactionEH(void *exc_ptr)
{
  gtm_transaction *tx = gtm_tx();
  if (!tx->trycommit_and_finalize ())
    {
      tx->eh_in_flight = exc_ptr;
      tx->restart (RESTART_VALIDATE_COMMIT);
    }
}