annotate AnimationMaster.c @ 273:40e7625e57bd

Compiles and runs, up to end of process, working on end process and shutdown
author Sean Halle <seanhalle@yahoo.com>
date Sat, 02 Mar 2013 09:43:45 -0800
parents bc5030385120
children 1d7ea1b0f176
rev   line source
seanhalle@230 1 /*
seanhalle@270 2 * Copyright 2012 OpenSourceResearchInstitute.org
seanhalle@230 3 *
seanhalle@230 4 * Licensed under BSD
seanhalle@230 5 */
seanhalle@230 6
seanhalle@230 7
seanhalle@230 8
seanhalle@230 9 #include <stdio.h>
seanhalle@230 10 #include <stddef.h>
seanhalle@230 11
seanhalle@260 12 #include "PR.h"
seanhalle@261 13 #include "VSs_impl/VSs.h"
seanhalle@230 14
seanhalle@273 15 //========================= Local Declarations ========================
seanhalle@273 16 inline PRProcess *
seanhalle@273 17 pickAProcess( AnimSlot *slot );
seanhalle@273 18 inline bool32
seanhalle@273 19 assignWork( PRProcess *process, AnimSlot *slot );
seanhalle@230 20
seanhalle@273 21 inline void
seanhalle@273 22 PRHandle__CreateTask( PRReqst *req, SlaveVP *slave );
seanhalle@273 23 inline void
seanhalle@273 24 PRHandle__EndTask( PRReqst *req, SlaveVP *slave );
seanhalle@273 25 inline void
seanhalle@273 26 PRHandle__CreateSlave(PRReqst *req, SlaveVP *slave );
seanhalle@273 27 inline void
seanhalle@273 28 PRHandle__EndSlave( PRReqst *req, SlaveVP *slave );
seanhalle@268 29
seanhalle@273 30 inline void
seanhalle@273 31 PRHandle__LangShutdown( PRReqst *req, SlaveVP *requestingSlv );
seanhalle@268 32
seanhalle@273 33 inline void
seanhalle@273 34 handleMakeProbe( PRServiceReq *langReq, PRLangEnv *protoLangEnv );
seanhalle@273 35 inline void
seanhalle@273 36 handleThrowException( PRServiceReq *langReq, PRLangEnv *protoLangEnv );
seanhalle@230 37
seanhalle@273 38 //===========================================================================
seanhalle@272 39
seanhalle@272 40 /*Note: there used to be a coreController that was another animation
seanhalle@272 41 * layer below both the masterVP and the slaveVPs.. in that case, the
seanhalle@272 42 * masterVP was a virtual processor whose processor-state was the same
seanhalle@272 43 * as a slaveVP's processor sate, both implemented as a SlaveVP struct.
seanhalle@272 44 * Have removed that, and
seanhalle@272 45 * changed the masterVP implementation. Instead of being a special version
seanhalle@272 46 * of a proto-runtime virtual processor, using the slaveVP stuct, the
seanhalle@272 47 * Master "virtual processor" is now implemented as a pthread pinned to
seanhalle@272 48 * a physical core.
seanhalle@260 49 */
seanhalle@260 50
seanhalle@272 51 /*This is the behavior of the Master. The physical processor switches
seanhalle@272 52 * between animating the master, and animating a slave. When a slave
seanhalle@272 53 * suspends, the PR "suspend" primitive switches the physical core over
seanhalle@272 54 * to animating the masterVP, which is implemented as a pinned pthread.
seanhalle@272 55 * This function is the behavior of that masterVP.
seanhalle@272 56 *This function's job is to manage processing
seanhalle@272 57 * requests and to trigger assignment of new work to the physical core,
seanhalle@272 58 * and to manage sharing the core among processes.
seanhalle@272 59 */
seanhalle@261 60 inline
seanhalle@270 61 bool32
seanhalle@269 62 masterFunction( AnimSlot *slot )
seanhalle@261 63 { //Scan the animation slots
seanhalle@261 64 int32 magicNumber;
seanhalle@261 65 SlaveVP *slave;
seanhalle@268 66 PRLangEnv *langEnv;
seanhalle@261 67 PRReqst *req;
seanhalle@268 68 PRProcess *process;
seanhalle@273 69 bool32 didAssignWork;
seanhalle@260 70
seanhalle@268 71 //Check if newly-done slave in slot, which will need request handled
seanhalle@272 72 //NOTE: left over from when had a coreController & MasterVP managed
seanhalle@272 73 // several slots
seanhalle@268 74 if( slot->workIsDone )
seanhalle@268 75 { slot->workIsDone = FALSE;
seanhalle@268 76 slot->needsWorkAssigned = TRUE;
seanhalle@273 77
seanhalle@273 78 //An Idle VP has no request to handle, so skip to assign..
seanhalle@273 79 if( slot->slaveAssignedToSlot->typeOfVP != IdleVP )
seanhalle@273 80 {
seanhalle@273 81 HOLISTIC__Record_AppResponder_start; //TODO: update to check which process for each slot
seanhalle@273 82 MEAS__startReqHdlr;
seanhalle@261 83
seanhalle@261 84
seanhalle@273 85 //process the request made by the slave (held inside slave struc)
seanhalle@273 86 slave = slot->slaveAssignedToSlot;
seanhalle@273 87 req = slave->request;
seanhalle@268 88
seanhalle@273 89 //If the requesting slave is a slot slave, and request is not
seanhalle@273 90 // task-end, then turn it into a free task slave & continue
seanhalle@273 91 if( slave->typeOfVP == SlotTaskSlv && req->reqType != TaskEnd )
seanhalle@273 92 PR_int__replace_with_new_slot_slv( slave );
seanhalle@268 93
seanhalle@273 94 //Handle task create and end first -- they're special cases..
seanhalle@273 95 switch( req->reqType )
seanhalle@273 96 { case TaskEnd:
seanhalle@273 97 { //do PR handler, which calls lang's hdlr and does recycle of
seanhalle@273 98 // free task slave if needed -- PR handler checks for free task Slv
seanhalle@273 99 PRHandle__EndTask( req, slave ); break;
seanhalle@273 100 }
seanhalle@273 101 case TaskCreate:
seanhalle@273 102 { //Do PR's create-task handler, which calls the lang's hdlr
seanhalle@273 103 // PR handler checks for free task Slv
seanhalle@273 104 PRHandle__CreateTask( req, slave ); break;
seanhalle@273 105 }
seanhalle@273 106 case SlvCreate: PRHandle__CreateSlave( req, slave ); break;
seanhalle@273 107 case SlvDissipate: PRHandle__EndSlave( req, slave ); break;
seanhalle@273 108 case Service: PRHandle__ServiceReq( slave ); break; //resumes into Service lang env
seanhalle@273 109 case Hardware: //for future expansion
seanhalle@273 110 case IO: //for future expansion
seanhalle@273 111 case OSCall: //for future expansion
seanhalle@273 112 PR_int__throw_exception("Not implemented", slave, NULL); break;
seanhalle@273 113 case LangShutdown: PRHandle__LangShutdown( req, slave ); break;
seanhalle@273 114 case Language: //normal lang request
seanhalle@273 115 { magicNumber = req->langMagicNumber;
seanhalle@273 116 langEnv = PR_PI__give_lang_env_for_slave( slave, magicNumber );
seanhalle@273 117 (*req->handler)( req->langReq, slave, langEnv );
seanhalle@273 118 }
seanhalle@267 119 }
seanhalle@273 120
seanhalle@268 121 MEAS__endReqHdlr;
seanhalle@267 122 HOLISTIC__Record_AppResponder_end;
seanhalle@273 123 }//if not idleVP
seanhalle@268 124 } //if have request to be handled
seanhalle@268 125
seanhalle@272 126 //NOTE: IF statement is leftover from when master managed many slots
seanhalle@273 127 didAssignWork = FALSE;
seanhalle@273 128 if( slot->needsWorkAssigned ) //can probably remove IF, now that only one slot
seanhalle@268 129 {
seanhalle@268 130 HOLISTIC__Record_Assigner_start;
seanhalle@272 131
seanhalle@268 132 //Pick a process to get this slot
seanhalle@268 133 process = pickAProcess( slot );
seanhalle@268 134
seanhalle@268 135 //Scan lang environs, looking for langEnv with ready work.
seanhalle@268 136 // call the Assigner for that lang Env, to get a slave for the slot
seanhalle@273 137 if( process != NULL )
seanhalle@273 138 { didAssignWork =
seanhalle@273 139 assignWork( process, slot );
seanhalle@273 140 }
seanhalle@268 141 HOLISTIC__Record_Assigner_end;
seanhalle@272 142
seanhalle@273 143 if( !didAssignWork ) //if no work assigned, be sure idle slave is in slot
seanhalle@273 144 { slot->slaveAssignedToSlot = _PRTopEnv->idleSlv[slot->coreSlotIsOn][0];
seanhalle@273 145 }
seanhalle@273 146 // fixme; //make into a loop that tries more processes if fails to assign
seanhalle@268 147 }//if slot needs slave assigned
seanhalle@270 148
seanhalle@273 149 return didAssignWork;
seanhalle@261 150 }
seanhalle@260 151
seanhalle@268 152 /*When several processes exist, use some pattern for picking one to give
seanhalle@268 153 * the animation slot to.
seanhalle@268 154 *First, it has to be a process that has work available.
seanhalle@268 155 *For now, just do a round-robin
seanhalle@261 156 */
seanhalle@268 157 inline
seanhalle@268 158 PRProcess *
seanhalle@268 159 pickAProcess( AnimSlot *slot )
seanhalle@268 160 { int32 idx;
seanhalle@268 161 PRProcess *process;
seanhalle@268 162
seanhalle@268 163 for( idx = _PRTopEnv->currProcessIdx; idx < _PRTopEnv->numProcesses; idx++)
seanhalle@268 164 {
seanhalle@268 165 process = _PRTopEnv->processes[ idx ];
seanhalle@268 166 if( process->numEnvsWithWork != 0 )
seanhalle@268 167 { _PRTopEnv->currProcessIdx = idx;
seanhalle@268 168 return process;
seanhalle@268 169 }
seanhalle@261 170 }
seanhalle@268 171 for( idx = 0; idx < _PRTopEnv->currProcessIdx; idx++)
seanhalle@268 172 {
seanhalle@268 173 process = _PRTopEnv->processes[ idx ];
seanhalle@268 174 if( process->numEnvsWithWork != 0 )
seanhalle@268 175 { _PRTopEnv->currProcessIdx = idx;
seanhalle@268 176 return process;
seanhalle@268 177 }
seanhalle@268 178 }
seanhalle@268 179 //none found
seanhalle@268 180 return NULL;
seanhalle@260 181 }
seanhalle@260 182
seanhalle@261 183 /*This does:
seanhalle@268 184 * 1) searches the language environments for one with work ready
seanhalle@261 185 * if finds one, asks its assigner to return work
seanhalle@261 186 * 2) checks what kind of work: new task, resuming task, resuming slave
seanhalle@261 187 * if new task, gets the slot slave and assigns task to it and returns slave
seanhalle@261 188 * else, gets the slave attached to the metaTask and returns that.
seanhalle@261 189 * 3) if no work found, then prune former task slaves waiting to be recycled.
seanhalle@261 190 * If no work and no slaves to prune, check for shutdown conditions.
seanhalle@261 191 *
seanhalle@268 192 * language env keeps its own work in its own structures, and has its own
seanhalle@261 193 * assigner. It chooses
seanhalle@261 194 * However, include a switch that switches-in an override assigner, which
seanhalle@268 195 * sees all the work in all the language env's. This is most likely
seanhalle@261 196 * generated by static tools and included in the executable. That means it
seanhalle@261 197 * has to be called via a registered pointer from here. The idea is that
seanhalle@261 198 * the static tools know which languages are grouped together.. and the
seanhalle@261 199 * override enables them to generate a custom assigner that uses info from
seanhalle@261 200 * all the languages in a unified way.. Don't really expect this to happen,
seanhalle@261 201 * but am making it possible.
seanhalle@260 202 */
seanhalle@268 203 inline
seanhalle@270 204 bool32
seanhalle@261 205 assignWork( PRProcess *process, AnimSlot *slot )
seanhalle@272 206 { int32 coreNum;
seanhalle@260 207
seanhalle@261 208 coreNum = slot->coreSlotIsOn;
seanhalle@260 209
seanhalle@267 210 if( process->overrideAssigner != NULL )
seanhalle@268 211 { if( process->numEnvsWithWork != 0 )
seanhalle@268 212 { (*process->overrideAssigner)( process, slot ); //calls PR fn that inserts work into slot
seanhalle@268 213 goto ReturnAfterAssigningWork; //quit for-loop, cause found work
seanhalle@261 214 }
seanhalle@268 215 else
seanhalle@261 216 goto NoWork;
seanhalle@261 217 }
seanhalle@261 218
seanhalle@268 219 //If here, then no override assigner, so search language envs for work
seanhalle@273 220 int32 envIdx, numEnvs; PRLangEnv **protoLangEnvsList, *protoLangEnv;
seanhalle@273 221 protoLangEnvsList = process->protoLangEnvsList;
seanhalle@268 222 numEnvs = process->numLangEnvs;
seanhalle@268 223 for( envIdx = 0; envIdx < numEnvs; envIdx++ ) //keep langEnvs in hash & array
seanhalle@273 224 { protoLangEnv = protoLangEnvsList[envIdx];
seanhalle@273 225 if( protoLangEnv->numReadyWork > 0 )
seanhalle@272 226 { bool32
seanhalle@272 227 didAssignWork =
seanhalle@273 228 (*protoLangEnv->workAssigner)( PR_int__give_lang_env(protoLangEnv), slot ); //assigner calls PR to put slave/task into slot
seanhalle@272 229
seanhalle@272 230 if(didAssignWork)
seanhalle@273 231 { protoLangEnv->numReadyWork -= 1;
seanhalle@273 232 if( protoLangEnv->numReadyWork == 0 )
seanhalle@272 233 { process->numEnvsWithWork -= 1;
seanhalle@272 234 }
seanhalle@272 235 goto ReturnAfterAssigningWork; //quit for-loop, 'cause found work
seanhalle@272 236 }
seanhalle@272 237 else
seanhalle@272 238 goto NoWork; //quit for-loop, cause found work
seanhalle@272 239
seanhalle@268 240 //NOTE: bad search alg -- should start where left off, then wrap around
seanhalle@260 241 }
seanhalle@260 242 }
seanhalle@268 243 //If reach here, then have searched all langEnv's & none have work..
seanhalle@260 244
seanhalle@268 245 NoWork: //No work, if end up here..
seanhalle@260 246 {
seanhalle@260 247 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
seanhalle@272 248 returnSlv = process->idleSlv[coreNum][0]; //only one slot now, so [0]
seanhalle@260 249
seanhalle@260 250 //things that would normally happen in resume(), but idle VPs
seanhalle@260 251 // never go there
seanhalle@261 252 returnSlv->numTimesAssignedToASlot++; //gives each idle unit a unique ID
seanhalle@260 253 Unit newU;
seanhalle@268 254 newU.vp = returnSlv->slaveNum;
seanhalle@261 255 newU.task = returnSlv->numTimesAssignedToASlot;
seanhalle@261 256 addToListOfArrays(Unit,newU,process->unitList);
seanhalle@260 257
seanhalle@261 258 if (returnSlv->numTimesAssignedToASlot > 1) //make a dependency from prev idle unit
seanhalle@260 259 { Dependency newD; // to this one
seanhalle@268 260 newD.from_vp = returnSlv->slaveNum;
seanhalle@261 261 newD.from_task = returnSlv->numTimesAssignedToASlot - 1;
seanhalle@268 262 newD.to_vp = returnSlv->slaveNum;
seanhalle@261 263 newD.to_task = returnSlv->numTimesAssignedToASlot;
seanhalle@261 264 addToListOfArrays(Dependency, newD ,process->ctlDependenciesList);
seanhalle@260 265 }
seanhalle@268 266 #endif
seanhalle@268 267 HOLISTIC__Record_Assigner_end;
seanhalle@269 268 return FALSE;
seanhalle@260 269 }
seanhalle@268 270
seanhalle@268 271 ReturnAfterAssigningWork: //All paths goto here.. to provide single point for holistic..
seanhalle@268 272 {
seanhalle@268 273 HOLISTIC__Record_Assigner_end;
seanhalle@269 274 return TRUE;
seanhalle@260 275 }
seanhalle@260 276 }
seanhalle@260 277
seanhalle@260 278
seanhalle@273 279 //=================================
seanhalle@273 280 //===
seanhalle@273 281 //=
seanhalle@273 282 /*Create task is a special form, that has PR behavior in addition to plugin
seanhalle@273 283 * behavior. Master calls this first, and it then calls the plugin's
seanhalle@273 284 * create task handler.
seanhalle@273 285 *
seanhalle@273 286 *Note: the requesting slave must be either generic slave or free task slave
seanhalle@273 287 */
seanhalle@273 288 inline
seanhalle@273 289 void
seanhalle@273 290 PRHandle__CreateTask( PRReqst *req, SlaveVP *slave )
seanhalle@273 291 { PRMetaTask *protoMetaTask;
seanhalle@273 292 PRProcess *process;
seanhalle@273 293 PRLangEnv *protoLangEnv;
seanhalle@273 294 void *task;
seanhalle@273 295
seanhalle@273 296 process = slave->processSlaveIsIn;
seanhalle@273 297
seanhalle@273 298 protoLangEnv = PR_int__give_proto_lang_env_for_slave( slave,
seanhalle@273 299 req->langMagicNumber );
seanhalle@273 300
seanhalle@273 301 //Do the langlet's create-task handler, which keeps the task
seanhalle@273 302 // inside the langlet's lang env, but returns the langMetaTask
seanhalle@273 303 // so that PR can then put stuff into the prolog
seanhalle@273 304 //typedef void * (*CreateHandler)( void *, SlaveVP *, void * ); //req, slv, langEnv
seanhalle@273 305 //
seanhalle@273 306 task =
seanhalle@273 307 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
seanhalle@273 308 protoMetaTask = PR_int__give_prolog_of_lang_meta_task( task );
seanhalle@273 309 protoMetaTask->ID = req->ID; //may be NULL
seanhalle@273 310 protoMetaTask->topLevelFn = req->topLevelFn;
seanhalle@273 311 protoMetaTask->initData = req->initData;
seanhalle@273 312 protoMetaTask->processTaskIsIn = process;
seanhalle@273 313
seanhalle@273 314 process->numLiveTasks += 1;
seanhalle@273 315 protoLangEnv->numLiveWork += 1; //used in wait statements -- small added overhead
seanhalle@273 316
seanhalle@273 317 return;
seanhalle@273 318 }
seanhalle@273 319
seanhalle@273 320 /*When a task ends, have two scenarios: 1) task ran to completion, or 2) task
seanhalle@273 321 * has been suspended at some point in its code.
seanhalle@273 322 *For 1, just decr count of live tasks (and check for end condition) -- the
seanhalle@273 323 * master loop will decide what goes into the slot freed up by this task end,
seanhalle@273 324 * so, here, don't worry about assigning a new task to the slot slave.
seanhalle@273 325 *For 2, the task's slot slave has been converted to a free task slave, which
seanhalle@273 326 * now has nothing more to do, so send it to the recycle Q (which includes
seanhalle@273 327 * freeing all the langData and meta task structs alloc'd for it). Then
seanhalle@273 328 * decrement the live task count and check end condition.
seanhalle@273 329 *
seanhalle@273 330 *PR has to update count of live tasks, and check end of process condition.
seanhalle@273 331 * The "main" can invoke constructs that wait for a process to end, so when
seanhalle@273 332 * end detected, have to resume what's waiting..
seanhalle@273 333 *Thing is, that wait involves the main OS thread. That means
seanhalle@273 334 * PR internals have to do OS thread signaling. Want to do that in the
seanhalle@273 335 * core controller, which has the original stack of an OS thread. So the
seanhalle@273 336 * end process handling happens in the core controller.
seanhalle@273 337 *
seanhalle@273 338 *So here, when detect process end, signal to the core controller, which will
seanhalle@273 339 * then do the condition variable notify to the OS thread that's waiting.
seanhalle@273 340 *
seanhalle@273 341 *Note: slave may be either a slot slave or a free task slave.
seanhalle@273 342 */
seanhalle@273 343 inline
seanhalle@273 344 void
seanhalle@273 345 PRHandle__EndTask( PRReqst *req, SlaveVP *requestingSlv )
seanhalle@273 346 { void *langEnv;
seanhalle@273 347 PRLangEnv *protoLangEnv;
seanhalle@273 348 PRProcess *process;
seanhalle@273 349 void *langMetaTask;
seanhalle@273 350
seanhalle@273 351 process = requestingSlv->processSlaveIsIn;
seanhalle@273 352 langEnv = PR_int__give_lang_env_of_req( req, requestingSlv ); //magic num in req
seanhalle@273 353 protoLangEnv = PR_int__give_proto_lang_env( langEnv );
seanhalle@273 354
seanhalle@273 355 langMetaTask = PR_int__give_lang_meta_task_from_slave( requestingSlv, req->langMagicNumber);
seanhalle@273 356
seanhalle@273 357 //Do the langlet's request handler
seanhalle@273 358 //Want to keep PR structs hidden from plugin, so extract langReq..
seanhalle@273 359 //This is supposed to free any langlet-malloc'd mem, including meta task
seanhalle@273 360 (*req->handler)( req->langReq, requestingSlv, langEnv );
seanhalle@273 361
seanhalle@273 362 protoLangEnv->numLiveWork -= 1; //used in wait statements -- small added overhead
seanhalle@273 363 if( protoLangEnv->numLiveWork == 0 &&
seanhalle@273 364 numInPrivQ( protoLangEnv->waitingForWorkToEndQ ) > 0 )
seanhalle@273 365 { SlaveVP *
seanhalle@273 366 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
seanhalle@273 367 //can't resume into langlet that just ended its last work!
seanhalle@273 368 // and don't have env that the waiter was created in, so resume
seanhalle@273 369 // into PRServ env..
seanhalle@273 370 void *
seanhalle@273 371 resumeEnv = PR_PI__give_lang_env_from_process( process, PRServ_MAGIC_NUMBER );
seanhalle@273 372 while( waitingSlave != NULL )
seanhalle@273 373 { //resume a slave that was waiting for work in this env to finish
seanhalle@273 374 PR_PI__make_slave_ready( waitingSlave, resumeEnv );
seanhalle@273 375 //get next waiting slave, repeat..
seanhalle@273 376 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
seanhalle@273 377 }
seanhalle@273 378 }
seanhalle@273 379
seanhalle@273 380
seanhalle@273 381 //Now that the langlet's done with it, recycle the slave if it's a freeTaskSlv
seanhalle@273 382 if( requestingSlv->typeOfVP == FreeTaskSlv )
seanhalle@273 383 PR_int__recycle_slaveVP( requestingSlv ); //Doesn't decr num live slaves
seanhalle@273 384
seanhalle@273 385 process->numLiveTasks -= 1;
seanhalle@273 386 //NOTE: end-task is unrelated to work available (just in case wondering)
seanhalle@273 387
seanhalle@273 388 //check End Of Process Condition
seanhalle@273 389 if( process->numLiveTasks == 0 &&
seanhalle@273 390 process->numLiveGenericSlvs == 0 )
seanhalle@273 391 { //Tell the core controller to do wakeup of any waiting OS thread
seanhalle@273 392 PR_SS__end_process_normally( process );
seanhalle@273 393 }
seanhalle@273 394 }
seanhalle@273 395
seanhalle@273 396
seanhalle@268 397
seanhalle@268 398 /*This is first thing called when creating a slave.. it hands off to the
seanhalle@268 399 * langlet's creator, then adds updates of its own..
seanhalle@268 400 *
seanhalle@268 401 *There's a question of things like lang data, meta tasks, and such..
seanhalle@268 402 *In creator, only PR related things happen, and things for the langlet whose
seanhalle@261 403 * creator construct was used.
seanhalle@268 404 *
seanhalle@268 405 *Other langlets still get a chance to create langData -- but by registering a
seanhalle@268 406 * "createLangData" handler in the langEnv. When a construct of the langlet
seanhalle@268 407 * calls "PR__give_lang_data()", if there is no langData for that langlet,
seanhalle@268 408 * the PR will call the creator in the langlet's langEnv, place whatever it
seanhalle@268 409 * makes as the langData in that slave for that langlet, and return that langData
seanhalle@261 410 *
seanhalle@261 411 *So, as far as counting things, a langlet is only allowed to count creation
seanhalle@261 412 * of slaves it creates itself.. may have to change this later.. add a way for
seanhalle@261 413 * langlet to register a trigger Fn called each time a slave gets created..
seanhalle@261 414 * need more experience with what langlets will do at create time.. think Cilk
seanhalle@261 415 * has interesting create behavior.. not sure how that will differ in light
seanhalle@261 416 * of true tasks and langlet approach. Look at it after all done and start
seanhalle@261 417 * modifying the langs to be langlets..
seanhalle@261 418 *
seanhalle@261 419 *PR itself needs to create the slave, then update numLiveSlaves in process,
seanhalle@261 420 * copy processID from requestor to newly created
seanhalle@261 421 */
seanhalle@268 422 inline
seanhalle@268 423 void
seanhalle@272 424 PRHandle__CreateSlave( PRReqst *req, SlaveVP *slave )
seanhalle@268 425 { SlaveVP *newSlv;
seanhalle@261 426 PRProcess *process;
seanhalle@268 427 PRLangEnv *protoLangEnv;
seanhalle@261 428
seanhalle@268 429 process = slave->processSlaveIsIn;
seanhalle@273 430 protoLangEnv = PR_int__give_proto_lang_env_for_slave( slave, req->langMagicNumber );
seanhalle@272 431
seanhalle@272 432 //create handler, or a future request handler will call PR_PI__make_slave_ready
seanhalle@272 433 // which will in turn handle updating which langlets and which processes have
seanhalle@272 434 // work available.
seanhalle@272 435 //NOTE: create slv has diff prototype than standard reqst hdlr
seanhalle@268 436 newSlv =
seanhalle@268 437 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv));
seanhalle@268 438
seanhalle@261 439 newSlv->typeOfVP = GenericSlv;
seanhalle@261 440 newSlv->processSlaveIsIn = process;
seanhalle@268 441 newSlv->ID = req->ID;
seanhalle@272 442 process->numLiveGenericSlvs += 1; //not same as work ready!
seanhalle@273 443 protoLangEnv->numLiveWork += 1; //used in wait statements -- small added overhead
seanhalle@260 444 }
seanhalle@260 445
seanhalle@268 446 /*The dissipate handler has to, update the number of slaves of the type, within
seanhalle@261 447 * the process, and call the langlet handler linked into the request,
seanhalle@261 448 * and after that returns, then call the PR function that frees the slave state
seanhalle@261 449 * (or recycles the slave).
seanhalle@261 450 *
seanhalle@261 451 *The PR function that frees the slave state has to also free all of the
seanhalle@268 452 * langData in the slave.. or else reset all of the langDatas.. by, say, marking
seanhalle@268 453 * them, then in PR__give_langData( magicNum ) call the langlet registered
seanhalle@268 454 * "resetLangData" Fn.
seanhalle@261 455 */
seanhalle@268 456 inline
seanhalle@268 457 void
seanhalle@272 458 PRHandle__EndSlave( PRReqst *req, SlaveVP *slave )
seanhalle@261 459 { PRProcess *process;
seanhalle@268 460 PRLangEnv *protoLangEnv;
seanhalle@261 461
seanhalle@261 462 process = slave->processSlaveIsIn;
seanhalle@261 463
seanhalle@261 464 //do the language's dissipate handler
seanhalle@273 465 protoLangEnv = PR_int__give_proto_lang_env_for_slave( slave, slave->request->langMagicNumber );
seanhalle@268 466
seanhalle@268 467 if(req->handler != NULL)
seanhalle@268 468 (*req->handler)( req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
seanhalle@261 469
seanhalle@273 470 protoLangEnv->numLiveWork -= 1; //used in wait statements -- small added overhead
seanhalle@273 471 if( protoLangEnv->numLiveWork == 0 &&
seanhalle@273 472 numInPrivQ( protoLangEnv->waitingForWorkToEndQ ) > 0 )
seanhalle@273 473 { SlaveVP *
seanhalle@273 474 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
seanhalle@273 475 //can't resume into langlet that just ended its last work!
seanhalle@273 476 // and don't have env that the waiter was created in, so resume
seanhalle@273 477 // into PRServ env..
seanhalle@273 478 void *
seanhalle@273 479 resumeEnv = PR_PI__give_lang_env_from_process( process, PRServ_MAGIC_NUMBER );
seanhalle@273 480 while( waitingSlave != NULL )
seanhalle@273 481 { //resume a slave that was waiting for work in this env to finish
seanhalle@273 482 PR_PI__make_slave_ready( waitingSlave, resumeEnv );
seanhalle@273 483 //get next waiting slave, repeat..
seanhalle@273 484 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
seanhalle@273 485 }
seanhalle@273 486 }
seanhalle@273 487
seanhalle@272 488 process->numLiveGenericSlvs -= 1;
seanhalle@273 489 PR_int__recycle_slaveVP( slave );
seanhalle@272 490 //NOTE: dissipate is unrelated to work available (just in case wondering)
seanhalle@272 491
seanhalle@261 492 //check End Of Process Condition
seanhalle@261 493 if( process->numLiveTasks == 0 &&
seanhalle@266 494 process->numLiveGenericSlvs == 0 )
seanhalle@273 495 PR_SS__end_process_normally( process );
seanhalle@261 496 }
seanhalle@261 497
seanhalle@273 498 //=======================
seanhalle@273 499 //===
seanhalle@273 500 //=
seanhalle@273 501 /*Langlet shutdown triggers this, which calls the registered shutdown
seanhalle@273 502 * handler for the langlet, and removes the lang's env from the process
seanhalle@261 503 */
seanhalle@268 504 inline
seanhalle@268 505 void
seanhalle@273 506 PRHandle__LangShutdown( PRReqst *req, SlaveVP *requestingSlv )
seanhalle@273 507 { void *langEnv;
seanhalle@273 508 PRLangEnv *protoLangEnv;
seanhalle@273 509 PRProcess *process;
seanhalle@268 510
seanhalle@273 511 process = requestingSlv->processSlaveIsIn;
seanhalle@273 512 protoLangEnv = PR_int__give_proto_lang_env_from_process( process, req->langMagicNumber );
seanhalle@273 513 langEnv = PR_int__give_lang_env( protoLangEnv );
seanhalle@268 514
seanhalle@273 515 //call the langlet's registered handler
seanhalle@273 516 (*protoLangEnv->shutdownHdlr)( langEnv );
seanhalle@267 517
seanhalle@273 518 PR_int__remove_lang_env_from_process_and_free( langEnv ); //removes from process and frees
seanhalle@261 519 }
seanhalle@261 520
seanhalle@272 521
seanhalle@272 522 /*This is for OS requests and PR infrastructure requests, which are not
seanhalle@272 523 * part of the PRServ language -- this is for things that have to be in the
seanhalle@272 524 * infrastructure of PR itself, such as I/O requests, which have to go through
seanhalle@272 525 * pthreads inside the core controller..
seanhalle@272 526 *
seanhalle@272 527 *As of Jan 2013, doesn't do much of anything..
seanhalle@272 528 */
seanhalle@272 529 void inline
seanhalle@272 530 PRHandle__ServiceReq( SlaveVP *requestingSlv )
seanhalle@273 531 { PRReqst *req;
seanhalle@273 532 PRServiceReq *langReq;
seanhalle@273 533 PRLangEnv *protoLangEnv;
seanhalle@273 534 int32 magicNumber;
seanhalle@272 535
seanhalle@272 536
seanhalle@272 537 req = requestingSlv->request;
seanhalle@272 538
seanhalle@272 539 magicNumber = req->langMagicNumber;
seanhalle@273 540 protoLangEnv = PR_int__give_proto_lang_env_for_slave( requestingSlv, magicNumber );
seanhalle@272 541
seanhalle@272 542 langReq = PR_PI__take_lang_reqst_from(req);
seanhalle@272 543 if( langReq == NULL ) return;
seanhalle@272 544 switch( langReq->reqType ) //lang handlers are all in other file
seanhalle@272 545 {
seanhalle@273 546 case make_probe: handleMakeProbe( langReq, protoLangEnv );
seanhalle@272 547 break;
seanhalle@273 548 case throw_excp: handleThrowException( langReq, protoLangEnv );
seanhalle@272 549 break;
seanhalle@272 550 }
seanhalle@272 551 }
seanhalle@273 552
seanhalle@273 553
seanhalle@273 554 /*These handlers are special -- they don't belong to a language, because they
seanhalle@273 555 * deal with things internal to PR, so put them here..
seanhalle@273 556 */
seanhalle@273 557 inline
seanhalle@273 558 void
seanhalle@273 559 handleMakeProbe( PRServiceReq *langReq, PRLangEnv *protoLangEnv )
seanhalle@273 560 { IntervalProbe *newProbe;
seanhalle@273 561
seanhalle@273 562 newProbe = PR_int__malloc( sizeof(IntervalProbe) );
seanhalle@273 563 newProbe->nameStr = PR_int__strDup( langReq->nameStr );
seanhalle@273 564 newProbe->hist = NULL;
seanhalle@273 565 newProbe->schedChoiceWasRecorded = FALSE;
seanhalle@273 566
seanhalle@273 567 //This runs in masterVP, so no race-condition worries
seanhalle@273 568 //BUG: move to process
seanhalle@273 569 newProbe->probeID =
seanhalle@273 570 addToDynArray( newProbe, _PRTopEnv->dynIntervalProbesInfo );
seanhalle@273 571
seanhalle@273 572 langReq->requestingSlv->dataRetFromReq = newProbe;
seanhalle@273 573
seanhalle@273 574 (*protoLangEnv->makeSlaveReadyFn)( langReq->requestingSlv, PR_int__give_lang_env(protoLangEnv) );
seanhalle@273 575 }
seanhalle@273 576
seanhalle@273 577 inline
seanhalle@273 578 void
seanhalle@273 579 handleThrowException( PRServiceReq *langReq, PRLangEnv *protoLangEnv )
seanhalle@273 580 {
seanhalle@273 581 PR_int__throw_exception( langReq->msgStr, langReq->requestingSlv, langReq->exceptionData );
seanhalle@273 582
seanhalle@273 583 (*protoLangEnv->makeSlaveReadyFn)( langReq->requestingSlv, PR_int__give_lang_env(protoLangEnv) );
seanhalle@273 584 }
seanhalle@273 585