view VSs.c @ 7:3999b8429ddd

Not working -- check point to share changes with Nina
author Sean Halle <seanhalle@yahoo.com>
date Wed, 01 Aug 2012 03:16:27 -0700
parents 1780f6b00e3d
children eb3d77ca9f59
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 *explPrTaskStub;
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 an explicit processor, so make one of the special
91 // task stubs for explicit processors, and attach it to the slave
92 explPrTaskStub = create_expl_proc_task_stub( initData );
94 semData = (VSsSemData *)seedSlv->semanticData;
95 //seedVP already has a permanent task
96 semData->needsTaskAssigned = FALSE;
97 semData->taskStub = explPrTaskStub;
99 resume_slaveVP( seedSlv, semEnv ); //returns right away, just queues Slv
101 VMS_SS__start_the_work_then_wait_until_done(); //normal multi-thd
103 VSs__cleanup_after_shutdown();
104 }
107 int32
108 VSs__giveMinWorkUnitCycles( float32 percentOverhead )
109 {
110 return MIN_WORK_UNIT_CYCLES;
111 }
113 int32
114 VSs__giveIdealNumWorkUnits()
115 {
116 return NUM_ANIM_SLOTS * NUM_CORES;
117 }
119 int32
120 VSs__give_number_of_cores_to_schedule_onto()
121 {
122 return NUM_CORES;
123 }
125 /*For now, use TSC -- later, make these two macros with assembly that first
126 * saves jump point, and second jumps back several times to get reliable time
127 */
128 void
129 VSs__start_primitive()
130 { saveLowTimeStampCountInto( ((VSsSemEnv *)(_VMSMasterEnv->semanticEnv))->
131 primitiveStartTime );
132 }
134 /*Just quick and dirty for now -- make reliable later
135 * will want this to jump back several times -- to be sure cache is warm
136 * because don't want comm time included in calc-time measurement -- and
137 * also to throw out any "weird" values due to OS interrupt or TSC rollover
138 */
139 int32
140 VSs__end_primitive_and_give_cycles()
141 { int32 endTime, startTime;
142 //TODO: fix by repeating time-measurement
143 saveLowTimeStampCountInto( endTime );
144 startTime =((VSsSemEnv*)(_VMSMasterEnv->semanticEnv))->primitiveStartTime;
145 return (endTime - startTime);
146 }
148 //===========================================================================
150 /*Initializes all the data-structures for a VSs system -- but doesn't
151 * start it running yet!
152 *
153 *This runs in the main thread -- before VMS starts up
154 *
155 *This sets up the semantic layer over the VMS system
156 *
157 *First, calls VMS_Setup, then creates own environment, making it ready
158 * for creating the seed processor and then starting the work.
159 */
160 void
161 VSs__init()
162 {
163 VMS_SS__init();
164 //masterEnv, a global var, now is partially set up by init_VMS
165 // after this, have VMS_int__malloc and VMS_int__free available
167 VSs__init_Helper();
168 }
171 void idle_fn(void* data, SlaveVP *animatingSlv){
172 while(1){
173 VMS_int__suspend_slaveVP_and_send_req(animatingSlv);
174 }
175 }
177 void
178 VSs__init_Helper()
179 { VSsSemEnv *semanticEnv;
180 int32 i, coreNum, slotNum;
182 //Hook up the semantic layer's plug-ins to the Master virt procr
183 _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
184 _VMSMasterEnv->slaveAssigner = &VSs__assign_slaveVP_to_slot;
185 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
186 _VMSMasterEnv->counterHandler = &VSs__counter_handler;
187 #endif
189 //create the semantic layer's environment (all its data) and add to
190 // the master environment
191 semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
192 _VMSMasterEnv->semanticEnv = semanticEnv;
194 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
195 VSs__init_counter_data_structs();
196 #endif
198 semanticEnv->shutdownInitiated = FALSE;
199 semanticEnv->coreIsDone = VMS_int__malloc( NUM_CORES * sizeof( bool32 ) );
200 //For each animation slot, there is an idle slave, and an initial
201 // slave assigned as the current-task-slave. Create them here.
202 SlaveVP *idleSlv, *currTaskSlv;
203 for( coreNum = 0; coreNum < NUM_CORES; coreNum++ )
204 { semanticEnv->coreIsDone[coreNum] = FALSE; //use during shutdown
206 for( slotNum = 0; slotNum < NUM_ANIM_SLOTS; ++slotNum )
207 { idleSlv = VMS_int__create_slaveVP(&idle_fn,NULL);
208 idleSlv->coreAnimatedBy = coreNum;
209 idleSlv->animSlotAssignedTo = slotNum;
210 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
212 currTaskSlv = VMS_int__create_slaveVP( &idle_fn, NULL );
213 currTaskSlv->coreAnimatedBy = coreNum;
214 currTaskSlv->animSlotAssignedTo = slotNum;
215 semanticEnv->currTaskSlvs[coreNum][slotNum] = currTaskSlv;
216 }
217 }
219 //create the ready queues, hash tables used for matching and so forth
220 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
221 semanticEnv->extraTaskSlvQ = makeVMSQ();
222 semanticEnv->taskReadyQ = makeVMSQ();
224 semanticEnv->argPtrHashTbl = makeHashTable32( 16, &VMS_int__free );
225 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
227 semanticEnv->nextCoreToGetNewSlv = 0;
230 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
231 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
232 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
233 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
234 {
235 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
236 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
237 semanticEnv->fnSingletons[i].hasFinished = FALSE;
238 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
239 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
240 }
242 semanticEnv->numAdditionalSlvs = 0; //must be last
244 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
245 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
246 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
247 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
248 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
249 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
251 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
252 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
253 #endif
254 }
257 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
258 */
259 void
260 VSs__cleanup_after_shutdown()
261 { VSsSemEnv *semanticEnv;
263 semanticEnv = _VMSMasterEnv->semanticEnv;
265 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
266 //UCC
267 FILE* output;
268 int n;
269 char filename[255];
270 for(n=0;n<255;n++)
271 {
272 sprintf(filename, "./counters/UCC.%d",n);
273 output = fopen(filename,"r");
274 if(output)
275 {
276 fclose(output);
277 }else{
278 break;
279 }
280 }
281 if(n<255){
282 printf("Saving UCC to File: %s ...\n", filename);
283 output = fopen(filename,"w+");
284 if(output!=NULL){
285 set_dependency_file(output);
286 //fprintf(output,"digraph Dependencies {\n");
287 //set_dot_file(output);
288 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
289 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
290 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
291 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
292 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
293 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
294 //fprintf(output,"}\n");
295 fflush(output);
297 } else
298 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
299 } else {
300 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
301 }
302 //Loop Graph
303 for(n=0;n<255;n++)
304 {
305 sprintf(filename, "./counters/LoopGraph.%d",n);
306 output = fopen(filename,"r");
307 if(output)
308 {
309 fclose(output);
310 }else{
311 break;
312 }
313 }
314 if(n<255){
315 printf("Saving LoopGraph to File: %s ...\n", filename);
316 output = fopen(filename,"w+");
317 if(output!=NULL){
318 set_dependency_file(output);
319 //fprintf(output,"digraph Dependencies {\n");
320 //set_dot_file(output);
321 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
322 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
323 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
324 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
325 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
326 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
327 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
328 //fprintf(output,"}\n");
329 fflush(output);
331 } else
332 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
333 } else {
334 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
335 }
338 freeListOfArrays(semanticEnv->unitList);
339 freeListOfArrays(semanticEnv->commDependenciesList);
340 freeListOfArrays(semanticEnv->ctlDependenciesList);
341 freeListOfArrays(semanticEnv->dynDependenciesList);
343 #endif
344 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
345 for(n=0;n<255;n++)
346 {
347 sprintf(filename, "./counters/Counters.%d.csv",n);
348 output = fopen(filename,"r");
349 if(output)
350 {
351 fclose(output);
352 }else{
353 break;
354 }
355 }
356 if(n<255){
357 printf("Saving Counter measurements to File: %s ...\n", filename);
358 output = fopen(filename,"w+");
359 if(output!=NULL){
360 set_counter_file(output);
361 int i;
362 for(i=0;i<NUM_CORES;i++){
363 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
364 fflush(output);
365 }
367 } else
368 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
369 } else {
370 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
371 }
373 #endif
374 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
375 * nothing to do here
378 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
379 {
380 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
381 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
382 }
383 VMS_int__free( semanticEnv->readyVPQs );
385 freeHashTable( semanticEnv->commHashTbl );
386 VMS_int__free( _VMSMasterEnv->semanticEnv );
387 */
388 VMS_SS__cleanup_at_end_of_shutdown();
389 }
392 //===========================================================================
394 SlaveVP *
395 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
396 SlaveVP *creatingThd )
397 { VSsSemReq reqData;
399 //the semantic request data is on the stack and disappears when this
400 // call returns -- it's guaranteed to remain in the VP's stack for as
401 // long as the VP is suspended.
402 reqData.reqType = 0; //know type because in a VMS create req
403 reqData.fnPtr = fnPtr;
404 reqData.initData = initData;
405 reqData.callingSlv = creatingThd;
407 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
409 return creatingThd->dataRetFromReq;
410 }
412 /*This is always the last thing done in the code animated by a thread.
413 * Normally, this would be the last line of the thread's top level function.
414 * But, if the thread exits from any point, it has to do so by calling
415 * this.
416 *
417 *This must update the count of active sub-tasks (sub-threads) of parents,
418 * and the semantic data and task stub must stay
419 */
420 void
421 VSs__end_thread( SlaveVP *thdToEnd )
422 {
423 //check whether all sub-tasks have ended.. if not, don't free the
424 // semantic data nor task stub of this thread.
425 check_sub_tasks();
427 //Update the count of live sub-tasks in parent. If parent was a
428 // thread and has already ended, then if this was the last sub-task,
429 // free the semantic data and task stub of the parent.
430 VMS_WL__send_dissipate_req( thdToEnd );
431 }
434 //===========================================================================
437 //======================= task submit and end ==============================
438 /*
439 */
440 void
441 VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
442 { VSsSemReq reqData;
444 reqData.reqType = submit_task;
446 reqData.taskType = taskType;
447 reqData.args = args;
448 reqData.callingSlv = animSlv;
450 reqData.taskID = NULL;
452 VMS_WL__send_sem_request( &reqData, animSlv );
453 }
455 inline int32 *
456 VSs__create_taskID_of_size( int32 numInts, SlaveVP *animSlv )
457 { int32 *taskID;
459 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
460 taskID[0] = numInts;
461 return taskID;
462 }
464 void
465 VSs__submit_task_with_ID( VSsTaskType *taskType, void *args, int32 *taskID,
466 SlaveVP *animSlv)
467 { VSsSemReq reqData;
469 reqData.reqType = submit_task;
471 reqData.taskType = taskType;
472 reqData.args = args;
473 reqData.taskID = taskID;
474 reqData.callingSlv = animSlv;
476 VMS_WL__send_sem_request( &reqData, animSlv );
477 }
480 /*This call is the last to happen in every task. It causes the slave to
481 * suspend and get the next task out of the task-queue. Notice there is no
482 * assigner here.. only one slave, no slave ReadyQ, and so on..
483 *Can either make the assigner take the next task out of the taskQ, or can
484 * leave all as it is, and make task-end take the next task.
485 *Note: this fits the case in the new VMS for no-context tasks, so will use
486 * the built-in taskQ of new VMS, and should be local and much faster.
487 *
488 *The task-stub is saved in the animSlv, so the request handler will get it
489 * from there, along with the task-type which has arg types, and so on..
490 *
491 * NOTE: if want, don't need to send the animating SlaveVP around..
492 * instead, can make a single slave per core, and coreCtrlr looks up the
493 * slave from having the core number.
494 *
495 *But, to stay compatible with all the other VMS languages, leave it in..
496 */
497 void
498 VSs__end_task( SlaveVP *animSlv )
499 { VSsSemReq reqData;
501 reqData.reqType = end_task;
502 reqData.callingSlv = animSlv;
504 VMS_WL__send_sem_request( &reqData, animSlv );
505 }
508 void
509 VSs__taskwait(SlaveVP *animSlv)
510 {
511 VSsSemReq reqData;
513 reqData.reqType = taskwait;
514 reqData.callingSlv = animSlv;
516 VMS_WL__send_sem_request( &reqData, animSlv );
517 }
521 //========================== send and receive ============================
522 //
524 inline int32 *
525 VSs__give_self_taskID( SlaveVP *animSlv )
526 {
527 return ((VSsSemData*)animSlv->semanticData)->taskStub->taskID;
528 }
530 //================================ send ===================================
532 void
533 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID,
534 SlaveVP *senderSlv )
535 { VSsSemReq reqData;
537 reqData.reqType = send_type_to;
539 reqData.msg = msg;
540 reqData.msgType = type;
541 reqData.receiverID = receiverID;
542 reqData.senderSlv = senderSlv;
544 reqData.nextReqInHashEntry = NULL;
546 VMS_WL__send_sem_request( &reqData, senderSlv );
548 //When come back from suspend, no longer own data reachable from msg
549 }
551 void
552 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID, SlaveVP *senderSlv )
553 { VSsSemReq reqData;
555 reqData.reqType = send_from_to;
557 reqData.msg = msg;
558 reqData.senderID = senderID;
559 reqData.receiverID = receiverID;
560 reqData.senderSlv = senderSlv;
562 reqData.nextReqInHashEntry = NULL;
564 VMS_WL__send_sem_request( &reqData, senderSlv );
565 }
568 //================================ receive ================================
570 /*The "type" version of send and receive creates a many-to-one relationship.
571 * The sender is anonymous, and many sends can stack up, waiting to be
572 * received. The same receiver can also have send from-to's
573 * waiting for it, and those will be kept separate from the "type"
574 * messages.
575 */
576 void *
577 VSs__receive_type_to( const int32 type, int32* receiverID, SlaveVP *receiverSlv )
578 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
579 VSsSemReq reqData;
581 reqData.reqType = receive_type_to;
583 reqData.msgType = type;
584 reqData.receiverID = receiverID;
585 reqData.receiverSlv = receiverSlv;
587 reqData.nextReqInHashEntry = NULL;
589 VMS_WL__send_sem_request( &reqData, receiverSlv );
591 return receiverSlv->dataRetFromReq;
592 }
596 /*Call this at the point a receiving task wants in-coming data.
597 * Use this from-to form when know senderID -- it makes a direct channel
598 * between sender and receiver.
599 */
600 void *
601 VSs__receive_from_to( int32 *senderID, int32 *receiverID, SlaveVP *receiverSlv )
602 {
603 VSsSemReq reqData;
605 reqData.reqType = receive_from_to;
607 reqData.senderID = senderID;
608 reqData.receiverID = receiverID;
609 reqData.receiverSlv = receiverSlv;
611 reqData.nextReqInHashEntry = NULL;
612 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
614 VMS_WL__send_sem_request( &reqData, receiverSlv );
616 return receiverSlv->dataRetFromReq;
617 }
622 //==========================================================================
623 //
624 /*A function singleton is a function whose body executes exactly once, on a
625 * single core, no matter how many times the fuction is called and no
626 * matter how many cores or the timing of cores calling it.
627 *
628 *A data singleton is a ticket attached to data. That ticket can be used
629 * to get the data through the function exactly once, no matter how many
630 * times the data is given to the function, and no matter the timing of
631 * trying to get the data through from different cores.
632 */
634 /*asm function declarations*/
635 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
636 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
638 /*Fn singleton uses ID as index into array of singleton structs held in the
639 * semantic environment.
640 */
641 void
642 VSs__start_fn_singleton( int32 singletonID, SlaveVP *animSlv )
643 {
644 VSsSemReq reqData;
646 //
647 reqData.reqType = singleton_fn_start;
648 reqData.singletonID = singletonID;
650 VMS_WL__send_sem_request( &reqData, animSlv );
651 if( animSlv->dataRetFromReq ) //will be 0 or addr of label in end singleton
652 {
653 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
654 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
655 }
656 }
658 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
659 * The start_data_singleton makes the structure and puts its addr into the
660 * location.
661 */
662 void
663 VSs__start_data_singleton( VSsSingleton **singletonAddr, SlaveVP *animSlv )
664 {
665 VSsSemReq reqData;
667 if( *singletonAddr && (*singletonAddr)->hasFinished )
668 goto JmpToEndSingleton;
670 reqData.reqType = singleton_data_start;
671 reqData.singletonPtrAddr = singletonAddr;
673 VMS_WL__send_sem_request( &reqData, animSlv );
674 if( animSlv->dataRetFromReq ) //either 0 or end singleton's return addr
675 { //Assembly code changes the return addr on the stack to the one
676 // saved into the singleton by the end-singleton-fn
677 //The return addr is at 0x4(%%ebp)
678 JmpToEndSingleton:
679 asm_write_ret_from_singleton(*singletonAddr);
680 }
681 //now, simply return
682 //will exit either from the start singleton call or the end-singleton call
683 }
685 /*Uses ID as index into array of flags. If flag already set, resumes from
686 * end-label. Else, sets flag and resumes normally.
687 *
688 *Note, this call cannot be inlined because the instr addr at the label
689 * inside is shared by all invocations of a given singleton ID.
690 */
691 void
692 VSs__end_fn_singleton( int32 singletonID, SlaveVP *animSlv )
693 {
694 VSsSemReq reqData;
696 //don't need this addr until after at least one singleton has reached
697 // this function
698 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
699 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
701 reqData.reqType = singleton_fn_end;
702 reqData.singletonID = singletonID;
704 VMS_WL__send_sem_request( &reqData, animSlv );
706 EndSingletonInstrAddr:
707 return;
708 }
710 void
711 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr, SlaveVP *animSlv )
712 {
713 VSsSemReq reqData;
715 //don't need this addr until after singleton struct has reached
716 // this function for first time
717 //do assembly that saves the return addr of this fn call into the
718 // data singleton -- that data-singleton can only be given to exactly
719 // one instance in the code of this function. However, can use this
720 // function in different places for different data-singletons.
721 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
724 asm_save_ret_to_singleton(*singletonPtrAddr);
726 reqData.reqType = singleton_data_end;
727 reqData.singletonPtrAddr = singletonPtrAddr;
729 VMS_WL__send_sem_request( &reqData, animSlv );
730 }
732 /*This executes the function in the masterVP, so it executes in isolation
733 * from any other copies -- only one copy of the function can ever execute
734 * at a time.
735 *
736 *It suspends to the master, and the request handler takes the function
737 * pointer out of the request and calls it, then resumes the VP.
738 *Only very short functions should be called this way -- for longer-running
739 * isolation, use transaction-start and transaction-end, which run the code
740 * between as work-code.
741 */
742 void
743 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
744 void *data, SlaveVP *animSlv )
745 {
746 VSsSemReq reqData;
748 //
749 reqData.reqType = atomic;
750 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
751 reqData.dataForFn = data;
753 VMS_WL__send_sem_request( &reqData, animSlv );
754 }
757 /*This suspends to the master.
758 *First, it looks at the VP's data, to see the highest transactionID that VP
759 * already has entered. If the current ID is not larger, it throws an
760 * exception stating a bug in the code. Otherwise it puts the current ID
761 * there, and adds the ID to a linked list of IDs entered -- the list is
762 * used to check that exits are properly ordered.
763 *Next it is uses transactionID as index into an array of transaction
764 * structures.
765 *If the "VP_currently_executing" field is non-null, then put requesting VP
766 * into queue in the struct. (At some point a holder will request
767 * end-transaction, which will take this VP from the queue and resume it.)
768 *If NULL, then write requesting into the field and resume.
769 */
770 void
771 VSs__start_transaction( int32 transactionID, SlaveVP *animSlv )
772 {
773 VSsSemReq reqData;
775 //
776 reqData.callingSlv = animSlv;
777 reqData.reqType = trans_start;
778 reqData.transID = transactionID;
780 VMS_WL__send_sem_request( &reqData, animSlv );
781 }
783 /*This suspends to the master, then uses transactionID as index into an
784 * array of transaction structures.
785 *It looks at VP_currently_executing to be sure it's same as requesting VP.
786 * If different, throws an exception, stating there's a bug in the code.
787 *Next it looks at the queue in the structure.
788 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
789 *If something in, gets it, sets VP_currently_executing to that VP, then
790 * resumes both.
791 */
792 void
793 VSs__end_transaction( int32 transactionID, SlaveVP *animSlv )
794 {
795 VSsSemReq reqData;
797 //
798 reqData.callingSlv = animSlv;
799 reqData.reqType = trans_end;
800 reqData.transID = transactionID;
802 VMS_WL__send_sem_request( &reqData, animSlv );
803 }
805 //======================== Internal ==================================
806 /*
807 */
808 SlaveVP *
809 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
810 SlaveVP *creatingSlv )
811 { VSsSemReq reqData;
813 //the semantic request data is on the stack and disappears when this
814 // call returns -- it's guaranteed to remain in the VP's stack for as
815 // long as the VP is suspended.
816 reqData.reqType = 0; //know type because in a VMS create req
817 reqData.coreToAssignOnto = -1; //means round-robin assign
818 reqData.fnPtr = fnPtr;
819 reqData.initData = initData;
820 reqData.callingSlv = creatingSlv;
822 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
824 return creatingSlv->dataRetFromReq;
825 }
827 SlaveVP *
828 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
829 SlaveVP *creatingSlv, int32 coreToAssignOnto )
830 { VSsSemReq reqData;
832 //the semantic request data is on the stack and disappears when this
833 // call returns -- it's guaranteed to remain in the VP's stack for as
834 // long as the VP is suspended.
835 reqData.reqType = create_slave_w_aff; //not used, May 2012
836 reqData.coreToAssignOnto = coreToAssignOnto;
837 reqData.fnPtr = fnPtr;
838 reqData.initData = initData;
839 reqData.callingSlv = creatingSlv;
841 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
843 return creatingSlv->dataRetFromReq;
844 }