cadedced0087ff17124635699f29dea26eb8b5d3
[cdsspec-compiler.git] / benchmark / cliffc-hashtable / main.cc
1 #include <iostream>
2 #include <threads.h>
3 #include "cliffc_hashtable.h"
4
5 using namespace std;
6
7 template<typename TypeK, typename TypeV>
8 slot* const cliffc_hashtable<TypeK, TypeV>::MATCH_ANY = new slot(false, NULL);
9
10 template<typename TypeK, typename TypeV>
11 slot* const cliffc_hashtable<TypeK, TypeV>::NO_MATCH_OLD = new slot(false, NULL);
12
13 template<typename TypeK, typename TypeV>
14 slot* const cliffc_hashtable<TypeK, TypeV>::TOMBPRIME = new slot(true, NULL);
15
16 template<typename TypeK, typename TypeV>
17 slot* const cliffc_hashtable<TypeK, TypeV>::TOMBSTONE = new slot(false, NULL);
18
19
20 class IntWrapper {
21         private:
22                 public:
23             int _val;
24
25                 IntWrapper(int val) : _val(val) {}
26
27                 IntWrapper() : _val(0) {}
28
29                 IntWrapper(IntWrapper& copy) : _val(copy._val) {}
30
31                 int get() {
32                         return _val;
33                 }
34
35                 int hashCode() {
36                         return _val;
37                 }
38
39                 bool equals(const void *another) {
40                         if (another == NULL)
41                                 return false;
42                         IntWrapper *ptr =
43                                 (IntWrapper*) another;
44                         return ptr->_val == _val;
45                 }
46 };
47
48 cliffc_hashtable<IntWrapper, IntWrapper> *table;
49 IntWrapper *val1, *val2;
50
51 void threadA(void *arg) {
52         /*
53         IntWrapper k1(3), k2(5), k3(1024), k4(1025);
54         IntWrapper v1(1), v2(2), v3(73), v4(81);
55         table->put(k1, v1);
56         table->put(k2, v2);
57         val1 = table->get(k3);
58         table->put(k3, v3);
59         */
60         for (int i = 200; i < 205; i++) {
61                 IntWrapper k(i), v(i * 2);
62                 table->put(k, v);
63         }
64 }
65
66 void threadB(void *arg) {
67         IntWrapper k1(3), k2(5), k3(1024), k4(1025);
68         IntWrapper v1(1), v2(2), v3(73), v4(81);
69         table->put(k1, v3);
70         table->put(k2, v4);
71         val1 = table->get(k2);
72 }
73
74 void threadC(void *arg) {
75         IntWrapper k1(3), k2(5), k3(1024), k4(1025);
76         IntWrapper v1(1), v2(2), v3(73), v4(81);
77         table->put(k1, v1);
78         table->put(k2, v2);
79         val2 = table->get(k1);
80 }
81
82 void threadD(void *arg) {
83         IntWrapper k1(3), k2(5), k3(1024), k4(1025);
84         IntWrapper v1(1), v2(2), v3(73), v4(81);
85         table->put(k1, v2);
86         table->put(k2, v1);
87         val2 = table->get(k2);
88 }
89
90 int user_main(int argc, char *argv[]) {
91         thrd_t t1, t2, t3, t4;
92         table = new cliffc_hashtable<IntWrapper, IntWrapper>();
93         val1 = NULL;
94         val2 = NULL;
95         thrd_create(&t1, threadA, NULL);
96         thrd_create(&t2, threadB, NULL);
97         thrd_create(&t3, threadC, NULL);
98         //thrd_create(&t4, threadD, NULL);
99
100         thrd_join(t1);
101         thrd_join(t2);
102         thrd_join(t3);
103         //thrd_join(t4);
104         
105         if (val1 == NULL) {
106                 cout << "val1: NULL" << endl;
107         } else {
108                 cout << val1->get() << endl;
109         }
110         MODEL_ASSERT(val1 == NULL || val1->get() == 2 || val1->get() == 81);
111         if (val2 == NULL) {
112                 cout << "val2: NULL" << endl;
113         } else {
114                 cout << val2->get() << endl;
115         }
116         return 0;
117 }