Vuo  2.0.3
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  delete static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
104  if (nodeContext->executingGroup)
105  dispatch_release(nodeContext->executingGroup);
106  free(nodeContext->outputEvents);
107  delete static_cast<std::mutex *>(nodeContext->executingEventIdsSync);
108  free(nodeContext);
109 }
110 
114 void vuoSetPortContextEvent(struct PortContext *portContext, bool event)
115 {
116  portContext->event = event;
117 }
118 
122 void vuoSetPortContextData(struct PortContext *portContext, void *data)
123 {
124  portContext->data = data;
125 }
126 
130 void vuoSetPortContextTriggerFunction(struct PortContext *portContext, void *triggerFunction)
131 {
132  portContext->triggerFunction = triggerFunction;
133 }
134 
138 bool vuoGetPortContextEvent(struct PortContext *portContext)
139 {
140  return portContext->event;
141 }
142 
146 void * vuoGetPortContextData(struct PortContext *portContext)
147 {
148  return portContext->data;
149 }
150 
154 dispatch_queue_t vuoGetPortContextTriggerQueue(struct PortContext *portContext)
155 {
156  return portContext->triggerQueue;
157 }
158 
162 dispatch_semaphore_t vuoGetPortContextTriggerSemaphore(struct PortContext *portContext)
163 {
164  return portContext->triggerSemaphore;
165 }
166 
171 {
172  return portContext->triggerFunction;
173 }
174 
178 void vuoRetainPortContextData(struct PortContext *portContext)
179 {
180  portContext->dataRetained = true;
181 }
182 
186 void vuoSetNodeContextPortContexts(struct NodeContext *nodeContext, struct PortContext **portContexts, unsigned long portContextCount)
187 {
188  nodeContext->portContexts = portContexts;
189  nodeContext->portContextCount = portContextCount;
190 }
191 
196 {
197  free(nodeContext->instanceData);
198  nodeContext->instanceData = instanceData;
199 }
200 
204 void vuoSetNodeContextClaimingEventId(struct NodeContext *nodeContext, unsigned long claimingEventId)
205 {
206  nodeContext->claimingEventId = claimingEventId;
207 }
208 
212 void vuoSetNodeContextOutputEvent(struct NodeContext *nodeContext, size_t index, bool event)
213 {
214  nodeContext->outputEvents[index] = event;
215 }
216 
220 struct PortContext * vuoGetNodeContextPortContext(struct NodeContext *nodeContext, size_t index)
221 {
222  return nodeContext->portContexts[index];
223 }
224 
228 void * vuoGetNodeContextInstanceData(struct NodeContext *nodeContext)
229 {
230  return nodeContext->instanceData;
231 }
232 
236 dispatch_semaphore_t vuoGetNodeContextSemaphore(struct NodeContext *nodeContext)
237 {
238  return nodeContext->semaphore;
239 }
240 
244 unsigned long vuoGetNodeContextClaimingEventId(struct NodeContext *nodeContext)
245 {
246  return nodeContext->claimingEventId;
247 }
248 
252 dispatch_group_t vuoGetNodeContextExecutingGroup(struct NodeContext *nodeContext)
253 {
254  return nodeContext->executingGroup;
255 }
256 
260 bool vuoGetNodeContextOutputEvent(struct NodeContext *nodeContext, size_t index)
261 {
262  return nodeContext->outputEvents[index];
263 }
264 
268 void vuoResetNodeContextEvents(struct NodeContext *nodeContext)
269 {
270  for (size_t i = 0; i < nodeContext->portContextCount; ++i)
271  nodeContext->portContexts[i]->event = false;
272 }
273 
277 void vuoStartedExecutingEvent(struct NodeContext *nodeContext, unsigned long eventId)
278 {
279  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
280 
281  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
282  executingEventIds->clear();
283  executingEventIds->push_back(eventId);
284 }
285 
289 void vuoSpunOffExecutingEvent(struct NodeContext *nodeContext, unsigned long eventId)
290 {
291  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
292 
293  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
294  if (! executingEventIds->empty())
295  executingEventIds->push_back(eventId);
296 }
297 
302 bool vuoFinishedExecutingEvent(struct NodeContext *nodeContext, unsigned long eventId)
303 {
304  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
305 
306  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
307  auto found = find(executingEventIds->begin(), executingEventIds->end(), eventId);
308  if (found != executingEventIds->end())
309  {
310  executingEventIds->erase(found);
311  return executingEventIds->empty();
312  }
313  return false;
314 }
315 
320 unsigned long vuoGetOneExecutingEvent(struct NodeContext *nodeContext)
321 {
322  std::lock_guard<std::mutex> guard(*static_cast<std::mutex *>(nodeContext->executingEventIdsSync));
323 
324  vector<unsigned long> *executingEventIds = static_cast< vector<unsigned long> *>(nodeContext->executingEventIds);
325  return executingEventIds->at(0);
326 }