annotate VMS.c @ 31:e69579a0e797

Works multi-core.. pinned VP to a core loop
author Me
date Wed, 01 Sep 2010 08:23:39 -0700
parents c8823e0bb2b4
children 17d20e5cf924
rev   line source
Me@0 1 /*
Me@0 2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
Me@0 3 *
Me@0 4 * Licensed under BSD
Me@0 5 */
Me@0 6
Me@0 7 #include <stdio.h>
Me@0 8 #include <stdlib.h>
Me@0 9 #include <malloc.h>
Me@0 10
Me@0 11 #include "VMS.h"
Me@0 12 #include "Queue_impl/BlockingQueue.h"
Me@0 13
Me@0 14
Me@26 15 #define thdAttrs NULL
Me@26 16
Me@22 17 //===========================================================================
Me@22 18 void
Me@22 19 shutdownFn( void *dummy, VirtProcr *dummy2 );
Me@22 20
Me@31 21 SchedSlot **
Me@31 22 create_sched_slots();
Me@22 23
Me@28 24 void
Me@28 25 create_masterEnv();
Me@28 26
Me@28 27 void
Me@28 28 create_the_coreLoop_OS_threads();
Me@28 29
Me@26 30 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
Me@26 31 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
Me@26 32
Me@22 33 //===========================================================================
Me@22 34
Me@0 35 /*Setup has two phases:
Me@0 36 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
Me@8 37 * the master virt procr into the work-queue, ready for first "call"
Me@8 38 * 2) Semantic layer then does its own init, which creates the seed virt
Me@8 39 * procr inside the semantic layer, ready to schedule it when
Me@0 40 * asked by the first run of the masterLoop.
Me@0 41 *
Me@0 42 *This part is bit weird because VMS really wants to be "always there", and
Me@0 43 * have applications attach and detach.. for now, this VMS is part of
Me@0 44 * the app, so the VMS system starts up as part of running the app.
Me@0 45 *
Me@8 46 *The semantic layer is isolated from the VMS internals by making the
Me@8 47 * semantic layer do setup to a state that it's ready with its
Me@8 48 * initial virt procrs, ready to schedule them to slots when the masterLoop
Me@0 49 * asks. Without this pattern, the semantic layer's setup would
Me@8 50 * have to modify slots directly to assign the initial virt-procrs, and put
Me@31 51 * them into the readyToAnimateQ itself, breaking the isolation completely.
Me@0 52 *
Me@0 53 *
Me@8 54 *The semantic layer creates the initial virt procr(s), and adds its
Me@8 55 * own environment to masterEnv, and fills in the pointers to
Me@0 56 * the requestHandler and slaveScheduler plug-in functions
Me@8 57 */
Me@8 58
Me@8 59 /*This allocates VMS data structures, populates the master VMSProc,
Me@0 60 * and master environment, and returns the master environment to the semantic
Me@0 61 * layer.
Me@0 62 */
Me@8 63 void
Me@8 64 VMS__init()
Me@28 65 {
Me@28 66 create_masterEnv();
Me@28 67 create_the_coreLoop_OS_threads();
Me@28 68 }
Me@28 69
Me@28 70 /*To initialize the sequential version, just don't create the threads
Me@28 71 */
Me@28 72 void
Me@28 73 VMS__init_Seq()
Me@28 74 {
Me@28 75 create_masterEnv();
Me@28 76 }
Me@28 77
Me@28 78 void
Me@28 79 create_masterEnv()
Me@31 80 { MasterEnv *masterEnv;
Me@31 81 SRSWQueueStruc **readyToAnimateQs;
Me@31 82 int coreIdx;
Me@31 83 VirtProcr **masterVPs;
Me@31 84 SchedSlot ***allSchedSlots; //ptr to array of ptrs
Me@31 85
Me@31 86 //Make the master env, which holds everything else
Me@1 87 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
Me@1 88 masterEnv = _VMSMasterEnv;
Me@31 89 //Need to set start pt here 'cause used by seed procr, which is created
Me@31 90 // before the first core loop starts up. -- not sure how yet..
Me@31 91 // masterEnv->coreLoopStartPt = ;
Me@31 92 // masterEnv->coreLoopEndPt = ;
Me@31 93
Me@31 94 //Make a readyToAnimateQ for each core loop
Me@31 95 readyToAnimateQs = malloc( NUM_CORES * sizeof(SRSWQueueStruc *) );
Me@31 96 masterVPs = malloc( NUM_CORES * sizeof(VirtProcr *) );
Me@0 97
Me@31 98 //One array for each core, 3 in array, core's masterVP scheds all
Me@31 99 allSchedSlots = malloc( NUM_CORES * sizeof(SchedSlot *) );
Me@0 100
Me@31 101 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
Me@31 102 {
Me@31 103 readyToAnimateQs[ coreIdx ] = makeSRSWQ();
Me@31 104
Me@31 105 //Q: should give masterVP core-specific into as its init data?
Me@31 106 masterVPs[ coreIdx ] = VMS__create_procr( &masterLoop, masterEnv );
Me@31 107 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
Me@31 108 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
Me@31 109 }
Me@31 110 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
Me@31 111 _VMSMasterEnv->masterVPs = masterVPs;
Me@31 112 _VMSMasterEnv->allSchedSlots = allSchedSlots;
Me@0 113
Me@28 114
Me@12 115
Me@31 116 //Aug 19, 2010: no longer need to place initial masterVP into queue
Me@31 117 // because coreLoop now controls -- animates its masterVP when no work
Me@31 118
Me@30 119
Me@30 120 //==================== malloc substitute ========================
Me@30 121 //
Me@30 122 //Testing whether malloc is using thread-local storage and therefore
Me@30 123 // causing unreliable behavior.
Me@30 124 //Just allocate a massive chunk of memory and roll own malloc/free and
Me@30 125 // make app use VMS__malloc_to, which will suspend and perform malloc
Me@30 126 // in the master, taking from this massive chunk.
Me@30 127
Me@30 128 // initFreeList();
Me@0 129 }
Me@0 130
Me@30 131 /*
Me@30 132 void
Me@30 133 initMasterMalloc()
Me@30 134 {
Me@30 135 _VMSMasterEnv->mallocChunk = malloc( MASSIVE_MALLOC_SIZE );
Me@30 136
Me@30 137 //The free-list element is the first several locations of an
Me@30 138 // allocated chunk -- the address given to the application is pre-
Me@30 139 // pended with both the ownership structure and the free-list struc.
Me@30 140 //So, write the values of these into the first locations of
Me@30 141 // mallocChunk -- which marks it as free & puts in its size.
Me@30 142 listElem = (FreeListElem *)_VMSMasterEnv->mallocChunk;
Me@30 143 listElem->size = MASSIVE_MALLOC_SIZE - NUM_PREPEND_BYTES
Me@30 144 listElem->next = NULL;
Me@30 145 }
Me@30 146
Me@30 147 void
Me@30 148 dissipateMasterMalloc()
Me@30 149 {
Me@30 150 //Just foo code -- to get going -- doing as if free list were link-list
Me@30 151 currElem = _VMSMasterEnv->freeList;
Me@30 152 while( currElem != NULL )
Me@30 153 {
Me@30 154 nextElem = currElem->next;
Me@30 155 masterFree( currElem );
Me@30 156 currElem = nextElem;
Me@30 157 }
Me@30 158 free( _VMSMasterEnv->freeList );
Me@30 159 }
Me@30 160 */
Me@30 161
Me@31 162 SchedSlot **
Me@31 163 create_sched_slots()
Me@31 164 { SchedSlot **schedSlots;
Me@0 165 int i;
Me@0 166
Me@8 167 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
Me@8 168
Me@1 169 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
Me@0 170 {
Me@8 171 schedSlots[i] = malloc( sizeof(SchedSlot) );
Me@8 172
Me@1 173 //Set state to mean "handling requests done, slot needs filling"
Me@8 174 schedSlots[i]->workIsDone = FALSE;
Me@8 175 schedSlots[i]->needsProcrAssigned = TRUE;
Me@0 176 }
Me@31 177 return schedSlots;
Me@31 178 }
Me@31 179
Me@31 180
Me@31 181 void
Me@31 182 freeSchedSlots( SchedSlot **schedSlots )
Me@31 183 { int i;
Me@31 184 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
Me@31 185 {
Me@31 186 free( schedSlots[i] );
Me@31 187 }
Me@31 188 free( schedSlots );
Me@0 189 }
Me@0 190
Me@8 191
Me@28 192 void
Me@28 193 create_the_coreLoop_OS_threads()
Me@28 194 {
Me@28 195 //========================================================================
Me@28 196 // Create the Threads
Me@28 197 int coreIdx, retCode;
Me@28 198
Me@28 199 //Need the threads to be created suspended, and wait for a signal
Me@28 200 // before proceeding -- gives time after creating to initialize other
Me@28 201 // stuff before the coreLoops set off.
Me@28 202 _VMSMasterEnv->setupComplete = 0;
Me@28 203
Me@28 204 //Make the threads that animate the core loops
Me@28 205 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@28 206 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
Me@28 207 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
Me@28 208
Me@28 209 retCode =
Me@28 210 pthread_create( &(coreLoopThdHandles[coreIdx]),
Me@28 211 thdAttrs,
Me@28 212 &coreLoop,
Me@28 213 (void *)(coreLoopThdParams[coreIdx]) );
Me@28 214 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
Me@28 215 }
Me@28 216 }
Me@28 217
Me@0 218 /*Semantic layer calls this when it want the system to start running..
Me@0 219 *
Me@24 220 *This starts the core loops running then waits for them to exit.
Me@0 221 */
Me@12 222 void
Me@24 223 VMS__start_the_work_then_wait_until_done()
Me@12 224 { int coreIdx;
Me@24 225 //Start the core loops running
Me@24 226 //===========================================================================
Me@25 227 TSCount startCount, endCount;
Me@24 228 unsigned long long count = 0, freq = 0;
Me@25 229 double runTime;
Me@0 230
Me@25 231 startCount = getTSCount();
Me@25 232
Me@25 233 //tell the core loop threads that setup is complete
Me@25 234 //get lock, to lock out any threads still starting up -- they'll see
Me@25 235 // that setupComplete is true before entering while loop, and so never
Me@25 236 // wait on the condition
Me@26 237 pthread_mutex_lock( &suspendLock );
Me@25 238 _VMSMasterEnv->setupComplete = 1;
Me@26 239 pthread_mutex_unlock( &suspendLock );
Me@26 240 pthread_cond_broadcast( &suspend_cond );
Me@25 241
Me@25 242
Me@24 243 //wait for all to complete
Me@8 244 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@8 245 {
Me@25 246 pthread_join( coreLoopThdHandles[coreIdx], NULL );
Me@24 247 }
Me@25 248
Me@24 249 //NOTE: do not clean up VMS env here -- semantic layer has to have
Me@24 250 // a chance to clean up its environment first, then do a call to free
Me@24 251 // the Master env and rest of VMS locations
Me@24 252
Me@24 253
Me@25 254 endCount = getTSCount();
Me@25 255 count = endCount - startCount;
Me@24 256
Me@25 257 runTime = (double)count / (double)TSCOUNT_FREQ;
Me@25 258
Me@25 259 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
Me@8 260 }
Me@0 261
Me@28 262 /*Only difference between version with an OS thread pinned to each core and
Me@28 263 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
Me@28 264 */
Me@28 265 void
Me@28 266 VMS__start_the_work_then_wait_until_done_Seq()
Me@28 267 {
Me@28 268 //Instead of un-suspending threads, just call the one and only
Me@28 269 // core loop (sequential version), in the main thread.
Me@28 270 coreLoop_Seq( NULL );
Me@28 271
Me@28 272 }
Me@28 273
Me@0 274
Me@0 275
Me@8 276 /*Create stack, then create __cdecl structure on it and put initialData and
Me@8 277 * pointer to the new structure instance into the parameter positions on
Me@8 278 * the stack
Me@8 279 *Then put function pointer into nextInstrPt -- the stack is setup in std
Me@8 280 * call structure, so jumping to function ptr is same as a GCC generated
Me@8 281 * function call
Me@8 282 *No need to save registers on old stack frame, because there's no old
Me@8 283 * animator state to return to --
Me@8 284 *
Me@8 285 */
Me@8 286 VirtProcr *
Me@8 287 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
Me@8 288 { VirtProcr *newPr;
Me@8 289 char *stackLocs, *stackPtr;
Me@8 290
Me@8 291 newPr = malloc( sizeof(VirtProcr) );
Me@12 292 newPr->procrID = numProcrsCreated++;
Me@8 293 newPr->nextInstrPt = fnPtr;
Me@8 294 newPr->initialData = initialData;
Me@31 295 newPr->requests = NULL;
Me@31 296 // newPr->coreLoopStartPt = _VMSMasterEnv->coreLoopStartPt;
Me@8 297
Me@14 298 //fnPtr takes two params -- void *initData & void *animProcr
Me@8 299 //alloc stack locations, make stackPtr be the highest addr minus room
Me@14 300 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
Me@14 301 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
Me@22 302 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
Me@26 303 if(stackLocs == 0)
Me@26 304 {perror("malloc stack"); exit(1);}
Me@22 305 newPr->startOfStack = stackLocs;
Me@22 306 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
Me@8 307 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
Me@22 308 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
Me@14 309 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
Me@8 310 newPr->stackPtr = stackPtr; //core loop will switch to this, then
Me@8 311 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
Me@8 312
Me@8 313 return newPr;
Me@8 314 }
Me@8 315
Me@8 316
Me@26 317 /*there is a label inside this function -- save the addr of this label in
Me@0 318 * the callingPr struc, as the pick-up point from which to start the next
Me@0 319 * work-unit for that procr. If turns out have to save registers, then
Me@0 320 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
Me@0 321 * "done with work-unit" label. The procr struc is in the request in the
Me@0 322 * slave that animated the just-ended work-unit, so all the state is saved
Me@0 323 * there, and will get passed along, inside the request handler, to the
Me@0 324 * next work-unit for that procr.
Me@0 325 */
Me@8 326 void
Me@22 327 VMS__suspend_procr( VirtProcr *callingPr )
Me@14 328 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
Me@14 329 void *coreLoopFramePtr;
Me@0 330
Me@14 331 //The request to master will cause this suspended virt procr to get
Me@14 332 // scheduled again at some future point -- to resume, core loop jumps
Me@14 333 // to the resume point (below), which causes restore of saved regs and
Me@14 334 // "return" from this call.
Me@1 335 callingPr->nextInstrPt = &&ResumePt;
Me@1 336
Me@1 337 //return ownership of the virt procr and sched slot to Master virt pr
Me@1 338 callingPr->schedSlot->workIsDone = TRUE;
Me@14 339 // coreIdx = callingPr->coreAnimatedBy;
Me@1 340
Me@18 341 stackPtrAddr = &(callingPr->stackPtr);
Me@18 342 framePtrAddr = &(callingPr->framePtr);
Me@26 343
Me@31 344 jmpPt = _VMSMasterEnv->coreLoopStartPt;
Me@14 345 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only
Me@18 346 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety
Me@1 347
Me@26 348 //Eclipse's compilation sequence complains -- so break into two
Me@26 349 // separate in-line assembly pieces
Me@26 350 //Save the virt procr's stack and frame ptrs,
Me@18 351 asm volatile("movl %0, %%eax; \
Me@18 352 movl %%esp, (%%eax); \
Me@18 353 movl %1, %%eax; \
Me@26 354 movl %%ebp, (%%eax) "\
Me@26 355 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
Me@26 356 /* inputs */ : \
Me@26 357 /* clobber */ : "%eax" \
Me@26 358 );
Me@26 359
Me@26 360 //restore coreloop's frame ptr, then jump back to "start" of core loop
Me@26 361 //Note, GCC compiles to assembly that saves esp and ebp in the stack
Me@26 362 // frame -- so have to explicitly do assembly that saves to memory
Me@26 363 asm volatile("movl %0, %%eax; \
Me@26 364 movl %1, %%esp; \
Me@26 365 movl %2, %%ebp; \
Me@18 366 jmp %%eax " \
Me@26 367 /* outputs */ : \
Me@26 368 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
Me@18 369 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
Me@12 370 ); //list everything as clobbered to force GCC to save all
Me@12 371 // live vars that are in regs on stack before this
Me@12 372 // assembly, so that stack pointer is correct, before jmp
Me@1 373
Me@1 374 ResumePt:
Me@0 375 return;
Me@0 376 }
Me@0 377
Me@22 378
Me@22 379
Me@22 380
Me@22 381 /*Not sure yet the form going to put "dissipate" in, so this is the third
Me@22 382 * possibility -- the semantic layer can just make a macro that looks like
Me@22 383 * a call to its name, then expands to a call to this.
Me@8 384 *
Me@22 385 *As of June 30, 2010 this looks like the top choice..
Me@8 386 *
Me@22 387 *This adds a request to dissipate, then suspends the processor so that the
Me@22 388 * request handler will receive the request. The request handler is what
Me@22 389 * does the work of freeing memory and removing the processor from the
Me@22 390 * semantic environment's data structures.
Me@22 391 *The request handler also is what figures out when to shutdown the VMS
Me@22 392 * system -- which causes all the core loop threads to die, and returns from
Me@22 393 * the call that started up VMS to perform the work.
Me@22 394 *
Me@22 395 *This form is a bit misleading to understand if one is trying to figure out
Me@22 396 * how VMS works -- it looks like a normal function call, but inside it
Me@22 397 * sends a request to the request handler and suspends the processor, which
Me@22 398 * jumps out of the VMS__dissipate_procr function, and out of all nestings
Me@22 399 * above it, transferring the work of dissipating to the request handler,
Me@22 400 * which then does the actual work -- causing the processor that animated
Me@22 401 * the call of this function to disappear and the "hanging" state of this
Me@22 402 * function to just poof into thin air -- the virtual processor's trace
Me@22 403 * never returns from this call, but instead the virtual processor's trace
Me@22 404 * gets suspended in this call and all the virt processor's state disap-
Me@22 405 * pears -- making that suspend the last thing in the virt procr's trace.
Me@8 406 */
Me@8 407 void
Me@22 408 VMS__dissipate_procr( VirtProcr *procrToDissipate )
Me@22 409 { VMSReqst *req;
Me@22 410
Me@22 411 req = malloc( sizeof(VMSReqst) );
Me@22 412 // req->virtProcrFrom = callingPr;
Me@22 413 req->reqType = dissipate;
Me@22 414 req->nextReqst = procrToDissipate->requests;
Me@22 415 procrToDissipate->requests = req;
Me@22 416
Me@22 417 VMS__suspend_procr( procrToDissipate );
Me@22 418 }
Me@22 419
Me@22 420
Me@22 421 /*This inserts the semantic-layer's request data into standard VMS carrier
Me@22 422 */
Me@22 423 inline void
Me@24 424 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
Me@22 425 { VMSReqst *req;
Me@22 426
Me@22 427 req = malloc( sizeof(VMSReqst) );
Me@22 428 // req->virtProcrFrom = callingPr;
Me@22 429 req->reqType = semantic;
Me@22 430 req->semReqData = semReqData;
Me@22 431 req->nextReqst = callingPr->requests;
Me@22 432 callingPr->requests = req;
Me@22 433 }
Me@22 434
Me@22 435
Me@22 436
Me@22 437 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
Me@22 438 // of a request -- IE call with both a virt procr and a fn-ptr to request
Me@22 439 // freer (or maybe put request freer as a field in virt procr?)
Me@22 440 void
Me@22 441 VMS__remove_and_free_top_request( VirtProcr *procrWithReq )
Me@22 442 { VMSReqst *req;
Me@22 443
Me@22 444 req = procrWithReq->requests;
Me@29 445 if( req == NULL ) return;
Me@22 446 procrWithReq->requests = procrWithReq->requests->nextReqst;
Me@29 447 VMS__free_request( req );
Me@22 448 }
Me@22 449
Me@24 450
Me@24 451 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
Me@24 452 // of a request -- IE call with both a virt procr and a fn-ptr to request
Me@24 453 // freer (also maybe put sem request freer as a field in virt procr?)
Me@31 454 //SSR relies right now on this only freeing VMS layer of request -- the
Me@26 455 // semantic portion of request is alloc'd and freed by request handler
Me@22 456 void
Me@24 457 VMS__free_request( VMSReqst *req )
Me@24 458 {
Me@24 459 free( req );
Me@24 460 }
Me@24 461
Me@24 462 VMSReqst *
Me@24 463 VMS__take_top_request_from( VirtProcr *procrWithReq )
Me@24 464 { VMSReqst *req;
Me@24 465
Me@24 466 req = procrWithReq->requests;
Me@24 467 if( req == NULL ) return req;
Me@31 468
Me@24 469 procrWithReq->requests = procrWithReq->requests->nextReqst;
Me@24 470 return req;
Me@24 471 }
Me@24 472
Me@31 473 VMSReqst *
Me@31 474 VMS__free_top_and_give_next_request_from( VirtProcr *procrWithReq )
Me@31 475 { VMSReqst *req;
Me@31 476
Me@31 477 req = procrWithReq->requests;
Me@31 478 if( req == NULL ) return req;
Me@31 479
Me@31 480 procrWithReq->requests = procrWithReq->requests->nextReqst;
Me@31 481 VMS__free_request( req );
Me@31 482 return procrWithReq->requests;
Me@31 483 }
Me@31 484
Me@24 485 inline int
Me@24 486 VMS__isSemanticReqst( VMSReqst *req )
Me@22 487 {
Me@24 488 return ( req->reqType == semantic );
Me@24 489 }
Me@22 490
Me@24 491
Me@24 492 inline void *
Me@24 493 VMS__take_sem_reqst_from( VMSReqst *req )
Me@24 494 {
Me@24 495 return req->semReqData;
Me@24 496 }
Me@24 497
Me@24 498 inline int
Me@24 499 VMS__isDissipateReqst( VMSReqst *req )
Me@24 500 {
Me@24 501 return ( req->reqType == dissipate );
Me@24 502 }
Me@24 503
Me@24 504 inline int
Me@24 505 VMS__isCreateReqst( VMSReqst *req )
Me@24 506 {
Me@24 507 return ( req->reqType == regCreated );
Me@24 508 }
Me@24 509
Me@24 510 void
Me@24 511 VMS__send_register_new_procr_request(VirtProcr *newPr, VirtProcr *reqstingPr)
Me@24 512 { VMSReqst *req;
Me@24 513
Me@24 514 req = malloc( sizeof(VMSReqst) );
Me@24 515 req->reqType = regCreated;
Me@24 516 req->semReqData = newPr;
Me@24 517 req->nextReqst = reqstingPr->requests;
Me@24 518 reqstingPr->requests = req;
Me@24 519
Me@24 520 VMS__suspend_procr( reqstingPr );
Me@22 521 }
Me@22 522
Me@22 523
Me@22 524
Me@24 525 /*This must be called by the request handler plugin -- it cannot be called
Me@24 526 * from the semantic library "dissipate processor" function -- instead, the
Me@24 527 * semantic layer has to generate a request for the plug-in to call this
Me@24 528 * function.
Me@24 529 *The reason is that this frees the virtual processor's stack -- which is
Me@24 530 * still in use inside semantic library calls!
Me@24 531 *
Me@24 532 *This frees or recycles all the state owned by and comprising the VMS
Me@24 533 * portion of the animating virtual procr. The request handler must first
Me@24 534 * free any semantic data created for the processor that didn't use the
Me@24 535 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
Me@24 536 * system to disown any state that did use VMS_malloc, and then frees the
Me@24 537 * statck and the processor-struct itself.
Me@24 538 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
Me@24 539 * state, then that state gets freed (or sent to recycling) as a side-effect
Me@24 540 * of dis-owning it.
Me@24 541 */
Me@24 542 void
Me@29 543 VMS__handle_dissipate_reqst( VirtProcr *animatingPr )
Me@24 544 {
Me@24 545 //dis-own all locations owned by this processor, causing to be freed
Me@24 546 // any locations that it is (was) sole owner of
Me@29 547 //TODO: implement VMS__malloc system, including "give up ownership"
Me@24 548
Me@24 549 //The dissipate request might still be attached, so remove and free it
Me@24 550 VMS__remove_and_free_top_request( animatingPr );
Me@24 551
Me@24 552 //NOTE: initialData was given to the processor, so should either have
Me@24 553 // been alloc'd with VMS__malloc, or freed by the level above animPr.
Me@24 554 //So, all that's left to free here is the stack and the VirtProcr struc
Me@24 555 // itself
Me@24 556 free( animatingPr->startOfStack );
Me@24 557 free( animatingPr );
Me@24 558 }
Me@24 559
Me@24 560
Me@29 561 //TODO: re-architect so that have clean separation between request handler
Me@29 562 // and master loop, for dissipate, create, shutdown, and other non-semantic
Me@29 563 // requests. Issue is chain: one removes requests from AppVP, one dispatches
Me@29 564 // on type of request, and one handles each type.. but some types require
Me@29 565 // action from both request handler and master loop -- maybe just give the
Me@29 566 // request handler calls like: VMS__handle_X_request_type
Me@24 567
Me@29 568 void
Me@29 569 endOSThreadFn( void *initData, VirtProcr *animatingPr );
Me@29 570
Me@29 571 /*This is called by the semantic layer's request handler when it decides its
Me@29 572 * time to shut down the VMS system. Calling this causes the core loop OS
Me@29 573 * threads to exit, which unblocks the entry-point function that started up
Me@29 574 * VMS, and allows it to grab the result and return to the original single-
Me@29 575 * threaded application.
Me@22 576 *
Me@29 577 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
Me@29 578 * and-wait function has to free a bunch of stuff after it detects the
Me@29 579 * threads have all died: the masterEnv, the thread-related locations,
Me@29 580 * masterVP any AppVPs that might still be allocated and sitting in the
Me@29 581 * semantic environment, or have been orphaned in the _VMSWorkQ.
Me@29 582 *
Me@29 583 *NOTE: the semantic plug-in is expected to use VMS__malloc_to to get all the
Me@29 584 * locations it needs, and give ownership to masterVP. Then, they will be
Me@29 585 * automatically freed when the masterVP is dissipated. (This happens after
Me@29 586 * the core loop threads have all exited)
Me@22 587 *
Me@29 588 *In here,create one core-loop shut-down processor for each core loop and put
Me@31 589 * them all directly into the readyToAnimateQ.
Me@29 590 *Note, this function can ONLY be called after the semantic environment no
Me@29 591 * longer cares if AppVPs get animated after the point this is called. In
Me@29 592 * other words, this can be used as an abort, or else it should only be
Me@29 593 * called when all AppVPs have finished dissipate requests -- only at that
Me@29 594 * point is it sure that all results have completed.
Me@22 595 */
Me@22 596 void
Me@29 597 VMS__handle_shutdown_reqst( void *dummy, VirtProcr *animatingPr )
Me@8 598 { int coreIdx;
Me@14 599 VirtProcr *shutDownPr;
Me@22 600
Me@29 601 //create the shutdown processors, one for each core loop -- put them
Me@31 602 // directly into the Q -- each core will die when gets one
Me@8 603 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@8 604 {
Me@29 605 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
Me@31 606 writeSRSWQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
Me@8 607 }
Me@22 608
Me@12 609 }
Me@12 610
Me@12 611
Me@29 612 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
Me@29 613 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
Me@29 614 *This function has the sole purpose of setting the stack and framePtr
Me@29 615 * to the coreLoop's stack and framePtr.. it does that then jumps to the
Me@29 616 * core loop's shutdown point -- might be able to just call Pthread_exit
Me@30 617 * from here, but am going back to the pthread's stack and setting everything
Me@29 618 * up just as if it never jumped out, before calling pthread_exit.
Me@29 619 *The end-point of core loop will free the stack and so forth of the
Me@29 620 * processor that animates this function, (this fn is transfering the
Me@29 621 * animator of the AppVP that is in turn animating this function over
Me@29 622 * to core loop function -- note that this slices out a level of virtual
Me@29 623 * processors).
Me@29 624 */
Me@29 625 void
Me@29 626 endOSThreadFn( void *initData, VirtProcr *animatingPr )
Me@29 627 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
Me@29 628
Me@29 629 jmpPt = _VMSMasterEnv->coreLoopEndPt;
Me@29 630 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
Me@29 631 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
Me@29 632
Me@29 633
Me@29 634 asm volatile("movl %0, %%eax; \
Me@29 635 movl %1, %%esp; \
Me@29 636 movl %2, %%ebp; \
Me@29 637 jmp %%eax " \
Me@29 638 /* outputs */ : \
Me@29 639 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
Me@29 640 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
Me@29 641 );
Me@29 642 }
Me@29 643
Me@29 644
Me@31 645 /*This is called after the threads have shut down and control has returned
Me@30 646 * to the semantic layer, in the entry point function in the main thread.
Me@30 647 * It has to free anything allocated during VMS_init, and any other alloc'd
Me@24 648 * locations that might be left over.
Me@24 649 */
Me@24 650 void
Me@29 651 VMS__cleanup_after_shutdown()
Me@31 652 {
Me@31 653 SRSWQueueStruc **readyToAnimateQs;
Me@31 654 int coreIdx;
Me@31 655 VirtProcr **masterVPs;
Me@31 656 SchedSlot ***allSchedSlots; //ptr to array of ptrs
Me@31 657
Me@31 658 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
Me@31 659 masterVPs = _VMSMasterEnv->masterVPs;
Me@31 660 allSchedSlots = _VMSMasterEnv->allSchedSlots;
Me@31 661
Me@31 662 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
Me@24 663 {
Me@31 664 freeSRSWQ( readyToAnimateQs[ coreIdx ] );
Me@31 665
Me@31 666 VMS__handle_dissipate_reqst( masterVPs[ coreIdx ] );
Me@31 667
Me@31 668 freeSchedSlots( allSchedSlots[ coreIdx ] );
Me@24 669 }
Me@31 670
Me@31 671 free( _VMSMasterEnv->readyToAnimateQs );
Me@31 672 free( _VMSMasterEnv->masterVPs );
Me@31 673 free( _VMSMasterEnv->allSchedSlots );
Me@24 674
Me@24 675 free( _VMSMasterEnv );
Me@24 676 }
Me@24 677
Me@24 678
Me@24 679 //===========================================================================
Me@12 680
Me@12 681 inline TSCount getTSCount()
Me@12 682 { unsigned int low, high;
Me@12 683 TSCount out;
Me@12 684
Me@12 685 saveTimeStampCountInto( low, high );
Me@12 686 out = high;
Me@12 687 out = (out << 32) + low;
Me@12 688 return out;
Me@12 689 }
Me@12 690