summaryrefslogtreecommitdiff
path: root/drd/tests/qt4_rwlock.cpp
blob: 2299eb0394fe7c59c69ae6b22cbf7f5667fadd11 (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
/// Qt4 reader-writer lock test.

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <QThread>               // class QThread
#include <QReadWriteLock>        // class QReadWriteLock
#include <cstdio>                // fprintf()
#include <cstdlib>               // atoi()
#include <new>
#include <pthread.h>             // pthread_barrier_t
#include <vector>


static pthread_barrier_t s_barrier;
static QReadWriteLock* s_pRWlock;
static int s_iterations;
static int s_counter;


class IncThread: public QThread
{
  virtual void run();
};

void IncThread::run()
{
  int i;

  pthread_barrier_wait(&s_barrier);
  for (i = s_iterations; i > 0; i--)
  {
    s_pRWlock->lockForWrite();
    s_counter++;
    s_pRWlock->unlock();
  }
}

int main(int argc, char** argv)
{
  int i;
  const int n_threads = 10;
  std::vector<QThread*> tid(n_threads);

  s_iterations = argc > 1 ? atoi(argv[1]) : 1000;

  fprintf(stderr, "Start of test.\n");

  {
    // Stack-allocated reader-writer lock.
    QReadWriteLock RWL;
    RWL.lockForRead();
    RWL.unlock();
    RWL.lockForWrite();
    RWL.unlock();
  }

  pthread_barrier_init(&s_barrier, 0, n_threads);
  s_pRWlock = new QReadWriteLock();
  for (i = 0; i < n_threads; i++)
  {
    tid[i] = new IncThread;
    tid[i]->start();
  }
  for (i = 0; i < n_threads; i++)
  {
    tid[i]->wait();
    delete tid[i];
  }
  delete s_pRWlock;
  s_pRWlock = 0;
  pthread_barrier_destroy(&s_barrier);

  if (s_counter == n_threads * s_iterations)
    fprintf(stderr, "Test successful.\n");
  else
    fprintf(stderr, "Test failed: counter = %d, should be %d\n",
            s_counter, n_threads * s_iterations);

  return 0;
}