view VSs.c @ 8:eb3d77ca9f59

Code complete -- not debuggedd yet
author Sean Halle <seanhalle@yahoo.com>
date Thu, 02 Aug 2012 01:03:14 -0700
parents 3999b8429ddd
children 832bc715fbf2
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 "Queue_impl/PrivateQueue.h"
12 #include "Hash_impl/PrivateHash.h"
14 #include "VSs.h"
15 #include "Measurement/VSs_Counter_Recording.h"
17 //==========================================================================
19 void
20 VSs__init();
22 void
23 VSs__init_Helper();
24 //==========================================================================
28 //===========================================================================
31 /*These are the library functions *called in the application*
32 *
33 *There's a pattern for the outside sequential code to interact with the
34 * VMS_HW code.
35 *The VMS_HW system is inside a boundary.. every VSs system is in its
36 * own directory that contains the functions for each of the processor types.
37 * One of the processor types is the "seed" processor that starts the
38 * cascade of creating all the processors that do the work.
39 *So, in the directory is a file called "EntryPoint.c" that contains the
40 * function, named appropriately to the work performed, that the outside
41 * sequential code calls. This function follows a pattern:
42 *1) it calls VSs__init()
43 *2) it creates the initial data for the seed processor, which is passed
44 * in to the function
45 *3) it creates the seed VSs processor, with the data to start it with.
46 *4) it calls startVSsThenWaitUntilWorkDone
47 *5) it gets the returnValue from the transfer struc and returns that
48 * from the function
49 *
50 *For now, a new VSs system has to be created via VSs__init every
51 * time an entry point function is called -- later, might add letting the
52 * VSs system be created once, and let all the entry points just reuse
53 * it -- want to be as simple as possible now, and see by using what makes
54 * sense for later..
55 */
59 //===========================================================================
61 /*This is the "border crossing" function -- the thing that crosses from the
62 * outside world, into the VMS_HW world. It initializes and starts up the
63 * VMS system, then creates one processor from the specified function and
64 * puts it into the readyQ. From that point, that one function is resp.
65 * for creating all the other processors, that then create others, and so
66 * forth.
67 *When all the processors, including the seed, have dissipated, then this
68 * function returns. The results will have been written by side-effect via
69 * pointers read from, or written into initData.
70 *
71 *NOTE: no Threads should exist in the outside program that might touch
72 * any of the data reachable from initData passed in to here
73 */
74 void
75 VSs__create_seed_slave_and_do_work( TopLevelFnPtr fnPtr, void *initData )
76 { VSsSemEnv *semEnv;
77 SlaveVP *seedSlv;
78 VSsSemData *semData;
79 VSsTaskStub *threadTaskStub, *parentTaskStub;
81 VSs__init(); //normal multi-thd
83 semEnv = _VMSMasterEnv->semanticEnv;
85 //VSs starts with one processor, which is put into initial environ,
86 // and which then calls create() to create more, thereby expanding work
87 seedSlv = VSs__create_slave_helper( fnPtr, initData,
88 semEnv, semEnv->nextCoreToGetNewSlv++ );
90 //seed slave is a thread slave, so make a thread's task stub for it
91 // and then make another to stand for the seed's parent task. Make
92 // the parent be already ended, and have one child (the seed). This
93 // will make the dissipate handler do the right thing when the seed
94 // is dissipated.
95 threadTaskStub = create_thread_task_stub( initData );
96 parentTaskStub = create_thread_task_stub( NULL );
97 parentTaskStub->isEnded = TRUE;
98 parentTaskStub->numLiveChildThreads = 1; //so dissipate works for seed
99 threadTaskStub->parentTasksStub = parentTaskStub;
101 semData = (VSsSemData *)seedSlv->semanticData;
102 //seedVP is a thread, so has a permanent task
103 semData->needsTaskAssigned = FALSE;
104 semData->taskStub = threadTaskStub;
106 resume_slaveVP( seedSlv, semEnv ); //returns right away, just queues Slv
108 VMS_SS__start_the_work_then_wait_until_done(); //normal multi-thd
110 VSs__cleanup_after_shutdown();
111 }
114 int32
115 VSs__giveMinWorkUnitCycles( float32 percentOverhead )
116 {
117 return MIN_WORK_UNIT_CYCLES;
118 }
120 int32
121 VSs__giveIdealNumWorkUnits()
122 {
123 return NUM_ANIM_SLOTS * NUM_CORES;
124 }
126 int32
127 VSs__give_number_of_cores_to_schedule_onto()
128 {
129 return NUM_CORES;
130 }
132 /*For now, use TSC -- later, make these two macros with assembly that first
133 * saves jump point, and second jumps back several times to get reliable time
134 */
135 void
136 VSs__start_primitive()
137 { saveLowTimeStampCountInto( ((VSsSemEnv *)(_VMSMasterEnv->semanticEnv))->
138 primitiveStartTime );
139 }
141 /*Just quick and dirty for now -- make reliable later
142 * will want this to jump back several times -- to be sure cache is warm
143 * because don't want comm time included in calc-time measurement -- and
144 * also to throw out any "weird" values due to OS interrupt or TSC rollover
145 */
146 int32
147 VSs__end_primitive_and_give_cycles()
148 { int32 endTime, startTime;
149 //TODO: fix by repeating time-measurement
150 saveLowTimeStampCountInto( endTime );
151 startTime =((VSsSemEnv*)(_VMSMasterEnv->semanticEnv))->primitiveStartTime;
152 return (endTime - startTime);
153 }
155 //===========================================================================
157 /*Initializes all the data-structures for a VSs system -- but doesn't
158 * start it running yet!
159 *
160 *This runs in the main thread -- before VMS starts up
161 *
162 *This sets up the semantic layer over the VMS system
163 *
164 *First, calls VMS_Setup, then creates own environment, making it ready
165 * for creating the seed processor and then starting the work.
166 */
167 void
168 VSs__init()
169 {
170 VMS_SS__init();
171 //masterEnv, a global var, now is partially set up by init_VMS
172 // after this, have VMS_int__malloc and VMS_int__free available
174 VSs__init_Helper();
175 }
178 void idle_fn(void* data, SlaveVP *animatingSlv){
179 while(1){
180 VMS_int__suspend_slaveVP_and_send_req(animatingSlv);
181 }
182 }
184 void
185 VSs__init_Helper()
186 { VSsSemEnv *semanticEnv;
187 int32 i, coreNum, slotNum;
189 //Hook up the semantic layer's plug-ins to the Master virt procr
190 _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
191 _VMSMasterEnv->slaveAssigner = &VSs__assign_slaveVP_to_slot;
192 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
193 _VMSMasterEnv->counterHandler = &VSs__counter_handler;
194 #endif
196 //create the semantic layer's environment (all its data) and add to
197 // the master environment
198 semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
199 _VMSMasterEnv->semanticEnv = semanticEnv;
201 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
202 VSs__init_counter_data_structs();
203 #endif
205 semanticEnv->shutdownInitiated = FALSE;
206 semanticEnv->coreIsDone = VMS_int__malloc( NUM_CORES * sizeof( bool32 ) );
207 //For each animation slot, there is an idle slave, and an initial
208 // slave assigned as the current-task-slave. Create them here.
209 SlaveVP *idleSlv, *currTaskSlv;
210 for( coreNum = 0; coreNum < NUM_CORES; coreNum++ )
211 { semanticEnv->coreIsDone[coreNum] = FALSE; //use during shutdown
213 for( slotNum = 0; slotNum < NUM_ANIM_SLOTS; ++slotNum )
214 { idleSlv = VMS_int__create_slaveVP(&idle_fn,NULL);
215 idleSlv->coreAnimatedBy = coreNum;
216 idleSlv->animSlotAssignedTo = slotNum;
217 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
219 currTaskSlv = VMS_int__create_slaveVP( &idle_fn, NULL );
220 currTaskSlv->coreAnimatedBy = coreNum;
221 currTaskSlv->animSlotAssignedTo = slotNum;
222 semanticEnv->currTaskSlvs[coreNum][slotNum] = currTaskSlv;
223 }
224 }
226 //create the ready queues, hash tables used for matching and so forth
227 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
228 semanticEnv->extraTaskSlvQ = makeVMSQ();
229 semanticEnv->taskReadyQ = makeVMSQ();
231 semanticEnv->argPtrHashTbl = makeHashTable32( 16, &VMS_int__free );
232 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
234 semanticEnv->nextCoreToGetNewSlv = 0;
237 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
238 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
239 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
240 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
241 {
242 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
243 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
244 semanticEnv->fnSingletons[i].hasFinished = FALSE;
245 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
246 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
247 }
249 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
250 semanticEnv->numLiveThreadSlvs = 1; //must be last, count the seed
252 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
253 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
254 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
255 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
256 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
257 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
259 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
260 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
261 #endif
262 }
265 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
266 */
267 void
268 VSs__cleanup_after_shutdown()
269 { VSsSemEnv *semanticEnv;
271 semanticEnv = _VMSMasterEnv->semanticEnv;
273 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
274 //UCC
275 FILE* output;
276 int n;
277 char filename[255];
278 for(n=0;n<255;n++)
279 {
280 sprintf(filename, "./counters/UCC.%d",n);
281 output = fopen(filename,"r");
282 if(output)
283 {
284 fclose(output);
285 }else{
286 break;
287 }
288 }
289 if(n<255){
290 printf("Saving UCC to File: %s ...\n", filename);
291 output = fopen(filename,"w+");
292 if(output!=NULL){
293 set_dependency_file(output);
294 //fprintf(output,"digraph Dependencies {\n");
295 //set_dot_file(output);
296 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
297 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
298 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
299 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
300 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
301 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
302 //fprintf(output,"}\n");
303 fflush(output);
305 } else
306 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
307 } else {
308 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
309 }
310 //Loop Graph
311 for(n=0;n<255;n++)
312 {
313 sprintf(filename, "./counters/LoopGraph.%d",n);
314 output = fopen(filename,"r");
315 if(output)
316 {
317 fclose(output);
318 }else{
319 break;
320 }
321 }
322 if(n<255){
323 printf("Saving LoopGraph to File: %s ...\n", filename);
324 output = fopen(filename,"w+");
325 if(output!=NULL){
326 set_dependency_file(output);
327 //fprintf(output,"digraph Dependencies {\n");
328 //set_dot_file(output);
329 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
330 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
331 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
332 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
333 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
334 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
335 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
336 //fprintf(output,"}\n");
337 fflush(output);
339 } else
340 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
341 } else {
342 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
343 }
346 freeListOfArrays(semanticEnv->unitList);
347 freeListOfArrays(semanticEnv->commDependenciesList);
348 freeListOfArrays(semanticEnv->ctlDependenciesList);
349 freeListOfArrays(semanticEnv->dynDependenciesList);
351 #endif
352 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
353 for(n=0;n<255;n++)
354 {
355 sprintf(filename, "./counters/Counters.%d.csv",n);
356 output = fopen(filename,"r");
357 if(output)
358 {
359 fclose(output);
360 }else{
361 break;
362 }
363 }
364 if(n<255){
365 printf("Saving Counter measurements to File: %s ...\n", filename);
366 output = fopen(filename,"w+");
367 if(output!=NULL){
368 set_counter_file(output);
369 int i;
370 for(i=0;i<NUM_CORES;i++){
371 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
372 fflush(output);
373 }
375 } else
376 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
377 } else {
378 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
379 }
381 #endif
382 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
383 * nothing to do here
386 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
387 {
388 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
389 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
390 }
391 VMS_int__free( semanticEnv->readyVPQs );
393 freeHashTable( semanticEnv->commHashTbl );
394 VMS_int__free( _VMSMasterEnv->semanticEnv );
395 */
396 VMS_SS__cleanup_at_end_of_shutdown();
397 }
400 //===========================================================================
402 SlaveVP *
403 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
404 SlaveVP *creatingThd )
405 { VSsSemReq reqData;
407 //the semantic request data is on the stack and disappears when this
408 // call returns -- it's guaranteed to remain in the VP's stack for as
409 // long as the VP is suspended.
410 reqData.reqType = 0; //know type because in a VMS create req
411 reqData.fnPtr = fnPtr;
412 reqData.initData = initData;
413 reqData.callingSlv = creatingThd;
415 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
417 return creatingThd->dataRetFromReq;
418 }
420 /*This is always the last thing done in the code animated by a thread.
421 * Normally, this would be the last line of the thread's top level function.
422 * But, if the thread exits from any point, it has to do so by calling
423 * this.
424 *
425 *This must update the count of active sub-tasks (sub-threads) of parents,
426 * and the semantic data and task stub must stay.
427 */
428 void
429 VSs__end_thread( SlaveVP *thdToEnd )
430 { VSsSemData *semData;
432 //check whether all sub-tasks have ended.. if not, don't free the
433 // semantic data nor task stub of this thread.
434 semData = (VSsSemData *)thdToEnd->semanticData;
435 if( semData->taskStub->numLiveChildTasks != 0 )
436 {
437 fix_me();
438 }
440 //Update the count of live sub-tasks in parent. If parent was a
441 // thread and has already ended, then if this was the last sub-task,
442 // free the semantic data and task stub of the parent.
444 VMS_WL__send_dissipate_req( thdToEnd );
445 }
448 //===========================================================================
451 //======================= task submit and end ==============================
452 /*
453 */
454 void
455 VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
456 { VSsSemReq reqData;
458 reqData.reqType = submit_task;
460 reqData.taskType = taskType;
461 reqData.args = args;
462 reqData.callingSlv = animSlv;
464 reqData.taskID = NULL;
466 VMS_WL__send_sem_request( &reqData, animSlv );
467 }
469 inline int32 *
470 VSs__create_taskID_of_size( int32 numInts, SlaveVP *animSlv )
471 { int32 *taskID;
473 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
474 taskID[0] = numInts;
475 return taskID;
476 }
478 void
479 VSs__submit_task_with_ID( VSsTaskType *taskType, void *args, int32 *taskID,
480 SlaveVP *animSlv)
481 { VSsSemReq reqData;
483 reqData.reqType = submit_task;
485 reqData.taskType = taskType;
486 reqData.args = args;
487 reqData.taskID = taskID;
488 reqData.callingSlv = animSlv;
490 VMS_WL__send_sem_request( &reqData, animSlv );
491 }
494 /*This call is the last to happen in every task. It causes the slave to
495 * suspend and get the next task out of the task-queue. Notice there is no
496 * assigner here.. only one slave, no slave ReadyQ, and so on..
497 *Can either make the assigner take the next task out of the taskQ, or can
498 * leave all as it is, and make task-end take the next task.
499 *Note: this fits the case in the new VMS for no-context tasks, so will use
500 * the built-in taskQ of new VMS, and should be local and much faster.
501 *
502 *The task-stub is saved in the animSlv, so the request handler will get it
503 * from there, along with the task-type which has arg types, and so on..
504 *
505 * NOTE: if want, don't need to send the animating SlaveVP around..
506 * instead, can make a single slave per core, and coreCtrlr looks up the
507 * slave from having the core number.
508 *
509 *But, to stay compatible with all the other VMS languages, leave it in..
510 */
511 void
512 VSs__end_task( SlaveVP *animSlv )
513 { VSsSemReq reqData;
515 reqData.reqType = end_task;
516 reqData.callingSlv = animSlv;
518 VMS_WL__send_sem_request( &reqData, animSlv );
519 }
522 void
523 VSs__taskwait(SlaveVP *animSlv)
524 {
525 VSsSemReq reqData;
527 reqData.reqType = taskwait;
528 reqData.callingSlv = animSlv;
530 VMS_WL__send_sem_request( &reqData, animSlv );
531 }
535 //========================== send and receive ============================
536 //
538 inline int32 *
539 VSs__give_self_taskID( SlaveVP *animSlv )
540 {
541 return ((VSsSemData*)animSlv->semanticData)->taskStub->taskID;
542 }
544 //================================ send ===================================
546 void
547 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID,
548 SlaveVP *senderSlv )
549 { VSsSemReq reqData;
551 reqData.reqType = send_type_to;
553 reqData.msg = msg;
554 reqData.msgType = type;
555 reqData.receiverID = receiverID;
556 reqData.senderSlv = senderSlv;
558 reqData.nextReqInHashEntry = NULL;
560 VMS_WL__send_sem_request( &reqData, senderSlv );
562 //When come back from suspend, no longer own data reachable from msg
563 }
565 void
566 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID, SlaveVP *senderSlv )
567 { VSsSemReq reqData;
569 reqData.reqType = send_from_to;
571 reqData.msg = msg;
572 reqData.senderID = senderID;
573 reqData.receiverID = receiverID;
574 reqData.senderSlv = senderSlv;
576 reqData.nextReqInHashEntry = NULL;
578 VMS_WL__send_sem_request( &reqData, senderSlv );
579 }
582 //================================ receive ================================
584 /*The "type" version of send and receive creates a many-to-one relationship.
585 * The sender is anonymous, and many sends can stack up, waiting to be
586 * received. The same receiver can also have send from-to's
587 * waiting for it, and those will be kept separate from the "type"
588 * messages.
589 */
590 void *
591 VSs__receive_type_to( const int32 type, int32* receiverID, SlaveVP *receiverSlv )
592 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
593 VSsSemReq reqData;
595 reqData.reqType = receive_type_to;
597 reqData.msgType = type;
598 reqData.receiverID = receiverID;
599 reqData.receiverSlv = receiverSlv;
601 reqData.nextReqInHashEntry = NULL;
603 VMS_WL__send_sem_request( &reqData, receiverSlv );
605 return receiverSlv->dataRetFromReq;
606 }
610 /*Call this at the point a receiving task wants in-coming data.
611 * Use this from-to form when know senderID -- it makes a direct channel
612 * between sender and receiver.
613 */
614 void *
615 VSs__receive_from_to( int32 *senderID, int32 *receiverID, SlaveVP *receiverSlv )
616 {
617 VSsSemReq reqData;
619 reqData.reqType = receive_from_to;
621 reqData.senderID = senderID;
622 reqData.receiverID = receiverID;
623 reqData.receiverSlv = receiverSlv;
625 reqData.nextReqInHashEntry = NULL;
626 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
628 VMS_WL__send_sem_request( &reqData, receiverSlv );
630 return receiverSlv->dataRetFromReq;
631 }
636 //==========================================================================
637 //
638 /*A function singleton is a function whose body executes exactly once, on a
639 * single core, no matter how many times the fuction is called and no
640 * matter how many cores or the timing of cores calling it.
641 *
642 *A data singleton is a ticket attached to data. That ticket can be used
643 * to get the data through the function exactly once, no matter how many
644 * times the data is given to the function, and no matter the timing of
645 * trying to get the data through from different cores.
646 */
648 /*asm function declarations*/
649 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
650 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
652 /*Fn singleton uses ID as index into array of singleton structs held in the
653 * semantic environment.
654 */
655 void
656 VSs__start_fn_singleton( int32 singletonID, SlaveVP *animSlv )
657 {
658 VSsSemReq reqData;
660 //
661 reqData.reqType = singleton_fn_start;
662 reqData.singletonID = singletonID;
664 VMS_WL__send_sem_request( &reqData, animSlv );
665 if( animSlv->dataRetFromReq ) //will be 0 or addr of label in end singleton
666 {
667 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
668 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
669 }
670 }
672 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
673 * The start_data_singleton makes the structure and puts its addr into the
674 * location.
675 */
676 void
677 VSs__start_data_singleton( VSsSingleton **singletonAddr, SlaveVP *animSlv )
678 {
679 VSsSemReq reqData;
681 if( *singletonAddr && (*singletonAddr)->hasFinished )
682 goto JmpToEndSingleton;
684 reqData.reqType = singleton_data_start;
685 reqData.singletonPtrAddr = singletonAddr;
687 VMS_WL__send_sem_request( &reqData, animSlv );
688 if( animSlv->dataRetFromReq ) //either 0 or end singleton's return addr
689 { //Assembly code changes the return addr on the stack to the one
690 // saved into the singleton by the end-singleton-fn
691 //The return addr is at 0x4(%%ebp)
692 JmpToEndSingleton:
693 asm_write_ret_from_singleton(*singletonAddr);
694 }
695 //now, simply return
696 //will exit either from the start singleton call or the end-singleton call
697 }
699 /*Uses ID as index into array of flags. If flag already set, resumes from
700 * end-label. Else, sets flag and resumes normally.
701 *
702 *Note, this call cannot be inlined because the instr addr at the label
703 * inside is shared by all invocations of a given singleton ID.
704 */
705 void
706 VSs__end_fn_singleton( int32 singletonID, SlaveVP *animSlv )
707 {
708 VSsSemReq reqData;
710 //don't need this addr until after at least one singleton has reached
711 // this function
712 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
713 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
715 reqData.reqType = singleton_fn_end;
716 reqData.singletonID = singletonID;
718 VMS_WL__send_sem_request( &reqData, animSlv );
720 EndSingletonInstrAddr:
721 return;
722 }
724 void
725 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr, SlaveVP *animSlv )
726 {
727 VSsSemReq reqData;
729 //don't need this addr until after singleton struct has reached
730 // this function for first time
731 //do assembly that saves the return addr of this fn call into the
732 // data singleton -- that data-singleton can only be given to exactly
733 // one instance in the code of this function. However, can use this
734 // function in different places for different data-singletons.
735 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
738 asm_save_ret_to_singleton(*singletonPtrAddr);
740 reqData.reqType = singleton_data_end;
741 reqData.singletonPtrAddr = singletonPtrAddr;
743 VMS_WL__send_sem_request( &reqData, animSlv );
744 }
746 /*This executes the function in the masterVP, so it executes in isolation
747 * from any other copies -- only one copy of the function can ever execute
748 * at a time.
749 *
750 *It suspends to the master, and the request handler takes the function
751 * pointer out of the request and calls it, then resumes the VP.
752 *Only very short functions should be called this way -- for longer-running
753 * isolation, use transaction-start and transaction-end, which run the code
754 * between as work-code.
755 */
756 void
757 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
758 void *data, SlaveVP *animSlv )
759 {
760 VSsSemReq reqData;
762 //
763 reqData.reqType = atomic;
764 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
765 reqData.dataForFn = data;
767 VMS_WL__send_sem_request( &reqData, animSlv );
768 }
771 /*This suspends to the master.
772 *First, it looks at the VP's data, to see the highest transactionID that VP
773 * already has entered. If the current ID is not larger, it throws an
774 * exception stating a bug in the code. Otherwise it puts the current ID
775 * there, and adds the ID to a linked list of IDs entered -- the list is
776 * used to check that exits are properly ordered.
777 *Next it is uses transactionID as index into an array of transaction
778 * structures.
779 *If the "VP_currently_executing" field is non-null, then put requesting VP
780 * into queue in the struct. (At some point a holder will request
781 * end-transaction, which will take this VP from the queue and resume it.)
782 *If NULL, then write requesting into the field and resume.
783 */
784 void
785 VSs__start_transaction( int32 transactionID, SlaveVP *animSlv )
786 {
787 VSsSemReq reqData;
789 //
790 reqData.callingSlv = animSlv;
791 reqData.reqType = trans_start;
792 reqData.transID = transactionID;
794 VMS_WL__send_sem_request( &reqData, animSlv );
795 }
797 /*This suspends to the master, then uses transactionID as index into an
798 * array of transaction structures.
799 *It looks at VP_currently_executing to be sure it's same as requesting VP.
800 * If different, throws an exception, stating there's a bug in the code.
801 *Next it looks at the queue in the structure.
802 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
803 *If something in, gets it, sets VP_currently_executing to that VP, then
804 * resumes both.
805 */
806 void
807 VSs__end_transaction( int32 transactionID, SlaveVP *animSlv )
808 {
809 VSsSemReq reqData;
811 //
812 reqData.callingSlv = animSlv;
813 reqData.reqType = trans_end;
814 reqData.transID = transactionID;
816 VMS_WL__send_sem_request( &reqData, animSlv );
817 }
819 //======================== Internal ==================================
820 /*
821 */
822 SlaveVP *
823 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
824 SlaveVP *creatingSlv )
825 { VSsSemReq reqData;
827 //the semantic request data is on the stack and disappears when this
828 // call returns -- it's guaranteed to remain in the VP's stack for as
829 // long as the VP is suspended.
830 reqData.reqType = 0; //know type because in a VMS create req
831 reqData.coreToAssignOnto = -1; //means round-robin assign
832 reqData.fnPtr = fnPtr;
833 reqData.initData = initData;
834 reqData.callingSlv = creatingSlv;
836 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
838 return creatingSlv->dataRetFromReq;
839 }
841 SlaveVP *
842 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
843 SlaveVP *creatingSlv, int32 coreToAssignOnto )
844 { VSsSemReq reqData;
846 //the semantic request data is on the stack and disappears when this
847 // call returns -- it's guaranteed to remain in the VP's stack for as
848 // long as the VP is suspended.
849 reqData.reqType = create_slave_w_aff; //not used, May 2012
850 reqData.coreToAssignOnto = coreToAssignOnto;
851 reqData.fnPtr = fnPtr;
852 reqData.initData = initData;
853 reqData.callingSlv = creatingSlv;
855 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
857 return creatingSlv->dataRetFromReq;
858 }