view VMS.c @ 56:420a09d3f32a

changed sem-lib interface for dissipating to send_dissipate_req
author Me
date Thu, 04 Nov 2010 17:57:39 -0700
parents f59cfa31a579
children 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();
37 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
38 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
40 //===========================================================================
42 /*Setup has two phases:
43 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
44 * the master virt procr into the work-queue, ready for first "call"
45 * 2) Semantic layer then does its own init, which creates the seed virt
46 * procr inside the semantic layer, ready to schedule it when
47 * asked by the first run of the masterLoop.
48 *
49 *This part is bit weird because VMS really wants to be "always there", and
50 * have applications attach and detach.. for now, this VMS is part of
51 * the app, so the VMS system starts up as part of running the app.
52 *
53 *The semantic layer is isolated from the VMS internals by making the
54 * semantic layer do setup to a state that it's ready with its
55 * initial virt procrs, ready to schedule them to slots when the masterLoop
56 * asks. Without this pattern, the semantic layer's setup would
57 * have to modify slots directly to assign the initial virt-procrs, and put
58 * them into the readyToAnimateQ itself, breaking the isolation completely.
59 *
60 *
61 *The semantic layer creates the initial virt procr(s), and adds its
62 * own environment to masterEnv, and fills in the pointers to
63 * the requestHandler and slaveScheduler plug-in functions
64 */
66 /*This allocates VMS data structures, populates the master VMSProc,
67 * and master environment, and returns the master environment to the semantic
68 * layer.
69 */
70 void
71 VMS__init()
72 {
73 create_masterEnv();
74 create_the_coreLoop_OS_threads();
75 }
77 /*To initialize the sequential version, just don't create the threads
78 */
79 void
80 VMS__init_Seq()
81 {
82 create_masterEnv();
83 }
85 void
86 create_masterEnv()
87 { MasterEnv *masterEnv;
88 SRSWQueueStruc **readyToAnimateQs;
89 int coreIdx;
90 VirtProcr **masterVPs;
91 SchedSlot ***allSchedSlots; //ptr to array of ptrs
93 //Make the master env, which holds everything else
94 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
95 masterEnv = _VMSMasterEnv;
96 //Need to set start pt here 'cause used by seed procr, which is created
97 // before the first core loop starts up. -- not sure how yet..
98 // masterEnv->coreLoopStartPt = ;
99 // masterEnv->coreLoopEndPt = ;
101 //Make a readyToAnimateQ for each core loop
102 readyToAnimateQs = malloc( NUM_CORES * sizeof(SRSWQueueStruc *) );
103 masterVPs = malloc( NUM_CORES * sizeof(VirtProcr *) );
105 //One array for each core, 3 in array, core's masterVP scheds all
106 allSchedSlots = malloc( NUM_CORES * sizeof(SchedSlot *) );
108 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
109 { //running in main thread -- normal malloc inside makeSRSWQ
110 readyToAnimateQs[ coreIdx ] = makeSRSWQ();
112 //Q: should give masterVP core-specific info as its init data?
113 masterVPs[ coreIdx ] = VMS_ext__create_procr( &masterLoop, masterEnv );
114 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
115 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
116 _VMSMasterEnv->numMasterInARow[ coreIdx ] = FALSE;
117 }
118 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
119 _VMSMasterEnv->masterVPs = masterVPs;
120 _VMSMasterEnv->masterLock = UNLOCKED;
121 _VMSMasterEnv->allSchedSlots = allSchedSlots;
122 _VMSMasterEnv->numProcrsCreated = 0;
125 //Aug 19, 2010: no longer need to place initial masterVP into queue
126 // because coreLoop now controls -- animates its masterVP when no work
128 _VMSMasterEnv->freeListHead = VMS__create_free_list();
129 _VMSMasterEnv->amtOfOutstandingMem = 0; //none allocated yet
131 //============================= MEASUREMENT STUFF ========================
132 #ifdef STATS__TURN_ON_PROBES
133 //creates intervalProbes array and sets pointer to it in masterEnv too
134 _VMSMasterEnv->dynIntervalProbesInfo =
135 makeDynArrayOfSize( &(_VMSMasterEnv->intervalProbes), 20 );
137 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, NULL );
138 _VMSMasterEnv->masterCreateProbeID =
139 VMS_ext__record_time_point_into_new_probe( "masterCreateProbe" );
140 //Also put creation time directly into master env, for fast retrieval
141 struct timeval timeStamp;
142 gettimeofday( &(timeStamp), NULL);
143 _VMSMasterEnv->createPtInSecs =
144 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
145 #endif
146 //========================================================================
148 }
150 SchedSlot **
151 create_sched_slots()
152 { SchedSlot **schedSlots;
153 int i;
155 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
157 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
158 {
159 schedSlots[i] = malloc( sizeof(SchedSlot) );
161 //Set state to mean "handling requests done, slot needs filling"
162 schedSlots[i]->workIsDone = FALSE;
163 schedSlots[i]->needsProcrAssigned = TRUE;
164 }
165 return schedSlots;
166 }
169 void
170 freeSchedSlots( SchedSlot **schedSlots )
171 { int i;
172 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
173 {
174 free( schedSlots[i] );
175 }
176 free( schedSlots );
177 }
180 void
181 create_the_coreLoop_OS_threads()
182 {
183 //========================================================================
184 // Create the Threads
185 int coreIdx, retCode;
187 //Need the threads to be created suspended, and wait for a signal
188 // before proceeding -- gives time after creating to initialize other
189 // stuff before the coreLoops set off.
190 _VMSMasterEnv->setupComplete = 0;
192 //Make the threads that animate the core loops
193 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
194 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
195 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
197 retCode =
198 pthread_create( &(coreLoopThdHandles[coreIdx]),
199 thdAttrs,
200 &coreLoop,
201 (void *)(coreLoopThdParams[coreIdx]) );
202 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
203 }
204 }
206 /*Semantic layer calls this when it want the system to start running..
207 *
208 *This starts the core loops running then waits for them to exit.
209 */
210 void
211 VMS__start_the_work_then_wait_until_done()
212 { int coreIdx;
213 //Start the core loops running
215 //tell the core loop threads that setup is complete
216 //get lock, to lock out any threads still starting up -- they'll see
217 // that setupComplete is true before entering while loop, and so never
218 // wait on the condition
219 pthread_mutex_lock( &suspendLock );
220 _VMSMasterEnv->setupComplete = 1;
221 pthread_mutex_unlock( &suspendLock );
222 pthread_cond_broadcast( &suspend_cond );
225 //wait for all to complete
226 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
227 {
228 pthread_join( coreLoopThdHandles[coreIdx], NULL );
229 }
231 //NOTE: do not clean up VMS env here -- semantic layer has to have
232 // a chance to clean up its environment first, then do a call to free
233 // the Master env and rest of VMS locations
234 }
236 /*Only difference between version with an OS thread pinned to each core and
237 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
238 */
239 void
240 VMS__start_the_work_then_wait_until_done_Seq()
241 {
242 //Instead of un-suspending threads, just call the one and only
243 // core loop (sequential version), in the main thread.
244 coreLoop_Seq( NULL );
246 }
250 /*Create stack, then create __cdecl structure on it and put initialData and
251 * pointer to the new structure instance into the parameter positions on
252 * the stack
253 *Then put function pointer into nextInstrPt -- the stack is setup in std
254 * call structure, so jumping to function ptr is same as a GCC generated
255 * function call
256 *No need to save registers on old stack frame, because there's no old
257 * animator state to return to --
258 *
259 */
260 inline VirtProcr *
261 create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr,
262 void *initialData, char *stackLocs )
263 {
264 char *stackPtr;
266 newPr->procrID = _VMSMasterEnv->numProcrsCreated++;
267 newPr->nextInstrPt = fnPtr;
268 newPr->initialData = initialData;
269 newPr->requests = NULL;
270 newPr->schedSlot = NULL;
272 //fnPtr takes two params -- void *initData & void *animProcr
273 //alloc stack locations, make stackPtr be the highest addr minus room
274 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
275 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
276 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
278 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
279 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
280 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
281 newPr->stackPtr = stackPtr; //core loop will switch to this, then
282 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
284 //============================= MEASUREMENT STUFF ========================
285 #ifdef STATS__TURN_ON_PROBES
286 struct timeval timeStamp;
287 gettimeofday( &(timeStamp), NULL);
288 newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
289 #endif
290 //========================================================================
292 return newPr;
293 }
295 inline VirtProcr *
296 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
297 { VirtProcr *newPr;
298 char *stackLocs;
300 newPr = VMS__malloc( sizeof(VirtProcr) );
301 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
302 if( stackLocs == 0 )
303 { perror("VMS__malloc stack"); exit(1); }
304 newPr->startOfStack = stackLocs;
306 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
307 }
309 /* "ext" designates that it's for use outside the VMS system -- should only
310 * be called from main thread or other thread -- never from code animated by
311 * a VMS virtual processor.
312 */
313 inline VirtProcr *
314 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
315 { VirtProcr *newPr;
316 char *stackLocs;
318 newPr = malloc( sizeof(VirtProcr) );
319 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
320 if( stackLocs == 0 )
321 { perror("malloc stack"); exit(1); }
322 newPr->startOfStack = stackLocs;
324 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
325 }
328 /*there is a label inside this function -- save the addr of this label in
329 * the callingPr struc, as the pick-up point from which to start the next
330 * work-unit for that procr. If turns out have to save registers, then
331 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
332 * "done with work-unit" label. The procr struc is in the request in the
333 * slave that animated the just-ended work-unit, so all the state is saved
334 * there, and will get passed along, inside the request handler, to the
335 * next work-unit for that procr.
336 */
337 void
338 VMS__suspend_procr( VirtProcr *animatingPr )
339 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
340 void *coreLoopFramePtr;
342 //The request to master will cause this suspended virt procr to get
343 // scheduled again at some future point -- to resume, core loop jumps
344 // to the resume point (below), which causes restore of saved regs and
345 // "return" from this call.
346 animatingPr->nextInstrPt = &&ResumePt;
348 //return ownership of the virt procr and sched slot to Master virt pr
349 animatingPr->schedSlot->workIsDone = TRUE;
351 stackPtrAddr = &(animatingPr->stackPtr);
352 framePtrAddr = &(animatingPr->framePtr);
354 jmpPt = _VMSMasterEnv->coreLoopStartPt;
355 coreLoopFramePtr = animatingPr->coreLoopFramePtr;//need this only
356 coreLoopStackPtr = animatingPr->coreLoopStackPtr;//safety
358 //Save the virt procr's stack and frame ptrs,
359 asm volatile("movl %0, %%eax; \
360 movl %%esp, (%%eax); \
361 movl %1, %%eax; \
362 movl %%ebp, (%%eax) "\
363 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
364 /* inputs */ : \
365 /* clobber */ : "%eax" \
366 );
368 //=========================== Measurement stuff ========================
369 #ifdef MEAS__TIME_STAMP_SUSP
370 //record time stamp: compare to time-stamp recorded below
371 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
372 #endif
373 //=======================================================================
375 //restore coreloop's frame ptr, then jump back to "start" of core loop
376 //Note, GCC compiles to assembly that saves esp and ebp in the stack
377 // frame -- so have to explicitly do assembly that saves to memory
378 asm volatile("movl %0, %%eax; \
379 movl %1, %%esp; \
380 movl %2, %%ebp; \
381 jmp %%eax " \
382 /* outputs */ : \
383 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
384 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
385 ); //list everything as clobbered to force GCC to save all
386 // live vars that are in regs on stack before this
387 // assembly, so that stack pointer is correct, before jmp
389 ResumePt:
390 #ifdef MEAS__TIME_STAMP_SUSP
391 //NOTE: only take low part of count -- do sanity check when take diff
392 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
393 #endif
395 return;
396 }
400 /*For this implementation of VMS, it may not make much sense to have the
401 * system of requests for creating a new processor done this way.. but over
402 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
403 * distributed-memory, and so on, this gives VMS implementation a chance to
404 * do stuff before suspend, in the AppVP, and in the Master before the plugin
405 * is called, as well as in the lang-lib before this is called, and in the
406 * plugin. So, this gives both VMS and language implementations a chance to
407 * intercept at various points and do order-dependent stuff.
408 *Having a standard VMSNewPrReqData struc allows the language to create and
409 * free the struc, while VMS knows how to get the newPr if it wants it, and
410 * it lets the lang have lang-specific data related to creation transported
411 * to the plugin.
412 */
413 void
414 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
415 { VMSReqst req;
417 req.reqType = createReq;
418 req.semReqData = semReqData;
419 req.nextReqst = reqstingPr->requests;
420 reqstingPr->requests = &req;
422 VMS__suspend_procr( reqstingPr );
423 }
426 /*
427 *This adds a request to dissipate, then suspends the processor so that the
428 * request handler will receive the request. The request handler is what
429 * does the work of freeing memory and removing the processor from the
430 * semantic environment's data structures.
431 *The request handler also is what figures out when to shutdown the VMS
432 * system -- which causes all the core loop threads to die, and returns from
433 * the call that started up VMS to perform the work.
434 *
435 *This form is a bit misleading to understand if one is trying to figure out
436 * how VMS works -- it looks like a normal function call, but inside it
437 * sends a request to the request handler and suspends the processor, which
438 * jumps out of the VMS__dissipate_procr function, and out of all nestings
439 * above it, transferring the work of dissipating to the request handler,
440 * which then does the actual work -- causing the processor that animated
441 * the call of this function to disappear and the "hanging" state of this
442 * function to just poof into thin air -- the virtual processor's trace
443 * never returns from this call, but instead the virtual processor's trace
444 * gets suspended in this call and all the virt processor's state disap-
445 * pears -- making that suspend the last thing in the virt procr's trace.
446 */
447 void
448 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
449 { VMSReqst req;
451 req.reqType = dissipate;
452 req.nextReqst = procrToDissipate->requests;
453 procrToDissipate->requests = &req;
455 VMS__suspend_procr( procrToDissipate );
456 }
459 /* "ext" designates that it's for use outside the VMS system -- should only
460 * be called from main thread or other thread -- never from code animated by
461 * a VMS virtual processor.
462 *
463 *Use this version to dissipate VPs created outside the VMS system.
464 */
465 void
466 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
467 {
468 //NOTE: initialData was given to the processor, so should either have
469 // been alloc'd with VMS__malloc, or freed by the level above animPr.
470 //So, all that's left to free here is the stack and the VirtProcr struc
471 // itself
472 //Note, should not stack-allocate initial data -- no guarantee, in
473 // general that creating processor will outlive ones it creates.
474 free( procrToDissipate->startOfStack );
475 free( procrToDissipate );
476 }
480 /*This inserts the semantic-layer's request data into standard VMS carrier
481 * request data-struct is allocated on stack of this call & ptr to it sent
482 * to plugin
483 */
484 inline void
485 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
486 { VMSReqst req;
488 req.reqType = semantic;
489 req.semReqData = semReqData;
490 req.nextReqst = callingPr->requests;
491 callingPr->requests = &req;
492 }
494 /*This inserts the semantic-layer's request data into standard VMS carrier
495 * request data-struct is allocated on stack of this call & ptr to it sent
496 * to plugin
497 *Then it does suspend, to cause request to be sent.
498 */
499 inline void
500 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
501 { VMSReqst req;
503 req.reqType = semantic;
504 req.semReqData = semReqData;
505 req.nextReqst = callingPr->requests;
506 callingPr->requests = &req;
508 VMS__suspend_procr( callingPr );
509 }
512 inline void
513 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
514 { VMSReqst req;
516 req.reqType = VMSSemantic;
517 req.semReqData = semReqData;
518 req.nextReqst = callingPr->requests; //gab any other preceeding
519 callingPr->requests = &req;
521 VMS__suspend_procr( callingPr );
522 }
525 /*
526 */
527 VMSReqst *
528 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
529 { VMSReqst *req;
531 req = procrWithReq->requests;
532 if( req == NULL ) return NULL;
534 procrWithReq->requests = procrWithReq->requests->nextReqst;
535 return req;
536 }
539 inline void *
540 VMS__take_sem_reqst_from( VMSReqst *req )
541 {
542 return req->semReqData;
543 }
547 /* This is for OS requests and VMS infrastructure requests, such as to create
548 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
549 * language -- but it's also a semantic thing that's triggered from and used
550 * in the application.. so it crosses abstractions.. so, need some special
551 * pattern here for handling such requests.
552 * Doing this just like it were a second language sharing VMS-core.
553 *
554 * This is called from the language's request handler when it sees a request
555 * of type VMSSemReq
556 *
557 * TODO: Later change this, to give probes their own separate plugin & have
558 * VMS-core steer the request to appropriate plugin
559 * Do the same for OS calls -- look later at it..
560 */
561 void inline
562 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
563 ResumePrFnPtr resumePrFnPtr )
564 { VMSSemReq *semReq;
565 IntervalProbe *newProbe;
566 int32 nameLen;
568 semReq = req->semReqData;
570 newProbe = VMS__malloc( sizeof(IntervalProbe) );
571 nameLen = strlen( semReq->nameStr );
572 newProbe->nameStr = VMS__malloc( nameLen );
573 memcpy( newProbe->nameStr, semReq->nameStr, nameLen );
574 newProbe->hist = NULL;
575 newProbe->schedChoiceWasRecorded = FALSE;
576 newProbe->probeID =
577 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
579 requestingPr->dataReturnedFromReq = newProbe;
581 (*resumePrFnPtr)( requestingPr, semEnv );
582 }
586 /*This must be called by the request handler plugin -- it cannot be called
587 * from the semantic library "dissipate processor" function -- instead, the
588 * semantic layer has to generate a request, and the plug-in calls this
589 * function.
590 *The reason is that this frees the virtual processor's stack -- which is
591 * still in use inside semantic library calls!
592 *
593 *This frees or recycles all the state owned by and comprising the VMS
594 * portion of the animating virtual procr. The request handler must first
595 * free any semantic data created for the processor that didn't use the
596 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
597 * system to disown any state that did use VMS_malloc, and then frees the
598 * statck and the processor-struct itself.
599 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
600 * state, then that state gets freed (or sent to recycling) as a side-effect
601 * of dis-owning it.
602 */
603 void
604 VMS__dissipate_procr( VirtProcr *animatingPr )
605 {
606 //dis-own all locations owned by this processor, causing to be freed
607 // any locations that it is (was) sole owner of
608 //TODO: implement VMS__malloc system, including "give up ownership"
611 //NOTE: initialData was given to the processor, so should either have
612 // been alloc'd with VMS__malloc, or freed by the level above animPr.
613 //So, all that's left to free here is the stack and the VirtProcr struc
614 // itself
615 //Note, should not stack-allocate initial data -- no guarantee, in
616 // general that creating processor will outlive ones it creates.
617 VMS__free( animatingPr->startOfStack );
618 VMS__free( animatingPr );
619 }
622 //TODO: re-architect so that have clean separation between request handler
623 // and master loop, for dissipate, create, shutdown, and other non-semantic
624 // requests. Issue is chain: one removes requests from AppVP, one dispatches
625 // on type of request, and one handles each type.. but some types require
626 // action from both request handler and master loop -- maybe just give the
627 // request handler calls like: VMS__handle_X_request_type
629 void
630 endOSThreadFn( void *initData, VirtProcr *animatingPr );
632 /*This is called by the semantic layer's request handler when it decides its
633 * time to shut down the VMS system. Calling this causes the core loop OS
634 * threads to exit, which unblocks the entry-point function that started up
635 * VMS, and allows it to grab the result and return to the original single-
636 * threaded application.
637 *
638 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
639 * and-wait function has to free a bunch of stuff after it detects the
640 * threads have all died: the masterEnv, the thread-related locations,
641 * masterVP any AppVPs that might still be allocated and sitting in the
642 * semantic environment, or have been orphaned in the _VMSWorkQ.
643 *
644 *NOTE: the semantic plug-in is expected to use VMS__malloc_to to get all the
645 * locations it needs, and give ownership to masterVP. Then, they will be
646 * automatically freed when the masterVP is dissipated. (This happens after
647 * the core loop threads have all exited)
648 *
649 *In here,create one core-loop shut-down processor for each core loop and put
650 * them all directly into the readyToAnimateQ.
651 *Note, this function can ONLY be called after the semantic environment no
652 * longer cares if AppVPs get animated after the point this is called. In
653 * other words, this can be used as an abort, or else it should only be
654 * called when all AppVPs have finished dissipate requests -- only at that
655 * point is it sure that all results have completed.
656 */
657 void
658 VMS__handle_shutdown_reqst( void *dummy, VirtProcr *animatingPr )
659 { int coreIdx;
660 VirtProcr *shutDownPr;
662 //create the shutdown processors, one for each core loop -- put them
663 // directly into the Q -- each core will die when gets one
664 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
665 { //Note, this is running in the master
666 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
667 writeSRSWQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
668 }
670 }
673 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
674 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
675 *This function has the sole purpose of setting the stack and framePtr
676 * to the coreLoop's stack and framePtr.. it does that then jumps to the
677 * core loop's shutdown point -- might be able to just call Pthread_exit
678 * from here, but am going back to the pthread's stack and setting everything
679 * up just as if it never jumped out, before calling pthread_exit.
680 *The end-point of core loop will free the stack and so forth of the
681 * processor that animates this function, (this fn is transfering the
682 * animator of the AppVP that is in turn animating this function over
683 * to core loop function -- note that this slices out a level of virtual
684 * processors).
685 */
686 void
687 endOSThreadFn( void *initData, VirtProcr *animatingPr )
688 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
690 jmpPt = _VMSMasterEnv->coreLoopEndPt;
691 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
692 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
695 asm volatile("movl %0, %%eax; \
696 movl %1, %%esp; \
697 movl %2, %%ebp; \
698 jmp %%eax " \
699 /* outputs */ : \
700 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
701 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
702 );
703 }
706 /*This is called after the threads have shut down and control has returned
707 * to the semantic layer, in the entry point function in the main thread.
708 * It has to free anything allocated during VMS_init, and any other alloc'd
709 * locations that might be left over.
710 */
711 void
712 VMS__cleanup_after_shutdown()
713 {
714 SRSWQueueStruc **readyToAnimateQs;
715 int coreIdx;
716 VirtProcr **masterVPs;
717 SchedSlot ***allSchedSlots; //ptr to array of ptrs
719 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
720 masterVPs = _VMSMasterEnv->masterVPs;
721 allSchedSlots = _VMSMasterEnv->allSchedSlots;
723 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
724 {
725 freeSRSWQ( readyToAnimateQs[ coreIdx ] );
726 //master VPs were created external to VMS, so use external free
727 VMS_ext__dissipate_procr( masterVPs[ coreIdx ] );
729 freeSchedSlots( allSchedSlots[ coreIdx ] );
730 }
732 free( _VMSMasterEnv->readyToAnimateQs );
733 free( _VMSMasterEnv->masterVPs );
734 free( _VMSMasterEnv->allSchedSlots );
736 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
738 //============================= MEASUREMENT STUFF ========================
739 #ifdef STATS__TURN_ON_PROBES
740 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &free );
741 #endif
742 //========================================================================
744 free( _VMSMasterEnv );
745 }