annotate AnimationMaster.c @ 278:2fc69e6c14ea

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