aboutsummaryrefslogtreecommitdiff
path: root/odb/transaction.cxx
blob: 6ac397f158219282b245b7ddeb8f0cbfa9dc4a93 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// file      : odb/transaction.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <odb/transaction.hxx>
#include <odb/exceptions.hxx>

#include <odb/details/tls.hxx>

using namespace std;

namespace odb
{
  using namespace details;

  //
  // transaction
  //

  static ODB_TLS_POINTER (transaction) current_transaction;

  transaction::
  ~transaction ()
  {
    if (!finalized_)
      try {rollback ();} catch (...) {}
  }

  void transaction::
  reset (transaction_impl* impl, bool make_current)
  {
    details::unique_ptr<transaction_impl> i (impl);

    if (!finalized_)
      rollback ();

    impl_.reset (i.release ());

    if (make_current && tls_get (current_transaction) != 0)
      throw already_in_transaction ();

    impl_->start ();
    finalized_ = false;

    if (make_current)
      tls_set (current_transaction, this);
  }

  bool transaction::
  has_current ()
  {
    return tls_get (current_transaction) != 0;
  }

  transaction& transaction::
  current ()
  {
    transaction* cur (tls_get (current_transaction));

    if (cur == 0)
      throw not_in_transaction ();

    return *cur;
  }

  void transaction::
  current (transaction& t)
  {
    tls_set (current_transaction, &t);
  }

  void transaction::
  reset_current ()
  {
    transaction* t (0);
    tls_set (current_transaction, t);
  }

  struct rollback_guard
  {
    rollback_guard (transaction& t): t_ (&t) {}
    ~rollback_guard ()
    {if (t_ != 0) t_->callback_call (transaction::event_rollback);}
    void release () {t_ = 0;}
  private:
    transaction* t_;
  };

  void transaction::
  commit ()
  {
    if (finalized_)
      throw transaction_already_finalized ();

    finalized_ = true;
    rollback_guard rg (*this);

    impl_->connection ().transaction_tracer_ = 0;

    if (tls_get (current_transaction) == this)
    {
      transaction* t (0);
      tls_set (current_transaction, t);
    }

    impl_->commit ();
    rg.release ();

    if (callback_count_ != 0)
      callback_call (event_commit);
  }

  void transaction::
  rollback ()
  {
    if (finalized_)
      throw transaction_already_finalized ();

    finalized_ = true;
    rollback_guard rg (*this);

    impl_->connection ().transaction_tracer_ = 0;

    if (tls_get (current_transaction) == this)
    {
      transaction* t (0);
      tls_set (current_transaction, t);
    }

    impl_->rollback ();
    rg.release ();

    if (callback_count_ != 0)
      callback_call (event_rollback);
  }

  void transaction::
  callback_call (unsigned short event)
  {
    size_t stack_count (callback_count_ < stack_callback_count
                        ? callback_count_ : stack_callback_count);
    size_t dyn_count (callback_count_ - stack_count);

    // We need to be careful with the situation where a callback
    // throws and we neither call the rest of the callbacks nor
    // reset their states. To make sure this doesn't happen, we
    // do a first pass and reset all the states.
    //
    for (size_t i (0); i < stack_count; ++i)
    {
      callback_data& d (stack_callbacks_[i]);
      if (d.event != 0 && d.state != 0)
        *d.state = 0;
    }

    for (size_t i (0); i < dyn_count; ++i)
    {
      callback_data& d (dyn_callbacks_[i]);
      if (d.event != 0 && d.state != 0)
        *d.state = 0;
    }

    // Now do the actual calls.
    //
    for (size_t i (0); i < stack_count; ++i)
    {
      callback_data& d (stack_callbacks_[i]);
      if (d.event & event)
        d.func (event, d.key, d.data);
    }

    for (size_t i (0); i < dyn_count; ++i)
    {
      callback_data& d (dyn_callbacks_[i]);
      if (d.event & event)
        d.func (event, d.key, d.data);
    }

    // Clean things up in case this instance is going to be reused.
    //
    if (dyn_count != 0)
      dyn_callbacks_.clear ();

    free_callback_ = max_callback_count;
    callback_count_ = 0;
  }

  void transaction::
  callback_register (callback_type func,
                     void* key,
                     unsigned short event,
                     unsigned long long data,
                     transaction** state)
  {
    callback_data* s;

    // If we have a free slot, use it.
    //
    if (free_callback_ != max_callback_count)
    {
      s = (free_callback_ < stack_callback_count)
        ? stack_callbacks_ + free_callback_
        : &dyn_callbacks_[free_callback_ - stack_callback_count];

      free_callback_ = reinterpret_cast<size_t> (s->key);
    }
    // If we have space in the stack, grab that.
    //
    else if (callback_count_ < stack_callback_count)
    {
      s = stack_callbacks_ + callback_count_;
      callback_count_++;
    }
    // Otherwise use the dynamic storage.
    //
    else
    {
      dyn_callbacks_.push_back (callback_data ());
      s = &dyn_callbacks_.back ();
      callback_count_++;
    }

    s->func = func;
    s->key = key;
    s->event = event;
    s->data = data;
    s->state = state;
  }

  size_t transaction::
  callback_find (void* key)
  {
    if (callback_count_ == 0)
      return 0;

    size_t stack_count;

    // See if this is the last slot registered. This will be a fast path if,
    // for example, things are going to be unregistered from destructors.
    //
    if (callback_count_ <= stack_callback_count)
    {
      if (stack_callbacks_[callback_count_ - 1].key == key)
        return callback_count_ - 1;

      stack_count = callback_count_;
    }
    else
    {
      if (dyn_callbacks_.back ().key == key)
        return callback_count_ - 1;

      stack_count = stack_callback_count;
    }

    // Otherwise do a linear search.
    //
    for (size_t i (0); i < stack_count; ++i)
      if (stack_callbacks_[i].key == key)
        return i;

    for (size_t i (0), dyn_count (callback_count_ - stack_count);
         i < dyn_count; ++i)
      if (dyn_callbacks_[i].key == key)
        return i + stack_callback_count;

    return callback_count_;
  }

  void transaction::
  callback_unregister (void* key)
  {
    size_t i (callback_find (key));

    // It is ok for this function not to find the key.
    //
    if (i == callback_count_)
      return;

    // See if this is the last slot registered.
    //
    if (i == callback_count_ - 1)
    {
      if (i >= stack_callback_count)
        dyn_callbacks_.pop_back ();

      callback_count_--;
    }
    else
    {
      callback_data& d (
        i < stack_callback_count
        ? stack_callbacks_[i]
        : dyn_callbacks_[i - stack_callback_count]);

      // Add to the free list.
      //
      d.event = 0;
      d.key = reinterpret_cast<void*> (free_callback_);
      free_callback_ = i;
    }
  }

  void transaction::
  callback_update (void* key,
                   unsigned short event,
                   unsigned long long data,
                   transaction** state)
  {
    size_t i (callback_find (key));

    // It is ok for this function not to find the key.
    //
    if (i == callback_count_)
      return;

    callback_data& d (
      i < stack_callback_count
      ? stack_callbacks_[i]
      : dyn_callbacks_[i - stack_callback_count]);

    d.event = event;
    d.data = data;
    d.state = state;
  }

  //
  // transaction_impl
  //

  transaction_impl::
  ~transaction_impl ()
  {
  }
}