nodestack: only create may_read_from sets for read operations
[cdsspec-compiler.git] / snapshot.cc
index af4284361d61f076c16c12675945ec5d364b968c..5fb1a85ebd4c4e633e1da3812b97776a893dccd8 100644 (file)
@@ -104,19 +104,22 @@ static void HandlePF( int sig, siginfo_t *si, void * unused){
 void createSharedLibrary(){
 #if !USE_MPROTECT_SNAPSHOT
        //step 1. create shared memory.
-       if( sTheRecord ) return;
+       if ( sTheRecord )
+               return;
        int fd = shm_open( "/ModelChecker-Snapshotter", O_RDWR | O_CREAT, 0777 ); //universal permissions.
-       if( -1 == fd ) FAILURE("shm_open");
-       if( -1 == ftruncate( fd, ( size_t )SHARED_MEMORY_DEFAULT + ( size_t )STACK_SIZE_DEFAULT ) ) FAILURE( "ftruncate" );
-       void * memMapBase = mmap( 0, ( size_t )SHARED_MEMORY_DEFAULT + ( size_t )STACK_SIZE_DEFAULT, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
-       if( MAP_FAILED == memMapBase ) FAILURE("mmap");
+       if ( -1 == fd )
+               FAILURE("shm_open");
+       if ( -1 == ftruncate( fd, SHARED_MEMORY_DEFAULT + STACK_SIZE_DEFAULT ) )
+               FAILURE( "ftruncate" );
+       void * memMapBase = mmap( 0, SHARED_MEMORY_DEFAULT + STACK_SIZE_DEFAULT, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
+       if( MAP_FAILED == memMapBase )
+               FAILURE("mmap");
        sTheRecord = ( struct Snapshot * )memMapBase;
        sTheRecord->mSharedMemoryBase = (void *)((uintptr_t)memMapBase + sizeof(struct Snapshot));
-       sTheRecord->mStackBase = (void *)((uintptr_t)memMapBase + (size_t)SHARED_MEMORY_DEFAULT);
+       sTheRecord->mStackBase = (void *)((uintptr_t)memMapBase + SHARED_MEMORY_DEFAULT);
        sTheRecord->mStackSize = STACK_SIZE_DEFAULT;
        sTheRecord->mIDToRollback = -1;
        sTheRecord->currSnapShotID = 0;
-       sTheRecord->mbFinalize = false;
 #endif
 }
 
@@ -174,26 +177,20 @@ void initSnapShotLibrary(unsigned int numbackingpages,
        createSharedLibrary();
 
        //step 2 setup the stack context.
-
-       int alreadySwapped = 0;
-       getcontext( &savedSnapshotContext );
-       if( !alreadySwapped ){
-               alreadySwapped = 1;
-               ucontext_t currentContext, swappedContext, newContext;
-               getcontext( &newContext );
-               newContext.uc_stack.ss_sp = sTheRecord->mStackBase;
-               newContext.uc_stack.ss_size = STACK_SIZE_DEFAULT;
-               newContext.uc_link = &currentContext;
-               makecontext( &newContext, entryPoint, 0 );
-               swapcontext( &swappedContext, &newContext );
-       }
+       ucontext_t newContext;
+       getcontext( &newContext );
+       newContext.uc_stack.ss_sp = sTheRecord->mStackBase;
+       newContext.uc_stack.ss_size = STACK_SIZE_DEFAULT;
+       makecontext( &newContext, entryPoint, 0 );
+       /* switch to a new entryPoint context, on a new stack */
+       swapcontext(&savedSnapshotContext, &newContext);
 
        //add the code to take a snapshot here...
        //to return to user process, do a second swapcontext...
        pid_t forkedID = 0;
        snapshotid = sTheRecord->currSnapShotID;
        bool swapContext = false;
-       while( !sTheRecord->mbFinalize ){
+       while( true ){
                sTheRecord->currSnapShotID=snapshotid+1;
                forkedID = fork();
                if( 0 == forkedID ){
@@ -307,14 +304,7 @@ void rollBack( snapshot_id theID ){
                SSDEBUG("Invoked rollback");
                exit(EXIT_SUCCESS);
        }
+       sTheRecord->mIDToRollback = -1;
 #endif
 }
 
-/** The finalize method shuts down the snapshotting system.  */
-//Subramanian -- remove this function from the external interface and
-//have us call it internally
-void finalize(){
-#if !USE_MPROTECT_SNAPSHOT
-       sTheRecord->mbFinalize = true;
-#endif
-}