/*
 * Copyright 2010  OpenSourceCodeStewardshipFoundation
 *
 * Licensed under BSD
 */

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#include "Queue_impl/PrivateQueue.h"
#include "Hash_impl/PrivateHash.h"

#include "VSs.h"
#include "Measurement/VSs_Counter_Recording.h"

//==========================================================================

void
VSs__init();

void
VSs__init_Helper();
//==========================================================================



//===========================================================================


/*These are the library functions *called in the application*
 * 
 *There's a pattern for the outside sequential code to interact with the
 * VMS_HW code.
 *The VMS_HW system is inside a boundary..  every VSs system is in its
 * own directory that contains the functions for each of the processor types.
 * One of the processor types is the "seed" processor that starts the
 * cascade of creating all the processors that do the work.
 *So, in the directory is a file called "EntryPoint.c" that contains the
 * function, named appropriately to the work performed, that the outside
 * sequential code calls.  This function follows a pattern:
 *1) it calls VSs__init()
 *2) it creates the initial data for the seed processor, which is passed
 *    in to the function
 *3) it creates the seed VSs processor, with the data to start it with.
 *4) it calls startVSsThenWaitUntilWorkDone
 *5) it gets the returnValue from the transfer struc and returns that
 *    from the function
 *
 *For now, a new VSs system has to be created via VSs__init every
 * time an entry point function is called -- later, might add letting the
 * VSs system be created once, and let all the entry points just reuse
 * it -- want to be as simple as possible now, and see by using what makes
 * sense for later..
 */



//===========================================================================

/*This is the "border crossing" function -- the thing that crosses from the
 * outside world, into the VMS_HW world.  It initializes and starts up the
 * VMS system, then creates one processor from the specified function and
 * puts it into the readyQ.  From that point, that one function is resp.
 * for creating all the other processors, that then create others, and so
 * forth.
 *When all the processors, including the seed, have dissipated, then this
 * function returns.  The results will have been written by side-effect via
 * pointers read from, or written into initData.
 *
 *NOTE: no Threads should exist in the outside program that might touch
 * any of the data reachable from initData passed in to here
 */
void
VSs__create_seed_slave_and_do_work( TopLevelFnPtr fnPtr, void *initData )
 { VSsSemEnv *semEnv;
   SlaveVP *seedSlv;

   VSs__init();      //normal multi-thd
   
   semEnv = _VMSMasterEnv->semanticEnv;

      //VSs starts with one processor, which is put into initial environ,
      // and which then calls create() to create more, thereby expanding work
   seedSlv = VSs__create_slave_helper( fnPtr, initData,
                                      semEnv, semEnv->nextCoreToGetNewSlv++ );
   
      //seedVP doesn't do tasks
   ((VSsSemData *)seedSlv->semanticData)->needsTaskAssigned = FALSE;

   resume_slaveVP( seedSlv, semEnv );
   
   VMS_SS__start_the_work_then_wait_until_done();      //normal multi-thd

   VSs__cleanup_after_shutdown();
 }


int32
VSs__giveMinWorkUnitCycles( float32 percentOverhead )
 {
   return MIN_WORK_UNIT_CYCLES;
 }

int32
VSs__giveIdealNumWorkUnits()
 {
   return NUM_ANIM_SLOTS * NUM_CORES;
 }

int32
VSs__give_number_of_cores_to_schedule_onto()
 {
   return NUM_CORES;
 }

/*For now, use TSC -- later, make these two macros with assembly that first
 * saves jump point, and second jumps back several times to get reliable time
 */
void
VSs__start_primitive()
 { saveLowTimeStampCountInto( ((VSsSemEnv *)(_VMSMasterEnv->semanticEnv))->
                              primitiveStartTime );
 }

/*Just quick and dirty for now -- make reliable later
 * will want this to jump back several times -- to be sure cache is warm
 * because don't want comm time included in calc-time measurement -- and
 * also to throw out any "weird" values due to OS interrupt or TSC rollover
 */
int32
VSs__end_primitive_and_give_cycles()
 { int32 endTime, startTime;
   //TODO: fix by repeating time-measurement
   saveLowTimeStampCountInto( endTime );
   startTime =((VSsSemEnv*)(_VMSMasterEnv->semanticEnv))->primitiveStartTime;
   return (endTime - startTime);
 }

//===========================================================================

/*Initializes all the data-structures for a VSs system -- but doesn't
 * start it running yet!
 *
 *This runs in the main thread -- before VMS starts up
 * 
 *This sets up the semantic layer over the VMS system
 *
 *First, calls VMS_Setup, then creates own environment, making it ready
 * for creating the seed processor and then starting the work.
 */
void
VSs__init()
 {
   VMS_SS__init();
      //masterEnv, a global var, now is partially set up by init_VMS
      // after this, have VMS_int__malloc and VMS_int__free available

   VSs__init_Helper();
 }


void idle_fn(void* data, SlaveVP *animatingSlv){
    while(1){
        VMS_int__suspend_slaveVP_and_send_req(animatingSlv);
    }
}

void
VSs__init_Helper()
 { VSsSemEnv       *semanticEnv;
   PrivQueueStruc **readyVPQs;
   int              coreIdx, i, j;
 
      //Hook up the semantic layer's plug-ins to the Master virt procr
   _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
   _VMSMasterEnv->slaveAssigner  = &VSs__assign_slaveVP_to_slot;
   #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
   _VMSMasterEnv->counterHandler = &VSs__counter_handler;
   #endif

      //create the semantic layer's environment (all its data) and add to
      // the master environment
   semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
   _VMSMasterEnv->semanticEnv = semanticEnv;
   
   #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
   VSs__init_counter_data_structs();
   #endif

   semanticEnv->shutdownInitiated = FALSE;
   semanticEnv->coreIsDone = VMS_int__malloc( NUM_CORES * sizeof( bool32 ) );
   for( i = 0; i < NUM_CORES; ++i )
    { semanticEnv->coreIsDone[i] = FALSE;
      for( j = 0; j < NUM_ANIM_SLOTS; ++j )
       {
         semanticEnv->idleSlv[i][j] = VMS_int__create_slaveVP(&idle_fn,NULL);
         semanticEnv->idleSlv[i][j]->coreAnimatedBy = i;
       }
    }

   #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
   semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
   semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
   semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
   semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
   semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
   
   semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
   memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
   #endif

      //create the ready queue, hash tables used for matching and so forth
   readyVPQs = VMS_int__malloc( NUM_CORES * sizeof(PrivQueueStruc *) );

   for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
    {
      readyVPQs[ coreIdx ] = makeVMSQ();
    }
   
   semanticEnv->readyVPQs = readyVPQs;
   
   semanticEnv->taskReadyQ = makeVMSQ();
   
   semanticEnv->nextCoreToGetNewSlv = 0;
   semanticEnv->numSlaveVP = 0;
   
   semanticEnv->argPtrHashTbl  = makeHashTable32( 16, &VMS_int__free );
   semanticEnv->commHashTbl    = makeHashTable32( 16, &VMS_int__free );

   //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
   //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
   //semanticEnv->transactionStrucs = makeDynArrayInfo( );
   for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
    {
      semanticEnv->fnSingletons[i].endInstrAddr      = NULL;
      semanticEnv->fnSingletons[i].hasBeenStarted    = FALSE;
      semanticEnv->fnSingletons[i].hasFinished       = FALSE;
      semanticEnv->fnSingletons[i].waitQ             = makeVMSQ();
      semanticEnv->transactionStrucs[i].waitingVPQ   = makeVMSQ();
    }
 }


/*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
 */
void
VSs__cleanup_after_shutdown()
 { VSsSemEnv *semanticEnv;
   
   semanticEnv = _VMSMasterEnv->semanticEnv;

   #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
   //UCC
   FILE* output;
   int n;
   char filename[255];    
    for(n=0;n<255;n++)
    {
        sprintf(filename, "./counters/UCC.%d",n);
        output = fopen(filename,"r");
        if(output)
        {
            fclose(output);
        }else{
            break;
        }
    }
   if(n<255){
    printf("Saving UCC to File: %s ...\n", filename);
    output = fopen(filename,"w+");
    if(output!=NULL){
        set_dependency_file(output);
        //fprintf(output,"digraph Dependencies {\n");
        //set_dot_file(output);
        //FIXME:  first line still depends on counters being enabled, replace w/ unit struct!
        //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
        forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
        forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
        forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
        forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
        //fprintf(output,"}\n");
        fflush(output);

    } else
        printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
   } else {
       printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
   }
   //Loop Graph
   for(n=0;n<255;n++)
    {
        sprintf(filename, "./counters/LoopGraph.%d",n);
        output = fopen(filename,"r");
        if(output)
        {
            fclose(output);
        }else{
            break;
        }
    }
   if(n<255){
    printf("Saving LoopGraph to File: %s ...\n", filename);
    output = fopen(filename,"w+");
    if(output!=NULL){
        set_dependency_file(output);
        //fprintf(output,"digraph Dependencies {\n");
        //set_dot_file(output);
        //FIXME:  first line still depends on counters being enabled, replace w/ unit struct!
        //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
        forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
        forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
        forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
        forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
        forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
        //fprintf(output,"}\n");
        fflush(output);

    } else
        printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
   } else {
       printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
   }
   
   
   freeListOfArrays(semanticEnv->unitList);
   freeListOfArrays(semanticEnv->commDependenciesList);
   freeListOfArrays(semanticEnv->ctlDependenciesList);
   freeListOfArrays(semanticEnv->dynDependenciesList);
   
   #endif
#ifdef HOLISTIC__TURN_ON_PERF_COUNTERS    
    for(n=0;n<255;n++)
    {
        sprintf(filename, "./counters/Counters.%d.csv",n);
        output = fopen(filename,"r");
        if(output)
        {
            fclose(output);
        }else{
            break;
        }
    }
    if(n<255){
    printf("Saving Counter measurements to File: %s ...\n", filename);
    output = fopen(filename,"w+");
    if(output!=NULL){
        set_counter_file(output);
        int i;
        for(i=0;i<NUM_CORES;i++){
            forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
            fflush(output);
        }

    } else
        printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
   } else {
       printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
   }
    
#endif
/* It's all allocated inside VMS's big chunk -- that's about to be freed, so
 *  nothing to do here
   

   for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
    {
      VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
      VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
    }
   VMS_int__free( semanticEnv->readyVPQs );
   
   freeHashTable( semanticEnv->commHashTbl );
   VMS_int__free( _VMSMasterEnv->semanticEnv );
 */
   VMS_SS__cleanup_at_end_of_shutdown();
 }


//===========================================================================

/*
 */
SlaveVP *
VSs__create_slave_with( TopLevelFnPtr fnPtr,   void *initData,
                        SlaveVP *creatingSlv )
 { VSsSemReq reqData;

      //the semantic request data is on the stack and disappears when this
      // call returns -- it's guaranteed to remain in the VP's stack for as
      // long as the VP is suspended.
   reqData.reqType            = 0; //know type because in a VMS create req
   reqData.coreToAssignOnto = -1; //means round-robin assign
   reqData.fnPtr              = fnPtr;
   reqData.initData           = initData;
   reqData.callingSlv             = creatingSlv;

   VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );

   return creatingSlv->dataRetFromReq;
 }

SlaveVP *
VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
                        SlaveVP *creatingSlv,  int32  coreToAssignOnto )
 { VSsSemReq  reqData;

      //the semantic request data is on the stack and disappears when this
      // call returns -- it's guaranteed to remain in the VP's stack for as
      // long as the VP is suspended.
   reqData.reqType            = create_slave_w_aff; //not used, May 2012
   reqData.coreToAssignOnto   = coreToAssignOnto;
   reqData.fnPtr              = fnPtr;
   reqData.initData           = initData;
   reqData.callingSlv         = creatingSlv;

   VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );

   return creatingSlv->dataRetFromReq;
 }


void
VSs__dissipate_slave( SlaveVP *slaveToDissipate )
 {
   VMS_WL__send_dissipate_req( slaveToDissipate );
 }


//===========================================================================


//======================= task submit and end ==============================
/*
 */
void
VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
 { VSsSemReq  reqData;

   reqData.reqType    = submit_task;
   
   reqData.taskType   = taskType;
   reqData.args       = args;
   reqData.callingSlv = animSlv;
  
   reqData.taskID     = NULL;
 
   VMS_WL__send_sem_request( &reqData, animSlv );
 }

inline int32 *
VSs__create_taskID_of_size( int32 numInts, SlaveVP *animSlv )
 { int32 *taskID;
   
   taskID    = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
   taskID[0] = numInts;
   return taskID;
 }

void
VSs__submit_task_with_ID( VSsTaskType *taskType, void *args, int32 *taskID, 
                          SlaveVP     *animSlv)
 { VSsSemReq  reqData;

   reqData.reqType    = submit_task;
   
   reqData.taskType   = taskType;
   reqData.args       = args;
   reqData.taskID     = taskID;
   reqData.callingSlv = animSlv;
 
   VMS_WL__send_sem_request( &reqData, animSlv );
 }


/*This call is the last to happen in every task.  It causes the slave to
 * suspend and get the next task out of the task-queue.  Notice there is no
 * assigner here.. only one slave, no slave ReadyQ, and so on..
 *Can either make the assigner take the next task out of the taskQ, or can
 * leave all as it is, and make task-end take the next task.
 *Note: this fits the case in the new VMS for no-context tasks, so will use
 * the built-in taskQ of new VMS, and should be local and much faster.
 * 
 *The task-stub is saved in the animSlv, so the request handler will get it
 * from there, along with the task-type which has arg types, and so on..
 * 
 * NOTE: if want, don't need to send the animating SlaveVP around.. 
 * instead, can make a single slave per core, and coreCtrlr looks up the
 * slave from having the core number.
 * 
 *But, to stay compatible with all the other VMS languages, leave it in..
 */
void
VSs__end_task( SlaveVP *animSlv )
 { VSsSemReq  reqData;

   reqData.reqType      = end_task;
   reqData.callingSlv   = animSlv;
   
   VMS_WL__send_sem_request( &reqData, animSlv );
 }


//==========================  send and receive ============================
//

inline int32 *
VSs__give_self_taskID( SlaveVP *animSlv )
 {
   return ((VSsSemData*)animSlv->semanticData)->taskStub->taskID;
 }

//================================ send ===================================

void
VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID,
                      SlaveVP *senderSlv )
 { VSsSemReq  reqData;

   reqData.reqType    = send_type_to;
   
   reqData.msg        = msg;
   reqData.msgType    = type;
   reqData.receiverID = receiverID;
   reqData.senderSlv  = senderSlv;
   
   reqData.nextReqInHashEntry = NULL;

   VMS_WL__send_sem_request( &reqData, senderSlv );

      //When come back from suspend, no longer own data reachable from msg
 }

void
VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID, SlaveVP *senderSlv )
 { VSsSemReq  reqData;

   reqData.reqType     = send_from_to;
   
   reqData.msg         = msg;
   reqData.senderID    = senderID;
   reqData.receiverID  = receiverID;
   reqData.senderSlv   = senderSlv;

   reqData.nextReqInHashEntry = NULL;

   VMS_WL__send_sem_request( &reqData, senderSlv );
 }


//================================ receive ================================

/*The "type" version of send and receive creates a many-to-one relationship.
 * The sender is anonymous, and many sends can stack up, waiting to be
 * received.  The same receiver can also have send from-to's
 * waiting for it, and those will be kept separate from the "type"
 * messages.
 */
void *
VSs__receive_type_to( const int32 type, int32* receiverID, SlaveVP *receiverSlv )
 {       DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
   VSsSemReq  reqData;

   reqData.reqType     = receive_type_to;
   
   reqData.msgType     = type;
   reqData.receiverID  = receiverID;
   reqData.receiverSlv = receiverSlv;
   
   reqData.nextReqInHashEntry = NULL;

   VMS_WL__send_sem_request( &reqData, receiverSlv );
   
   return receiverSlv->dataRetFromReq;
 }



/*Call this at the point a receiving task wants in-coming data.
 * Use this from-to form when know senderID -- it makes a direct channel
 * between sender and receiver.
 */
void *
VSs__receive_from_to( int32 *senderID, int32 *receiverID, SlaveVP *receiverSlv )
 { 
   VSsSemReq  reqData;

   reqData.reqType     = receive_from_to;

   reqData.senderID    = senderID;
   reqData.receiverID  = receiverID;
   reqData.receiverSlv = receiverSlv;

   reqData.nextReqInHashEntry = NULL;
      DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
      
   VMS_WL__send_sem_request( &reqData, receiverSlv );

   return receiverSlv->dataRetFromReq;
 }




//==========================================================================
//
/*A function singleton is a function whose body executes exactly once, on a
 * single core, no matter how many times the fuction is called and no
 * matter how many cores or the timing of cores calling it.
 *
 *A data singleton is a ticket attached to data.  That ticket can be used
 * to get the data through the function exactly once, no matter how many
 * times the data is given to the function, and no matter the timing of
 * trying to get the data through from different cores.
 */

/*asm function declarations*/
void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);

/*Fn singleton uses ID as index into array of singleton structs held in the
 * semantic environment.
 */
void
VSs__start_fn_singleton( int32 singletonID,   SlaveVP *animSlv )
 {
   VSsSemReq  reqData;

      //
   reqData.reqType     = singleton_fn_start;
   reqData.singletonID = singletonID;

   VMS_WL__send_sem_request( &reqData, animSlv );
   if( animSlv->dataRetFromReq ) //will be 0 or addr of label in end singleton
    {
       VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
       asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
    }
 }

/*Data singleton hands addr of loc holding a pointer to a singleton struct.
 * The start_data_singleton makes the structure and puts its addr into the
 * location.
 */
void
VSs__start_data_singleton( VSsSingleton **singletonAddr,  SlaveVP *animSlv )
 {
   VSsSemReq  reqData;

   if( *singletonAddr && (*singletonAddr)->hasFinished )
       goto JmpToEndSingleton;
   
   reqData.reqType          = singleton_data_start;
   reqData.singletonPtrAddr = singletonAddr;

   VMS_WL__send_sem_request( &reqData, animSlv );
   if( animSlv->dataRetFromReq ) //either 0 or end singleton's return addr
    {    //Assembly code changes the return addr on the stack to the one
         // saved into the singleton by the end-singleton-fn
         //The return addr is at 0x4(%%ebp)
        JmpToEndSingleton:
          asm_write_ret_from_singleton(*singletonAddr);
    }
   //now, simply return
   //will exit either from the start singleton call or the end-singleton call
 }

/*Uses ID as index into array of flags.  If flag already set, resumes from
 * end-label.  Else, sets flag and resumes normally.
 *
 *Note, this call cannot be inlined because the instr addr at the label
 * inside is shared by all invocations of a given singleton ID.
 */
void
VSs__end_fn_singleton( int32 singletonID, SlaveVP *animSlv )
 {
   VSsSemReq  reqData;

      //don't need this addr until after at least one singleton has reached
      // this function
   VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
   asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));

   reqData.reqType     = singleton_fn_end;
   reqData.singletonID = singletonID;

   VMS_WL__send_sem_request( &reqData, animSlv );

EndSingletonInstrAddr:
   return;
 }

void
VSs__end_data_singleton(  VSsSingleton **singletonPtrAddr, SlaveVP *animSlv )
 {
   VSsSemReq  reqData;

      //don't need this addr until after singleton struct has reached
      // this function for first time
      //do assembly that saves the return addr of this fn call into the
      // data singleton -- that data-singleton can only be given to exactly
      // one instance in the code of this function.  However, can use this
      // function in different places for different data-singletons.
//   (*(singletonAddr))->endInstrAddr =  &&EndDataSingletonInstrAddr;


   asm_save_ret_to_singleton(*singletonPtrAddr);

   reqData.reqType          = singleton_data_end;
   reqData.singletonPtrAddr = singletonPtrAddr;

   VMS_WL__send_sem_request( &reqData, animSlv );
 }

/*This executes the function in the masterVP, so it executes in isolation
 * from any other copies -- only one copy of the function can ever execute
 * at a time.
 *
 *It suspends to the master, and the request handler takes the function
 * pointer out of the request and calls it, then resumes the VP.
 *Only very short functions should be called this way -- for longer-running
 * isolation, use transaction-start and transaction-end, which run the code
 * between as work-code.
 */
void
VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
                                    void *data, SlaveVP *animSlv )
 {
   VSsSemReq  reqData;

      //
   reqData.reqType          = atomic;
   reqData.fnToExecInMaster = ptrToFnToExecInMaster;
   reqData.dataForFn        = data;

   VMS_WL__send_sem_request( &reqData, animSlv );
 }


/*This suspends to the master.
 *First, it looks at the VP's data, to see the highest transactionID that VP
 * already has entered.  If the current ID is not larger, it throws an
 * exception stating a bug in the code.  Otherwise it puts the current ID
 * there, and adds the ID to a linked list of IDs entered -- the list is
 * used to check that exits are properly ordered.
 *Next it is uses transactionID as index into an array of transaction
 * structures.
 *If the "VP_currently_executing" field is non-null, then put requesting VP
 * into queue in the struct.  (At some point a holder will request
 * end-transaction, which will take this VP from the queue and resume it.)
 *If NULL, then write requesting into the field and resume.
 */
void
VSs__start_transaction( int32 transactionID, SlaveVP *animSlv )
 {
   VSsSemReq  reqData;

      //
   reqData.callingSlv      = animSlv;
   reqData.reqType     = trans_start;
   reqData.transID     = transactionID;

   VMS_WL__send_sem_request( &reqData, animSlv );
 }

/*This suspends to the master, then uses transactionID as index into an
 * array of transaction structures.
 *It looks at VP_currently_executing to be sure it's same as requesting VP.
 * If different, throws an exception, stating there's a bug in the code.
 *Next it looks at the queue in the structure.
 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
 *If something in, gets it, sets VP_currently_executing to that VP, then
 * resumes both.
 */
void
VSs__end_transaction( int32 transactionID, SlaveVP *animSlv )
 {
   VSsSemReq  reqData;

      //
   reqData.callingSlv      = animSlv;
   reqData.reqType     = trans_end;
   reqData.transID     = transactionID;

   VMS_WL__send_sem_request( &reqData, animSlv );
 }
