view VSs.c @ 12:f56e3beac86b

wasn't a double free, some tasks have 0 args -> ptrEntries=NULL
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Mon, 20 Aug 2012 13:42:19 +0200
parents ed268fc7376a b13fbd445e0a
children 2bf83f932705
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;
188 VSsSemData *semData;
190 //Hook up the semantic layer's plug-ins to the Master virt procr
191 _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
192 _VMSMasterEnv->slaveAssigner = &VSs__assign_slaveVP_to_slot;
193 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
194 _VMSMasterEnv->counterHandler = &VSs__counter_handler;
195 #endif
197 //create the semantic layer's environment (all its data) and add to
198 // the master environment
199 semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
200 _VMSMasterEnv->semanticEnv = semanticEnv;
202 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
203 VSs__init_counter_data_structs();
204 #endif
206 semanticEnv->shutdownInitiated = FALSE;
207 semanticEnv->coreIsDone = VMS_int__malloc( NUM_CORES * sizeof( bool32 ) );
208 //For each animation slot, there is an idle slave, and an initial
209 // slave assigned as the current-task-slave. Create them here.
210 SlaveVP *idleSlv, *slotTaskSlv;
211 for( coreNum = 0; coreNum < NUM_CORES; coreNum++ )
212 { semanticEnv->coreIsDone[coreNum] = FALSE; //use during shutdown
214 for( slotNum = 0; slotNum < NUM_ANIM_SLOTS; ++slotNum )
215 { idleSlv = VSs__create_slave_helper( &idle_fn, NULL, semanticEnv, 0);
216 idleSlv->coreAnimatedBy = coreNum;
217 idleSlv->animSlotAssignedTo =
218 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
219 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
221 slotTaskSlv = VSs__create_slave_helper( &idle_fn, NULL, semanticEnv, 0);
222 slotTaskSlv->coreAnimatedBy = coreNum;
223 slotTaskSlv->animSlotAssignedTo =
224 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
226 semData = slotTaskSlv->semanticData;
227 semData->needsTaskAssigned = TRUE;
228 semData->slaveType = SlotTaskSlv;
229 semanticEnv->slotTaskSlvs[coreNum][slotNum] = slotTaskSlv;
230 }
231 }
233 //create the ready queues, hash tables used for matching and so forth
234 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
235 semanticEnv->freeExtraTaskSlvQ = makeVMSQ();
236 semanticEnv->taskReadyQ = makeVMSQ();
238 semanticEnv->argPtrHashTbl = makeHashTable32( 16, &VMS_int__free );
239 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
241 semanticEnv->nextCoreToGetNewSlv = 0;
244 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
245 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
246 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
247 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
248 {
249 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
250 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
251 semanticEnv->fnSingletons[i].hasFinished = FALSE;
252 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
253 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
254 }
256 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
257 semanticEnv->numLiveThreadSlvs = 1; //must be last, count the seed
259 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
260 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
261 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
262 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
263 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
264 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
266 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
267 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
268 #endif
269 }
272 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
273 */
274 void
275 VSs__cleanup_after_shutdown()
276 { VSsSemEnv *semanticEnv;
278 semanticEnv = _VMSMasterEnv->semanticEnv;
280 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
281 //UCC
282 FILE* output;
283 int n;
284 char filename[255];
285 for(n=0;n<255;n++)
286 {
287 sprintf(filename, "./counters/UCC.%d",n);
288 output = fopen(filename,"r");
289 if(output)
290 {
291 fclose(output);
292 }else{
293 break;
294 }
295 }
296 if(n<255){
297 printf("Saving UCC to File: %s ...\n", filename);
298 output = fopen(filename,"w+");
299 if(output!=NULL){
300 set_dependency_file(output);
301 //fprintf(output,"digraph Dependencies {\n");
302 //set_dot_file(output);
303 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
304 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
305 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
306 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
307 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
308 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
309 //fprintf(output,"}\n");
310 fflush(output);
312 } else
313 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
314 } else {
315 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
316 }
317 //Loop Graph
318 for(n=0;n<255;n++)
319 {
320 sprintf(filename, "./counters/LoopGraph.%d",n);
321 output = fopen(filename,"r");
322 if(output)
323 {
324 fclose(output);
325 }else{
326 break;
327 }
328 }
329 if(n<255){
330 printf("Saving LoopGraph to File: %s ...\n", filename);
331 output = fopen(filename,"w+");
332 if(output!=NULL){
333 set_dependency_file(output);
334 //fprintf(output,"digraph Dependencies {\n");
335 //set_dot_file(output);
336 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
337 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
338 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
339 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
340 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
341 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
342 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
343 //fprintf(output,"}\n");
344 fflush(output);
346 } else
347 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
348 } else {
349 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
350 }
353 freeListOfArrays(semanticEnv->unitList);
354 freeListOfArrays(semanticEnv->commDependenciesList);
355 freeListOfArrays(semanticEnv->ctlDependenciesList);
356 freeListOfArrays(semanticEnv->dynDependenciesList);
358 #endif
359 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
360 for(n=0;n<255;n++)
361 {
362 sprintf(filename, "./counters/Counters.%d.csv",n);
363 output = fopen(filename,"r");
364 if(output)
365 {
366 fclose(output);
367 }else{
368 break;
369 }
370 }
371 if(n<255){
372 printf("Saving Counter measurements to File: %s ...\n", filename);
373 output = fopen(filename,"w+");
374 if(output!=NULL){
375 set_counter_file(output);
376 int i;
377 for(i=0;i<NUM_CORES;i++){
378 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
379 fflush(output);
380 }
382 } else
383 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
384 } else {
385 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
386 }
388 #endif
389 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
390 * nothing to do here
393 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
394 {
395 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
396 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
397 }
398 VMS_int__free( semanticEnv->readyVPQs );
400 freeHashTable( semanticEnv->commHashTbl );
401 VMS_int__free( _VMSMasterEnv->semanticEnv );
402 */
403 VMS_SS__cleanup_at_end_of_shutdown();
404 }
407 //===========================================================================
409 SlaveVP *
410 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
411 SlaveVP *creatingThd )
412 { VSsSemReq reqData;
414 //the semantic request data is on the stack and disappears when this
415 // call returns -- it's guaranteed to remain in the VP's stack for as
416 // long as the VP is suspended.
417 reqData.reqType = 0; //know type because in a VMS create req
418 reqData.fnPtr = fnPtr;
419 reqData.initData = initData;
420 reqData.callingSlv = creatingThd;
422 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
424 return creatingThd->dataRetFromReq;
425 }
427 /*This is always the last thing done in the code animated by a thread VP.
428 * Normally, this would be the last line of the thread's top level function.
429 * But, if the thread exits from any point, it has to do so by calling
430 * this.
431 *
432 *It simply sends a dissipate request, which handles all the state cleanup.
433 */
434 void
435 VSs__end_thread( SlaveVP *thdToEnd )
436 { VSsSemData *semData;
438 VMS_WL__send_dissipate_req( thdToEnd );
439 }
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 }