12 #include "allocator.h"
17 #include "transaction.h"
19 #include "xthread.cpp"
49 operator long()
const {
return m_x;}
50 Payload &operator=(
const long &x) {
55 Payload &operator+=(
const long &x) {
70 m_1.reset(create<LongNode>());
72 var->insert(tr, m_1,
false);
86 const shared_ptr<LongNode> &n1()
const {
return m_1;}
88 const shared_ptr<LongNode> m_var;
89 shared_ptr<LongNode> m_1, m_2, m_3, m_4;
92 #define trans(node) for(Transaction \
93 implicit_tr(node, false); !implicit_tr.isModified() || !implicit_tr.commitOrNext(); ) implicit_tr[node]
96 typename std::enable_if<std::is_base_of<LongNode, T>::value,
102 #include "transaction_impl.h"
105 shared_ptr<LongNode> gn1, gn2, gn3, gn4;
108 start_routine(
void) {
110 shared_ptr<LongNode> p1(LongNode::create<LongNode>());
111 shared_ptr<LongNode> p2(LongNode::create<LongNode>());
112 for(
int i = 0; i < 2500; i++) {
115 gn1->iterate_commit_if([=](
Transaction &tr1)->
bool{
116 if( !gn2->insert(tr1, p2))
127 tr1[gn1] = ctr1[gn1] + 1;
128 tr1[gn3] = ctr1[gn3] + 1;
130 tr1[gn1] = str1[gn1] - 1;
131 tr1[gn2] = str1[gn2] + 1;
133 tr1[p2] = str1[p2] + 1;
139 tr1[gn4] = tr1[gn4] + 1;
140 tr1[gn4] = tr1[gn4] - 1;
146 tr1[gn2] = tr1[gn2] - 1;
147 tr1[gn3] = str1[gn3] - 1;
149 tr1[p2] = str1[p2] - 1;
155 gn1->iterate_commit_if([=](
Transaction &tr1)->
bool{
156 if( !gn2->release(tr1, p2))
165 printf(
"Error! P2=%ld\n", y);
173 #define NUM_THREADS 4
176 main(
int argc,
char **argv) {
177 for(
int k = 0; k < 100; k++) {
178 gn1.reset(LongNode::create<LongNode>());
179 gn2.reset(LongNode::create<LongNode>());
180 gn3.reset(LongNode::create<LongNode>());
181 gn4.reset(LongNode::create<LongNode>());
183 gn1->iterate_commit_if([=](
Transaction &tr1)->
bool{
185 if( !gn1->insert(tr1, gn2,
true))
187 tr1[ *gn2] = tr1[ *gn2] + 1;
188 if( !gn2->insert(tr1, gn3,
true))
191 if( !gn3->insert(tr1, gn4,
true))
194 if( !gn3->release(tr1, gn4))
206 tr1[ *gn2] = tr1[ *gn2] - 1;
212 long x = shot1[*gn3];
213 printf(
"Gn3:%ld\n", x);
217 printf(
"Gn3:%ld\n", x);
220 shared_ptr<LongNode> p1(LongNode::create<LongNode>());
228 shared_ptr<LongNode> p2(LongNode::create<LongNode>());
229 shared_ptr<LongNode> p22(LongNode::create<LongNode>());
230 shared_ptr<LongNode> p211(LongNode::create<LongNode>());
231 shared_ptr<LongNode> p21(LongNode::create<LongNode>());
244 shared_ptr<ComplexNode> p2111;
246 gn3->iterate_commit_if([=, &p2111](
Transaction &tr1)->
bool{
248 if( !p1->insert(tr1, p22,
true))
250 if( !gn3->insert(tr1, p2,
true))
253 p2111.reset(LongNode::create<ComplexNode>(ref(tr1), gn3));
254 shared_ptr<LongNode> p2112(LongNode::create<LongNode>());
255 shared_ptr<LongNode> p2113(LongNode::create<LongNode>());
256 shared_ptr<LongNode> p2114(LongNode::create<LongNode>());
257 p2111->insert(p2112);
258 p2112->insert(p2113);
259 p2111->insert(p2114);
263 if( !p21->insert(tr1, p2111,
true))
265 if( !gn3->insert(tr1, p2111,
true))
276 shot1[ *p2111->n1()];
278 gn1->iterate_commit_if([=](
Transaction &tr1)->
bool{
280 if( !gn3->insert(tr1, gn4,
true))
282 if( !gn2->insert(tr1, gn4,
true))
286 gn1->iterate_commit_if([=](
Transaction &tr1)->
bool{
288 if( !gn3->release(tr1, gn4))
290 if( !gn2->release(tr1, gn4))
301 gn3->iterate_commit_if([=](
Transaction &tr1)->
bool{
302 if( !p1->release(tr1, p22))
304 if( !gn3->release(tr1, p2))
306 if( !gn3->release(tr1, p2111))
314 tr1[gn1] = ctr1[gn1] + 1;
315 tr1[gn3] = ctr1[gn3] + 1;
317 tr1[gn1] = str1[gn1] - 1;
318 tr1[gn3] = str1[gn3] - 1;
325 std::thread threads[NUM_THREADS];
327 for(
int i = 0; i < NUM_THREADS; i++) {
328 std::thread th( &start_routine);
332 for(
int i = 0; i < 100; i++) {
336 for(
int i = 0; i < NUM_THREADS; i++) {
341 if(***gn1 || ***gn2 || ***gn3 || ***gn4) {
343 printf(
"Gn1:%ld\n", (
long)***gn1);
344 printf(
"Gn2:%ld\n", (
long)***gn2);
345 printf(
"Gn3:%ld\n", (
long)***gn3);
346 printf(
"Gn4:%ld\n", (
long)***gn4);
361 printf(
"failed total=%ld\n", (
long)total);
365 printf(
"succeeded\n");