-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfree_quadtree.cpp
More file actions
379 lines (331 loc) · 11 KB
/
free_quadtree.cpp
File metadata and controls
379 lines (331 loc) · 11 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <iostream>
#include <vector>
#include "quadtree.h"
#include "free_quadtree.h"
#include <atomic>
#include <memory>
#include <algorithm>
namespace
{
using std::vector;
using std::cout;
using std::endl;
using std::atomic;
/// Each thread has lists of pointers to delete.
/// These are compared to the non-thread-local hazard pointer list.
/// If no hazard pointer contains the pointer, it is safe for deletion.
thread_local std::vector<quadtree::PointList*> deleteList;
thread_local std::vector<quadtree::PointList*> deleteWithNodeList;
}
namespace quadtree
{
std::atomic<LockfreeQuadtree::HazardPointer*> LockfreeQuadtree::HazardPointer::head;
LockfreeQuadtree::LockfreeQuadtree(BoundingBox boundary_, size_t capacity_)
: boundary(boundary_)
, points(new PointList(capacity_))
, Nw(nullptr)
, Ne(nullptr)
, Sw(nullptr)
, Se(nullptr)
{
subdividing.store(false);
}
/*
/// @todo finish this
bool LockfreeQuadtree::Delete(const Point& p)
{
if(!boundary.Contains(p))
return false;
HazardPointer* hazardPointer = HazardPointer::Acquire(); // @todo create a finaliser/whileinscope class for HazardPointers.
while(true)
{
while(hazardPointer->Hazard.load() != points.load())
hazardPointer->Hazard.store(points.load());
PointList* oldPoints = hazardPointer->Hazard.load();
if(oldPoints == nullptr || oldPoints->Length >= oldPoints->Capacity)
break;
PointListNode* node = oldPoints->First;
while(node != nullptr)
{
if(node->NodePoint == p)
{
const bool ok = points.compare_exchange_strong(node->Next, newPoints);
}
}
PointList* newPoints = new PointList(oldPoints->Capacity);
newPoints->First = new PointListNode(p, oldPoints->First);
newPoints->Length = oldPoints->Length + 1;
const bool ok = points.compare_exchange_strong(oldPoints, newPoints);
hazardPointer->Hazard.store(nullptr);
if(ok)
{
deleteList.push_back(oldPoints);
gc();
HazardPointer::Release(hazardPointer); /// @todo create a finaliser. This is dangerous. I don't like it. Not one bit.
return true;
}
else
{
delete newPoints->First;
delete newPoints;
}
}
HazardPointer::Release(hazardPointer);
if(points.load() != nullptr)
subdivide();
return Nw.load()->Delete(p) || Ne.load()->Delete(p) || Sw.load()->Delete(p) || Se.load()->Delete(p);
}
*/
bool LockfreeQuadtree::Insert(const Point& p)
{
if(!boundary.Contains(p))
return false;
HazardPointer* hazardPointer = HazardPointer::Acquire(); // @todo create a finaliser/whileinscope class for HazardPointers.
while(true)
{
while(hazardPointer->Hazard.load() != points.load())
hazardPointer->Hazard.store(points.load());
PointList* oldPoints = hazardPointer->Hazard.load();
if(oldPoints == nullptr || oldPoints->Length >= oldPoints->Capacity)
break;
PointList* newPoints = new PointList(oldPoints->Capacity);
newPoints->First = new PointListNode(p, oldPoints->First);
newPoints->Length = oldPoints->Length + 1;
const bool ok = points.compare_exchange_strong(oldPoints, newPoints);
hazardPointer->Hazard.store(nullptr);
if(ok)
{
deleteList.push_back(oldPoints);
gc();
HazardPointer::Release(hazardPointer); /// @todo create a finaliser. This is dangerous. I don't like it. Not one bit.
return true;
}
else
{
delete newPoints->First;
delete newPoints;
}
}
HazardPointer::Release(hazardPointer);
PointList* localPoints = points.load(); // we don't need to set the Hazard Pointer because we never dereference the pointer
if(localPoints != nullptr)
subdivide();
// these will each need Hazard Pointers if it's ever possible for a subtree to be deleted
return Nw.load()->Insert(p) || Ne.load()->Insert(p) || Sw.load()->Insert(p) || Se.load()->Insert(p);
}
void LockfreeQuadtree::subdivide()
{
subdividing.store(true);
HazardPointer* hazardPointer = HazardPointer::Acquire(); // @todo pass this rather than expensively reacquiring
while(hazardPointer->Hazard.load() != points.load())
hazardPointer->Hazard.store(points.load());
PointList* oldPoints = hazardPointer->Hazard.load();
if(oldPoints == nullptr)
{
return;
}
size_t capacity = oldPoints->Capacity;
HazardPointer::Release(hazardPointer);
if(capacity == 0) // if cap=0 someone beat us here. Skip to dispersing.
{
disperse();
return;
}
const double dx = 0.000001;
// don't subdivide further if we reach the limits of double precision
if(fabs(boundary.HalfDimension.X/2.0) < dx || fabs(boundary.HalfDimension.Y/2.0) < dx)
capacity = std::numeric_limits<size_t>::max();
const Point newHalf = {boundary.HalfDimension.X / 2.0, boundary.HalfDimension.Y / 2.0};
LockfreeQuadtree* lval = nullptr;
Point newCenter = {boundary.Center.X - boundary.HalfDimension.X/2.0, boundary.Center.Y - boundary.HalfDimension.Y/2.0};
BoundingBox newBoundary = {newCenter, newHalf};
LockfreeQuadtree* q = new LockfreeQuadtree(newBoundary, capacity);
const bool nwOk = Nw.compare_exchange_strong(lval, q);
if(!nwOk)
{
delete q;
while(Nw.load() == nullptr);
}
newCenter = {boundary.Center.X + boundary.HalfDimension.X/2.0, boundary.Center.Y - boundary.HalfDimension.Y/2.0};
newBoundary = {newCenter, newHalf};
q = new LockfreeQuadtree(newBoundary, capacity);
const bool neOk = Ne.compare_exchange_strong(lval, q);
if(!neOk)
{
delete q;
while(Ne.load() == nullptr);
}
newCenter = {boundary.Center.X + boundary.HalfDimension.X/2.0, boundary.Center.Y + boundary.HalfDimension.Y/2.0};
newBoundary = {newCenter, newHalf};
q = new LockfreeQuadtree(newBoundary, capacity);
const bool seOk = Se.compare_exchange_strong(lval, q);
if(!seOk)
{
delete q;
while(Se.load() == nullptr);
}
newCenter = {boundary.Center.X - boundary.HalfDimension.X/2.0, boundary.Center.Y + boundary.HalfDimension.Y/2.0};
newBoundary = {newCenter, newHalf};
q = new LockfreeQuadtree(newBoundary, capacity);
const bool swOk = Sw.compare_exchange_strong(lval, q);
if(!swOk)
{
delete q;
while(Sw.load() == nullptr);
}
disperse();
}
void LockfreeQuadtree::disperse()
{
PointList* oldPoints;
HazardPointer* hazardPointer = HazardPointer::Acquire(); // @todo pass this rather than expensively reacquiring
while(true)
{
while(hazardPointer->Hazard.load() != points.load())
hazardPointer->Hazard.store(points.load());
oldPoints = hazardPointer->Hazard.load();
if(oldPoints == nullptr || oldPoints->Length == 0)
break;
PointList* newPoints = new PointList(0); // set the capacity to 0, so no one else tries to add
Point p = oldPoints->First->NodePoint;
newPoints->First = oldPoints->First->Next;
newPoints->Length = oldPoints->Length - 1;
/// @todo we must atomically swap the new points, and insert the point into the child.
/// we can do this by making Query() help in the dispersal
bool ok = points.compare_exchange_strong(oldPoints, newPoints);
hazardPointer->Hazard.store(nullptr);
if(!ok)
{
delete newPoints;
continue;
}
deleteWithNodeList.push_back(oldPoints);
gc();
ok = Nw.load()->Insert(p) || Ne.load()->Insert(p) || Sw.load()->Insert(p) || Se.load()->Insert(p);
}
HazardPointer::Release(hazardPointer); /// @todo create a finaliser. This is dangerous. I don't like it. Not one bit.
if(oldPoints != nullptr)
points.store(nullptr);
}
vector<Point> LockfreeQuadtree::Query(const BoundingBox& b)
{
vector<Point> found;
if(!boundary.Intersects(b))
return found;
HazardPointer* hazardPointer = HazardPointer::Acquire();
while(hazardPointer->Hazard.load() != points.load())
hazardPointer->Hazard.store(points.load());
PointList* localPoints = hazardPointer->Hazard.load();
const bool previouslySubdivided = localPoints == nullptr;
if(!previouslySubdivided && subdividing.load() == true)
{
cout << "query helping\n";
HazardPointer::Release(hazardPointer);
subdivide();
return Query(b);
}
if(localPoints != nullptr)
{
for(auto node = localPoints->First; node != nullptr; node = node->Next)
{
if(b.Contains(node->NodePoint))
found.push_back(node->NodePoint);
}
}
HazardPointer::Release(hazardPointer);
LockfreeQuadtree* nw = Nw.load();
if(nw != nullptr)
{
vector<Point> f = nw->Query(b);
found.insert(found.end(), f.begin(), f.end());
}
LockfreeQuadtree* ne = Ne.load();
if(ne != nullptr)
{
vector<Point> f = ne->Query(b);
found.insert(found.end(), f.begin(), f.end());
}
LockfreeQuadtree* sw = Sw.load();
if(sw != nullptr)
{
vector<Point> f = sw->Query(b);
found.insert(found.end(), f.begin(), f.end());
}
LockfreeQuadtree* se = Se.load();
if(se != nullptr)
{
vector<Point> f = se->Query(b);
found.insert(found.end(), f.begin(), f.end());
}
// if the tree subdivided while we were querying, redo the query. We probably missed some points as they were being moved.
if(!previouslySubdivided && (subdividing.load() == true || points.load() == nullptr))
{
cout << "query subdividied\n";
return Query(b); // absolutely necessary
}
return found;
}
/// tries to delete everything in this thread's delete lists.
/// This is part of the hazard pointer implementation
void LockfreeQuadtree::gc()
{
if(deleteList.empty() && deleteWithNodeList.empty())
return;
vector<PointList*> hazards;
for(HazardPointer* head = HazardPointer::Head(); head != nullptr; head = head->Next)
{
PointList* p = head->Hazard.load();
if(p != nullptr)
hazards.push_back(p);
}
std::sort(hazards.begin(), hazards.end(), std::less<PointList*>());
for(auto i = deleteList.begin(); i != deleteList.end();)
{
if(!std::binary_search(hazards.begin(), hazards.end(), *i))
{
delete (*i);
i = deleteList.erase(i);
}
else
++i;
}
for(auto i = deleteWithNodeList.begin(); i != deleteWithNodeList.end();)
{
if(!std::binary_search(hazards.begin(), hazards.end(), *i))
{
delete (*i)->First;
delete (*i);
i = deleteWithNodeList.erase(i);
}
else
++i;
}
// return deleteList.empty() && deleteWithNodeList.empty();
}
/// @return whether there is nothing more for this thread to delete, i.e. whether this thread's delete lists are empty.
bool LockfreeQuadtree::ThreadCanComplete()
{
return deleteWithNodeList.empty() && deleteList.empty();
}
LockfreeQuadtree::HazardPointer* LockfreeQuadtree::HazardPointer::Acquire()
{
// try to reuse a released HazardPointer
for(HazardPointer* p = LockfreeQuadtree::HazardPointer::head.load(); p != nullptr; p = p->Next)
{
if(p->active.test_and_set())
continue;
return p;
}
// no old released HazardPointers. Allocate a new one
HazardPointer* p = new HazardPointer();
p->active.test_and_set();
p->Hazard.store(nullptr);
/// @todo change this to a for loop. Because I like for.
HazardPointer* old;
do {
old = LockfreeQuadtree::HazardPointer::head.load();
p->Next = old;
} while(!LockfreeQuadtree::HazardPointer::head.compare_exchange_strong(old, p));
return p;
}
}