aboutsummaryrefslogtreecommitdiff
path: root/common/threads/driver.cxx
blob: 1add0112d879b9277d741de547dfcd0da3a951ab (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
// file      : common/threads/driver.cxx
// license   : GNU GPL v2; see accompanying LICENSE file

// Test operations in a multi-threaded environment.
//

#include <vector>
#include <memory>   // std::unique_ptr
#include <cstddef>  // std::size_t
#include <iostream>

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

#include <odb/details/shared-ptr.hxx>
#include <odb/details/thread.hxx>

#include <libcommon/config.hxx> // DATABASE_*
#include <libcommon/common.hxx>

#if defined(DATABASE_SQLITE)
#  include <odb/sqlite/database.hxx>
#endif

#include "test.hxx"
#include "test-odb.hxx"

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace odb::core;
namespace details = odb::details;

const unsigned long thread_count = 24;
const unsigned long iteration_count = 30;
const unsigned long sub_iteration_count = 40;

struct task
{
  task (database& db, unsigned long n)
      : db_ (db), n_ (n)
  {
  }

  void*
  execute ()
  {
    try
    {
      for (unsigned long i (0); i < iteration_count; ++i)
      {
        unsigned long id ((n_ * iteration_count + i) * 3);

        object o1 (id, "first object");
        object o2 (id + 1, "second object");
        object o3 (id + 2, "third object");

        // The following transactions may lead to deadlocks.
        //
        while (true)
        {
          try
          {
            transaction t (db_.begin ());

            db_.persist (o1);
            db_.persist (o2);
            db_.persist (o3);
            t.commit ();
            break;
          }
          catch (const deadlock&) {}
        }

        while (true)
        {
          try
          {
#if !defined(DATABASE_SQLITE)
            transaction t (db_.begin ());
#else
            // SQLite has a peculiar table locking mode (shared cache)
            // which can lead to any of the transactions in this test
            // deadlocking even though they shouldn't from the user's
            // perspective. One way to work around this problem is to
            // start a "write" transaction as such right away.
            //
            transaction t;

            if (db_.id () != odb::id_sqlite)
              t.reset (db_.begin ());
            else
            {
              t.reset (
                static_cast<odb::sqlite::database&> (db_).begin_immediate ());
            }
#endif
            unique_ptr<object> o (db_.load<object> (id));
            assert (o->str_ == "first object");
            o->str_ = "another value";
            db_.update (*o);
            t.commit ();
            break;
          }
          catch (const deadlock&) {}
        }

        for (unsigned long j (0); j < sub_iteration_count; ++j)
        {
          typedef odb::query<object> query;
          typedef odb::prepared_query<object> prep_query;
          typedef odb::result<object> result;

          while (true)
          {
            try
            {
              transaction t (db_.begin ());

              prep_query pq (db_.lookup_query<object> ("object-query"));

              if (!pq)
              {
                pq = db_.prepare_query<object> (
                  "object-query", query::str == "another value");
                db_.cache_query (pq);
              }

              result r (pq.execute (false));

              bool found (false);
              for (result::iterator i (r.begin ()); i != r.end (); ++i)
              {
                if (i->id_ == id)
                {
                  found = true;
                  break;
                }
              }
              assert (found);
              t.commit ();
              break;
            }
            catch (const deadlock&) {}
          }
        }

        while (true)
        {
          try
          {
            transaction t (db_.begin ());
            db_.erase<object> (id);
            t.commit ();
            break;
          }
          catch (const deadlock&) {}
        }
      }
    }
    catch (const odb::exception& e)
    {
      cerr << e.what () << endl;
      return reinterpret_cast<void*> (1);
    }

    return 0;
  }

  static void*
  execute (void* arg)
  {
    return static_cast<task*> (arg)->execute ();
  }

  database& db_;
  unsigned long n_;
};

bool
test (int argc, char* argv[], size_t max_connections)
{
  unique_ptr<database> db (create_database (argc, argv, true, max_connections));

  vector<details::shared_ptr<details::thread> > threads;
  vector<details::shared_ptr<task> > tasks;

  for (unsigned long i (0); i < thread_count; ++i)
  {
    details::shared_ptr<task> t (new (details::shared) task (*db, i));
    tasks.push_back (t);

    threads.push_back (
      details::shared_ptr<details::thread> (
        new (details::shared) details::thread (&task::execute, t.get ())));
  }

  bool r (true);

  for (unsigned long i (0); i < thread_count; ++i)
    if (threads[i]->join () != 0)
      r = false;

  {
    typedef odb::result<object> result;

    transaction t (db->begin ());
    result r (db->query<object> ());

    for (result::iterator i (r.begin ()); i != r.end (); ++i)
      db->erase<object> (i->id_);

    t.commit ();
  }

  return r;
}

int
main (int argc, char* argv[])
{
  try
  {
    if (!(test (argc, argv, 0) &&
          test (argc, argv, thread_count - 1) &&
          test (argc, argv, thread_count / 2) &&
          test (argc, argv, thread_count / 4)))
      return 1;
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}