diff VMS.c @ 26:668278fa7a63

Sequential -- just starting to add sequential version
author Me
date Mon, 26 Jul 2010 15:25:53 -0700
parents c556193f7211
children 8b9e4c333fe6
line diff
     1.1 --- a/VMS.c	Sat Jul 24 08:58:47 2010 -0700
     1.2 +++ b/VMS.c	Mon Jul 26 15:25:53 2010 -0700
     1.3 @@ -12,6 +12,8 @@
     1.4  #include "Queue_impl/BlockingQueue.h"
     1.5  
     1.6  
     1.7 +#define thdAttrs NULL
     1.8 +
     1.9  //===========================================================================
    1.10  void
    1.11  shutdownFn( void *dummy, VirtProcr *dummy2 );
    1.12 @@ -19,6 +21,9 @@
    1.13  void
    1.14  create_sched_slots( MasterEnv *masterEnv );
    1.15  
    1.16 +pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
    1.17 +pthread_cond_t  suspend_cond  = PTHREAD_COND_INITIALIZER;
    1.18 +
    1.19  //===========================================================================
    1.20  
    1.21  /*Setup has two phases:
    1.22 @@ -52,10 +57,10 @@
    1.23  void
    1.24  VMS__init()
    1.25   { MasterEnv  *masterEnv;
    1.26 -   CASQueueStruc *workQ;
    1.27 +   VMSQueueStruc *workQ;
    1.28  
    1.29        //Make the central work-queue
    1.30 -   _VMSWorkQ = makeCASQ();
    1.31 +   _VMSWorkQ = makeVMSQ();
    1.32     workQ     = _VMSWorkQ;
    1.33  
    1.34     _VMSMasterEnv = malloc( sizeof(MasterEnv) );
    1.35 @@ -66,33 +71,25 @@
    1.36  
    1.37     create_sched_slots( masterEnv );
    1.38  
    1.39 -     //Set slot 0 to be the master virt procr & set flags just in case
    1.40 -   masterEnv->schedSlots[0]->needsProcrAssigned  = FALSE;  //says don't touch
    1.41 -   masterEnv->schedSlots[0]->workIsDone          = FALSE;  //says don't touch
    1.42 -   masterEnv->schedSlots[0]->procrAssignedToSlot = masterEnv->masterVirtPr;
    1.43 -   masterEnv->masterVirtPr->schedSlot = masterEnv->schedSlots[0];
    1.44     masterEnv->stillRunning = FALSE;
    1.45 +   masterEnv->numToPrecede = NUM_CORES;
    1.46     
    1.47        //First core loop to start up gets this, which will schedule seed Pr
    1.48        //TODO: debug: check address of masterVirtPr
    1.49 -   writeCASQ( masterEnv->masterVirtPr, workQ );
    1.50 +   writeVMSQ( masterEnv->masterVirtPr, workQ );
    1.51  
    1.52     numProcrsCreated = 1;
    1.53  
    1.54     //========================================================================
    1.55     //                      Create the Threads
    1.56     int coreIdx, retCode;
    1.57 -   #define thdAttrs NULL
    1.58 -
    1.59 -   _VMSMasterEnv->setupComplete = 0;
    1.60 -   _VMSMasterEnv->suspend_mutex = PTHREAD_MUTEX_INITIALIZER;
    1.61 -   _VMSMasterEnv->suspend_cond  = PTHREAD_COND_INITIALIZER;
    1.62 -
    1.63 +   
    1.64        //Need the threads to be created suspended, and wait for a signal
    1.65        // before proceeding -- gives time after creating to initialize other
    1.66        // stuff before the coreLoops set off.
    1.67 -   
    1.68 -   //Make params given to the win threads that animate the core loops
    1.69 +   _VMSMasterEnv->setupComplete = 0;
    1.70 +
    1.71 +      //Make the threads that animate the core loops
    1.72     for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
    1.73      { coreLoopThdParams[coreIdx]          = malloc( sizeof(ThdParams) );
    1.74        coreLoopThdParams[coreIdx]->coreNum = coreIdx;
    1.75 @@ -102,10 +99,8 @@
    1.76                          thdAttrs,
    1.77                         &coreLoop,
    1.78                 (void *)(coreLoopThdParams[coreIdx]) );
    1.79 -      if(!retCode){printf("ERROR creating thread: %d\n", retCode); exit();}
    1.80 +      if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
    1.81      }
    1.82 -
    1.83 -
    1.84   }
    1.85  
    1.86  void
    1.87 @@ -148,10 +143,10 @@
    1.88        //get lock, to lock out any threads still starting up -- they'll see
    1.89        // that setupComplete is true before entering while loop, and so never
    1.90        // wait on the condition
    1.91 -   pthread_mutex_lock(     _VMSMasterEnv->suspend_mutex );
    1.92 +   pthread_mutex_lock(     &suspendLock );
    1.93     _VMSMasterEnv->setupComplete = 1;
    1.94 -   pthread_mutex_unlock(   _VMSMasterEnv->suspend_mutex );
    1.95 -   pthread_cond_broadcast( _VMSMasterEnv->suspend_cond );
    1.96 +   pthread_mutex_unlock(   &suspendLock );
    1.97 +   pthread_cond_broadcast( &suspend_cond );
    1.98     
    1.99     
   1.100        //wait for all to complete
   1.101 @@ -200,6 +195,8 @@
   1.102        // for 2 params + return addr.  Return addr (NULL) is in loc pointed to
   1.103        // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
   1.104     stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
   1.105 +   if(stackLocs == 0)
   1.106 +   {perror("malloc stack"); exit(1);}
   1.107     newPr->startOfStack = stackLocs;
   1.108     stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
   1.109        //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
   1.110 @@ -212,7 +209,7 @@
   1.111   }
   1.112  
   1.113  
   1.114 - /*there is a label inside this function -- save the addr of this label in
   1.115 +/*there is a label inside this function -- save the addr of this label in
   1.116   * the callingPr struc, as the pick-up point from which to start the next
   1.117   * work-unit for that procr.  If turns out have to save registers, then
   1.118   * save them in the procr struc too.  Then do assembly jump to the CoreLoop's
   1.119 @@ -238,25 +235,32 @@
   1.120  
   1.121     stackPtrAddr      = &(callingPr->stackPtr);
   1.122     framePtrAddr      = &(callingPr->framePtr);
   1.123 -   
   1.124 +
   1.125     jmpPt             = callingPr->coreLoopStartPt;
   1.126     coreLoopFramePtr  = callingPr->coreLoopFramePtr;//need this only
   1.127     coreLoopStackPtr  = callingPr->coreLoopStackPtr;//shouldn't need -- safety
   1.128  
   1.129 -      //Save the virt procr's stack and frame ptrs, restore coreloop's frame
   1.130 -      // ptr, then jump back to "start" of core loop
   1.131 -      //Note, GCC compiles to assembly that saves esp and ebp in the stack
   1.132 -      // frame -- so have to explicitly do assembly that saves to memory
   1.133 +      //Eclipse's compilation sequence complains -- so break into two
   1.134 +      // separate in-line assembly pieces
   1.135 +      //Save the virt procr's stack and frame ptrs,
   1.136     asm volatile("movl %0,     %%eax;  \
   1.137                   movl %%esp, (%%eax); \
   1.138                   movl %1,     %%eax;  \
   1.139 -                 movl %%ebp, (%%eax); \
   1.140 -                 movl %2, %%eax;      \
   1.141 -                 movl %3, %%esp;      \
   1.142 -                 movl %4, %%ebp;      \
   1.143 +                 movl %%ebp, (%%eax) "\
   1.144 +   /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
   1.145 +   /* inputs  */ :        \
   1.146 +   /* clobber */ : "%eax" \
   1.147 +                );
   1.148 +
   1.149 +      //restore coreloop's frame ptr, then jump back to "start" of core loop
   1.150 +      //Note, GCC compiles to assembly that saves esp and ebp in the stack
   1.151 +      // frame -- so have to explicitly do assembly that saves to memory
   1.152 +   asm volatile("movl %0, %%eax;      \
   1.153 +                 movl %1, %%esp;      \
   1.154 +                 movl %2, %%ebp;      \
   1.155                   jmp  %%eax    "      \
   1.156 -   /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
   1.157 -   /* inputs  */ : "g" (jmpPt), "g"(coreLoopStackPtr), "g"(coreLoopFramePtr)\
   1.158 +   /* outputs */ :                    \
   1.159 +   /* inputs  */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
   1.160     /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi"  \
   1.161                  ); //list everything as clobbered to force GCC to save all
   1.162                     // live vars that are in regs on stack before this
   1.163 @@ -356,6 +360,8 @@
   1.164  //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
   1.165  // of a request -- IE call with both a virt procr and a fn-ptr to request
   1.166  // freer (also maybe put sem request freer as a field in virt procr?)
   1.167 +//VMSHW relies right now on this only freeing VMS layer of request -- the
   1.168 +// semantic portion of request is alloc'd and freed by request handler
   1.169  void
   1.170  VMS__free_request( VMSReqst *req )
   1.171   { 
   1.172 @@ -500,7 +506,7 @@
   1.173  shutdownFn( void *dummy, VirtProcr *animatingPr )
   1.174   { int coreIdx;
   1.175     VirtProcr *shutDownPr;
   1.176 -   CASQueueStruc *workQ = _VMSWorkQ;
   1.177 +   VMSQueueStruc *workQ = _VMSWorkQ;
   1.178  
   1.179        //free all the locations owned within the VMS system
   1.180     //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt
   1.181 @@ -510,7 +516,7 @@
   1.182      {
   1.183        shutDownPr = VMS__create_procr( NULL, NULL );
   1.184        shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt;
   1.185 -      writeCASQ( shutDownPr, workQ );
   1.186 +      writeVMSQ( shutDownPr, workQ );
   1.187      }
   1.188  
   1.189        //This is an issue: the animating processor of this function may not