# HG changeset patch # User Me # Date 1277928659 25200 # Node ID 1dbc7f6e3e67bd591d4de7714f6349665aed3279 # Parent a0af8d4fca35e601b790bfea4678b3bffdb492bd Full VMS test -- works diff -r a0af8d4fca35 -r 1dbc7f6e3e67 VMS.c --- a/VMS.c Wed Jun 30 13:10:34 2010 -0700 +++ b/VMS.c Wed Jun 30 13:10:59 2010 -0700 @@ -12,6 +12,15 @@ #include "Queue_impl/BlockingQueue.h" +//=========================================================================== +void +shutdownFn( void *dummy, VirtProcr *dummy2 ); + +void +create_sched_slots( MasterEnv *masterEnv ); + +//=========================================================================== + /*Setup has two phases: * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts * the master virt procr into the work-queue, ready for first "call" @@ -36,10 +45,6 @@ * the requestHandler and slaveScheduler plug-in functions */ -void -create_sched_slots( MasterEnv *masterEnv ); - - /*This allocates VMS data structures, populates the master VMSProc, * and master environment, and returns the master environment to the semantic * layer. @@ -65,11 +70,11 @@ masterEnv->schedSlots[0]->needsProcrAssigned = FALSE; //says don't touch masterEnv->schedSlots[0]->workIsDone = FALSE; //says don't touch masterEnv->schedSlots[0]->procrAssignedToSlot = masterEnv->masterVirtPr; - + masterEnv->masterVirtPr->schedSlot = masterEnv->schedSlots[0]; + //First core loop to start up gets this, which will schedule seed Pr //TODO: debug: check address of masterVirtPr -//TODO: commented out for debugging -- put it back in!! -// writeCASQ( masterEnv->masterVirtPr, workQ ); + writeCASQ( masterEnv->masterVirtPr, workQ ); numProcrsCreated = 1; } @@ -150,10 +155,11 @@ //alloc stack locations, make stackPtr be the highest addr minus room // for 2 params + return addr. Return addr (NULL) is in loc pointed to // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above - stackLocs = malloc( 0x100000 ); //1 meg stack -- default Win thread's size - stackPtr = ( (char *)stackLocs + 0x100000 - 0x10 ); + stackLocs = malloc( VIRT_PROCR_STACK_SIZE ); + newPr->startOfStack = stackLocs; + stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 ); //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp - *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer + *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left newPr->stackPtr = stackPtr; //core loop will switch to this, then newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr @@ -162,19 +168,6 @@ } -/*This inserts the semantic-layer's data into the standard VMS carrier - */ -inline void -VMS__send_sem_request( void *semReqData, VirtProcr *callingPr ) - { SlaveReqst *req; - - req = malloc( sizeof(SlaveReqst) ); - req->slaveFrom = callingPr; - req->semReqData = semReqData; - req->nextRequest = callingPr->requests; - callingPr->requests = req; - } - /*there is a label inside this function -- save the addr of this label in * the callingPr struc, as the pick-up point from which to start the next * work-unit for that procr. If turns out have to save registers, then @@ -185,10 +178,9 @@ * next work-unit for that procr. */ void -VMS__suspend_processor( VirtProcr *callingPr ) +VMS__suspend_procr( VirtProcr *callingPr ) { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr; void *coreLoopFramePtr; - int coreIdx; //The request to master will cause this suspended virt procr to get // scheduled again at some future point -- to resume, core loop jumps @@ -209,6 +201,8 @@ //Save the virt procr's stack and frame ptrs, restore coreloop's frame // ptr, then jump back to "start" of core loop + //Note, GCC compiles to assembly that saves esp and ebp in the stack + // frame -- so have to explicitly do assembly that saves to memory asm volatile("movl %0, %%eax; \ movl %%esp, (%%eax); \ movl %1, %%eax; \ @@ -228,37 +222,208 @@ return; } + + +/*This is equivalent to "jump back to core loop" -- it's mainly only used + * just after adding dissipate request to a processor -- so the semantic + * layer is the only place it will be seen and/or used. + * + *It does almost the same thing as suspend, except don't need to save the + * stack nor set the nextInstrPt + * + *As of June 30, 2010 just implementing as a call to suspend -- just sugar + */ void -VMS__dissipate_animating_processor( VirtProcr *animatingPr ) +VMS__return_from_fn( VirtProcr *animatingPr ) { - + VMS__suspend_procr( animatingPr ); } -/*This runs in main thread -- so can only signal to the core loop to shut - * itself down -- + +/*Not sure yet the form going to put "dissipate" in, so this is the third + * possibility -- the semantic layer can just make a macro that looks like + * a call to its name, then expands to a call to this. * - *Want the master to decide when to shut down -- when semantic layer tells it - * to -- say, when all the application-virtual processors have dissipated. + *As of June 30, 2010 this looks like the top choice.. * - *Maybe return a special code from scheduling plug-in.. master checks and - * when sees, it shuts down the core loops -- does this by scheduling a - * special virt processor whose next instr pt is the core-end label. + *This adds a request to dissipate, then suspends the processor so that the + * request handler will receive the request. The request handler is what + * does the work of freeing memory and removing the processor from the + * semantic environment's data structures. + *The request handler also is what figures out when to shutdown the VMS + * system -- which causes all the core loop threads to die, and returns from + * the call that started up VMS to perform the work. + * + *This form is a bit misleading to understand if one is trying to figure out + * how VMS works -- it looks like a normal function call, but inside it + * sends a request to the request handler and suspends the processor, which + * jumps out of the VMS__dissipate_procr function, and out of all nestings + * above it, transferring the work of dissipating to the request handler, + * which then does the actual work -- causing the processor that animated + * the call of this function to disappear and the "hanging" state of this + * function to just poof into thin air -- the virtual processor's trace + * never returns from this call, but instead the virtual processor's trace + * gets suspended in this call and all the virt processor's state disap- + * pears -- making that suspend the last thing in the virt procr's trace. */ void -VMS__shutdown() +VMS__dissipate_procr( VirtProcr *procrToDissipate ) + { VMSReqst *req; + + req = malloc( sizeof(VMSReqst) ); +// req->virtProcrFrom = callingPr; + req->reqType = dissipate; + req->nextReqst = procrToDissipate->requests; + procrToDissipate->requests = req; + + VMS__suspend_procr( procrToDissipate ); +} + + +/*This inserts the semantic-layer's request data into standard VMS carrier + */ +inline void +VMS__send_sem_request( void *semReqData, VirtProcr *callingPr ) + { VMSReqst *req; + + req = malloc( sizeof(VMSReqst) ); +// req->virtProcrFrom = callingPr; + req->reqType = semantic; + req->semReqData = semReqData; + req->nextReqst = callingPr->requests; + callingPr->requests = req; + } + + +/*This creates a request of type "dissipate" -- which will cause the virt + * processor's state and owned locations to be freed + */ +inline void +VMS__send_dissipate_request( VirtProcr *procrToDissipate ) + { VMSReqst *req; + + req = malloc( sizeof(VMSReqst) ); +// req->virtProcrFrom = callingPr; + req->reqType = dissipate; + req->nextReqst = procrToDissipate->requests; + procrToDissipate->requests = req; + } + + +//TODO: add a semantic-layer supplied "freer" for the semantic-data portion +// of a request -- IE call with both a virt procr and a fn-ptr to request +// freer (or maybe put request freer as a field in virt procr?) +void +VMS__remove_and_free_top_request( VirtProcr *procrWithReq ) + { VMSReqst *req; + + req = procrWithReq->requests; + procrWithReq->requests = procrWithReq->requests->nextReqst; + free( req ); + } + +/*This must be called by the request handler plugin -- it cannot be called + * from the semantic library "dissipate processor" function -- instead, the + * semantic layer has to generate a request for the plug-in to call this + * function. + *The reason is that this frees the virtual processor's stack -- which is + * still in use inside semantic library calls! + * + *This frees or recycles all the state owned by and comprising the animating + * virtual procr. It frees any state that was malloc'd by the VMS system + * itself, and asks the VMS system to dis-own any VMS__malloc'd locations. + *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd + * state, then that state gets freed (or sent to recycling) as a side-effect + * of dis-owning it. + */ +void +VMS__free_procr_locs( VirtProcr *animatingPr ) + { + //dis-own all locations owned by this processor, causing to be freed + // any locations that it is (was) sole owner of + //TODO: implement VMS__malloc system, including "give up ownership" + + VMS__remove_and_free_top_request( animatingPr ); + free( animatingPr->startOfStack ); + + //NOTE: animatingPr->semanticData should either have been allocated + // with VMS__malloc, or else freed in the request handler plug-in. + //NOTE: initialData was given to the processor, so should either have + // been alloc'd with VMS__malloc, or freed by the level above animPr. + //So, all that's left to free here is the VirtProcr struc itself + free( animatingPr ); + } + + +/*The semantic layer figures out when the work is done ( perhaps by a call + * in the application to "work all done", or perhaps all the virtual + * processors have dissipated.. a.s.o. ) + * + *The semantic layer is responsible for making sure all work has fully + * completed before using this to shutdown the VMS system. + * + *After the semantic layer has determined it wants to shut down, the + * next time the Master Loop calls the scheduler plug-in, the scheduler + * then calls this function and returns the virtual processor it gets back. + * + *When the shut-down processor runs, it first frees all locations malloc'd to + * the VMS system (that wasn't + * specified as return-locations). Then it creates one core-loop shut-down + * processor for each core loop and puts them all into the workQ. When a + * core loop animates a core loop shut-down processor, it causes exit-thread + * to run, and when all core loop threads have exited, then the "wait for + * work to finish" in the main thread is woken, and the function-call that + * started all the work returns. + * + *The function animated by this processor performs the shut-down work. + */ +VirtProcr * +VMS__create_the_shutdown_procr() + { + return VMS__create_procr( &shutdownFn, NULL ); + } + + +/*This is the function run by the special "shut-down" processor + * + *The _VMSMasterEnv is needed by this shut down function, so the "wait" + * function run in the main loop has to free it, and the thread-related + * locations (coreLoopThdParams a.s.o.). + *However, the semantic environment and all data malloc'd to VMS can be + * freed here. + * + *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the + * locations it needs -- they will be automatically freed by the standard + * "free all owned locations" + * + *Free any locations malloc'd to the VMS system (that weren't + * specified as return-locations). + *Then create one core-loop shut-down processor for each core loop and puts + * them all into the workQ. + */ +void +shutdownFn( void *dummy, VirtProcr *animatingPr ) { int coreIdx; VirtProcr *shutDownPr; - - //TODO: restore the "orig" stack pointer and frame ptr saved in VMS__start - //create a "special" virtual processor, one for each core loop that has - // the "loop end" point as its "next instr" point -- when the core loop - // jumps to animate the virt procr, the jump lands it at its own - // shut-down code. + CASQueueStruc *workQ = _VMSWorkQ; + + //free all the locations owned within the VMS system + //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt + + //make the core loop shut-down processors and put them into the workQ for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ ) { shutDownPr = VMS__create_procr( NULL, NULL ); shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt; + writeCASQ( shutDownPr, workQ ); } + + //This is an issue: the animating processor of this function may not + // get its request handled before all the cores have shutdown. + //TODO: after all the threads stop, clean out the MasterEnv, the + // SemanticEnv, and the workQ before returning. + VMS__send_dissipate_request( animatingPr ); + VMS__suspend_procr( animatingPr ); //will never come back from this }