view VSs.c @ 11:ed268fc7376a

minor fixes
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Wed, 15 Aug 2012 17:05:02 +0200
parents 832bc715fbf2
children f56e3beac86b
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->parentTaskStub = 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 =
217 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
218 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
220 currTaskSlv = VMS_int__create_slaveVP( &idle_fn, NULL );
221 currTaskSlv->coreAnimatedBy = coreNum;
222 currTaskSlv->animSlotAssignedTo =
223 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
224 semanticEnv->currTaskSlvs[coreNum][slotNum] = currTaskSlv;
225 }
226 }
228 //create the ready queues, hash tables used for matching and so forth
229 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
230 semanticEnv->freeExtraTaskSlvQ = makeVMSQ();
231 semanticEnv->taskReadyQ = makeVMSQ();
233 semanticEnv->argPtrHashTbl = makeHashTable32( 16, &VMS_int__free );
234 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
236 semanticEnv->nextCoreToGetNewSlv = 0;
239 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
240 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
241 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
242 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
243 {
244 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
245 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
246 semanticEnv->fnSingletons[i].hasFinished = FALSE;
247 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
248 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
249 }
251 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
252 semanticEnv->numLiveThreadSlvs = 1; //must be last, count the seed
254 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
255 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
256 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
257 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
258 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
259 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
261 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
262 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
263 #endif
264 }
267 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
268 */
269 void
270 VSs__cleanup_after_shutdown()
271 { VSsSemEnv *semanticEnv;
273 semanticEnv = _VMSMasterEnv->semanticEnv;
275 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
276 //UCC
277 FILE* output;
278 int n;
279 char filename[255];
280 for(n=0;n<255;n++)
281 {
282 sprintf(filename, "./counters/UCC.%d",n);
283 output = fopen(filename,"r");
284 if(output)
285 {
286 fclose(output);
287 }else{
288 break;
289 }
290 }
291 if(n<255){
292 printf("Saving UCC to File: %s ...\n", filename);
293 output = fopen(filename,"w+");
294 if(output!=NULL){
295 set_dependency_file(output);
296 //fprintf(output,"digraph Dependencies {\n");
297 //set_dot_file(output);
298 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
299 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
300 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
301 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
302 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
303 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
304 //fprintf(output,"}\n");
305 fflush(output);
307 } else
308 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
309 } else {
310 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
311 }
312 //Loop Graph
313 for(n=0;n<255;n++)
314 {
315 sprintf(filename, "./counters/LoopGraph.%d",n);
316 output = fopen(filename,"r");
317 if(output)
318 {
319 fclose(output);
320 }else{
321 break;
322 }
323 }
324 if(n<255){
325 printf("Saving LoopGraph to File: %s ...\n", filename);
326 output = fopen(filename,"w+");
327 if(output!=NULL){
328 set_dependency_file(output);
329 //fprintf(output,"digraph Dependencies {\n");
330 //set_dot_file(output);
331 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
332 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
333 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
334 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
335 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
336 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
337 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
338 //fprintf(output,"}\n");
339 fflush(output);
341 } else
342 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
343 } else {
344 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
345 }
348 freeListOfArrays(semanticEnv->unitList);
349 freeListOfArrays(semanticEnv->commDependenciesList);
350 freeListOfArrays(semanticEnv->ctlDependenciesList);
351 freeListOfArrays(semanticEnv->dynDependenciesList);
353 #endif
354 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
355 for(n=0;n<255;n++)
356 {
357 sprintf(filename, "./counters/Counters.%d.csv",n);
358 output = fopen(filename,"r");
359 if(output)
360 {
361 fclose(output);
362 }else{
363 break;
364 }
365 }
366 if(n<255){
367 printf("Saving Counter measurements to File: %s ...\n", filename);
368 output = fopen(filename,"w+");
369 if(output!=NULL){
370 set_counter_file(output);
371 int i;
372 for(i=0;i<NUM_CORES;i++){
373 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
374 fflush(output);
375 }
377 } else
378 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
379 } else {
380 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
381 }
383 #endif
384 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
385 * nothing to do here
388 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
389 {
390 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
391 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
392 }
393 VMS_int__free( semanticEnv->readyVPQs );
395 freeHashTable( semanticEnv->commHashTbl );
396 VMS_int__free( _VMSMasterEnv->semanticEnv );
397 */
398 VMS_SS__cleanup_at_end_of_shutdown();
399 }
402 //===========================================================================
404 SlaveVP *
405 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
406 SlaveVP *creatingThd )
407 { VSsSemReq reqData;
409 //the semantic request data is on the stack and disappears when this
410 // call returns -- it's guaranteed to remain in the VP's stack for as
411 // long as the VP is suspended.
412 reqData.reqType = 0; //know type because in a VMS create req
413 reqData.fnPtr = fnPtr;
414 reqData.initData = initData;
415 reqData.callingSlv = creatingThd;
417 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
419 return creatingThd->dataRetFromReq;
420 }
422 /*This is always the last thing done in the code animated by a thread.
423 * Normally, this would be the last line of the thread's top level function.
424 * But, if the thread exits from any point, it has to do so by calling
425 * this.
426 *
427 *This must update the count of active sub-tasks (sub-threads) of parents,
428 * and the semantic data and task stub must stay.
429 */
430 void
431 VSs__end_thread( SlaveVP *thdToEnd )
432 { VSsSemData *semData;
435 //Update the count of live sub-tasks in parent. If parent was a
436 // thread and has already ended, then if this was the last sub-task,
437 // free the semantic data and task stub of the parent.
439 VMS_WL__send_dissipate_req( thdToEnd );
440 }
443 //===========================================================================
446 //======================= task submit and end ==============================
447 /*
448 */
449 void
450 VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
451 { VSsSemReq reqData;
453 reqData.reqType = submit_task;
455 reqData.taskType = taskType;
456 reqData.args = args;
457 reqData.callingSlv = animSlv;
459 reqData.taskID = NULL;
461 VMS_WL__send_sem_request( &reqData, animSlv );
462 }
464 inline int32 *
465 VSs__create_taskID_of_size( int32 numInts, SlaveVP *animSlv )
466 { int32 *taskID;
468 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
469 taskID[0] = numInts;
470 return taskID;
471 }
473 void
474 VSs__submit_task_with_ID( VSsTaskType *taskType, void *args, int32 *taskID,
475 SlaveVP *animSlv)
476 { VSsSemReq reqData;
478 reqData.reqType = submit_task;
480 reqData.taskType = taskType;
481 reqData.args = args;
482 reqData.taskID = taskID;
483 reqData.callingSlv = animSlv;
485 VMS_WL__send_sem_request( &reqData, animSlv );
486 }
489 /*This call is the last to happen in every task. It causes the slave to
490 * suspend and get the next task out of the task-queue. Notice there is no
491 * assigner here.. only one slave, no slave ReadyQ, and so on..
492 *Can either make the assigner take the next task out of the taskQ, or can
493 * leave all as it is, and make task-end take the next task.
494 *Note: this fits the case in the new VMS for no-context tasks, so will use
495 * the built-in taskQ of new VMS, and should be local and much faster.
496 *
497 *The task-stub is saved in the animSlv, so the request handler will get it
498 * from there, along with the task-type which has arg types, and so on..
499 *
500 * NOTE: if want, don't need to send the animating SlaveVP around..
501 * instead, can make a single slave per core, and coreCtrlr looks up the
502 * slave from having the core number.
503 *
504 *But, to stay compatible with all the other VMS languages, leave it in..
505 */
506 void
507 VSs__end_task( SlaveVP *animSlv )
508 { VSsSemReq reqData;
510 reqData.reqType = end_task;
511 reqData.callingSlv = animSlv;
513 VMS_WL__send_sem_request( &reqData, animSlv );
514 }
517 void
518 VSs__taskwait(SlaveVP *animSlv)
519 {
520 VSsSemReq reqData;
522 reqData.reqType = taskwait;
523 reqData.callingSlv = animSlv;
525 VMS_WL__send_sem_request( &reqData, animSlv );
526 }
530 //========================== send and receive ============================
531 //
533 inline int32 *
534 VSs__give_self_taskID( SlaveVP *animSlv )
535 {
536 return ((VSsSemData*)animSlv->semanticData)->taskStub->taskID;
537 }
539 //================================ send ===================================
541 void
542 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID,
543 SlaveVP *senderSlv )
544 { VSsSemReq reqData;
546 reqData.reqType = send_type_to;
548 reqData.msg = msg;
549 reqData.msgType = type;
550 reqData.receiverID = receiverID;
551 reqData.senderSlv = senderSlv;
553 reqData.nextReqInHashEntry = NULL;
555 VMS_WL__send_sem_request( &reqData, senderSlv );
557 //When come back from suspend, no longer own data reachable from msg
558 }
560 void
561 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID, SlaveVP *senderSlv )
562 { VSsSemReq reqData;
564 reqData.reqType = send_from_to;
566 reqData.msg = msg;
567 reqData.senderID = senderID;
568 reqData.receiverID = receiverID;
569 reqData.senderSlv = senderSlv;
571 reqData.nextReqInHashEntry = NULL;
573 VMS_WL__send_sem_request( &reqData, senderSlv );
574 }
577 //================================ receive ================================
579 /*The "type" version of send and receive creates a many-to-one relationship.
580 * The sender is anonymous, and many sends can stack up, waiting to be
581 * received. The same receiver can also have send from-to's
582 * waiting for it, and those will be kept separate from the "type"
583 * messages.
584 */
585 void *
586 VSs__receive_type_to( const int32 type, int32* receiverID, SlaveVP *receiverSlv )
587 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
588 VSsSemReq reqData;
590 reqData.reqType = receive_type_to;
592 reqData.msgType = type;
593 reqData.receiverID = receiverID;
594 reqData.receiverSlv = receiverSlv;
596 reqData.nextReqInHashEntry = NULL;
598 VMS_WL__send_sem_request( &reqData, receiverSlv );
600 return receiverSlv->dataRetFromReq;
601 }
605 /*Call this at the point a receiving task wants in-coming data.
606 * Use this from-to form when know senderID -- it makes a direct channel
607 * between sender and receiver.
608 */
609 void *
610 VSs__receive_from_to( int32 *senderID, int32 *receiverID, SlaveVP *receiverSlv )
611 {
612 VSsSemReq reqData;
614 reqData.reqType = receive_from_to;
616 reqData.senderID = senderID;
617 reqData.receiverID = receiverID;
618 reqData.receiverSlv = receiverSlv;
620 reqData.nextReqInHashEntry = NULL;
621 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
623 VMS_WL__send_sem_request( &reqData, receiverSlv );
625 return receiverSlv->dataRetFromReq;
626 }
631 //==========================================================================
632 //
633 /*A function singleton is a function whose body executes exactly once, on a
634 * single core, no matter how many times the fuction is called and no
635 * matter how many cores or the timing of cores calling it.
636 *
637 *A data singleton is a ticket attached to data. That ticket can be used
638 * to get the data through the function exactly once, no matter how many
639 * times the data is given to the function, and no matter the timing of
640 * trying to get the data through from different cores.
641 */
643 /*asm function declarations*/
644 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
645 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
647 /*Fn singleton uses ID as index into array of singleton structs held in the
648 * semantic environment.
649 */
650 void
651 VSs__start_fn_singleton( int32 singletonID, SlaveVP *animSlv )
652 {
653 VSsSemReq reqData;
655 //
656 reqData.reqType = singleton_fn_start;
657 reqData.singletonID = singletonID;
659 VMS_WL__send_sem_request( &reqData, animSlv );
660 if( animSlv->dataRetFromReq ) //will be 0 or addr of label in end singleton
661 {
662 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
663 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
664 }
665 }
667 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
668 * The start_data_singleton makes the structure and puts its addr into the
669 * location.
670 */
671 void
672 VSs__start_data_singleton( VSsSingleton **singletonAddr, SlaveVP *animSlv )
673 {
674 VSsSemReq reqData;
676 if( *singletonAddr && (*singletonAddr)->hasFinished )
677 goto JmpToEndSingleton;
679 reqData.reqType = singleton_data_start;
680 reqData.singletonPtrAddr = singletonAddr;
682 VMS_WL__send_sem_request( &reqData, animSlv );
683 if( animSlv->dataRetFromReq ) //either 0 or end singleton's return addr
684 { //Assembly code changes the return addr on the stack to the one
685 // saved into the singleton by the end-singleton-fn
686 //The return addr is at 0x4(%%ebp)
687 JmpToEndSingleton:
688 asm_write_ret_from_singleton(*singletonAddr);
689 }
690 //now, simply return
691 //will exit either from the start singleton call or the end-singleton call
692 }
694 /*Uses ID as index into array of flags. If flag already set, resumes from
695 * end-label. Else, sets flag and resumes normally.
696 *
697 *Note, this call cannot be inlined because the instr addr at the label
698 * inside is shared by all invocations of a given singleton ID.
699 */
700 void
701 VSs__end_fn_singleton( int32 singletonID, SlaveVP *animSlv )
702 {
703 VSsSemReq reqData;
705 //don't need this addr until after at least one singleton has reached
706 // this function
707 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
708 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
710 reqData.reqType = singleton_fn_end;
711 reqData.singletonID = singletonID;
713 VMS_WL__send_sem_request( &reqData, animSlv );
715 EndSingletonInstrAddr:
716 return;
717 }
719 void
720 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr, SlaveVP *animSlv )
721 {
722 VSsSemReq reqData;
724 //don't need this addr until after singleton struct has reached
725 // this function for first time
726 //do assembly that saves the return addr of this fn call into the
727 // data singleton -- that data-singleton can only be given to exactly
728 // one instance in the code of this function. However, can use this
729 // function in different places for different data-singletons.
730 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
733 asm_save_ret_to_singleton(*singletonPtrAddr);
735 reqData.reqType = singleton_data_end;
736 reqData.singletonPtrAddr = singletonPtrAddr;
738 VMS_WL__send_sem_request( &reqData, animSlv );
739 }
741 /*This executes the function in the masterVP, so it executes in isolation
742 * from any other copies -- only one copy of the function can ever execute
743 * at a time.
744 *
745 *It suspends to the master, and the request handler takes the function
746 * pointer out of the request and calls it, then resumes the VP.
747 *Only very short functions should be called this way -- for longer-running
748 * isolation, use transaction-start and transaction-end, which run the code
749 * between as work-code.
750 */
751 void
752 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
753 void *data, SlaveVP *animSlv )
754 {
755 VSsSemReq reqData;
757 //
758 reqData.reqType = atomic;
759 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
760 reqData.dataForFn = data;
762 VMS_WL__send_sem_request( &reqData, animSlv );
763 }
766 /*This suspends to the master.
767 *First, it looks at the VP's data, to see the highest transactionID that VP
768 * already has entered. If the current ID is not larger, it throws an
769 * exception stating a bug in the code. Otherwise it puts the current ID
770 * there, and adds the ID to a linked list of IDs entered -- the list is
771 * used to check that exits are properly ordered.
772 *Next it is uses transactionID as index into an array of transaction
773 * structures.
774 *If the "VP_currently_executing" field is non-null, then put requesting VP
775 * into queue in the struct. (At some point a holder will request
776 * end-transaction, which will take this VP from the queue and resume it.)
777 *If NULL, then write requesting into the field and resume.
778 */
779 void
780 VSs__start_transaction( int32 transactionID, SlaveVP *animSlv )
781 {
782 VSsSemReq reqData;
784 //
785 reqData.callingSlv = animSlv;
786 reqData.reqType = trans_start;
787 reqData.transID = transactionID;
789 VMS_WL__send_sem_request( &reqData, animSlv );
790 }
792 /*This suspends to the master, then uses transactionID as index into an
793 * array of transaction structures.
794 *It looks at VP_currently_executing to be sure it's same as requesting VP.
795 * If different, throws an exception, stating there's a bug in the code.
796 *Next it looks at the queue in the structure.
797 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
798 *If something in, gets it, sets VP_currently_executing to that VP, then
799 * resumes both.
800 */
801 void
802 VSs__end_transaction( int32 transactionID, SlaveVP *animSlv )
803 {
804 VSsSemReq reqData;
806 //
807 reqData.callingSlv = animSlv;
808 reqData.reqType = trans_end;
809 reqData.transID = transactionID;
811 VMS_WL__send_sem_request( &reqData, animSlv );
812 }
814 //======================== Internal ==================================
815 /*
816 */
817 SlaveVP *
818 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
819 SlaveVP *creatingSlv )
820 { VSsSemReq reqData;
822 //the semantic request data is on the stack and disappears when this
823 // call returns -- it's guaranteed to remain in the VP's stack for as
824 // long as the VP is suspended.
825 reqData.reqType = 0; //know type because in a VMS create req
826 reqData.coreToAssignOnto = -1; //means round-robin assign
827 reqData.fnPtr = fnPtr;
828 reqData.initData = initData;
829 reqData.callingSlv = creatingSlv;
831 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
833 return creatingSlv->dataRetFromReq;
834 }
836 SlaveVP *
837 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
838 SlaveVP *creatingSlv, int32 coreToAssignOnto )
839 { VSsSemReq reqData;
841 //the semantic request data is on the stack and disappears when this
842 // call returns -- it's guaranteed to remain in the VP's stack for as
843 // long as the VP is suspended.
844 reqData.reqType = create_slave_w_aff; //not used, May 2012
845 reqData.coreToAssignOnto = coreToAssignOnto;
846 reqData.fnPtr = fnPtr;
847 reqData.initData = initData;
848 reqData.callingSlv = creatingSlv;
850 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
852 return creatingSlv->dataRetFromReq;
853 }