-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.c
216 lines (186 loc) · 5.34 KB
/
set.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "set.h"
#include "capsule.h"
#include "flow.h" //join declaration
#include "memUtilities.h"
#include "procMap.h" //hard who am I
#include "pStack.h"
#include "scheduler.h"
#include "assertion.h"
#include "typesAndDecs.h"
/*
* points to a SET_POOL_SIZE array of sets in PM
*/
PMem setPool;
Set* quickGetSet(int idx) {
return ((Set*) PMAddr(setPool))+idx;
}
/*
* persistent function to be used with pcall, takes continuation Job
* as an argument, and a owner tag, acquires a new set and initializes it. Then returns
* the idx of that set in the pool. this should be stored and
* retrieved during a join call.
*/
Capsule getAndInitSet(void) {
pPushCalleeArg((int)0);
//Job, owner goes through to cnt
pcall(&loopGetSet, &gotSet);
}
Capsule aquireSet(void);
/*
* gets an int, tries to acquire said set
*
* does not block, fail assert if goes off the end of the list,
* consider if that is acceptable
*/
Capsule loopGetSet(void) {
int idx;
pPopArg(idx);
rassert(idx < SET_POOL_SIZE, "could not allocate an additional set from set pool");
Set* ptr = quickGetSet(idx); //in pool
if (ptr->tagAndData.data) { //TODO atomic read. write is a CAM
pPushCntArg(idx+1) //move on, this is in use
pcnt(&loopGetSet);
} else {
//open? try to grab
pPushCntArg(ptr->tagAndData);
pPushCntArg(idx);
pcnt(&aquireSet);
}
}
/*
* gets idx of attempted acquire, ret idx on success, popping
* newOwner, cnt loop+1 on
* fail, repush newOwnser (DOES NOT BLOCK, safe)
*/
Capsule aquireSet(void) {
int idx;
struct swappable old;
pPopArg(old);
pPopArg(idx);
int newOwner;
pPopArg(newOwner); //from getAndInitSet
struct swappable replacement;
replacement.data = 2;
replacement.owner = newOwner;
replacement.isLast = 0; //not in use;
Set* ptr = quickGetSet(idx);
CAM(&(ptr->tagAndData), old, replacement);
if (ptr->tagAndData.owner == newOwner) {
//success!, return idx
pret(idx);
} else {
//failed, back to loop
pPushCntArg(newOwner); //save for next time
pPushCntArg(idx+1);
pcnt(&loopGetSet);
}
}
/*
* gets a idx of a grabbed set and a Job, do init
*/
Capsule gotSet(void) {
int idx;
Job postJoin;
pPopArg(idx);
pPopArg(postJoin);
Set* setPtr = quickGetSet(idx);
//owner is set already
setPtr->continuation = postJoin;
pret(idx);
}
/*
* takes no args, jumps to scheduler or cnt depending on order, reach
* via pcnt
*/
Capsule join(void) {
Capsule installed = quickGetInstalled();
int mySet = installed.joinLoc;
byte mySide = installed.forkSide;
int expectedOwner = installed.expectedOwner;
pPushCntArg(mySide);
pPushCntArg(expectedOwner);
pPushCntArg(mySet);
pcnt(&checkOut);
}
/*
* takes idx and side
*/
Capsule checkOut(void) {
int idx;
int expectedOwner;
byte mySide;
pPopArg(idx);
pPopArg(expectedOwner);
pPopArg(mySide);
Set* ptr = quickGetSet(idx);
struct swappable current;
struct swappable rep;
/*
* This is slightly strange, but I am worried about proceeding
* thinking that a CAM has worked without checking even in
* situation where I think it is safe. so even if you edit to be
* the last, then you still go around again to check that you
* are. Someone else can go over this and confirm that it is safe
* and optimize or whatever later
*/
while (true) {
current = ptr->tagAndData;
rep.owner = expectedOwner;
if (current.owner == expectedOwner) {
if (current.data == 2) {
//untouched
rep.data = 1;
rep.isLast = 0x02 | mySide;
CAM(&(ptr->tagAndData), current, rep);
} else if (current.data == 1 && current.isLast == (0x02 | mySide)) {
//I was the last edit and the first to arrive, free to leave
pcnt(&scheduler);
} else if (current.data == 1 && current.isLast == (0x02 | ((~mySide) & 0x01))) {
//I am second to arrive and the other guy has edited
rep.data = 0;
rep.isLast = 0x02 | mySide;
CAM(&(ptr->tagAndData), current, rep);
} else if (current.data == 0 && current.isLast == (0x02 | mySide)) {
//I was last edit and last to arrive, do the work
pPushCntArg(idx);
pcnt(&cleanup);
} else if (current.data == 0 && current.isLast == (0x02 | ((~mySide) & 0x01))) {
//I was not the last edit or the last to arrive, am
//free to go
pcnt(&scheduler);
} else {
/*
* current.data == 0 && !(current.isLast & 0x02)
*
* I am last to arrive and the other guy has started
* cleanup
*/
pcnt(&scheduler);
}
} else {
/*
* owner has changed, the other guy has cleaned up and
* someone else has moved in
*/
pcnt(&scheduler);
}
}
}
/*
* I was last and have to pick up the work. takes idx
*/
Capsule cleanup(void) {
int idx;
pPopArg(idx);
Set* ptr = quickGetSet(idx);
pPushCntArg(ptr->continuation);
pPushCntArg(idx);
pcnt(&cleanupCnt);
}
Capsule cleanupCnt(void) {
int idx;
pPopArg(idx);
quickGetSet(idx)->tagAndData.isLast = 0; //not in use anymore
//job goes through
pcnt(&singleJobPush); //handover to scheduler to work there
}