Tervel  1.0.0
A collection of wait-free containers and algorithms.
insertat_op.h
Go to the documentation of this file.
1 
2 
3 
4 class InsertAt : public OpRecord{
5 public:
6 
7  void *rvalue;
8  int pos;
9  std::atomic<ShiftHelper<InsertAt> *> next;
10  std::atomic<bool> isComplete;
11 
12  void init(int p, void *v){
13  assert(sizeof(RcObject) <= ALIGNLEN);
14 
15  rvalue=v;
16  pos=p;
17  next=NULL;
18  isComplete=false;
19  type = dt_insertAt;
20  };
21 
22  InsertAt(int p, void *v){
23  init(p, v);
24  };
25 
26  void execute(WFVector *vec){
27  s_execute(vec);
28  }
29  bool s_execute(WFVector *vec){
30  fcount=0;
31  controlWord=(std::atomic<void *> *)&(this->isComplete);
32 
33  complete(vec, pos);
34  if (next.load() != (ShiftHelper<InsertAt> *)(0x1) ){
35 
36  return true;
37  }
38  else {
39  return false; //throw out of bounds exception.
40  }
41  };
42 
43  void cleanup(WFVector *vec, int pos);
44  bool complete(WFVector *vec, int pos){
45  bool res= completeShift<InsertAt>(this, vec, pos);
46  vec->resetMyOp();
47  return res;
48  };
49  bool tryFree();
50 
51  void *readThrough(ShiftHelper<InsertAt> *helper);
52 
53 };
54 
56  ShiftHelper<InsertAt> *parent=helper->parent;
57  if(parent == NULL){
58  ShiftHelper<InsertAt> *temp=next.load();
59  assert(temp != NULL);//because op is done
60  if(temp == helper){
61  return rvalue;
62  }
63  else{
64  return helper->rvalue;
65  }
66  }
67  else{
68  ShiftHelper<InsertAt> *temp=parent->next.load();
69  assert(temp != NULL);//because op is done
70  if(temp == helper){
71  return parent->rvalue;
72  }
73  else{
74  return helper->rvalue;
75  }
76  }
77 };
78 
79 
81  return shiftTryFree<InsertAt>((InsertAt *)this);
82 }
83 
84 
85 
86 void InsertAt::cleanup(WFVector *vec, int pos){
87 
88  /* ArrayElement *spot=vec->getSpot(pos);
89  void *current=spot->load();
90  if (Helper::unmark(current) == next.load()) {
91  spot->compare_exchange_strong(current, rvalue);
92  }*/ //Already completed in the call to complete for the first guy
93 
95  ShiftHelper<InsertAt> *helper=parent->next.load();
96  int i;
97  for (i=pos+1; helper != NULL; i++) {
98 
99  ArrayElement *spot=vec->getSpot(i);
100  void *current=spot->load();
101  if (Helper::unmark(current) == helper) {
102  spot->compare_exchange_strong(current, parent->rvalue);
103  }
104  parent=helper;
105  helper=parent->next.load();
106  }
107 
108 };
109 
Definition: insertat_op.h:4
Definition: shift_helper.h:7
std::atomic< bool > isComplete
Definition: insertat_op.h:10
bool tryFree()
Definition: insertat_op.h:80
int pos
Definition: insertat_op.h:8
bool complete(WFVector *vec, int pos)
Definition: insertat_op.h:44
void * rvalue
Definition: insertat_op.h:7
void execute(WFVector *vec)
Definition: insertat_op.h:26
std::atomic< ShiftHelper< InsertAt > * > next
Definition: insertat_op.h:9
InsertAt(int p, void *v)
Definition: insertat_op.h:22
void * readThrough(ShiftHelper< InsertAt > *helper)
Definition: insertat_op.h:55
void cleanup(WFVector *vec, int pos)
Definition: insertat_op.h:86
void init(int p, void *v)
Definition: insertat_op.h:12
bool s_execute(WFVector *vec)
Definition: insertat_op.h:29