Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.c @ 29:0e008278fe3c
Works Sequentially -- took out all threads and debugged -- works
| author | Me |
|---|---|
| date | Wed, 28 Jul 2010 13:12:10 -0700 |
| parents | 8b9e4c333fe6 |
| children | c8823e0bb2b4 |
line source
1 /*
2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <malloc.h>
11 #include "VMS.h"
12 #include "Queue_impl/BlockingQueue.h"
15 #define thdAttrs NULL
17 //===========================================================================
18 void
19 shutdownFn( void *dummy, VirtProcr *dummy2 );
21 void
22 create_sched_slots( MasterEnv *masterEnv );
24 void
25 create_masterEnv();
27 void
28 create_the_coreLoop_OS_threads();
30 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
31 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
33 //===========================================================================
35 /*Setup has two phases:
36 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
37 * the master virt procr into the work-queue, ready for first "call"
38 * 2) Semantic layer then does its own init, which creates the seed virt
39 * procr inside the semantic layer, ready to schedule it when
40 * asked by the first run of the masterLoop.
41 *
42 *This part is bit weird because VMS really wants to be "always there", and
43 * have applications attach and detach.. for now, this VMS is part of
44 * the app, so the VMS system starts up as part of running the app.
45 *
46 *The semantic layer is isolated from the VMS internals by making the
47 * semantic layer do setup to a state that it's ready with its
48 * initial virt procrs, ready to schedule them to slots when the masterLoop
49 * asks. Without this pattern, the semantic layer's setup would
50 * have to modify slots directly to assign the initial virt-procrs, and put
51 * them into the workQ itself, breaking the isolation completely.
52 *
53 *
54 *The semantic layer creates the initial virt procr(s), and adds its
55 * own environment to masterEnv, and fills in the pointers to
56 * the requestHandler and slaveScheduler plug-in functions
57 */
59 /*This allocates VMS data structures, populates the master VMSProc,
60 * and master environment, and returns the master environment to the semantic
61 * layer.
62 */
63 void
64 VMS__init()
65 {
66 create_masterEnv();
67 create_the_coreLoop_OS_threads();
68 }
70 /*To initialize the sequential version, just don't create the threads
71 */
72 void
73 VMS__init_Seq()
74 {
75 create_masterEnv();
76 }
78 void
79 create_masterEnv()
80 { MasterEnv *masterEnv;
81 VMSQueueStruc *workQ;
83 //Make the central work-queue
84 _VMSWorkQ = makeVMSQ();
85 workQ = _VMSWorkQ;
87 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
88 masterEnv = _VMSMasterEnv;
90 //create the master virtual processor
91 masterEnv->masterVirtPr = VMS__create_procr( &masterLoop, masterEnv );
93 create_sched_slots( masterEnv );
95 masterEnv->stillRunning = FALSE;
96 masterEnv->numToPrecede = NUM_CORES;
98 //First core loop to start up gets this, which will schedule seed Pr
99 //TODO: debug: check address of masterVirtPr
100 writeVMSQ( masterEnv->masterVirtPr, workQ );
102 numProcrsCreated = 1; //global counter for debugging
103 }
105 void
106 create_sched_slots( MasterEnv *masterEnv )
107 { SchedSlot **schedSlots, **filledSlots;
108 int i;
110 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
111 filledSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
112 masterEnv->schedSlots = schedSlots;
113 masterEnv->filledSlots = filledSlots;
115 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
116 {
117 schedSlots[i] = malloc( sizeof(SchedSlot) );
119 //Set state to mean "handling requests done, slot needs filling"
120 schedSlots[i]->workIsDone = FALSE;
121 schedSlots[i]->needsProcrAssigned = TRUE;
122 }
123 }
126 void
127 create_the_coreLoop_OS_threads()
128 {
129 //========================================================================
130 // Create the Threads
131 int coreIdx, retCode;
133 //Need the threads to be created suspended, and wait for a signal
134 // before proceeding -- gives time after creating to initialize other
135 // stuff before the coreLoops set off.
136 _VMSMasterEnv->setupComplete = 0;
138 //Make the threads that animate the core loops
139 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
140 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
141 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
143 retCode =
144 pthread_create( &(coreLoopThdHandles[coreIdx]),
145 thdAttrs,
146 &coreLoop,
147 (void *)(coreLoopThdParams[coreIdx]) );
148 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
149 }
150 }
152 /*Semantic layer calls this when it want the system to start running..
153 *
154 *This starts the core loops running then waits for them to exit.
155 */
156 void
157 VMS__start_the_work_then_wait_until_done()
158 { int coreIdx;
159 //Start the core loops running
160 //===========================================================================
161 TSCount startCount, endCount;
162 unsigned long long count = 0, freq = 0;
163 double runTime;
165 startCount = getTSCount();
167 //tell the core loop threads that setup is complete
168 //get lock, to lock out any threads still starting up -- they'll see
169 // that setupComplete is true before entering while loop, and so never
170 // wait on the condition
171 pthread_mutex_lock( &suspendLock );
172 _VMSMasterEnv->setupComplete = 1;
173 pthread_mutex_unlock( &suspendLock );
174 pthread_cond_broadcast( &suspend_cond );
177 //wait for all to complete
178 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
179 {
180 pthread_join( coreLoopThdHandles[coreIdx], NULL );
181 }
183 //NOTE: do not clean up VMS env here -- semantic layer has to have
184 // a chance to clean up its environment first, then do a call to free
185 // the Master env and rest of VMS locations
188 endCount = getTSCount();
189 count = endCount - startCount;
191 runTime = (double)count / (double)TSCOUNT_FREQ;
193 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
194 }
196 /*Only difference between version with an OS thread pinned to each core and
197 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
198 */
199 void
200 VMS__start_the_work_then_wait_until_done_Seq()
201 {
202 //Instead of un-suspending threads, just call the one and only
203 // core loop (sequential version), in the main thread.
204 coreLoop_Seq( NULL );
206 }
210 /*Create stack, then create __cdecl structure on it and put initialData and
211 * pointer to the new structure instance into the parameter positions on
212 * the stack
213 *Then put function pointer into nextInstrPt -- the stack is setup in std
214 * call structure, so jumping to function ptr is same as a GCC generated
215 * function call
216 *No need to save registers on old stack frame, because there's no old
217 * animator state to return to --
218 *
219 */
220 VirtProcr *
221 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
222 { VirtProcr *newPr;
223 char *stackLocs, *stackPtr;
225 newPr = malloc( sizeof(VirtProcr) );
226 newPr->procrID = numProcrsCreated++;
227 newPr->nextInstrPt = fnPtr;
228 newPr->initialData = initialData;
230 //fnPtr takes two params -- void *initData & void *animProcr
231 //alloc stack locations, make stackPtr be the highest addr minus room
232 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
233 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
234 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
235 if(stackLocs == 0)
236 {perror("malloc stack"); exit(1);}
237 newPr->startOfStack = stackLocs;
238 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
239 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
240 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
241 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
242 newPr->stackPtr = stackPtr; //core loop will switch to this, then
243 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
245 return newPr;
246 }
249 /*there is a label inside this function -- save the addr of this label in
250 * the callingPr struc, as the pick-up point from which to start the next
251 * work-unit for that procr. If turns out have to save registers, then
252 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
253 * "done with work-unit" label. The procr struc is in the request in the
254 * slave that animated the just-ended work-unit, so all the state is saved
255 * there, and will get passed along, inside the request handler, to the
256 * next work-unit for that procr.
257 */
258 void
259 VMS__suspend_procr( VirtProcr *callingPr )
260 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
261 void *coreLoopFramePtr;
263 //The request to master will cause this suspended virt procr to get
264 // scheduled again at some future point -- to resume, core loop jumps
265 // to the resume point (below), which causes restore of saved regs and
266 // "return" from this call.
267 callingPr->nextInstrPt = &&ResumePt;
269 //return ownership of the virt procr and sched slot to Master virt pr
270 callingPr->schedSlot->workIsDone = TRUE;
271 // coreIdx = callingPr->coreAnimatedBy;
273 stackPtrAddr = &(callingPr->stackPtr);
274 framePtrAddr = &(callingPr->framePtr);
276 jmpPt = callingPr->coreLoopStartPt;
277 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only
278 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety
280 //Eclipse's compilation sequence complains -- so break into two
281 // separate in-line assembly pieces
282 //Save the virt procr's stack and frame ptrs,
283 asm volatile("movl %0, %%eax; \
284 movl %%esp, (%%eax); \
285 movl %1, %%eax; \
286 movl %%ebp, (%%eax) "\
287 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
288 /* inputs */ : \
289 /* clobber */ : "%eax" \
290 );
292 //restore coreloop's frame ptr, then jump back to "start" of core loop
293 //Note, GCC compiles to assembly that saves esp and ebp in the stack
294 // frame -- so have to explicitly do assembly that saves to memory
295 asm volatile("movl %0, %%eax; \
296 movl %1, %%esp; \
297 movl %2, %%ebp; \
298 jmp %%eax " \
299 /* outputs */ : \
300 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
301 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
302 ); //list everything as clobbered to force GCC to save all
303 // live vars that are in regs on stack before this
304 // assembly, so that stack pointer is correct, before jmp
306 ResumePt:
307 return;
308 }
312 /*This is equivalent to "jump back to core loop" -- it's mainly only used
313 * just after adding dissipate request to a processor -- so the semantic
314 * layer is the only place it will be seen and/or used.
315 *
316 *It does almost the same thing as suspend, except don't need to save the
317 * stack nor set the nextInstrPt
318 *
319 *As of June 30, 2010 just implementing as a call to suspend -- just sugar
320 */
321 void
322 VMS__return_from_fn( VirtProcr *animatingPr )
323 {
324 VMS__suspend_procr( animatingPr );
325 }
328 /*Not sure yet the form going to put "dissipate" in, so this is the third
329 * possibility -- the semantic layer can just make a macro that looks like
330 * a call to its name, then expands to a call to this.
331 *
332 *As of June 30, 2010 this looks like the top choice..
333 *
334 *This adds a request to dissipate, then suspends the processor so that the
335 * request handler will receive the request. The request handler is what
336 * does the work of freeing memory and removing the processor from the
337 * semantic environment's data structures.
338 *The request handler also is what figures out when to shutdown the VMS
339 * system -- which causes all the core loop threads to die, and returns from
340 * the call that started up VMS to perform the work.
341 *
342 *This form is a bit misleading to understand if one is trying to figure out
343 * how VMS works -- it looks like a normal function call, but inside it
344 * sends a request to the request handler and suspends the processor, which
345 * jumps out of the VMS__dissipate_procr function, and out of all nestings
346 * above it, transferring the work of dissipating to the request handler,
347 * which then does the actual work -- causing the processor that animated
348 * the call of this function to disappear and the "hanging" state of this
349 * function to just poof into thin air -- the virtual processor's trace
350 * never returns from this call, but instead the virtual processor's trace
351 * gets suspended in this call and all the virt processor's state disap-
352 * pears -- making that suspend the last thing in the virt procr's trace.
353 */
354 void
355 VMS__dissipate_procr( VirtProcr *procrToDissipate )
356 { VMSReqst *req;
358 req = malloc( sizeof(VMSReqst) );
359 // req->virtProcrFrom = callingPr;
360 req->reqType = dissipate;
361 req->nextReqst = procrToDissipate->requests;
362 procrToDissipate->requests = req;
364 VMS__suspend_procr( procrToDissipate );
365 }
368 /*This inserts the semantic-layer's request data into standard VMS carrier
369 */
370 inline void
371 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
372 { VMSReqst *req;
374 req = malloc( sizeof(VMSReqst) );
375 // req->virtProcrFrom = callingPr;
376 req->reqType = semantic;
377 req->semReqData = semReqData;
378 req->nextReqst = callingPr->requests;
379 callingPr->requests = req;
380 }
384 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
385 // of a request -- IE call with both a virt procr and a fn-ptr to request
386 // freer (or maybe put request freer as a field in virt procr?)
387 void
388 VMS__remove_and_free_top_request( VirtProcr *procrWithReq )
389 { VMSReqst *req;
391 req = procrWithReq->requests;
392 if( req == NULL ) return;
393 procrWithReq->requests = procrWithReq->requests->nextReqst;
394 VMS__free_request( req );
395 }
398 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
399 // of a request -- IE call with both a virt procr and a fn-ptr to request
400 // freer (also maybe put sem request freer as a field in virt procr?)
401 //VMSHW relies right now on this only freeing VMS layer of request -- the
402 // semantic portion of request is alloc'd and freed by request handler
403 void
404 VMS__free_request( VMSReqst *req )
405 {
406 free( req );
407 }
409 VMSReqst *
410 VMS__take_top_request_from( VirtProcr *procrWithReq )
411 { VMSReqst *req;
413 req = procrWithReq->requests;
414 if( req == NULL ) return req;
416 procrWithReq->requests = procrWithReq->requests->nextReqst;
417 return req;
418 }
420 inline int
421 VMS__isSemanticReqst( VMSReqst *req )
422 {
423 return ( req->reqType == semantic );
424 }
427 inline void *
428 VMS__take_sem_reqst_from( VMSReqst *req )
429 {
430 return req->semReqData;
431 }
433 inline int
434 VMS__isDissipateReqst( VMSReqst *req )
435 {
436 return ( req->reqType == dissipate );
437 }
439 inline int
440 VMS__isCreateReqst( VMSReqst *req )
441 {
442 return ( req->reqType == regCreated );
443 }
445 void
446 VMS__send_register_new_procr_request(VirtProcr *newPr, VirtProcr *reqstingPr)
447 { VMSReqst *req;
449 req = malloc( sizeof(VMSReqst) );
450 req->reqType = regCreated;
451 req->semReqData = newPr;
452 req->nextReqst = reqstingPr->requests;
453 reqstingPr->requests = req;
455 VMS__suspend_procr( reqstingPr );
456 }
460 /*This must be called by the request handler plugin -- it cannot be called
461 * from the semantic library "dissipate processor" function -- instead, the
462 * semantic layer has to generate a request for the plug-in to call this
463 * function.
464 *The reason is that this frees the virtual processor's stack -- which is
465 * still in use inside semantic library calls!
466 *
467 *This frees or recycles all the state owned by and comprising the VMS
468 * portion of the animating virtual procr. The request handler must first
469 * free any semantic data created for the processor that didn't use the
470 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
471 * system to disown any state that did use VMS_malloc, and then frees the
472 * statck and the processor-struct itself.
473 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
474 * state, then that state gets freed (or sent to recycling) as a side-effect
475 * of dis-owning it.
476 */
477 void
478 VMS__handle_dissipate_reqst( VirtProcr *animatingPr )
479 {
480 //dis-own all locations owned by this processor, causing to be freed
481 // any locations that it is (was) sole owner of
482 //TODO: implement VMS__malloc system, including "give up ownership"
484 //The dissipate request might still be attached, so remove and free it
485 VMS__remove_and_free_top_request( animatingPr );
487 //NOTE: initialData was given to the processor, so should either have
488 // been alloc'd with VMS__malloc, or freed by the level above animPr.
489 //So, all that's left to free here is the stack and the VirtProcr struc
490 // itself
491 free( animatingPr->startOfStack );
492 free( animatingPr );
493 }
496 //TODO: re-architect so that have clean separation between request handler
497 // and master loop, for dissipate, create, shutdown, and other non-semantic
498 // requests. Issue is chain: one removes requests from AppVP, one dispatches
499 // on type of request, and one handles each type.. but some types require
500 // action from both request handler and master loop -- maybe just give the
501 // request handler calls like: VMS__handle_X_request_type
503 void
504 endOSThreadFn( void *initData, VirtProcr *animatingPr );
506 /*This is called by the semantic layer's request handler when it decides its
507 * time to shut down the VMS system. Calling this causes the core loop OS
508 * threads to exit, which unblocks the entry-point function that started up
509 * VMS, and allows it to grab the result and return to the original single-
510 * threaded application.
511 *
512 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
513 * and-wait function has to free a bunch of stuff after it detects the
514 * threads have all died: the masterEnv, the thread-related locations,
515 * masterVP any AppVPs that might still be allocated and sitting in the
516 * semantic environment, or have been orphaned in the _VMSWorkQ.
517 *
518 *NOTE: the semantic plug-in is expected to use VMS__malloc_to to get all the
519 * locations it needs, and give ownership to masterVP. Then, they will be
520 * automatically freed when the masterVP is dissipated. (This happens after
521 * the core loop threads have all exited)
522 *
523 *In here,create one core-loop shut-down processor for each core loop and put
524 * them all directly into the workQ.
525 *Note, this function can ONLY be called after the semantic environment no
526 * longer cares if AppVPs get animated after the point this is called. In
527 * other words, this can be used as an abort, or else it should only be
528 * called when all AppVPs have finished dissipate requests -- only at that
529 * point is it sure that all results have completed.
530 */
531 void
532 VMS__handle_shutdown_reqst( void *dummy, VirtProcr *animatingPr )
533 { int coreIdx;
534 VirtProcr *shutDownPr;
535 VMSQueueStruc *workQ = _VMSWorkQ;
537 //create the shutdown processors, one for each core loop -- put them
538 // directly into _VMSWorkQ -- each core will die when gets one, so
539 // the system distributes them evenly itself.
540 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
541 {
542 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
543 writeVMSQ( shutDownPr, workQ );
544 }
546 }
549 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
550 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
551 *This function has the sole purpose of setting the stack and framePtr
552 * to the coreLoop's stack and framePtr.. it does that then jumps to the
553 * core loop's shutdown point -- might be able to just call Pthread_exit
554 * from here, but going back to the pthread's stack and setting everything
555 * up just as if it never jumped out, before calling pthread_exit.
556 *The end-point of core loop will free the stack and so forth of the
557 * processor that animates this function, (this fn is transfering the
558 * animator of the AppVP that is in turn animating this function over
559 * to core loop function -- note that this slices out a level of virtual
560 * processors).
561 */
562 void
563 endOSThreadFn( void *initData, VirtProcr *animatingPr )
564 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
566 jmpPt = _VMSMasterEnv->coreLoopEndPt;
567 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
568 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
571 asm volatile("movl %0, %%eax; \
572 movl %1, %%esp; \
573 movl %2, %%ebp; \
574 jmp %%eax " \
575 /* outputs */ : \
576 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
577 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
578 );
579 }
583 /*This is called has to free anything allocated during VMS_init, and any other alloc'd
584 * locations that might be left over.
585 */
586 void
587 VMS__cleanup_after_shutdown()
588 { int i;
590 free( _VMSWorkQ );
591 free( _VMSMasterEnv->filledSlots );
592 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
593 {
594 free( _VMSMasterEnv->schedSlots[i] );
595 }
597 free( _VMSMasterEnv->schedSlots);
598 VMS__handle_dissipate_reqst( _VMSMasterEnv->masterVirtPr );
600 free( _VMSMasterEnv );
601 }
604 //===========================================================================
606 inline TSCount getTSCount()
607 { unsigned int low, high;
608 TSCount out;
610 saveTimeStampCountInto( low, high );
611 out = high;
612 out = (out << 32) + low;
613 return out;
614 }
