Vuo  2.0.0
VuoRuntimeContext.cc
Go to the documentation of this file.
1 
10 #include "VuoRuntimeContext.hh"
11 #include "VuoEventLoop.h"
12 #include <mutex>
13 
17 struct PortContext * vuoCreatePortContext(void *data, bool isTrigger, const char *triggerQueueName)
18 {
19  struct PortContext *portContext = (struct PortContext *)malloc(sizeof(struct PortContext));
20  portContext->event = false;
21  portContext->data = data;
22  portContext->dataRetained = false;
23  portContext->triggerFunction = NULL;
24 
25  if (isTrigger)
26  {
27  portContext->triggerQueue = dispatch_queue_create(triggerQueueName, VuoEventLoop_getDispatchInteractiveAttribute());
28  portContext->triggerSemaphore = dispatch_semaphore_create(1);
29  }
30  else
31  {
32  portContext->triggerQueue = NULL;
33  portContext->triggerSemaphore = NULL;
34  }
35 
36  return portContext;
37 }
38 
42 struct NodeContext * vuoCreateNodeContext(bool hasInstanceData, bool isComposition, size_t outputEventCount)
43 {
44  struct NodeContext *nodeContext = (struct NodeContext *)malloc(sizeof(struct NodeContext));
45  nodeContext->portContexts = NULL;
46  nodeContext->portContextCount = 0;
47  nodeContext->semaphore = dispatch_semaphore_create(1);
48  nodeContext->claimingEventId = 0;
49 
50  if (isComposition)
51  {
52  nodeContext->executingEventIds = new vector<unsigned long>();
53  nodeContext->executingGroup = dispatch_group_create();
54  nodeContext->outputEvents = (bool *)calloc(outputEventCount, sizeof(bool));
55  nodeContext->executingEventIdsSync = new std::mutex();
56  }
57  else
58  {
59  nodeContext->executingEventIds = NULL;
60  nodeContext->executingGroup = NULL;
61  nodeContext->outputEvents = NULL;
62  nodeContext->executingEventIdsSync = NULL;
63  }
64 
65  if (hasInstanceData)
66  {
67  void **instanceData = (void **)malloc(sizeof(void *));
68  *instanceData = NULL;
69  nodeContext->instanceData = (void *)instanceData;
70  }
71  else
72  nodeContext->instanceData = NULL;
73 
74  return nodeContext;
75 }
76 
80 void vuoFreePortContext(struct PortContext *portContext)
81 {
82  if (! portContext->dataRetained)
83  free(portContext->data);
84  if (portContext->triggerQueue)
85  dispatch_release(portContext->triggerQueue);
86  if (portContext->triggerSemaphore)
87  dispatch_release(portContext->triggerSemaphore);
88  free(portContext);
89 }
90 
94 void vuoFreeNodeContext(struct NodeContext *nodeContext)
95 {
96  for (size_t i = 0; i < nodeContext->portContextCount; ++i)
97  vuoFreePortContext(nodeContext->portContexts[i]);
98 
99  free(nodeContext->portContexts);
100  free(nodeContext->instanceData);
101  if (nodeContext->semaphore)
102  dispatch_release(nodeContext->semaphore);
103  if (nodeContext->executingEventIds)
104  delete static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
105  if (nodeContext->executingGroup)
106  dispatch_release(nodeContext->executingGroup);
107  free(nodeContext->outputEvents);
108  if (nodeContext->executingEventIdsSync)
109  delete static_cast<std::mutex *>(nodeContext->executingEventIdsSync);
110  free(nodeContext);
111 }
112 
116 void vuoSetPortContextEvent(struct PortContext *portContext, bool event)
117 {
118  portContext->event = event;
119 }
120 
124 void vuoSetPortContextData(struct PortContext *portContext, void *data)
125 {
126  portContext->data = data;
127 }
128 
132 void vuoSetPortContextTriggerFunction(struct PortContext *portContext, void *triggerFunction)
133 {
134  portContext->triggerFunction = triggerFunction;
135 }
136 
140 bool vuoGetPortContextEvent(struct PortContext *portContext)
141 {
142  return portContext->event;
143 }
144 
148 void * vuoGetPortContextData(struct PortContext *portContext)
149 {
150  return portContext->data;
151 }
152 
156 dispatch_queue_t vuoGetPortContextTriggerQueue(struct PortContext *portContext)
157 {
158  return portContext->triggerQueue;
159 }
160 
164 dispatch_semaphore_t vuoGetPortContextTriggerSemaphore(struct PortContext *portContext)
165 {
166  return portContext->triggerSemaphore;
167 }
168 
173 {
174  return portContext->triggerFunction;
175 }
176 
180 void vuoRetainPortContextData(struct PortContext *portContext)
181 {
182  portContext->dataRetained = true;
183 }
184 
188 void vuoSetNodeContextPortContexts(struct NodeContext *nodeContext, struct PortContext **portContexts, unsigned long portContextCount)
189 {
190  nodeContext->portContexts = portContexts;
191  nodeContext->portContextCount = portContextCount;
192 }
193 
198 {
199  free(nodeContext->instanceData);
200  nodeContext->instanceData = instanceData;
201 }
202 
206 void vuoSetNodeContextClaimingEventId(struct NodeContext *nodeContext, unsigned long claimingEventId)
207 {
208  nodeContext->claimingEventId = claimingEventId;
209 }
210 
214 void vuoSetNodeContextOutputEvent(struct NodeContext *nodeContext, size_t index, bool event)
215 {
216  nodeContext->outputEvents[index] = event;
217 }
218 
222 struct PortContext * vuoGetNodeContextPortContext(struct NodeContext *nodeContext, size_t index)
223 {
224  return nodeContext->portContexts[index];
225 }
226 
230 void * vuoGetNodeContextInstanceData(struct NodeContext *nodeContext)
231 {
232  return nodeContext->instanceData;
233 }
234 
238 dispatch_semaphore_t vuoGetNodeContextSemaphore(struct NodeContext *nodeContext)
239 {
240  return nodeContext->semaphore;
241 }
242 
246 unsigned long vuoGetNodeContextClaimingEventId(struct NodeContext *nodeContext)
247 {
248  return nodeContext->claimingEventId;
249 }
250 
254 dispatch_group_t vuoGetNodeContextExecutingGroup(struct NodeContext *nodeContext)
255 {
256  return nodeContext->executingGroup;
257 }
258 
262 bool vuoGetNodeContextOutputEvent(struct NodeContext *nodeContext, size_t index)
263 {
264  return nodeContext->outputEvents[index];
265 }
266 
270 void vuoResetNodeContextEvents(struct NodeContext *nodeContext)
271 {
272  for (size_t i = 0; i < nodeContext->portContextCount; ++i)
273  nodeContext->portContexts[i]->event = false;
274 }
275 
279 void vuoStartedExecutingEvent(struct NodeContext *nodeContext, unsigned long eventId)
280 {
281  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
282 
283  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
284  executingEventIds->clear();
285  executingEventIds->push_back(eventId);
286 }
287 
291 void vuoSpunOffExecutingEvent(struct NodeContext *nodeContext, unsigned long eventId)
292 {
293  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
294 
295  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
296  if (! executingEventIds->empty())
297  executingEventIds->push_back(eventId);
298 }
299 
304 bool vuoFinishedExecutingEvent(struct NodeContext *nodeContext, unsigned long eventId)
305 {
306  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
307 
308  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
309  auto found = find(executingEventIds->begin(), executingEventIds->end(), eventId);
310  if (found != executingEventIds->end())
311  {
312  executingEventIds->erase(found);
313  return executingEventIds->empty();
314  }
315  return false;
316 }
317 
322 unsigned long vuoGetOneExecutingEvent(struct NodeContext *nodeContext)
323 {
324  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
325 
326  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
327  return executingEventIds->at(0);
328 }