-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOAGGraph.cpp
More file actions
227 lines (201 loc) · 7.09 KB
/
OAGGraph.cpp
File metadata and controls
227 lines (201 loc) · 7.09 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
//
// OAGGraph.cpp
// OpenAttributeGraphCxx
#include <OpenAttributeGraph/OAGGraph.h>
#include <OpenAttributeGraphCxx/Graph/Graph.hpp>
#include <OpenAttributeGraphCxx/Misc/assert.hpp>
#include <OpenAttributeGraphCxx/Data/ClosureFunction.hpp>
#include <pthread.h>
OAGGraphRef OAGGraphCreate() {
return OAGGraphCreateShared(nullptr);
}
OAGGraphRef OAGGraphCreateShared(OAGGraphRef storage) {
const CFIndex extraSize = sizeof(OAGGraphStorage)-sizeof(CFRuntimeBase);
OAGGraphRef instance = (OAGGraphRef)_CFRuntimeCreateInstance(kCFAllocatorDefault, OAGGraphGetTypeID(), extraSize, nullptr);
if (instance == nullptr) {
OAG::precondition_failure("memory allocation failure.");
}
OAG::Graph *graph = nullptr;
if (storage == nullptr) {
graph = new OAG::Graph();
} else {
if (storage->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
graph = &storage->context.get_graph();
// [graph+0x10c] += 1
}
// AG::Context(instance->graph, graph)
// [graph+0x10c] -= 1
// if ([graph+0x10c] == 0) {
// delete graph
// }
instance->context.setInvalid(false);
return instance;
}
namespace {
CFRuntimeClass &graph_type_id() {
static auto dealloc = [](const void* ptr) {
OAGGraphRef storage = (OAGGraphRef)ptr;
if (storage->context.isInvalid()) {
return;
}
storage->context.~Context();
};
static CFRuntimeClass klass = {
0,
"OAGGraphStorage",
NULL, // init
NULL, // copy
dealloc,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
0,
};
return klass;
}
}
CFTypeID OAGGraphGetTypeID() {
static CFTypeID type = _CFRuntimeRegisterClass(&graph_type_id());
return type;
}
void OAGGraphStartProfiling(_Nullable OAGGraphRef graph) {
if (!graph) {
OAG::Graph::all_start_profiling(1);
return;
}
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
graph->context.get_graph().start_profiling(1);
}
void OAGGraphStopProfiling(_Nullable OAGGraphRef graph) {
if (!graph) {
OAG::Graph::all_stop_profiling();
return;
}
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
graph->context.get_graph().stop_profiling();
}
void OAGGraphResetProfile(_Nullable OAGGraphRef graph) {
// TODO
}
const void * _Nullable OAGGraphGetContext(OAGGraphRef graph) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
return graph->context.get_context();
}
void OAGGraphSetContext(OAGGraphRef graph, const void * _Nullable context) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
graph->context.set_context(context);
}
OAGUnownedGraphContextRef OAGGraphGetGraphContext(OAGGraphRef graph) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
return reinterpret_cast<OAGUnownedGraphContextRef>(&graph->context.get_graph());
}
void OAGGraphInvalidate(OAGGraphRef graph) {
if (graph->context.isInvalid()) {
return;
}
graph->context.~Context();
graph->context.setInvalid(true);
}
uint32_t OAGGraphInternAttributeType(OAGUnownedGraphContextRef graph, OAGTypeID type,
const OAGAttributeType *(*make_attribute_type)(const void *context OAG_SWIFT_CONTEXT) OAG_SWIFT_CC(swift),
const void *context) {
// TODO
return 0;
}
void OAGGraphInvalidateAllValues(OAGGraphRef graph) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
graph->context.get_graph().value_mark_all();
}
void OAGGraphSetInvalidationCallback(OAGGraphRef graph,
const void (*_Nullable function)(const void * _Nullable context OAG_SWIFT_CONTEXT, OAGAttribute) OAG_SWIFT_CC(swift),
const void * _Nullable context) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
graph->context.set_invalidation_callback(OAG::ClosureFunction<void, OAGAttribute>(function, context));
}
void OAGGraphSetUpdateCallback(OAGGraphRef graph,
const void (*_Nullable function)(const void * _Nullable context OAG_SWIFT_CONTEXT) OAG_SWIFT_CC(swift),
const void * _Nullable context) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
graph->context.set_update_callback(OAG::ClosureFunction<void>(function, context));
}
uint64_t OAGGraphGetCounter(OAGGraphRef graph, OAGGraphCounterQueryType query) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
OAG::Graph::Context& context = graph->context;
switch (query) {
case OAGGraphCounterQueryTypeNodes:
return context.get_graph().get_counter_0();
case OAGGraphCounterQueryTypeTransactions:
return context.get_graph().get_counter_1();
case OAGGraphCounterQueryTypeUpdates:
return context.get_graph().get_counter_2();
case OAGGraphCounterQueryTypeChanges:
return context.get_graph().get_counter_3();
case OAGGraphCounterQueryTypeContextID:
return context.get_graph().get_counter_4();
case OAGGraphCounterQueryTypeGraphID:
return context.get_graph().get_counter_5();
case OAGGraphCounterQueryTypeContextThreadUpdating:
return context.thread_is_updating();
case OAGGraphCounterQueryTypeThreadUpdating:
return context.get_graph().thread_is_updating();
case OAGGraphCounterQueryTypeContextNeedsUpdate:
return context.get_graph().get_counter_8();
case OAGGraphCounterQueryTypeNeedsUpdate:
return context.get_graph().get_counter_9();
case OAGGraphCounterQueryTypeMainThreadUpdates:
return context.get_graph().get_counter_10();
default:
return 0;
}
}
void OAGGraphSetUpdate(const void * _Nullable value) {
pthread_setspecific(OAG::Graph::current_key(), value);
}
const void * _Nullable OAGGraphClearUpdate(void) {
void * value = pthread_getspecific(OAG::Graph::current_key());
bool isDirty = (uint64_t)value & 0x1;
if (value != nullptr && isDirty) {
pthread_setspecific(OAG::Graph::current_key(), (void *)((uint64_t)value | 0x1));
}
return value;
}
void OAGGraphSetNeedsUpdate(OAGGraphRef graph) {
if (graph->context.isInvalid()) {
OAG::precondition_failure("invalidated graph");
}
// graph->context->set_needs_update();
}
bool OAGGraphAnyInputsChanged(const OAGAttribute *excluded_inputs, size_t count) {
// TODO
return false;
}
bool OAGGraphBeginDeferringSubgraphInvalidation(OAGGraphRef graph) {
// TODO
return false;
}
void OAGGraphEndDeferringSubgraphInvalidation(OAGGraphRef graph, bool was_deferring) {
// TODO
}