annotate VMS.c @ 26:668278fa7a63

Sequential -- just starting to add sequential version
author Me
date Mon, 26 Jul 2010 15:25:53 -0700
parents c556193f7211
children 8b9e4c333fe6
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@22 21 void
Me@22 22 create_sched_slots( MasterEnv *masterEnv );
Me@22 23
Me@26 24 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
Me@26 25 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
Me@26 26
Me@22 27 //===========================================================================
Me@22 28
Me@0 29 /*Setup has two phases:
Me@0 30 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
Me@8 31 * the master virt procr into the work-queue, ready for first "call"
Me@8 32 * 2) Semantic layer then does its own init, which creates the seed virt
Me@8 33 * procr inside the semantic layer, ready to schedule it when
Me@0 34 * asked by the first run of the masterLoop.
Me@0 35 *
Me@0 36 *This part is bit weird because VMS really wants to be "always there", and
Me@0 37 * have applications attach and detach.. for now, this VMS is part of
Me@0 38 * the app, so the VMS system starts up as part of running the app.
Me@0 39 *
Me@8 40 *The semantic layer is isolated from the VMS internals by making the
Me@8 41 * semantic layer do setup to a state that it's ready with its
Me@8 42 * initial virt procrs, ready to schedule them to slots when the masterLoop
Me@0 43 * asks. Without this pattern, the semantic layer's setup would
Me@8 44 * have to modify slots directly to assign the initial virt-procrs, and put
Me@0 45 * them into the workQ itself, breaking the isolation completely.
Me@0 46 *
Me@0 47 *
Me@8 48 *The semantic layer creates the initial virt procr(s), and adds its
Me@8 49 * own environment to masterEnv, and fills in the pointers to
Me@0 50 * the requestHandler and slaveScheduler plug-in functions
Me@8 51 */
Me@8 52
Me@8 53 /*This allocates VMS data structures, populates the master VMSProc,
Me@0 54 * and master environment, and returns the master environment to the semantic
Me@0 55 * layer.
Me@0 56 */
Me@8 57 void
Me@8 58 VMS__init()
Me@1 59 { MasterEnv *masterEnv;
Me@26 60 VMSQueueStruc *workQ;
Me@1 61
Me@0 62 //Make the central work-queue
Me@26 63 _VMSWorkQ = makeVMSQ();
Me@1 64 workQ = _VMSWorkQ;
Me@0 65
Me@1 66 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
Me@1 67 masterEnv = _VMSMasterEnv;
Me@0 68
Me@8 69 //create the master virtual processor
Me@8 70 masterEnv->masterVirtPr = VMS__create_procr( &masterLoop, masterEnv );
Me@0 71
Me@1 72 create_sched_slots( masterEnv );
Me@0 73
Me@24 74 masterEnv->stillRunning = FALSE;
Me@26 75 masterEnv->numToPrecede = NUM_CORES;
Me@22 76
Me@1 77 //First core loop to start up gets this, which will schedule seed Pr
Me@1 78 //TODO: debug: check address of masterVirtPr
Me@26 79 writeVMSQ( masterEnv->masterVirtPr, workQ );
Me@12 80
Me@12 81 numProcrsCreated = 1;
Me@24 82
Me@24 83 //========================================================================
Me@24 84 // Create the Threads
Me@25 85 int coreIdx, retCode;
Me@26 86
Me@25 87 //Need the threads to be created suspended, and wait for a signal
Me@25 88 // before proceeding -- gives time after creating to initialize other
Me@25 89 // stuff before the coreLoops set off.
Me@26 90 _VMSMasterEnv->setupComplete = 0;
Me@26 91
Me@26 92 //Make the threads that animate the core loops
Me@24 93 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@24 94 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
Me@24 95 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
Me@24 96
Me@25 97 retCode =
Me@25 98 pthread_create( &(coreLoopThdHandles[coreIdx]),
Me@25 99 thdAttrs,
Me@25 100 &coreLoop,
Me@25 101 (void *)(coreLoopThdParams[coreIdx]) );
Me@26 102 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
Me@24 103 }
Me@0 104 }
Me@0 105
Me@0 106 void
Me@1 107 create_sched_slots( MasterEnv *masterEnv )
Me@8 108 { SchedSlot **schedSlots, **filledSlots;
Me@0 109 int i;
Me@0 110
Me@8 111 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
Me@8 112 filledSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
Me@8 113 masterEnv->schedSlots = schedSlots;
Me@8 114 masterEnv->filledSlots = filledSlots;
Me@8 115
Me@1 116 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
Me@0 117 {
Me@8 118 schedSlots[i] = malloc( sizeof(SchedSlot) );
Me@8 119
Me@1 120 //Set state to mean "handling requests done, slot needs filling"
Me@8 121 schedSlots[i]->workIsDone = FALSE;
Me@8 122 schedSlots[i]->needsProcrAssigned = TRUE;
Me@0 123 }
Me@0 124 }
Me@0 125
Me@8 126
Me@0 127 /*Semantic layer calls this when it want the system to start running..
Me@0 128 *
Me@24 129 *This starts the core loops running then waits for them to exit.
Me@0 130 */
Me@12 131 void
Me@24 132 VMS__start_the_work_then_wait_until_done()
Me@12 133 { int coreIdx;
Me@24 134 //Start the core loops running
Me@24 135 //===========================================================================
Me@25 136 TSCount startCount, endCount;
Me@24 137 unsigned long long count = 0, freq = 0;
Me@25 138 double runTime;
Me@0 139
Me@25 140 startCount = getTSCount();
Me@25 141
Me@25 142 //tell the core loop threads that setup is complete
Me@25 143 //get lock, to lock out any threads still starting up -- they'll see
Me@25 144 // that setupComplete is true before entering while loop, and so never
Me@25 145 // wait on the condition
Me@26 146 pthread_mutex_lock( &suspendLock );
Me@25 147 _VMSMasterEnv->setupComplete = 1;
Me@26 148 pthread_mutex_unlock( &suspendLock );
Me@26 149 pthread_cond_broadcast( &suspend_cond );
Me@25 150
Me@25 151
Me@24 152 //wait for all to complete
Me@8 153 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@8 154 {
Me@25 155 pthread_join( coreLoopThdHandles[coreIdx], NULL );
Me@24 156 }
Me@25 157
Me@24 158 //NOTE: do not clean up VMS env here -- semantic layer has to have
Me@24 159 // a chance to clean up its environment first, then do a call to free
Me@24 160 // the Master env and rest of VMS locations
Me@24 161
Me@24 162
Me@25 163 endCount = getTSCount();
Me@25 164 count = endCount - startCount;
Me@24 165
Me@25 166 runTime = (double)count / (double)TSCOUNT_FREQ;
Me@25 167
Me@25 168 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
Me@8 169 }
Me@0 170
Me@0 171
Me@0 172
Me@8 173 /*Create stack, then create __cdecl structure on it and put initialData and
Me@8 174 * pointer to the new structure instance into the parameter positions on
Me@8 175 * the stack
Me@8 176 *Then put function pointer into nextInstrPt -- the stack is setup in std
Me@8 177 * call structure, so jumping to function ptr is same as a GCC generated
Me@8 178 * function call
Me@8 179 *No need to save registers on old stack frame, because there's no old
Me@8 180 * animator state to return to --
Me@8 181 *
Me@8 182 */
Me@8 183 VirtProcr *
Me@8 184 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
Me@8 185 { VirtProcr *newPr;
Me@8 186 char *stackLocs, *stackPtr;
Me@8 187
Me@8 188 newPr = malloc( sizeof(VirtProcr) );
Me@12 189 newPr->procrID = numProcrsCreated++;
Me@8 190 newPr->nextInstrPt = fnPtr;
Me@8 191 newPr->initialData = initialData;
Me@8 192
Me@14 193 //fnPtr takes two params -- void *initData & void *animProcr
Me@8 194 //alloc stack locations, make stackPtr be the highest addr minus room
Me@14 195 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
Me@14 196 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
Me@22 197 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
Me@26 198 if(stackLocs == 0)
Me@26 199 {perror("malloc stack"); exit(1);}
Me@22 200 newPr->startOfStack = stackLocs;
Me@22 201 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
Me@8 202 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
Me@22 203 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
Me@14 204 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
Me@8 205 newPr->stackPtr = stackPtr; //core loop will switch to this, then
Me@8 206 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
Me@8 207
Me@8 208 return newPr;
Me@8 209 }
Me@8 210
Me@8 211
Me@26 212 /*there is a label inside this function -- save the addr of this label in
Me@0 213 * the callingPr struc, as the pick-up point from which to start the next
Me@0 214 * work-unit for that procr. If turns out have to save registers, then
Me@0 215 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
Me@0 216 * "done with work-unit" label. The procr struc is in the request in the
Me@0 217 * slave that animated the just-ended work-unit, so all the state is saved
Me@0 218 * there, and will get passed along, inside the request handler, to the
Me@0 219 * next work-unit for that procr.
Me@0 220 */
Me@8 221 void
Me@22 222 VMS__suspend_procr( VirtProcr *callingPr )
Me@14 223 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
Me@14 224 void *coreLoopFramePtr;
Me@0 225
Me@14 226 //The request to master will cause this suspended virt procr to get
Me@14 227 // scheduled again at some future point -- to resume, core loop jumps
Me@14 228 // to the resume point (below), which causes restore of saved regs and
Me@14 229 // "return" from this call.
Me@1 230 callingPr->nextInstrPt = &&ResumePt;
Me@1 231
Me@1 232 //return ownership of the virt procr and sched slot to Master virt pr
Me@1 233 callingPr->schedSlot->workIsDone = TRUE;
Me@14 234 // coreIdx = callingPr->coreAnimatedBy;
Me@1 235
Me@18 236 stackPtrAddr = &(callingPr->stackPtr);
Me@18 237 framePtrAddr = &(callingPr->framePtr);
Me@26 238
Me@14 239 jmpPt = callingPr->coreLoopStartPt;
Me@14 240 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only
Me@18 241 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety
Me@1 242
Me@26 243 //Eclipse's compilation sequence complains -- so break into two
Me@26 244 // separate in-line assembly pieces
Me@26 245 //Save the virt procr's stack and frame ptrs,
Me@18 246 asm volatile("movl %0, %%eax; \
Me@18 247 movl %%esp, (%%eax); \
Me@18 248 movl %1, %%eax; \
Me@26 249 movl %%ebp, (%%eax) "\
Me@26 250 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
Me@26 251 /* inputs */ : \
Me@26 252 /* clobber */ : "%eax" \
Me@26 253 );
Me@26 254
Me@26 255 //restore coreloop's frame ptr, then jump back to "start" of core loop
Me@26 256 //Note, GCC compiles to assembly that saves esp and ebp in the stack
Me@26 257 // frame -- so have to explicitly do assembly that saves to memory
Me@26 258 asm volatile("movl %0, %%eax; \
Me@26 259 movl %1, %%esp; \
Me@26 260 movl %2, %%ebp; \
Me@18 261 jmp %%eax " \
Me@26 262 /* outputs */ : \
Me@26 263 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
Me@18 264 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
Me@12 265 ); //list everything as clobbered to force GCC to save all
Me@12 266 // live vars that are in regs on stack before this
Me@12 267 // assembly, so that stack pointer is correct, before jmp
Me@1 268
Me@1 269 ResumePt:
Me@0 270 return;
Me@0 271 }
Me@0 272
Me@22 273
Me@22 274
Me@22 275 /*This is equivalent to "jump back to core loop" -- it's mainly only used
Me@22 276 * just after adding dissipate request to a processor -- so the semantic
Me@22 277 * layer is the only place it will be seen and/or used.
Me@22 278 *
Me@22 279 *It does almost the same thing as suspend, except don't need to save the
Me@22 280 * stack nor set the nextInstrPt
Me@22 281 *
Me@22 282 *As of June 30, 2010 just implementing as a call to suspend -- just sugar
Me@22 283 */
Me@8 284 void
Me@22 285 VMS__return_from_fn( VirtProcr *animatingPr )
Me@8 286 {
Me@22 287 VMS__suspend_procr( animatingPr );
Me@1 288 }
Me@1 289
Me@22 290
Me@22 291 /*Not sure yet the form going to put "dissipate" in, so this is the third
Me@22 292 * possibility -- the semantic layer can just make a macro that looks like
Me@22 293 * a call to its name, then expands to a call to this.
Me@8 294 *
Me@22 295 *As of June 30, 2010 this looks like the top choice..
Me@8 296 *
Me@22 297 *This adds a request to dissipate, then suspends the processor so that the
Me@22 298 * request handler will receive the request. The request handler is what
Me@22 299 * does the work of freeing memory and removing the processor from the
Me@22 300 * semantic environment's data structures.
Me@22 301 *The request handler also is what figures out when to shutdown the VMS
Me@22 302 * system -- which causes all the core loop threads to die, and returns from
Me@22 303 * the call that started up VMS to perform the work.
Me@22 304 *
Me@22 305 *This form is a bit misleading to understand if one is trying to figure out
Me@22 306 * how VMS works -- it looks like a normal function call, but inside it
Me@22 307 * sends a request to the request handler and suspends the processor, which
Me@22 308 * jumps out of the VMS__dissipate_procr function, and out of all nestings
Me@22 309 * above it, transferring the work of dissipating to the request handler,
Me@22 310 * which then does the actual work -- causing the processor that animated
Me@22 311 * the call of this function to disappear and the "hanging" state of this
Me@22 312 * function to just poof into thin air -- the virtual processor's trace
Me@22 313 * never returns from this call, but instead the virtual processor's trace
Me@22 314 * gets suspended in this call and all the virt processor's state disap-
Me@22 315 * pears -- making that suspend the last thing in the virt procr's trace.
Me@8 316 */
Me@8 317 void
Me@22 318 VMS__dissipate_procr( VirtProcr *procrToDissipate )
Me@22 319 { VMSReqst *req;
Me@22 320
Me@22 321 req = malloc( sizeof(VMSReqst) );
Me@22 322 // req->virtProcrFrom = callingPr;
Me@22 323 req->reqType = dissipate;
Me@22 324 req->nextReqst = procrToDissipate->requests;
Me@22 325 procrToDissipate->requests = req;
Me@22 326
Me@22 327 VMS__suspend_procr( procrToDissipate );
Me@22 328 }
Me@22 329
Me@22 330
Me@22 331 /*This inserts the semantic-layer's request data into standard VMS carrier
Me@22 332 */
Me@22 333 inline void
Me@24 334 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
Me@22 335 { VMSReqst *req;
Me@22 336
Me@22 337 req = malloc( sizeof(VMSReqst) );
Me@22 338 // req->virtProcrFrom = callingPr;
Me@22 339 req->reqType = semantic;
Me@22 340 req->semReqData = semReqData;
Me@22 341 req->nextReqst = callingPr->requests;
Me@22 342 callingPr->requests = req;
Me@22 343 }
Me@22 344
Me@22 345
Me@22 346
Me@22 347 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
Me@22 348 // of a request -- IE call with both a virt procr and a fn-ptr to request
Me@22 349 // freer (or maybe put request freer as a field in virt procr?)
Me@22 350 void
Me@22 351 VMS__remove_and_free_top_request( VirtProcr *procrWithReq )
Me@22 352 { VMSReqst *req;
Me@22 353
Me@22 354 req = procrWithReq->requests;
Me@22 355 procrWithReq->requests = procrWithReq->requests->nextReqst;
Me@22 356 free( req );
Me@22 357 }
Me@22 358
Me@24 359
Me@24 360 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
Me@24 361 // of a request -- IE call with both a virt procr and a fn-ptr to request
Me@24 362 // freer (also maybe put sem request freer as a field in virt procr?)
Me@26 363 //VMSHW relies right now on this only freeing VMS layer of request -- the
Me@26 364 // semantic portion of request is alloc'd and freed by request handler
Me@22 365 void
Me@24 366 VMS__free_request( VMSReqst *req )
Me@24 367 {
Me@24 368 free( req );
Me@24 369 }
Me@24 370
Me@24 371 VMSReqst *
Me@24 372 VMS__take_top_request_from( VirtProcr *procrWithReq )
Me@24 373 { VMSReqst *req;
Me@24 374
Me@24 375 req = procrWithReq->requests;
Me@24 376 if( req == NULL ) return req;
Me@24 377
Me@24 378 procrWithReq->requests = procrWithReq->requests->nextReqst;
Me@24 379 return req;
Me@24 380 }
Me@24 381
Me@24 382 inline int
Me@24 383 VMS__isSemanticReqst( VMSReqst *req )
Me@22 384 {
Me@24 385 return ( req->reqType == semantic );
Me@24 386 }
Me@22 387
Me@24 388
Me@24 389 inline void *
Me@24 390 VMS__take_sem_reqst_from( VMSReqst *req )
Me@24 391 {
Me@24 392 return req->semReqData;
Me@24 393 }
Me@24 394
Me@24 395 inline int
Me@24 396 VMS__isDissipateReqst( VMSReqst *req )
Me@24 397 {
Me@24 398 return ( req->reqType == dissipate );
Me@24 399 }
Me@24 400
Me@24 401 inline int
Me@24 402 VMS__isCreateReqst( VMSReqst *req )
Me@24 403 {
Me@24 404 return ( req->reqType == regCreated );
Me@24 405 }
Me@24 406
Me@24 407 void
Me@24 408 VMS__send_register_new_procr_request(VirtProcr *newPr, VirtProcr *reqstingPr)
Me@24 409 { VMSReqst *req;
Me@24 410
Me@24 411 req = malloc( sizeof(VMSReqst) );
Me@24 412 req->reqType = regCreated;
Me@24 413 req->semReqData = newPr;
Me@24 414 req->nextReqst = reqstingPr->requests;
Me@24 415 reqstingPr->requests = req;
Me@24 416
Me@24 417 VMS__suspend_procr( reqstingPr );
Me@22 418 }
Me@22 419
Me@22 420
Me@22 421 /*The semantic layer figures out when the work is done ( perhaps by a call
Me@22 422 * in the application to "work all done", or perhaps all the virtual
Me@22 423 * processors have dissipated.. a.s.o. )
Me@22 424 *
Me@22 425 *The semantic layer is responsible for making sure all work has fully
Me@22 426 * completed before using this to shutdown the VMS system.
Me@22 427 *
Me@22 428 *After the semantic layer has determined it wants to shut down, the
Me@22 429 * next time the Master Loop calls the scheduler plug-in, the scheduler
Me@22 430 * then calls this function and returns the virtual processor it gets back.
Me@22 431 *
Me@22 432 *When the shut-down processor runs, it first frees all locations malloc'd to
Me@22 433 * the VMS system (that wasn't
Me@22 434 * specified as return-locations). Then it creates one core-loop shut-down
Me@22 435 * processor for each core loop and puts them all into the workQ. When a
Me@22 436 * core loop animates a core loop shut-down processor, it causes exit-thread
Me@22 437 * to run, and when all core loop threads have exited, then the "wait for
Me@22 438 * work to finish" in the main thread is woken, and the function-call that
Me@22 439 * started all the work returns.
Me@22 440 *
Me@22 441 *The function animated by this processor performs the shut-down work.
Me@22 442 */
Me@22 443 VirtProcr *
Me@22 444 VMS__create_the_shutdown_procr()
Me@22 445 {
Me@22 446 return VMS__create_procr( &shutdownFn, NULL );
Me@22 447 }
Me@22 448
Me@22 449
Me@24 450 /*This must be called by the request handler plugin -- it cannot be called
Me@24 451 * from the semantic library "dissipate processor" function -- instead, the
Me@24 452 * semantic layer has to generate a request for the plug-in to call this
Me@24 453 * function.
Me@24 454 *The reason is that this frees the virtual processor's stack -- which is
Me@24 455 * still in use inside semantic library calls!
Me@24 456 *
Me@24 457 *This frees or recycles all the state owned by and comprising the VMS
Me@24 458 * portion of the animating virtual procr. The request handler must first
Me@24 459 * free any semantic data created for the processor that didn't use the
Me@24 460 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
Me@24 461 * system to disown any state that did use VMS_malloc, and then frees the
Me@24 462 * statck and the processor-struct itself.
Me@24 463 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
Me@24 464 * state, then that state gets freed (or sent to recycling) as a side-effect
Me@24 465 * of dis-owning it.
Me@24 466 */
Me@24 467 void
Me@24 468 VMS__free_procr_locs( VirtProcr *animatingPr )
Me@24 469 {
Me@24 470 //dis-own all locations owned by this processor, causing to be freed
Me@24 471 // any locations that it is (was) sole owner of
Me@24 472 //TODO: implement VMS__malloc system, including "give up ownership"
Me@24 473
Me@24 474 //The dissipate request might still be attached, so remove and free it
Me@24 475 VMS__remove_and_free_top_request( animatingPr );
Me@24 476 free( animatingPr->startOfStack );
Me@24 477
Me@24 478 //NOTE: initialData was given to the processor, so should either have
Me@24 479 // been alloc'd with VMS__malloc, or freed by the level above animPr.
Me@24 480 //So, all that's left to free here is the stack and the VirtProcr struc
Me@24 481 // itself
Me@24 482 free( animatingPr->startOfStack );
Me@24 483 free( animatingPr );
Me@24 484 }
Me@24 485
Me@24 486
Me@24 487
Me@22 488 /*This is the function run by the special "shut-down" processor
Me@22 489 *
Me@22 490 *The _VMSMasterEnv is needed by this shut down function, so the "wait"
Me@22 491 * function run in the main loop has to free it, and the thread-related
Me@22 492 * locations (coreLoopThdParams a.s.o.).
Me@22 493 *However, the semantic environment and all data malloc'd to VMS can be
Me@22 494 * freed here.
Me@22 495 *
Me@22 496 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
Me@22 497 * locations it needs -- they will be automatically freed by the standard
Me@22 498 * "free all owned locations"
Me@22 499 *
Me@22 500 *Free any locations malloc'd to the VMS system (that weren't
Me@22 501 * specified as return-locations).
Me@22 502 *Then create one core-loop shut-down processor for each core loop and puts
Me@22 503 * them all into the workQ.
Me@22 504 */
Me@22 505 void
Me@22 506 shutdownFn( void *dummy, VirtProcr *animatingPr )
Me@8 507 { int coreIdx;
Me@14 508 VirtProcr *shutDownPr;
Me@26 509 VMSQueueStruc *workQ = _VMSWorkQ;
Me@22 510
Me@22 511 //free all the locations owned within the VMS system
Me@22 512 //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt
Me@22 513
Me@22 514 //make the core loop shut-down processors and put them into the workQ
Me@8 515 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@8 516 {
Me@14 517 shutDownPr = VMS__create_procr( NULL, NULL );
Me@14 518 shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt;
Me@26 519 writeVMSQ( shutDownPr, workQ );
Me@8 520 }
Me@22 521
Me@22 522 //This is an issue: the animating processor of this function may not
Me@22 523 // get its request handled before all the cores have shutdown.
Me@22 524 //TODO: after all the threads stop, clean out the MasterEnv, the
Me@22 525 // SemanticEnv, and the workQ before returning.
Me@24 526 VMS__dissipate_procr( animatingPr ); //will never come back from this
Me@12 527 }
Me@12 528
Me@12 529
Me@24 530 /*This has to free anything allocated during VMS_init, and any other alloc'd
Me@24 531 * locations that might be left over.
Me@24 532 */
Me@24 533 void
Me@24 534 VMS__shutdown()
Me@24 535 { int i;
Me@24 536
Me@24 537 free( _VMSWorkQ );
Me@24 538 free( _VMSMasterEnv->filledSlots );
Me@24 539 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
Me@24 540 {
Me@24 541 free( _VMSMasterEnv->schedSlots[i] );
Me@24 542 }
Me@24 543
Me@24 544 free( _VMSMasterEnv->schedSlots);
Me@24 545 VMS__free_procr_locs( _VMSMasterEnv->masterVirtPr );
Me@24 546
Me@24 547 free( _VMSMasterEnv );
Me@24 548 }
Me@24 549
Me@24 550
Me@24 551 //===========================================================================
Me@12 552
Me@12 553 inline TSCount getTSCount()
Me@12 554 { unsigned int low, high;
Me@12 555 TSCount out;
Me@12 556
Me@12 557 saveTimeStampCountInto( low, high );
Me@12 558 out = high;
Me@12 559 out = (out << 32) + low;
Me@12 560 return out;
Me@12 561 }
Me@12 562