view VMS.c @ 54:f8508572f3de

Added boolean guarded debug messages and VMS__throw_exception
author Me
date Tue, 02 Nov 2010 16:43:01 -0700
parents 42dd44df1bb0
children 3bac84e4e56e 85b731b290f8
line source
1 /*
2 * Copyright 2010 OpenSourceStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <malloc.h>
11 #include <sys/time.h>
13 #include "VMS.h"
14 #include "Queue_impl/BlockingQueue.h"
15 #include "Histogram/Histogram.h"
18 #define thdAttrs NULL
20 //===========================================================================
21 void
22 shutdownFn( void *dummy, VirtProcr *dummy2 );
24 SchedSlot **
25 create_sched_slots();
27 void
28 create_masterEnv();
30 void
31 create_the_coreLoop_OS_threads();
33 MallocProlog *
34 create_free_list();
36 void
37 endOSThreadFn( void *initData, VirtProcr *animatingPr );
39 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
40 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
42 //===========================================================================
44 /*Setup has two phases:
45 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
46 * the master virt procr into the work-queue, ready for first "call"
47 * 2) Semantic layer then does its own init, which creates the seed virt
48 * procr inside the semantic layer, ready to schedule it when
49 * asked by the first run of the masterLoop.
50 *
51 *This part is bit weird because VMS really wants to be "always there", and
52 * have applications attach and detach.. for now, this VMS is part of
53 * the app, so the VMS system starts up as part of running the app.
54 *
55 *The semantic layer is isolated from the VMS internals by making the
56 * semantic layer do setup to a state that it's ready with its
57 * initial virt procrs, ready to schedule them to slots when the masterLoop
58 * asks. Without this pattern, the semantic layer's setup would
59 * have to modify slots directly to assign the initial virt-procrs, and put
60 * them into the readyToAnimateQ itself, breaking the isolation completely.
61 *
62 *
63 *The semantic layer creates the initial virt procr(s), and adds its
64 * own environment to masterEnv, and fills in the pointers to
65 * the requestHandler and slaveScheduler plug-in functions
66 */
68 /*This allocates VMS data structures, populates the master VMSProc,
69 * and master environment, and returns the master environment to the semantic
70 * layer.
71 */
72 void
73 VMS__init()
74 {
75 create_masterEnv();
76 create_the_coreLoop_OS_threads();
77 }
79 /*To initialize the sequential version, just don't create the threads
80 */
81 void
82 VMS__init_Seq()
83 {
84 create_masterEnv();
85 }
87 void
88 create_masterEnv()
89 { MasterEnv *masterEnv;
90 SRSWQueueStruc **readyToAnimateQs;
91 int coreIdx;
92 VirtProcr **masterVPs;
93 SchedSlot ***allSchedSlots; //ptr to array of ptrs
96 //Make the master env, which holds everything else
97 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
99 //Very first thing put into the master env is the free-list, seeded
100 // with a massive initial chunk of memory.
101 //After this, all other mallocs are VMS__malloc.
102 _VMSMasterEnv->freeListHead = VMS_ext__create_free_list();
104 //===================== Only VMS__malloc after this ====================
105 masterEnv = _VMSMasterEnv;
107 //Make a readyToAnimateQ for each core loop
108 readyToAnimateQs = VMS__malloc( NUM_CORES * sizeof(SRSWQueueStruc *) );
109 masterVPs = VMS__malloc( NUM_CORES * sizeof(VirtProcr *) );
111 //One array for each core, 3 in array, core's masterVP scheds all
112 allSchedSlots = VMS__malloc( NUM_CORES * sizeof(SchedSlot *) );
114 _VMSMasterEnv->numProcrsCreated = 0; //used by create procr
115 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
116 {
117 readyToAnimateQs[ coreIdx ] = makeSRSWQ();
119 //Q: should give masterVP core-specific info as its init data?
120 masterVPs[ coreIdx ] = VMS__create_procr( &masterLoop, masterEnv );
121 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
122 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
123 _VMSMasterEnv->numMasterInARow[ coreIdx ] = 0;
124 }
125 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
126 _VMSMasterEnv->masterVPs = masterVPs;
127 _VMSMasterEnv->masterLock = UNLOCKED;
128 _VMSMasterEnv->allSchedSlots = allSchedSlots;
131 //Aug 19, 2010: no longer need to place initial masterVP into queue
132 // because coreLoop now controls -- animates its masterVP when no work
135 //============================= MEASUREMENT STUFF ========================
136 #ifdef STATS__TURN_ON_PROBES
137 _VMSMasterEnv->dynIntervalProbesInfo =
138 makePrivDynArrayOfSize( &(_VMSMasterEnv->intervalProbes), 200);
140 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, &VMS__free );
142 //put creation time directly into master env, for fast retrieval
143 struct timeval timeStamp;
144 gettimeofday( &(timeStamp), NULL);
145 _VMSMasterEnv->createPtInSecs =
146 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
147 #endif
148 //========================================================================
150 }
152 SchedSlot **
153 create_sched_slots()
154 { SchedSlot **schedSlots;
155 int i;
157 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
159 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
160 {
161 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
163 //Set state to mean "handling requests done, slot needs filling"
164 schedSlots[i]->workIsDone = FALSE;
165 schedSlots[i]->needsProcrAssigned = TRUE;
166 }
167 return schedSlots;
168 }
171 void
172 freeSchedSlots( SchedSlot **schedSlots )
173 { int i;
174 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
175 {
176 VMS__free( schedSlots[i] );
177 }
178 VMS__free( schedSlots );
179 }
182 void
183 create_the_coreLoop_OS_threads()
184 {
185 //========================================================================
186 // Create the Threads
187 int coreIdx, retCode;
189 //Need the threads to be created suspended, and wait for a signal
190 // before proceeding -- gives time after creating to initialize other
191 // stuff before the coreLoops set off.
192 _VMSMasterEnv->setupComplete = 0;
194 //Make the threads that animate the core loops
195 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
196 { coreLoopThdParams[coreIdx] = VMS__malloc( sizeof(ThdParams) );
197 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
199 retCode =
200 pthread_create( &(coreLoopThdHandles[coreIdx]),
201 thdAttrs,
202 &coreLoop,
203 (void *)(coreLoopThdParams[coreIdx]) );
204 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
205 }
206 }
208 /*Semantic layer calls this when it want the system to start running..
209 *
210 *This starts the core loops running then waits for them to exit.
211 */
212 void
213 VMS__start_the_work_then_wait_until_done()
214 { int coreIdx;
215 //Start the core loops running
217 //tell the core loop threads that setup is complete
218 //get lock, to lock out any threads still starting up -- they'll see
219 // that setupComplete is true before entering while loop, and so never
220 // wait on the condition
221 pthread_mutex_lock( &suspendLock );
222 _VMSMasterEnv->setupComplete = 1;
223 pthread_mutex_unlock( &suspendLock );
224 pthread_cond_broadcast( &suspend_cond );
227 //wait for all to complete
228 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
229 {
230 pthread_join( coreLoopThdHandles[coreIdx], NULL );
231 }
233 //NOTE: do not clean up VMS env here -- semantic layer has to have
234 // a chance to clean up its environment first, then do a call to free
235 // the Master env and rest of VMS locations
236 }
238 /*Only difference between version with an OS thread pinned to each core and
239 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
240 */
241 void
242 VMS__start_the_work_then_wait_until_done_Seq()
243 {
244 //Instead of un-suspending threads, just call the one and only
245 // core loop (sequential version), in the main thread.
246 coreLoop_Seq( NULL );
248 }
252 /*Create stack, then create __cdecl structure on it and put initialData and
253 * pointer to the new structure instance into the parameter positions on
254 * the stack
255 *Then put function pointer into nextInstrPt -- the stack is setup in std
256 * call structure, so jumping to function ptr is same as a GCC generated
257 * function call
258 *No need to save registers on old stack frame, because there's no old
259 * animator state to return to --
260 *
261 */
262 inline VirtProcr *
263 create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr,
264 void *initialData, char *stackLocs )
265 {
266 char *stackPtr;
268 newPr->startOfStack = stackLocs;
269 newPr->procrID = _VMSMasterEnv->numProcrsCreated++;
270 newPr->nextInstrPt = fnPtr;
271 newPr->initialData = initialData;
272 newPr->requests = NULL;
273 newPr->schedSlot = NULL;
275 //fnPtr takes two params -- void *initData & void *animProcr
276 //alloc stack locations, make stackPtr be the highest addr minus room
277 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
278 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
279 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
281 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
282 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
283 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
284 newPr->stackPtr = stackPtr; //core loop will switch to this, then
285 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
287 //============================= MEASUREMENT STUFF ========================
288 #ifdef STATS__TURN_ON_PROBES
289 struct timeval timeStamp;
290 gettimeofday( &(timeStamp), NULL);
291 newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
292 _VMSMasterEnv->createPtInSecs;
293 #endif
294 //========================================================================
296 return newPr;
297 }
299 inline VirtProcr *
300 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
301 { VirtProcr *newPr;
302 char *stackLocs;
304 newPr = VMS__malloc( sizeof(VirtProcr) );
305 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
306 if( stackLocs == 0 )
307 { perror("VMS__malloc stack"); exit(1); }
309 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
310 }
312 /* "ext" designates that it's for use outside the VMS system -- should only
313 * be called from main thread or other thread -- never from code animated by
314 * a VMS virtual processor.
315 */
316 inline VirtProcr *
317 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
318 { VirtProcr *newPr;
319 char *stackLocs;
321 newPr = malloc( sizeof(VirtProcr) );
322 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
323 if( stackLocs == 0 )
324 { perror("malloc stack"); exit(1); }
326 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
327 }
330 /*there is a label inside this function -- save the addr of this label in
331 * the callingPr struc, as the pick-up point from which to start the next
332 * work-unit for that procr. If turns out have to save registers, then
333 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
334 * "done with work-unit" label. The procr struc is in the request in the
335 * slave that animated the just-ended work-unit, so all the state is saved
336 * there, and will get passed along, inside the request handler, to the
337 * next work-unit for that procr.
338 */
339 void
340 VMS__suspend_procr( VirtProcr *animatingPr )
341 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
342 void *coreLoopFramePtr;
344 //The request to master will cause this suspended virt procr to get
345 // scheduled again at some future point -- to resume, core loop jumps
346 // to the resume point (below), which causes restore of saved regs and
347 // "return" from this call.
348 animatingPr->nextInstrPt = &&ResumePt;
350 //return ownership of the virt procr and sched slot to Master virt pr
351 animatingPr->schedSlot->workIsDone = TRUE;
353 stackPtrAddr = &(animatingPr->stackPtr);
354 framePtrAddr = &(animatingPr->framePtr);
356 jmpPt = _VMSMasterEnv->coreLoopStartPt;
357 coreLoopFramePtr = animatingPr->coreLoopFramePtr;//need this only
358 coreLoopStackPtr = animatingPr->coreLoopStackPtr;//safety
360 //Save the virt procr's stack and frame ptrs,
361 asm volatile("movl %0, %%eax; \
362 movl %%esp, (%%eax); \
363 movl %1, %%eax; \
364 movl %%ebp, (%%eax) "\
365 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
366 /* inputs */ : \
367 /* clobber */ : "%eax" \
368 );
370 //=========================== Measurement stuff ========================
371 #ifdef MEAS__TIME_STAMP_SUSP
372 //record time stamp: compare to time-stamp recorded below
373 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
374 #endif
375 //=======================================================================
377 //restore coreloop's frame ptr, then jump back to "start" of core loop
378 //Note, GCC compiles to assembly that saves esp and ebp in the stack
379 // frame -- so have to explicitly do assembly that saves to memory
380 asm volatile("movl %0, %%eax; \
381 movl %1, %%esp; \
382 movl %2, %%ebp; \
383 jmp %%eax " \
384 /* outputs */ : \
385 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
386 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
387 ); //list everything as clobbered to force GCC to save all
388 // live vars that are in regs on stack before this
389 // assembly, so that stack pointer is correct, before jmp
391 ResumePt:
392 #ifdef MEAS__TIME_STAMP_SUSP
393 //NOTE: only take low part of count -- do sanity check when take diff
394 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
395 #endif
397 return;
398 }
402 /*For this implementation of VMS, it may not make much sense to have the
403 * system of requests for creating a new processor done this way.. but over
404 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
405 * distributed-memory, and so on, this gives VMS implementation a chance to
406 * do stuff before suspend, in the AppVP, and in the Master before the plugin
407 * is called, as well as in the lang-lib before this is called, and in the
408 * plugin. So, this gives both VMS and language implementations a chance to
409 * intercept at various points and do order-dependent stuff.
410 *Having a standard VMSNewPrReqData struc allows the language to create and
411 * free the struc, while VMS knows how to get the newPr if it wants it, and
412 * it lets the lang have lang-specific data related to creation transported
413 * to the plugin.
414 */
415 void
416 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
417 { VMSReqst req;
419 req.reqType = createReq;
420 req.semReqData = semReqData;
421 req.nextReqst = reqstingPr->requests;
422 reqstingPr->requests = &req;
424 VMS__suspend_procr( reqstingPr );
425 }
428 /*
429 *This adds a request to dissipate, then suspends the processor so that the
430 * request handler will receive the request. The request handler is what
431 * does the work of freeing memory and removing the processor from the
432 * semantic environment's data structures.
433 *The request handler also is what figures out when to shutdown the VMS
434 * system -- which causes all the core loop threads to die, and returns from
435 * the call that started up VMS to perform the work.
436 *
437 *This form is a bit misleading to understand if one is trying to figure out
438 * how VMS works -- it looks like a normal function call, but inside it
439 * sends a request to the request handler and suspends the processor, which
440 * jumps out of the VMS__dissipate_procr function, and out of all nestings
441 * above it, transferring the work of dissipating to the request handler,
442 * which then does the actual work -- causing the processor that animated
443 * the call of this function to disappear and the "hanging" state of this
444 * function to just poof into thin air -- the virtual processor's trace
445 * never returns from this call, but instead the virtual processor's trace
446 * gets suspended in this call and all the virt processor's state disap-
447 * pears -- making that suspend the last thing in the virt procr's trace.
448 */
449 void
450 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
451 { VMSReqst req;
453 req.reqType = dissipate;
454 req.nextReqst = procrToDissipate->requests;
455 procrToDissipate->requests = &req;
457 VMS__suspend_procr( procrToDissipate );
458 }
461 /* "ext" designates that it's for use outside the VMS system -- should only
462 * be called from main thread or other thread -- never from code animated by
463 * a VMS virtual processor.
464 *
465 *Use this version to dissipate VPs created outside the VMS system.
466 */
467 void
468 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
469 {
470 //NOTE: initialData was given to the processor, so should either have
471 // been alloc'd with VMS__malloc, or freed by the level above animPr.
472 //So, all that's left to free here is the stack and the VirtProcr struc
473 // itself
474 //Note, should not stack-allocate initial data -- no guarantee, in
475 // general that creating processor will outlive ones it creates.
476 free( procrToDissipate->startOfStack );
477 free( procrToDissipate );
478 }
482 /*This call's name indicates that request is malloc'd -- so req handler
483 * has to free any extra requests tacked on before a send, using this.
484 *
485 * This inserts the semantic-layer's request data into standard VMS carrier
486 * request data-struct that is mallocd. The sem request doesn't need to
487 * be malloc'd if this is called inside the same call chain before the
488 * send of the last request is called.
489 *
490 *The request handler has to call VMS__free_VMSReq for any of these
491 */
492 inline void
493 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
494 VirtProcr *callingPr )
495 { VMSReqst *req;
497 req = VMS__malloc( sizeof(VMSReqst) );
498 req->reqType = semantic;
499 req->semReqData = semReqData;
500 req->nextReqst = callingPr->requests;
501 callingPr->requests = req;
502 }
504 /*This inserts the semantic-layer's request data into standard VMS carrier
505 * request data-struct is allocated on stack of this call & ptr to it sent
506 * to plugin
507 *Then it does suspend, to cause request to be sent.
508 */
509 inline void
510 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
511 { VMSReqst req;
513 req.reqType = semantic;
514 req.semReqData = semReqData;
515 req.nextReqst = callingPr->requests;
516 callingPr->requests = &req;
518 VMS__suspend_procr( callingPr );
519 }
522 inline void
523 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
524 { VMSReqst req;
526 req.reqType = VMSSemantic;
527 req.semReqData = semReqData;
528 req.nextReqst = callingPr->requests; //gab any other preceeding
529 callingPr->requests = &req;
531 VMS__suspend_procr( callingPr );
532 }
535 /*
536 */
537 VMSReqst *
538 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
539 { VMSReqst *req;
541 req = procrWithReq->requests;
542 if( req == NULL ) return NULL;
544 procrWithReq->requests = procrWithReq->requests->nextReqst;
545 return req;
546 }
549 inline void *
550 VMS__take_sem_reqst_from( VMSReqst *req )
551 {
552 return req->semReqData;
553 }
557 /* This is for OS requests and VMS infrastructure requests, such as to create
558 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
559 * language -- but it's also a semantic thing that's triggered from and used
560 * in the application.. so it crosses abstractions.. so, need some special
561 * pattern here for handling such requests.
562 * Doing this just like it were a second language sharing VMS-core.
563 *
564 * This is called from the language's request handler when it sees a request
565 * of type VMSSemReq
566 *
567 * TODO: Later change this, to give probes their own separate plugin & have
568 * VMS-core steer the request to appropriate plugin
569 * Do the same for OS calls -- look later at it..
570 */
571 void inline
572 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
573 ResumePrFnPtr resumePrFnPtr )
574 { VMSSemReq *semReq;
575 IntervalProbe *newProbe;
576 int32 nameLen;
578 semReq = req->semReqData;
580 newProbe = VMS__malloc( sizeof(IntervalProbe) );
581 nameLen = strlen( semReq->nameStr );
582 newProbe->nameStr = VMS__malloc( nameLen );
583 memcpy( newProbe->nameStr, semReq->nameStr, nameLen );
584 newProbe->hist = NULL;
585 newProbe->schedChoiceWasRecorded = FALSE;
587 //This runs in masterVP, so no race-condition worries
588 newProbe->probeID =
589 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
591 requestingPr->dataRetFromReq = newProbe;
593 (*resumePrFnPtr)( requestingPr, semEnv );
594 }
598 /*This must be called by the request handler plugin -- it cannot be called
599 * from the semantic library "dissipate processor" function -- instead, the
600 * semantic layer has to generate a request, and the plug-in calls this
601 * function.
602 *The reason is that this frees the virtual processor's stack -- which is
603 * still in use inside semantic library calls!
604 *
605 *This frees or recycles all the state owned by and comprising the VMS
606 * portion of the animating virtual procr. The request handler must first
607 * free any semantic data created for the processor that didn't use the
608 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
609 * system to disown any state that did use VMS_malloc, and then frees the
610 * statck and the processor-struct itself.
611 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
612 * state, then that state gets freed (or sent to recycling) as a side-effect
613 * of dis-owning it.
614 */
615 void
616 VMS__dissipate_procr( VirtProcr *animatingPr )
617 {
618 //dis-own all locations owned by this processor, causing to be freed
619 // any locations that it is (was) sole owner of
620 //TODO: implement VMS__malloc system, including "give up ownership"
623 //NOTE: initialData was given to the processor, so should either have
624 // been alloc'd with VMS__malloc, or freed by the level above animPr.
625 //So, all that's left to free here is the stack and the VirtProcr struc
626 // itself
627 //Note, should not stack-allocate initial data -- no guarantee, in
628 // general that creating processor will outlive ones it creates.
629 VMS__free( animatingPr->startOfStack );
630 VMS__free( animatingPr );
631 }
634 //TODO: look at architecting cleanest separation between request handler
635 // and master loop, for dissipate, create, shutdown, and other non-semantic
636 // requests. Issue is chain: one removes requests from AppVP, one dispatches
637 // on type of request, and one handles each type.. but some types require
638 // action from both request handler and master loop -- maybe just give the
639 // request handler calls like: VMS__handle_X_request_type
642 /*This is called by the semantic layer's request handler when it decides its
643 * time to shut down the VMS system. Calling this causes the core loop OS
644 * threads to exit, which unblocks the entry-point function that started up
645 * VMS, and allows it to grab the result and return to the original single-
646 * threaded application.
647 *
648 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
649 * and-wait function has to free a bunch of stuff after it detects the
650 * threads have all died: the masterEnv, the thread-related locations,
651 * masterVP any AppVPs that might still be allocated and sitting in the
652 * semantic environment, or have been orphaned in the _VMSWorkQ.
653 *
654 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
655 * locations it needs, and give ownership to masterVP. Then, they will be
656 * automatically freed.
657 *
658 *In here,create one core-loop shut-down processor for each core loop and put
659 * them all directly into the readyToAnimateQ.
660 *Note, this function can ONLY be called after the semantic environment no
661 * longer cares if AppVPs get animated after the point this is called. In
662 * other words, this can be used as an abort, or else it should only be
663 * called when all AppVPs have finished dissipate requests -- only at that
664 * point is it sure that all results have completed.
665 */
666 void
667 VMS__shutdown()
668 { int coreIdx;
669 VirtProcr *shutDownPr;
671 //create the shutdown processors, one for each core loop -- put them
672 // directly into the Q -- each core will die when gets one
673 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
674 { //Note, this is running in the master
675 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
676 writeSRSWQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
677 }
679 }
682 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
683 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
684 *This function has the sole purpose of setting the stack and framePtr
685 * to the coreLoop's stack and framePtr.. it does that then jumps to the
686 * core loop's shutdown point -- might be able to just call Pthread_exit
687 * from here, but am going back to the pthread's stack and setting everything
688 * up just as if it never jumped out, before calling pthread_exit.
689 *The end-point of core loop will free the stack and so forth of the
690 * processor that animates this function, (this fn is transfering the
691 * animator of the AppVP that is in turn animating this function over
692 * to core loop function -- note that this slices out a level of virtual
693 * processors).
694 */
695 void
696 endOSThreadFn( void *initData, VirtProcr *animatingPr )
697 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
699 jmpPt = _VMSMasterEnv->coreLoopEndPt;
700 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
701 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
704 asm volatile("movl %0, %%eax; \
705 movl %1, %%esp; \
706 movl %2, %%ebp; \
707 jmp %%eax " \
708 /* outputs */ : \
709 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
710 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
711 );
712 }
715 /*This is called from the startup & shutdown
716 */
717 void
718 VMS__cleanup_at_end_of_shutdown()
719 {
720 SRSWQueueStruc **readyToAnimateQs;
721 int coreIdx;
722 VirtProcr **masterVPs;
723 SchedSlot ***allSchedSlots; //ptr to array of ptrs
725 //All the environment data has been allocated with VMS__malloc, so just
726 // free its internal big-chunk and all inside it disappear.
727 /*
728 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
729 masterVPs = _VMSMasterEnv->masterVPs;
730 allSchedSlots = _VMSMasterEnv->allSchedSlots;
732 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
733 {
734 freeSRSWQ( readyToAnimateQs[ coreIdx ] );
735 //master VPs were created external to VMS, so use external free
736 VMS__dissipate_procr( masterVPs[ coreIdx ] );
738 freeSchedSlots( allSchedSlots[ coreIdx ] );
739 }
741 VMS__free( _VMSMasterEnv->readyToAnimateQs );
742 VMS__free( _VMSMasterEnv->masterVPs );
743 VMS__free( _VMSMasterEnv->allSchedSlots );
745 //============================= MEASUREMENT STUFF ========================
746 #ifdef STATS__TURN_ON_PROBES
747 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
748 #endif
749 //========================================================================
750 */
751 //These are the only two that use system free
752 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
753 free( (void *)_VMSMasterEnv );
754 }
757 //================================
760 /*Later, improve this -- for now, just exits the application after printing
761 * the error message.
762 */
763 void
764 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
765 {
766 printf(msgStr);
767 fflush(stdin);
768 exit(1);
769 }