-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrent_revisions.h
More file actions
252 lines (210 loc) · 6.03 KB
/
concurrent_revisions.h
File metadata and controls
252 lines (210 loc) · 6.03 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
#include <set>
#include <map>
#include <pthread.h>
class Revision;
class Segment;
class IAction;
class IAction {
public:
virtual void Do() = 0;
};
class IVersioned {
public:
virtual void Release(Segment *release) = 0;
virtual void Collapse(Revision *main, Segment *parent) = 0;
virtual void Merge(Revision *main, Revision *joinRev, Segment *join) = 0;
};
template <typename T>
class Versioned : private IVersioned {
std::map<int, T> versions;
public:
Versioned();
explicit Versioned(const T& v);
T Get();
void Set(T value);
private:
T Get(Revision *r);
void Set(Revision *r, T value);
void Release(Segment *segment);
void Collapse(Revision *main, Segment *parent);
void Merge(Revision *main, Revision *joinee, Segment *join);
Versioned(const Versioned&);
};
class Revision {
public:
Segment *const root;
Segment *current;
private:
pthread_t task;
static pthread_key_t key; // thread local storage
static pthread_once_t once; // once initializer
public:
~Revision();
Revision *Fork(IAction *action);
void Join(Revision *join);
static Revision* currentRevision();
private:
Revision();
Revision(Segment *root, Segment *current)
: root(root), current(current) {
}
static Revision* setCurrentRevision(Revision *r) {
printf("(%08lx) revision %10p -> %10p\n",
pthread_self(), pthread_getspecific(key), r);
return 0 == pthread_setspecific(key, r) ? r : 0;
}
static void create_tls() {
if (0 == pthread_key_create(&key, delete_tls)) {
static struct destruct {
~destruct() {
//TODO: resouce leak
//delete_tls(pthread_getspecific(key));
pthread_key_delete(key);
}
} destruct;
}
}
static void delete_tls(void *data) {
//TODO: resource leak
//delete static_cast<Revision*>(data);
//pthread_setspecific(key, 0);
}
class RunContext;
static void* run(void *);
};
class Segment {
public:
Segment *parent;
private:
int refcount;
public:
std::set<IVersioned*> written;
private:
static int versionCount;
public:
int version;
explicit Segment(Segment *parent)
: parent(parent), version(versionCount++), refcount(1) {
if (parent) ++parent->refcount;
}
void Release() {
if (0 == --refcount) {
std::set<IVersioned*>::iterator it;
for (it = written.begin(); it != written.end(); ++it) {
(*it)->Release(this);
}
if (parent) parent->Release();
}
}
void Collapse(Revision *main) {
// assert: main->current == this
while (parent != main->root && parent->refcount == 1) {
std::set<IVersioned*>::iterator it;
for (it = written.begin(); it != written.end(); ++it) {
(*it)->Collapse(main, parent);
}
parent = parent->parent;
}
}
};
pthread_key_t Revision::key; // thread local storage
pthread_once_t Revision::once = PTHREAD_ONCE_INIT;
int Segment::versionCount = 0;
template <typename T>
Versioned<T>::Versioned() { Set(T()); }
template <typename T>
Versioned<T>::Versioned(const T& v) { Set(v); }
template <typename T>
T Versioned<T>::Get() { return Get(Revision::currentRevision()); }
template <typename T>
void Versioned<T>::Set(T value) { Set(Revision::currentRevision(), value); }
template <typename T>
T Versioned<T>::Get(Revision *r) {
Segment *s = r->current;
printf("Get version %d of %p\n", s->version, this);
while (versions.find(s->version) == versions.end()) {
printf(" search parent of version %d\n", s->version);
s = s->parent;
}
return versions[s->version];
}
template <typename T>
void Versioned<T>::Set(Revision *r, T value) {
if (versions.find(r->current->version) == versions.end()) {
printf("insert version %d\n", r->current->version);
r->current->written.insert(this);
}
versions[r->current->version] = value;
}
template <typename T>
void Versioned<T>::Release(Segment *release) {
printf("erasing version %d\n", release->version);
versions.erase(release->version);
}
template <typename T>
void Versioned<T>::Collapse(Revision *main, Segment *parent) {
if (versions.find(main->current->version) == versions.end()) {
Set(main, versions[parent->version]);
}
versions.erase(parent->version);
}
template <typename T>
void Versioned<T>::Merge(Revision *main, Revision *joinee, Segment *join) {
Segment *s = joinee->current;
while (versions.find(s->version) == versions.end()) {
s = s->parent;
}
if (s == join) { // only merge if this was the last write
Set(main, versions[join->version]);
}
}
Revision::~Revision() {
delete current;
printf("deleting revision %10p\n", this);
}
class Revision::RunContext {
public:
IAction *const action;
Revision *const prev;
Revision *const next;
RunContext(IAction *action, Revision *prev, Revision *next)
: action(action), prev(prev), next(next) {}
};
Revision *Revision::Fork(IAction *action) {
Revision *r = new Revision(current, new Segment(current));
current->Release();
current = new Segment(current);
pthread_create(&task, NULL, run,
new RunContext(action, currentRevision(), r));
return r;
}
void Revision::Join(Revision *join) {
pthread_join(task, NULL);
Segment *s = join->current;
while (s != join->root) {
std::set<IVersioned*>::iterator it;
for (it = s->written.begin(); it != s->written.end(); ++it) {
(*it)->Merge(this, join, s);
}
s = s->parent;
}
join->current->Release();
current->Collapse(this);
}
Revision* Revision::currentRevision() {
pthread_once(&once, create_tls); // prepare TLS if not initialized yet
void *const revision = pthread_getspecific(key);
// in the IAction::Do, revision is set in its thread function.
// but main thread, there is no revision so create and set it.
return revision ? static_cast<Revision*>(revision)
: setCurrentRevision(new Revision(0, new Segment(0)));
}
void* Revision::run(void *context) {
RunContext ctx = *static_cast<RunContext*>(context); // copy
delete static_cast<RunContext*>(context); // release buffer on heap
pthread_setspecific(key, ctx.prev);
setCurrentRevision(ctx.next);
ctx.action->Do();
//setCurrentRevision(ctx.prev);
return 0;
}