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