-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cc
110 lines (93 loc) · 2.26 KB
/
test.cc
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
#include <pthread.h>
#include <stdio.h>
#include <iostream>
#include "color_log.hh"
#include "libgthread.hh"
#include "log.h"
#define THREAD_NUM 20
#define SECONDARY_THREAD_NUM 30
#define A_SIZE 100
#define B_SIZE 150
//#define DOUBLE
// Forward declaration
void *fn1(void *);
void *fn2(void *);
typedef struct blob {
int a[A_SIZE];
int b[B_SIZE];
size_t c;
} blob_t;
void *fn2(void *arg) {
blob_t *b = (blob_t *)arg;
for (int i = 0; i < A_SIZE; i++) {
b->a[i] = b->a[i] + 1;
}
for (int i = 0; i < B_SIZE; i++) {
b->b[i] = b->b[i] + 1;
}
b->c = b->c + 1;
return nullptr;
}
void *fn1(void *arg) {
blob_t *b = (blob_t *)arg;
for (int i = 0; i < A_SIZE; i++) {
b->a[i] = b->a[i] + 1;
}
for (int i = 0; i < B_SIZE; i++) {
b->b[i] = b->b[i] + 1;
}
b->c = b->c + 1;
#if defined(DOUBLE)
pthread_t threads[SECONDARY_THREAD_NUM];
for (int i = 0; i < SECONDARY_THREAD_NUM; i++) {
pthread_create(&threads[i], NULL, fn2, b);
}
for (int i = 0; i < SECONDARY_THREAD_NUM; i++) {
pthread_join(threads[i], NULL);
}
#endif
return nullptr;
}
void verify(blob_t *b) {
#if defined(DOUBLE)
int expected = THREAD_NUM * (SECONDARY_THREAD_NUM + 1);
#else
int expected = THREAD_NUM;
#endif
for (int i = 0; i < A_SIZE; i++) {
REQUIRE(b->a[i] == expected)
<< "Expect: " << expected << ", b->a[" << i << "]: " << b->a[i];
}
for (int i = 0; i < B_SIZE; i++) {
REQUIRE(b->b[i] == expected)
<< "Expect: " << expected << ", b->b[" << i << "]: " << b->b[i];
}
REQUIRE(b->c == (size_t)expected)
<< "Expect: " << expected << ", b->c: " << b->c;
}
void print_blob(blob_t *b) {
printf("a = ");
for (int i = 0; i < A_SIZE; i++) {
printf(" %d", b->a[i]);
}
printf("\n");
printf("b = ");
for (int i = 0; i < B_SIZE; i++) {
printf(" %d", b->b[i]);
}
printf("\n");
}
int main() {
pthread_t threads[THREAD_NUM];
blob_t *b = (blob_t *)malloc(sizeof(blob_t));
memset(b, 0, sizeof(blob_t));
for (int i = 0; i < THREAD_NUM; i++) {
pthread_create(&threads[i], NULL, fn1, b);
}
for (int i = 0; i < THREAD_NUM; i++) {
pthread_join(threads[i], NULL);
}
verify(b);
std::cout << "c = " << b->c << std::endl;
std::cout << "Rollback count = " << *Gstm::rollback_count_ << std::endl;
}