view AnimationMaster.c @ 272:bc5030385120

Progress -- back to compile process, fixing compile issues
author Sean Halle <seanhalle@yahoo.com>
date Tue, 05 Feb 2013 20:23:27 -0800
parents 292393c6bef1
children 40e7625e57bd
line source
1 /*
2 * Copyright 2012 OpenSourceResearchInstitute.org
3 *
4 * Licensed under BSD
5 */
9 #include <stdio.h>
10 #include <stddef.h>
12 #include "PR.h"
13 #include "VSs_impl/VSs.h"
15 /*
16 void PRHandle_CreateTask_SL(SlaveVP *slave);
18 void PRHandle_CreateSlave_SL(SlaveVP *slave);
19 void PRHandle_Dissipate_SL(SlaveVP *slave);
20 void PR_int__handle_PRServiceReq_SL(SlaveVP *slave);
21 */
22 inline void PRHandle__CreateTask( PRReqst *req, SlaveVP *slave );
23 inline void PRHandle__EndTask( PRReqst *req, SlaveVP *slave );
24 inline void PRHandle__CreateSlave(PRReqst *req, SlaveVP *slave );
25 void PRHandle__EndSlave( PRReqst *req, SlaveVP *slave );
28 //inline void masterFunction_SingleLang( PRLangEnv *protoLangEnv, AnimSlot *slot );
29 inline PRProcess * pickAProcess( AnimSlot *slot );
30 inline bool32 assignWork( PRProcess *process, AnimSlot *slot );
33 /*Note: there used to be a coreController that was another animation
34 * layer below both the masterVP and the slaveVPs.. in that case, the
35 * masterVP was a virtual processor whose processor-state was the same
36 * as a slaveVP's processor sate, both implemented as a SlaveVP struct.
37 * Have removed that, and
38 * changed the masterVP implementation. Instead of being a special version
39 * of a proto-runtime virtual processor, using the slaveVP stuct, the
40 * Master "virtual processor" is now implemented as a pthread pinned to
41 * a physical core.
42 */
44 /*This is the behavior of the Master. The physical processor switches
45 * between animating the master, and animating a slave. When a slave
46 * suspends, the PR "suspend" primitive switches the physical core over
47 * to animating the masterVP, which is implemented as a pinned pthread.
48 * This function is the behavior of that masterVP.
49 *This function's job is to manage processing
50 * requests and to trigger assignment of new work to the physical core,
51 * and to manage sharing the core among processes.
52 */
53 inline
54 bool32
55 masterFunction( AnimSlot *slot )
56 { //Scan the animation slots
57 int32 magicNumber;
58 SlaveVP *slave;
59 PRLangEnv *langEnv;
60 PRReqst *req;
61 PRProcess *process;
62 bool32 foundWork;
64 //Check if newly-done slave in slot, which will need request handled
65 //NOTE: left over from when had a coreController & MasterVP managed
66 // several slots
67 if( slot->workIsDone )
68 { slot->workIsDone = FALSE;
69 slot->needsWorkAssigned = TRUE;
71 HOLISTIC__Record_AppResponder_start; //TODO: update to check which process for each slot
72 MEAS__startReqHdlr;
75 //process the request made by the slave (held inside slave struc)
76 slave = slot->slaveAssignedToSlot;
77 req = slave->request;
79 //If the requesting slave is a slot slave, and request is not
80 // task-end, then turn it into a free task slave.
81 if( slave->typeOfVP == SlotTaskSlv && req->reqType != TaskEnd )
82 PR_int__replace_with_new_slot_slv( slave );
84 //Handle task create and end first -- they're special cases..
85 switch( req->reqType )
86 { case TaskEnd:
87 { //do PR handler, which calls lang's hdlr and does recycle of
88 // free task slave if needed -- PR handler checks for free task Slv
89 PRHandle__EndTask( req, slave ); break;
90 }
91 case TaskCreate:
92 { //Do PR's create-task handler, which calls the lang's hdlr
93 // PR handler checks for free task Slv
94 PRHandle__CreateTask( req, slave ); break;
95 }
96 case SlvCreate: PRHandle__CreateSlave( req, slave ); break;
97 case SlvDissipate: PRHandle__EndSlave( req, slave ); break;
98 case Service: PRHandle__ServiceReq( slave ); break; //resumes into Service lang env
99 case Hardware: //for future expansion
100 case IO: //for future expansion
101 case OSCall: //for future expansion
102 PR_int__throw_exception("Not implemented", slave, NULL); break;
103 case Language: //normal lang request
104 { magicNumber = req->langMagicNumber;
105 langEnv = PR_PI__give_lang_env_for( slave, magicNumber );
106 (*req->handler)( req->langReq, slave, langEnv );
107 }
108 }
110 MEAS__endReqHdlr;
111 HOLISTIC__Record_AppResponder_end;
112 } //if have request to be handled
114 //NOTE: IF statement is leftover from when master managed many slots
115 foundWork = FALSE;
116 if( slot->needsWorkAssigned ) //can probably remove IF, not that only one slot
117 {
118 HOLISTIC__Record_Assigner_start;
120 //Pick a process to get this slot
121 process = pickAProcess( slot );
123 //Scan lang environs, looking for langEnv with ready work.
124 // call the Assigner for that lang Env, to get a slave for the slot
125 foundWork =
126 assignWork( process, slot );
128 HOLISTIC__Record_Assigner_end;
130 // fixme; //make this a while loop that tries a different process if this one fails
131 }//if slot needs slave assigned
133 return foundWork;
134 }
136 /*When several processes exist, use some pattern for picking one to give
137 * the animation slot to.
138 *First, it has to be a process that has work available.
139 *For now, just do a round-robin
140 */
141 inline
142 PRProcess *
143 pickAProcess( AnimSlot *slot )
144 { int32 idx;
145 PRProcess *process;
147 for( idx = _PRTopEnv->currProcessIdx; idx < _PRTopEnv->numProcesses; idx++)
148 {
149 process = _PRTopEnv->processes[ idx ];
150 if( process->numEnvsWithWork != 0 )
151 { _PRTopEnv->currProcessIdx = idx;
152 return process;
153 }
154 }
155 for( idx = 0; idx < _PRTopEnv->currProcessIdx; idx++)
156 {
157 process = _PRTopEnv->processes[ idx ];
158 if( process->numEnvsWithWork != 0 )
159 { _PRTopEnv->currProcessIdx = idx;
160 return process;
161 }
162 }
163 //none found
164 return NULL;
165 }
167 /*This does:
168 * 1) searches the language environments for one with work ready
169 * if finds one, asks its assigner to return work
170 * 2) checks what kind of work: new task, resuming task, resuming slave
171 * if new task, gets the slot slave and assigns task to it and returns slave
172 * else, gets the slave attached to the metaTask and returns that.
173 * 3) if no work found, then prune former task slaves waiting to be recycled.
174 * If no work and no slaves to prune, check for shutdown conditions.
175 *
176 * language env keeps its own work in its own structures, and has its own
177 * assigner. It chooses
178 * However, include a switch that switches-in an override assigner, which
179 * sees all the work in all the language env's. This is most likely
180 * generated by static tools and included in the executable. That means it
181 * has to be called via a registered pointer from here. The idea is that
182 * the static tools know which languages are grouped together.. and the
183 * override enables them to generate a custom assigner that uses info from
184 * all the languages in a unified way.. Don't really expect this to happen,
185 * but am making it possible.
186 */
187 inline
188 bool32
189 assignWork( PRProcess *process, AnimSlot *slot )
190 { int32 coreNum;
192 coreNum = slot->coreSlotIsOn;
194 if( process->overrideAssigner != NULL )
195 { if( process->numEnvsWithWork != 0 )
196 { (*process->overrideAssigner)( process, slot ); //calls PR fn that inserts work into slot
197 goto ReturnAfterAssigningWork; //quit for-loop, cause found work
198 }
199 else
200 goto NoWork;
201 }
203 //If here, then no override assigner, so search language envs for work
204 int32 envIdx, numEnvs; PRLangEnv **langEnvsList, *langEnv;
205 langEnvsList = process->langEnvsList;
206 numEnvs = process->numLangEnvs;
207 for( envIdx = 0; envIdx < numEnvs; envIdx++ ) //keep langEnvs in hash & array
208 { langEnv = langEnvsList[envIdx];
209 if( langEnv->numReadyWork > 0 )
210 { bool32
211 didAssignWork =
212 (*langEnv->workAssigner)( langEnv, slot ); //assigner calls PR to put slave/task into slot
214 if(didAssignWork)
215 { langEnv->numReadyWork -= 1;
216 if( langEnv->numReadyWork == 0 )
217 { process->numEnvsWithWork -= 1;
218 }
219 goto ReturnAfterAssigningWork; //quit for-loop, 'cause found work
220 }
221 else
222 goto NoWork; //quit for-loop, cause found work
224 //NOTE: bad search alg -- should start where left off, then wrap around
225 }
226 }
227 //If reach here, then have searched all langEnv's & none have work..
229 NoWork: //No work, if end up here..
230 {
231 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
232 returnSlv = process->idleSlv[coreNum][0]; //only one slot now, so [0]
234 //things that would normally happen in resume(), but idle VPs
235 // never go there
236 returnSlv->numTimesAssignedToASlot++; //gives each idle unit a unique ID
237 Unit newU;
238 newU.vp = returnSlv->slaveNum;
239 newU.task = returnSlv->numTimesAssignedToASlot;
240 addToListOfArrays(Unit,newU,process->unitList);
242 if (returnSlv->numTimesAssignedToASlot > 1) //make a dependency from prev idle unit
243 { Dependency newD; // to this one
244 newD.from_vp = returnSlv->slaveNum;
245 newD.from_task = returnSlv->numTimesAssignedToASlot - 1;
246 newD.to_vp = returnSlv->slaveNum;
247 newD.to_task = returnSlv->numTimesAssignedToASlot;
248 addToListOfArrays(Dependency, newD ,process->ctlDependenciesList);
249 }
250 #endif
251 HOLISTIC__Record_Assigner_end;
252 return FALSE;
253 }
255 ReturnAfterAssigningWork: //All paths goto here.. to provide single point for holistic..
256 {
257 HOLISTIC__Record_Assigner_end;
258 return TRUE;
259 }
260 }
264 /*This is first thing called when creating a slave.. it hands off to the
265 * langlet's creator, then adds updates of its own..
266 *
267 *There's a question of things like lang data, meta tasks, and such..
268 *In creator, only PR related things happen, and things for the langlet whose
269 * creator construct was used.
270 *
271 *Other langlets still get a chance to create langData -- but by registering a
272 * "createLangData" handler in the langEnv. When a construct of the langlet
273 * calls "PR__give_lang_data()", if there is no langData for that langlet,
274 * the PR will call the creator in the langlet's langEnv, place whatever it
275 * makes as the langData in that slave for that langlet, and return that langData
276 *
277 *So, as far as counting things, a langlet is only allowed to count creation
278 * of slaves it creates itself.. may have to change this later.. add a way for
279 * langlet to register a trigger Fn called each time a slave gets created..
280 * need more experience with what langlets will do at create time.. think Cilk
281 * has interesting create behavior.. not sure how that will differ in light
282 * of true tasks and langlet approach. Look at it after all done and start
283 * modifying the langs to be langlets..
284 *
285 *PR itself needs to create the slave, then update numLiveSlaves in process,
286 * copy processID from requestor to newly created
287 */
288 inline
289 void
290 PRHandle__CreateSlave( PRReqst *req, SlaveVP *slave )
291 { SlaveVP *newSlv;
292 PRProcess *process;
293 PRLangEnv *protoLangEnv;
295 process = slave->processSlaveIsIn;
296 protoLangEnv = PR_int__give_proto_lang_env_for_slave__ML( slave, req->langMagicNumber );
298 //create handler, or a future request handler will call PR_PI__make_slave_ready
299 // which will in turn handle updating which langlets and which processes have
300 // work available.
301 //NOTE: create slv has diff prototype than standard reqst hdlr
302 newSlv =
303 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv));
305 newSlv->typeOfVP = GenericSlv;
306 newSlv->processSlaveIsIn = process;
307 newSlv->ID = req->ID;
308 process->numLiveGenericSlvs += 1; //not same as work ready!
309 }
311 /*The dissipate handler has to, update the number of slaves of the type, within
312 * the process, and call the langlet handler linked into the request,
313 * and after that returns, then call the PR function that frees the slave state
314 * (or recycles the slave).
315 *
316 *The PR function that frees the slave state has to also free all of the
317 * langData in the slave.. or else reset all of the langDatas.. by, say, marking
318 * them, then in PR__give_langData( magicNum ) call the langlet registered
319 * "resetLangData" Fn.
320 */
321 inline
322 void
323 PRHandle__EndSlave( PRReqst *req, SlaveVP *slave )
324 { PRProcess *process;
325 PRLangEnv *protoLangEnv;
327 process = slave->processSlaveIsIn;
329 //do the language's dissipate handler
330 protoLangEnv = PR_int__give_proto_lang_env_for_slave__ML( slave, slave->request->langMagicNumber );
332 if(req->handler != NULL)
333 (*req->handler)( req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
335 process->numLiveGenericSlvs -= 1;
336 PR_int__recycle_slave( slave );
337 //NOTE: dissipate is unrelated to work available (just in case wondering)
339 //check End Of Process Condition
340 if( process->numLiveTasks == 0 &&
341 process->numLiveGenericSlvs == 0 )
342 PR_SS__shutdown_process( process );
343 }
345 /*Create task is a special form, that has PR behavior in addition to plugin
346 * behavior. Master calls this first, and it then calls the plugin's
347 * create task handler.
348 *
349 *Note: the requesting slave must be either generic slave or free task slave
350 */
351 inline
352 void
353 PRHandle__CreateTask( PRReqst *req, SlaveVP *slave )
354 { PRMetaTask *metaTask;
355 PRProcess *process;
356 PRLangEnv *protoLangEnv;
357 void *task;
359 process = slave->processSlaveIsIn;
361 protoLangEnv = PR_int__give_proto_lang_env_for_slave__ML( slave,
362 req->langMagicNumber );
364 //Do the langlet's create-task handler, which keeps the task
365 // inside the langlet's lang env, but returns the langMetaTask
366 // so PR can put stuff into the prolog
367 task =
368 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
369 metaTask = PR_int__give_prolog_of_task( task );
370 metaTask->ID = req->ID; //may be NULL
371 metaTask->topLevelFn = req->topLevelFn;
372 metaTask->initData = req->initData;
374 process->numLiveTasks += 1;
376 return;
377 }
379 /*When a task ends, have two scenarios: 1) task ran to completion, or 2) task
380 * has been suspended at some point in its code.
381 *For 1, just decr count of live tasks (and check for end condition) -- the
382 * master loop will decide what goes into the slot freed up by this task end,
383 * so, here, don't worry about assigning a new task to the slot slave.
384 *For 2, the task's slot slave has been converted to a free task slave, which
385 * now has nothing more to do, so send it to the recycle Q (which includes
386 * freeing all the langData and meta task structs alloc'd for it). Then
387 * decrement the live task count and check end condition.
388 *
389 *PR has to update count of live tasks, and check end of process condition.
390 * The "main" can invoke constructs that wait for a process to end, so when
391 * end detected, have to resume what's waiting..
392 *Thing is, that wait involves the main OS thread. That means
393 * PR internals have to do OS thread signaling. Want to do that in the
394 * core controller, which has the original stack of an OS thread. So the
395 * end process handling happens in the core controller.
396 *
397 *So here, when detect process end, signal to the core controller, which will
398 * then do the condition variable notify to the OS thread that's waiting.
399 *
400 *Note: slave may be either a slot slave or a free task slave.
401 */
402 inline
403 void
404 PRHandle__EndTask( PRReqst *req, SlaveVP *requestingSlv )
405 { void *langEnv;
406 PRProcess *process;
407 void *langMetaTask;
409 langEnv = PR_int__give_lang_env_of_req( req, requestingSlv ); //magic num in req
410 langMetaTask = PR_int__give_lang_meta_task_from_slave( requestingSlv, req->langMagicNumber);
412 //Do the langlet's request handler
413 //Want to keep PR structs hidden from plugin, so extract langReq..
414 (*req->handler)( req->langReq, requestingSlv, langEnv );
416 //Now that the langlet's done with it, recycle the slave if it's a freeTaskSlv
417 if( requestingSlv->typeOfVP == FreeTaskSlv )
418 PR_int__recycle_slave( requestingSlv ); //Doesn't decr num live slaves
420 process->numLiveTasks -= 1;
421 //NOTE: end-task is unrelated to work available (just in case wondering)
423 //check End Of Process Condition
424 if( process->numLiveTasks == 0 &&
425 process->numLiveGenericSlvs == 0 )
426 { //Tell the core controller to do wakeup of any waiting OS thread
427 PR_SS__shutdown_process( process );
428 }
429 }
432 /*This is for OS requests and PR infrastructure requests, which are not
433 * part of the PRServ language -- this is for things that have to be in the
434 * infrastructure of PR itself, such as I/O requests, which have to go through
435 * pthreads inside the core controller..
436 *
437 *As of Jan 2013, doesn't do much of anything..
438 */
439 void inline
440 PRHandle__ServiceReq( SlaveVP *requestingSlv )
441 { PRReqst *req;
442 PRServReq *langReq;
443 void *langEnv;
444 int32 magicNumber;
447 req = requestingSlv->request;
449 magicNumber = req->langMagicNumber;
450 langEnv = PR_PI__give_lang_env_for( slave, magicNumber );
452 langReq = PR_PI__take_lang_reqst_from(req);
453 if( langReq == NULL ) return;
454 switch( langReq->reqType ) //lang handlers are all in other file
455 {
456 case make_probe: handleMakeProbe( langReq, langEnv );
457 break;
458 case throw_excp: handleThrowException( langReq, langEnv );
459 break;
460 }
461 }