Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.c @ 61:984f7d78bfdf
Merge See what happens -- merged test stuff into Nov 8 VMS version
| author | SeanHalle |
|---|---|
| date | Thu, 11 Nov 2010 06:19:51 -0800 |
| parents | 054006c26b92 26d53313a8f2 |
| children |
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 VMSQueueStruc **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(VMSQueueStruc *) );
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 ] = makeVMSQ();
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 _VMSMasterEnv->workStealingGates[ coreIdx ] = NULL;
125 }
126 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
127 _VMSMasterEnv->masterVPs = masterVPs;
128 _VMSMasterEnv->masterLock = UNLOCKED;
129 _VMSMasterEnv->allSchedSlots = allSchedSlots;
130 _VMSMasterEnv->workStealingLock = UNLOCKED;
132 //============================= MEASUREMENT STUFF ========================
133 #ifdef MEAS__TIME_MASTER
135 _VMSMasterEnv->stats->masterTimeHist = makeHistogram( 25, 500, 800 );
136 _VMSMasterEnv->stats->masterLockHist = makeHistogram( 25, 0, 100000 );
137 _VMSMasterEnv->stats->createHist = makeHistogram( 25, 0, 5000 );
138 #endif
139 //========================================================================
141 //Aug 19, 2010: no longer need to place initial masterVP into queue
142 // because coreLoop now controls -- animates its masterVP when no work
145 //============================= MEASUREMENT STUFF ========================
146 #ifdef STATS__TURN_ON_PROBES
147 _VMSMasterEnv->dynIntervalProbesInfo =
148 makePrivDynArrayOfSize( &(_VMSMasterEnv->intervalProbes), 200);
150 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, &VMS__free );
152 //put creation time directly into master env, for fast retrieval
153 struct timeval timeStamp;
154 gettimeofday( &(timeStamp), NULL);
155 _VMSMasterEnv->createPtInSecs =
156 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
157 #endif
158 //========================================================================
160 }
162 SchedSlot **
163 create_sched_slots()
164 { SchedSlot **schedSlots;
165 int i;
167 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
169 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
170 {
171 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
173 //Set state to mean "handling requests done, slot needs filling"
174 schedSlots[i]->workIsDone = FALSE;
175 schedSlots[i]->needsProcrAssigned = TRUE;
176 }
177 return schedSlots;
178 }
181 void
182 freeSchedSlots( SchedSlot **schedSlots )
183 { int i;
184 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
185 {
186 VMS__free( schedSlots[i] );
187 }
188 VMS__free( schedSlots );
189 }
192 void
193 create_the_coreLoop_OS_threads()
194 {
195 //========================================================================
196 // Create the Threads
197 int coreIdx, retCode;
199 //create the arrays used to measure TSC offsets between cores
200 pongNums = malloc( NUM_CORES * sizeof( int ) );
201 pingTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
202 pongTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
204 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
205 {
206 pongNums[ coreIdx ] = 0;
207 for( i = 0; i < NUM_TSC_ROUND_TRIPS; i++ )
208 {
209 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
210 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
211 }
212 }
214 //Need the threads to be created suspended, and wait for a signal
215 // before proceeding -- gives time after creating to initialize other
216 // stuff before the coreLoops set off.
217 _VMSMasterEnv->setupComplete = 0;
219 //Make the threads that animate the core loops
220 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
221 { coreLoopThdParams[coreIdx] = VMS__malloc( sizeof(ThdParams) );
222 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
224 retCode =
225 pthread_create( &(coreLoopThdHandles[coreIdx]),
226 thdAttrs,
227 &coreLoop,
228 (void *)(coreLoopThdParams[coreIdx]) );
229 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
230 }
231 }
233 /*Semantic layer calls this when it want the system to start running..
234 *
235 *This starts the core loops running then waits for them to exit.
236 */
237 void
238 VMS__start_the_work_then_wait_until_done()
239 { int coreIdx;
240 //Start the core loops running
241 //===========================================================================
242 TSCount startCount, endCount;
243 unsigned long long count = 0, freq = 0;
244 double runTime;
246 startCount = getTSC();
248 //tell the core loop threads that setup is complete
249 //get lock, to lock out any threads still starting up -- they'll see
250 // that setupComplete is true before entering while loop, and so never
251 // wait on the condition
252 pthread_mutex_lock( &suspendLock );
253 _VMSMasterEnv->setupComplete = 1;
254 pthread_mutex_unlock( &suspendLock );
255 pthread_cond_broadcast( &suspend_cond );
258 //wait for all to complete
259 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
260 {
261 pthread_join( coreLoopThdHandles[coreIdx], NULL );
262 }
264 //NOTE: do not clean up VMS env here -- semantic layer has to have
265 // a chance to clean up its environment first, then do a call to free
266 // the Master env and rest of VMS locations
269 endCount = getTSC();
270 count = endCount - startCount;
272 runTime = (double)count / (double)TSCOUNT_FREQ;
274 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
275 }
277 /*Only difference between version with an OS thread pinned to each core and
278 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
279 */
280 void
281 VMS__start_the_work_then_wait_until_done_Seq()
282 {
283 //Instead of un-suspending threads, just call the one and only
284 // core loop (sequential version), in the main thread.
285 coreLoop_Seq( NULL );
287 }
291 /*Create stack, then create __cdecl structure on it and put initialData and
292 * pointer to the new structure instance into the parameter positions on
293 * the stack
294 *Then put function pointer into nextInstrPt -- the stack is setup in std
295 * call structure, so jumping to function ptr is same as a GCC generated
296 * function call
297 *No need to save registers on old stack frame, because there's no old
298 * animator state to return to --
299 *
300 */
301 inline VirtProcr *
302 create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr,
303 void *initialData, char *stackLocs )
304 {
305 char *stackPtr;
307 //============================= MEASUREMENT STUFF ========================
308 #ifdef MEAS__TIME_MASTER
309 int32 startStamp;
310 saveLowTimeStampCountInto( startStamp );
311 #endif
312 //========================================================================
313 newPr->startOfStack = stackLocs;
314 newPr->procrID = _VMSMasterEnv->numProcrsCreated++;
315 newPr->nextInstrPt = fnPtr;
316 newPr->initialData = initialData;
317 newPr->requests = NULL;
318 newPr->schedSlot = NULL;
320 //fnPtr takes two params -- void *initData & void *animProcr
321 //alloc stack locations, make stackPtr be the highest addr minus room
322 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
323 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
324 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
326 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
327 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
328 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
329 newPr->stackPtr = stackPtr; //core loop will switch to this, then
330 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
332 //============================= MEASUREMENT STUFF ========================
333 #ifdef MEAS__TIME_MASTER
334 int32 endStamp;
335 saveLowTimeStampCountInto( endStamp );
336 addIntervalToHist( startStamp, endStamp,
337 _VMSMasterEnv->stats->createHist );
338 //============================= MEASUREMENT STUFF ========================
339 #ifdef STATS__TURN_ON_PROBES
340 struct timeval timeStamp;
341 gettimeofday( &(timeStamp), NULL);
342 newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
343 _VMSMasterEnv->createPtInSecs;
344 #endif
345 //========================================================================
347 return newPr;
348 }
350 inline VirtProcr *
351 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
352 { VirtProcr *newPr;
353 char *stackLocs;
355 newPr = VMS__malloc( sizeof(VirtProcr) );
356 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
357 if( stackLocs == 0 )
358 { perror("VMS__malloc stack"); exit(1); }
360 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
361 }
363 /* "ext" designates that it's for use outside the VMS system -- should only
364 * be called from main thread or other thread -- never from code animated by
365 * a VMS virtual processor.
366 */
367 inline VirtProcr *
368 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
369 { VirtProcr *newPr;
370 char *stackLocs;
372 newPr = malloc( sizeof(VirtProcr) );
373 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
374 if( stackLocs == 0 )
375 { perror("malloc stack"); exit(1); }
377 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
378 }
381 /*there is a label inside this function -- save the addr of this label in
382 * the callingPr struc, as the pick-up point from which to start the next
383 * work-unit for that procr. If turns out have to save registers, then
384 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
385 * "done with work-unit" label. The procr struc is in the request in the
386 * slave that animated the just-ended work-unit, so all the state is saved
387 * there, and will get passed along, inside the request handler, to the
388 * next work-unit for that procr.
389 */
390 void
391 VMS__suspend_procr( VirtProcr *animatingPr )
392 {
394 //The request to master will cause this suspended virt procr to get
395 // scheduled again at some future point -- to resume, core loop jumps
396 // to the resume point (below), which causes restore of saved regs and
397 // "return" from this call.
398 animatingPr->nextInstrPt = &&ResumePt;
400 //return ownership of the virt procr and sched slot to Master virt pr
401 animatingPr->schedSlot->workIsDone = TRUE;
403 //=========================== Measurement stuff ========================
404 #ifdef MEAS__TIME_STAMP_SUSP
405 //record time stamp: compare to time-stamp recorded below
406 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
407 #endif
408 //=======================================================================
411 SwitchToCoreLoop( animatingPr )
413 //=======================================================================
414 ResumePt:
415 #ifdef MEAS__TIME_STAMP_SUSP
416 //NOTE: only take low part of count -- do sanity check when take diff
417 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
418 #endif
420 return;
421 }
425 /*For this implementation of VMS, it may not make much sense to have the
426 * system of requests for creating a new processor done this way.. but over
427 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
428 * distributed-memory, and so on, this gives VMS implementation a chance to
429 * do stuff before suspend, in the AppVP, and in the Master before the plugin
430 * is called, as well as in the lang-lib before this is called, and in the
431 * plugin. So, this gives both VMS and language implementations a chance to
432 * intercept at various points and do order-dependent stuff.
433 *Having a standard VMSNewPrReqData struc allows the language to create and
434 * free the struc, while VMS knows how to get the newPr if it wants it, and
435 * it lets the lang have lang-specific data related to creation transported
436 * to the plugin.
437 */
438 void
439 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
440 { VMSReqst req;
442 req.reqType = createReq;
443 req.semReqData = semReqData;
444 req.nextReqst = reqstingPr->requests;
445 reqstingPr->requests = &req;
447 VMS__suspend_procr( reqstingPr );
448 }
451 /*
452 *This adds a request to dissipate, then suspends the processor so that the
453 * request handler will receive the request. The request handler is what
454 * does the work of freeing memory and removing the processor from the
455 * semantic environment's data structures.
456 *The request handler also is what figures out when to shutdown the VMS
457 * system -- which causes all the core loop threads to die, and returns from
458 * the call that started up VMS to perform the work.
459 *
460 *This form is a bit misleading to understand if one is trying to figure out
461 * how VMS works -- it looks like a normal function call, but inside it
462 * sends a request to the request handler and suspends the processor, which
463 * jumps out of the VMS__dissipate_procr function, and out of all nestings
464 * above it, transferring the work of dissipating to the request handler,
465 * which then does the actual work -- causing the processor that animated
466 * the call of this function to disappear and the "hanging" state of this
467 * function to just poof into thin air -- the virtual processor's trace
468 * never returns from this call, but instead the virtual processor's trace
469 * gets suspended in this call and all the virt processor's state disap-
470 * pears -- making that suspend the last thing in the virt procr's trace.
471 */
472 void
473 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
474 { VMSReqst req;
476 req.reqType = dissipate;
477 req.nextReqst = procrToDissipate->requests;
478 procrToDissipate->requests = &req;
480 VMS__suspend_procr( procrToDissipate );
481 }
484 /* "ext" designates that it's for use outside the VMS system -- should only
485 * be called from main thread or other thread -- never from code animated by
486 * a VMS virtual processor.
487 *
488 *Use this version to dissipate VPs created outside the VMS system.
489 */
490 void
491 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
492 {
493 //NOTE: initialData was given to the processor, so should either have
494 // been alloc'd with VMS__malloc, or freed by the level above animPr.
495 //So, all that's left to free here is the stack and the VirtProcr struc
496 // itself
497 //Note, should not stack-allocate initial data -- no guarantee, in
498 // general that creating processor will outlive ones it creates.
499 free( procrToDissipate->startOfStack );
500 free( procrToDissipate );
501 }
505 /*This call's name indicates that request is malloc'd -- so req handler
506 * has to free any extra requests tacked on before a send, using this.
507 *
508 * This inserts the semantic-layer's request data into standard VMS carrier
509 * request data-struct that is mallocd. The sem request doesn't need to
510 * be malloc'd if this is called inside the same call chain before the
511 * send of the last request is called.
512 *
513 *The request handler has to call VMS__free_VMSReq for any of these
514 */
515 inline void
516 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
517 VirtProcr *callingPr )
518 { VMSReqst *req;
520 req = VMS__malloc( sizeof(VMSReqst) );
521 req->reqType = semantic;
522 req->semReqData = semReqData;
523 req->nextReqst = callingPr->requests;
524 callingPr->requests = req;
525 }
527 /*This inserts the semantic-layer's request data into standard VMS carrier
528 * request data-struct is allocated on stack of this call & ptr to it sent
529 * to plugin
530 *Then it does suspend, to cause request to be sent.
531 */
532 inline void
533 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
534 { VMSReqst req;
536 req.reqType = semantic;
537 req.semReqData = semReqData;
538 req.nextReqst = callingPr->requests;
539 callingPr->requests = &req;
541 VMS__suspend_procr( callingPr );
542 }
545 inline void
546 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
547 { VMSReqst req;
549 req.reqType = VMSSemantic;
550 req.semReqData = semReqData;
551 req.nextReqst = callingPr->requests; //gab any other preceeding
552 callingPr->requests = &req;
554 VMS__suspend_procr( callingPr );
555 }
558 /*
559 */
560 VMSReqst *
561 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
562 { VMSReqst *req;
564 req = procrWithReq->requests;
565 if( req == NULL ) return NULL;
567 procrWithReq->requests = procrWithReq->requests->nextReqst;
568 return req;
569 }
572 inline void *
573 VMS__take_sem_reqst_from( VMSReqst *req )
574 {
575 return req->semReqData;
576 }
580 /* This is for OS requests and VMS infrastructure requests, such as to create
581 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
582 * language -- but it's also a semantic thing that's triggered from and used
583 * in the application.. so it crosses abstractions.. so, need some special
584 * pattern here for handling such requests.
585 * Doing this just like it were a second language sharing VMS-core.
586 *
587 * This is called from the language's request handler when it sees a request
588 * of type VMSSemReq
589 *
590 * TODO: Later change this, to give probes their own separate plugin & have
591 * VMS-core steer the request to appropriate plugin
592 * Do the same for OS calls -- look later at it..
593 */
594 void inline
595 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
596 ResumePrFnPtr resumePrFnPtr )
597 { VMSSemReq *semReq;
598 IntervalProbe *newProbe;
599 int32 nameLen;
601 semReq = req->semReqData;
603 newProbe = VMS__malloc( sizeof(IntervalProbe) );
604 nameLen = strlen( semReq->nameStr );
605 newProbe->nameStr = VMS__malloc( nameLen );
606 memcpy( newProbe->nameStr, semReq->nameStr, nameLen );
607 newProbe->hist = NULL;
608 newProbe->schedChoiceWasRecorded = FALSE;
610 //This runs in masterVP, so no race-condition worries
611 newProbe->probeID =
612 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
614 requestingPr->dataRetFromReq = newProbe;
616 (*resumePrFnPtr)( requestingPr, semEnv );
617 }
621 /*This must be called by the request handler plugin -- it cannot be called
622 * from the semantic library "dissipate processor" function -- instead, the
623 * semantic layer has to generate a request, and the plug-in calls this
624 * function.
625 *The reason is that this frees the virtual processor's stack -- which is
626 * still in use inside semantic library calls!
627 *
628 *This frees or recycles all the state owned by and comprising the VMS
629 * portion of the animating virtual procr. The request handler must first
630 * free any semantic data created for the processor that didn't use the
631 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
632 * system to disown any state that did use VMS_malloc, and then frees the
633 * statck and the processor-struct itself.
634 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
635 * state, then that state gets freed (or sent to recycling) as a side-effect
636 * of dis-owning it.
637 */
638 void
639 VMS__dissipate_procr( VirtProcr *animatingPr )
640 {
641 //dis-own all locations owned by this processor, causing to be freed
642 // any locations that it is (was) sole owner of
643 //TODO: implement VMS__malloc system, including "give up ownership"
646 //NOTE: initialData was given to the processor, so should either have
647 // been alloc'd with VMS__malloc, or freed by the level above animPr.
648 //So, all that's left to free here is the stack and the VirtProcr struc
649 // itself
650 //Note, should not stack-allocate initial data -- no guarantee, in
651 // general that creating processor will outlive ones it creates.
652 VMS__free( animatingPr->startOfStack );
653 VMS__free( animatingPr );
654 }
657 //TODO: look at architecting cleanest separation between request handler
658 // and master loop, for dissipate, create, shutdown, and other non-semantic
659 // requests. Issue is chain: one removes requests from AppVP, one dispatches
660 // on type of request, and one handles each type.. but some types require
661 // action from both request handler and master loop -- maybe just give the
662 // request handler calls like: VMS__handle_X_request_type
665 /*This is called by the semantic layer's request handler when it decides its
666 * time to shut down the VMS system. Calling this causes the core loop OS
667 * threads to exit, which unblocks the entry-point function that started up
668 * VMS, and allows it to grab the result and return to the original single-
669 * threaded application.
670 *
671 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
672 * and-wait function has to free a bunch of stuff after it detects the
673 * threads have all died: the masterEnv, the thread-related locations,
674 * masterVP any AppVPs that might still be allocated and sitting in the
675 * semantic environment, or have been orphaned in the _VMSWorkQ.
676 *
677 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
678 * locations it needs, and give ownership to masterVP. Then, they will be
679 * automatically freed.
680 *
681 *In here,create one core-loop shut-down processor for each core loop and put
682 * them all directly into the readyToAnimateQ.
683 *Note, this function can ONLY be called after the semantic environment no
684 * longer cares if AppVPs get animated after the point this is called. In
685 * other words, this can be used as an abort, or else it should only be
686 * called when all AppVPs have finished dissipate requests -- only at that
687 * point is it sure that all results have completed.
688 */
689 void
690 VMS__shutdown()
691 { int coreIdx;
692 VirtProcr *shutDownPr;
694 //create the shutdown processors, one for each core loop -- put them
695 // directly into the Q -- each core will die when gets one
696 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
697 { //Note, this is running in the master
698 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
699 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
700 }
702 }
705 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
706 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
707 *This function has the sole purpose of setting the stack and framePtr
708 * to the coreLoop's stack and framePtr.. it does that then jumps to the
709 * core loop's shutdown point -- might be able to just call Pthread_exit
710 * from here, but am going back to the pthread's stack and setting everything
711 * up just as if it never jumped out, before calling pthread_exit.
712 *The end-point of core loop will free the stack and so forth of the
713 * processor that animates this function, (this fn is transfering the
714 * animator of the AppVP that is in turn animating this function over
715 * to core loop function -- note that this slices out a level of virtual
716 * processors).
717 */
718 void
719 endOSThreadFn( void *initData, VirtProcr *animatingPr )
720 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
722 jmpPt = _VMSMasterEnv->coreLoopEndPt;
723 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
724 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
727 asm volatile("movl %0, %%eax; \
728 movl %1, %%esp; \
729 movl %2, %%ebp; \
730 jmp %%eax " \
731 /* outputs */ : \
732 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
733 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
734 );
735 }
738 /*This is called from the startup & shutdown
739 */
740 void
741 VMS__cleanup_at_end_of_shutdown()
742 {
743 VMSQueueStruc **readyToAnimateQs;
744 int coreIdx;
745 VirtProcr **masterVPs;
746 SchedSlot ***allSchedSlots; //ptr to array of ptrs
748 //All the environment data has been allocated with VMS__malloc, so just
749 // free its internal big-chunk and all inside it disappear.
750 /*
751 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
752 masterVPs = _VMSMasterEnv->masterVPs;
753 allSchedSlots = _VMSMasterEnv->allSchedSlots;
755 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
756 {
757 freeVMSQ( readyToAnimateQs[ coreIdx ] );
758 //master VPs were created external to VMS, so use external free
759 VMS__dissipate_procr( masterVPs[ coreIdx ] );
761 freeSchedSlots( allSchedSlots[ coreIdx ] );
762 }
764 VMS__free( _VMSMasterEnv->readyToAnimateQs );
765 VMS__free( _VMSMasterEnv->masterVPs );
766 VMS__free( _VMSMasterEnv->allSchedSlots );
768 //============================= MEASUREMENT STUFF ========================
769 #ifdef STATS__TURN_ON_PROBES
770 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
771 #endif
772 //========================================================================
773 */
774 //These are the only two that use system free
775 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
776 free( (void *)_VMSMasterEnv );
777 }
780 //================================
783 /*Later, improve this -- for now, just exits the application after printing
784 * the error message.
785 */
786 void
787 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
788 {
789 printf(msgStr);
790 fflush(stdin);
791 exit(1);
792 }
