start of new file
[IRC.git] / Robust / src / Runtime / DSTM / interface / dstmserver.c
index 505fd3489c141320ed00a3ce60554dc59276c741..0928321c310245151796b6cbe686c879d8c0a515 100644 (file)
@@ -1,6 +1,7 @@
 /* Coordinator => Machine that initiates the transaction request call for commiting a transaction
  * Participant => Machines that host the objects involved in a transaction commit */
 
+#include <netinet/tcp.h>
 #include "dstm.h"
 #include "mlookup.h"
 #include "llookup.h"
@@ -18,6 +19,7 @@ extern pthread_mutex_t notifymutex;
 
 objstr_t *mainobjstore;
 pthread_mutex_t mainobjstore_mutex;
+pthread_mutex_t lockObjHeader;
 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
 
 sockPoolHashTable_t *transPResponseSocketPool;
@@ -32,6 +34,7 @@ int dstmInit(void)
        pthread_mutexattr_init(&mainobjstore_mutex_attr);
        pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
        pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
+       pthread_mutex_init(&lockObjHeader,NULL);
        if (mhashCreate(HASH_SIZE, LOADFACTOR))
                return 1; //failure
        
@@ -50,63 +53,67 @@ int dstmInit(void)
        return 0;
 }
 
-/* This function starts the thread to listen on a socket 
- * for tranaction calls */
-void *dstmListen()
-{
-       int listenfd, acceptfd;
-       struct sockaddr_in my_addr;
-       struct sockaddr_in client_addr;
-       socklen_t addrlength = sizeof(struct sockaddr);
-       pthread_t thread_dstm_accept;
-       int i;
-       int setsockflag=1;
-
-       listenfd = socket(AF_INET, SOCK_STREAM, 0);
-       if (listenfd == -1)
-       {
-               perror("socket");
-               exit(1);
-       }
 
-       if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
-         perror("socket");
-         exit(1);
-       }
+int startlistening() {
+  int listenfd;
+  struct sockaddr_in my_addr;
+  socklen_t addrlength = sizeof(struct sockaddr);
+  int setsockflag=1;
+  
+  listenfd = socket(AF_INET, SOCK_STREAM, 0);
+  if (listenfd == -1) {
+    perror("socket");
+    exit(1);
+  }
+  
+  if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
+    perror("socket");
+    exit(1);
+  }
 #ifdef MAC
-       if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
-         perror("socket");
-         exit(1);
-       }
+  if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
+    perror("socket");
+    exit(1);
+  }
 #endif
+  
+  my_addr.sin_family = AF_INET;
+  my_addr.sin_port = htons(LISTEN_PORT);
+  my_addr.sin_addr.s_addr = INADDR_ANY;
+  memset(&(my_addr.sin_zero), '\0', 8);
+  
+  if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
+    perror("bind");
+    exit(1);
+  }
+  
+  if (listen(listenfd, BACKLOG) == -1) {
+    perror("listen");
+    exit(1);
+  }
+  return listenfd;
+}
 
-       my_addr.sin_family = AF_INET;
-       my_addr.sin_port = htons(LISTEN_PORT);
-       my_addr.sin_addr.s_addr = INADDR_ANY;
-       memset(&(my_addr.sin_zero), '\0', 8);
-
-       if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
-       {
-               perror("bind");
-               exit(1);
-       }
-       
-       if (listen(listenfd, BACKLOG) == -1)
-       {
-               perror("listen");
-               exit(1);
-       }
-
-       printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
-       while(1)
-       {
-         int retval;
-         acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
-         do {
-           retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
-         } while(retval!=0);
-         pthread_detach(thread_dstm_accept);
-       }
+/* This function starts the thread to listen on a socket 
+ * for tranaction calls */
+void *dstmListen(void *lfd) {
+  int listenfd=(int)lfd;
+  int acceptfd;
+  struct sockaddr_in client_addr;
+  socklen_t addrlength = sizeof(struct sockaddr);
+  pthread_t thread_dstm_accept;
+  
+  printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
+  while(1) {
+    int retval;
+    int flag=1;
+    acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
+    setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
+    do {
+      retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
+    } while(retval!=0);
+    pthread_detach(thread_dstm_accept);
+  }
 }
 /* This function accepts a new connection request, decodes the control message in the connection 
  * and accordingly calls other functions to process new requests */
@@ -125,6 +132,8 @@ void *dstmAccept(void *acceptfd) {
   /* Receive control messages from other machines */
   while(1) {
     int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
+    if (ret==0)
+      break;
     if (ret==-1) {
       printf("DEBUG -> RECV Error!.. retrying\n");
       break;
@@ -134,35 +143,34 @@ void *dstmAccept(void *acceptfd) {
       /* Read oid requested and search if available */
       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
       if((srcObj = mhashSearch(oid)) == NULL) {
-       printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
-       break;
+        printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
+        break;
       }
       h = (objheader_t *) srcObj;
       GETSIZE(size, h);
       size += sizeof(objheader_t);
       sockid = (int) acceptfd;
-      
       if (h == NULL) {
-       ctrl = OBJECT_NOT_FOUND;
-       send_data(sockid, &ctrl, sizeof(char));
+        ctrl = OBJECT_NOT_FOUND;
+        send_data(sockid, &ctrl, sizeof(char));
       } else {
-       /* Type */
-       char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
-       *((int *)&msg[1])=size;
-       send_data(sockid, &msg, sizeof(msg));
-       send_data(sockid, h, size);
+        // Type 
+        char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
+        *((int *)&msg[1])=size;
+        send_data(sockid, &msg, sizeof(msg));
+        send_data(sockid, h, size);
       }
       break;
-      
+
     case READ_MULT_REQUEST:
       break;
-      
+
     case MOVE_REQUEST:
       break;
-      
+
     case MOVE_MULT_REQUEST:
       break;
-      
+
     case TRANS_REQUEST:
       /* Read transaction request */
       transinfo.objlocked = NULL;
@@ -171,20 +179,20 @@ void *dstmAccept(void *acceptfd) {
       transinfo.numlocked = 0;
       transinfo.numnotfound = 0;
       if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
-       printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
-       pthread_exit(NULL);
+        printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
+        pthread_exit(NULL);
       }
       break;
     case TRANS_PREFETCH:
       if((val = prefetchReq((int)acceptfd)) != 0) {
-       printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
-       break;
+        printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
+        break;
       }
       break;
     case TRANS_PREFETCH_RESPONSE:
       if((val = getPrefetchResponse((int) acceptfd)) != 0) {
-       printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
-       break;
+        printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
+        break;
       }
       break;
     case START_REMOTE_THREAD:
@@ -192,17 +200,17 @@ void *dstmAccept(void *acceptfd) {
       objType = getObjType(oid);
       startDSMthread(oid, objType);
       break;
-      
+
     case THREAD_NOTIFY_REQUEST:
       recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
       size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
       if((buffer = calloc(1,size)) == NULL) {
-       printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
-       pthread_exit(NULL);
+        printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
+        pthread_exit(NULL);
       }
-      
+
       recv_data((int)acceptfd, buffer, size);
-      
+
       oidarry = calloc(numoid, sizeof(unsigned int)); 
       memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
       size = sizeof(unsigned int) * numoid;
@@ -214,18 +222,18 @@ void *dstmAccept(void *acceptfd) {
       threadid = *((unsigned int *)(buffer+size));
       processReqNotify(numoid, oidarry, versionarry, mid, threadid);
       free(buffer);
-      
+
       break;
 
     case THREAD_NOTIFY_RESPONSE:
       size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
       if((buffer = calloc(1,size)) == NULL) {
-       printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
-       pthread_exit(NULL);
+        printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
+        pthread_exit(NULL);
       }
-      
+
       recv_data((int)acceptfd, buffer, size);
-      
+
       oid = *((unsigned int *)buffer);
       size = sizeof(unsigned int);
       version = *((unsigned short *)(buffer+size));
@@ -243,56 +251,56 @@ void *dstmAccept(void *acceptfd) {
     }
   }
 
- closeconnection:
+closeconnection:
   /* Close connection */
   if (close((int)acceptfd) == -1)
     perror("close");
   pthread_exit(NULL);
 }
-  
+
 /* This function reads the information available in a transaction request
  * and makes a function call to process the request */
 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
-       char *ptr;
-       void *modptr;
-       unsigned int *oidmod, oid;
-       fixed_data_t fixed;
-       objheader_t *headaddr;
-       int sum, i, size, n, val;
-
-       oidmod = NULL;
-
-       /* Read fixed_data_t data structure */ 
-       size = sizeof(fixed) - 1;
-       ptr = (char *)&fixed;;
-       fixed.control = TRANS_REQUEST;
-       recv_data((int)acceptfd, ptr+1, size);
-
-       /* Read list of mids */
-       int mcount = fixed.mcount;
-       size = mcount * sizeof(unsigned int);
-       unsigned int listmid[mcount];
-       ptr = (char *) listmid;
-       recv_data((int)acceptfd, ptr, size);
-       
-       /* Read oid and version tuples for those objects that are not modified in the transaction */
-       int numread = fixed.numread;
-       size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
-       char objread[size];
-       if(numread != 0) { //If pile contains more than one object to be read, 
-                         // keep reading all objects
-               recv_data((int)acceptfd, objread, size);        
-       }
-       
-       /* Read modified objects */
-       if(fixed.nummod != 0) {
-               if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
-                       printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
-                       return 1;
-               }
-               size = fixed.sum_bytes;
-               recv_data((int)acceptfd, modptr, size); 
-       }
+  char *ptr;
+  void *modptr;
+  unsigned int *oidmod, oid;
+  fixed_data_t fixed;
+  objheader_t *headaddr;
+  int sum, i, size, n, val;
+
+  oidmod = NULL;
+
+  /* Read fixed_data_t data structure */ 
+  size = sizeof(fixed) - 1;
+  ptr = (char *)&fixed;;
+  fixed.control = TRANS_REQUEST;
+  recv_data((int)acceptfd, ptr+1, size);
+
+  /* Read list of mids */
+  int mcount = fixed.mcount;
+  size = mcount * sizeof(unsigned int);
+  unsigned int listmid[mcount];
+  ptr = (char *) listmid;
+  recv_data((int)acceptfd, ptr, size);
+
+  /* Read oid and version tuples for those objects that are not modified in the transaction */
+  int numread = fixed.numread;
+  size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
+  char objread[size];
+  if(numread != 0) { //If pile contains more than one object to be read, 
+    // keep reading all objects
+    recv_data((int)acceptfd, objread, size);   
+  }
+
+  /* Read modified objects */
+  if(fixed.nummod != 0) {
+    if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
+      printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
+      return 1;
+    }
+    size = fixed.sum_bytes;
+    recv_data((int)acceptfd, modptr, size);    
+  }
 
        /* Create an array of oids for modified objects */
        oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
@@ -358,7 +366,7 @@ int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
                                        printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
                                        return 1;
                                }
-                               STATUS(((objheader_t *)header)) &= ~(LOCK);             
+                               UnLock(STATUSPTR(header));
                        }
 
                        /* Send ack to Coordinator */
@@ -396,7 +404,6 @@ int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
        if (transinfo->objnotfound != NULL) {
                free(transinfo->objnotfound);
        }
-
        return 0;
 }
 
@@ -407,16 +414,17 @@ char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigne
        unsigned short version;
        char control = 0, *ptr;
        unsigned int oid;
-       unsigned int *oidnotfound, *oidlocked;
+       unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
        void *mobj;
        objheader_t *headptr;
 
        /* Counters and arrays to formulate decision on control message to be sent */
        oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
        oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
-       int objnotfound = 0, objlocked = 0;
+       oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
+       int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
        int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
-
+    int numBytes = 0;
        /* modptr points to the beginning of the object store 
         * created at the Pariticipant. 
         * Object store holds the modified objects involved in the transaction request */ 
@@ -434,68 +442,98 @@ char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigne
                  int tmpsize;
                  headptr = (objheader_t *) ptr;
                  oid = OID(headptr);
-                 version = headptr->version;
-                 GETSIZE(tmpsize, headptr);
-                 ptr += sizeof(objheader_t) + tmpsize;
-               }
-               
-               /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
-
-               if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
-                       /* Save the oids not found and number of oids not found for later use */
-                       oidnotfound[objnotfound] = oid;
-                       objnotfound++;
-               } else { /* If Obj found in machine (i.e. has not moved) */
-                       /* Check if Obj is locked by any previous transaction */
-                       if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {           
-                               if (version == ((objheader_t *)mobj)->version) {      /* If locked then match versions */
-                                       v_matchlock++;
-                               } else {/* If versions don't match ...HARD ABORT */
-                                       v_nomatch++;
-                                       /* Send TRANS_DISAGREE to Coordinator */
-                                       control = TRANS_DISAGREE;
-                                       if (objlocked > 0) {
-                                         for(j = 0; j < objlocked; j++) {
-                                                       if((headptr = mhashSearch(oidlocked[j])) == NULL) {
-                                                               printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
-                                                               return 0;
-                                                       }
-                                                       STATUS(headptr) &= ~(LOCK);
-                                               }
-                                               free(oidlocked);
-                                       }
-                                       send_data(acceptfd, &control, sizeof(char));
-                                       return control;
-                               }
-                       } else {/* If Obj is not locked then lock object */
-                               STATUS(((objheader_t *)mobj)) |= LOCK;
-                               /* Save all object oids that are locked on this machine during this transaction request call */
-                               oidlocked[objlocked] = OID(((objheader_t *)mobj));
-                               objlocked++;
-                               if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
-                                       v_matchnolock++;
-                               } else { /* If versions don't match ...HARD ABORT */
-                                       v_nomatch++;
-                                       control = TRANS_DISAGREE;
-                                       if (objlocked > 0) {
-                                               for(j = 0; j < objlocked; j++) {
-                                                       if((headptr = mhashSearch(oidlocked[j])) == NULL) {
-                                                               printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
-                                                               return 0;
-                                                       }
-                                                       STATUS(headptr) &= ~(LOCK);
-                                               }
-                                               free(oidlocked);
-                                       }
-
-                                       /* Send TRANS_DISAGREE to Coordinator */
-                                       send_data(acceptfd, &control, sizeof(char));
-                                       return control;
-                               }
-                       }
-               }
+          version = headptr->version;
+          GETSIZE(tmpsize, headptr);
+          ptr += sizeof(objheader_t) + tmpsize;
+        }
+
+        /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
+
+        if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
+          /* Save the oids not found and number of oids not found for later use */
+          oidnotfound[objnotfound] = oid;
+          objnotfound++;
+        } else { /* If Obj found in machine (i.e. has not moved) */
+          /* Check if Obj is locked by any previous transaction */
+          if (test_and_set(STATUSPTR(mobj))) {
+           //don't have lock
+            if (version == ((objheader_t *)mobj)->version) {      /* If locked then match versions */
+              v_matchlock++;
+            } else {/* If versions don't match ...HARD ABORT */
+              v_nomatch++;
+              oidvernotmatch[objvernotmatch] = oid;
+              objvernotmatch++;
+              int size;
+              GETSIZE(size, mobj);
+              size += sizeof(objheader_t);
+              numBytes += size;
+              /* Send TRANS_DISAGREE to Coordinator */
+              control = TRANS_DISAGREE;
+#ifdef CHECKTA
+  char b[] = "version mismatch";
+  char c[] = "object type";
+  TABORT3(__func__, b, c, TYPE(mobj));
+#endif
+
+            }
+          } else {/* If Obj is not locked then lock object */
+            /* Save all object oids that are locked on this machine during this transaction request call */
+            oidlocked[objlocked] = OID(((objheader_t *)mobj));
+            objlocked++;
+            if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
+              v_matchnolock++;
+            } else { /* If versions don't match ...HARD ABORT */
+              v_nomatch++;
+              oidvernotmatch[objvernotmatch] = oid;
+              objvernotmatch++;
+              int size;
+              GETSIZE(size, mobj);
+              size += sizeof(objheader_t);
+              numBytes += size;
+              control = TRANS_DISAGREE;
+
+#ifdef CHECKTA
+  char b[] = "version mismatch";
+  char c[] = "object type";
+  TABORT3(__func__, b, c, TYPE(mobj));
+#endif
+            }
+          }
+        }
        }
        
+       /* send TRANS_DISAGREE and objs*/
+    if(v_nomatch > 0) {
+      char *objs = calloc(1, numBytes);
+      int j, offset = 0;
+      for(j = 0; j<objvernotmatch; j++) {
+        objheader_t *header = mhashSearch(oidvernotmatch[j]);
+        int size = 0;
+        GETSIZE(size, header);
+        size += sizeof(objheader_t);
+        memcpy(objs+offset, header, size);
+        offset += size;
+      }
+      if (objlocked > 0) {
+        for(j = 0; j < objlocked; j++) {
+          if((headptr = mhashSearch(oidlocked[j])) == NULL) {
+            printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
+            return 0;
+          }
+          UnLock(STATUSPTR(headptr));
+        }
+        free(oidlocked);
+      }
+      send_data(acceptfd, &control, sizeof(char));
+      send_data(acceptfd, &numBytes, sizeof(int));
+      send_data(acceptfd, objs, numBytes);
+      transinfo->objvernotmatch = oidvernotmatch;
+      transinfo->numvernotmatch = objvernotmatch;
+      free(objs);
+      free(transinfo->objvernotmatch);
+      return control;
+    }
+
        /* Decide what control message to send to Coordinator */
        if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
                                        modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
@@ -504,7 +542,6 @@ char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigne
        }
        
        return control;
-
 }
 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
  * to send to Coordinator based on the votes of oids involved in the transaction */
@@ -527,7 +564,7 @@ char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int
                /* Send control message */
                send_data(acceptfd, &control, sizeof(char));
        
-               /* Send number of oids not found and the missing oids if objects are missing in the machine */
+               /*  FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
                if(*(objnotfound) != 0) { 
                        int msg[1];
                        msg[0] = *(objnotfound);
@@ -552,49 +589,45 @@ char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int
  * addresses in lookup table and also changes version number
  * Sends an ACK back to Coordinator */
 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
-       objheader_t *header;
-       objheader_t *newheader;
-       int i = 0, offset = 0;
-       char control;
-       int tmpsize;
-
-       /* Process each modified object saved in the mainobject store */
-       for(i = 0; i < nummod; i++) {
-               if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
-                       printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
-                       return 1;
-               }
-               GETSIZE(tmpsize,header);
-               pthread_mutex_lock(&mainobjstore_mutex);
-               memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
-               header->version += 1; 
-               /* If threads are waiting on this object to be updated, notify them */
-        pthread_mutex_lock(&notifymutex);
-               if(header->notifylist != NULL) {
-                       notifyAll(&header->notifylist, OID(header), header->version);
-               }
-        pthread_mutex_unlock(&notifymutex);
-               pthread_mutex_unlock(&mainobjstore_mutex);
-               offset += sizeof(objheader_t) + tmpsize;
-       }
-
-       if (nummod > 0)
-               free(modptr);
-
-       /* Unlock locked objects */
-       for(i = 0; i < numlocked; i++) {
-               if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
-                       printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
-                       return 1;
-               }
-               STATUS(header) &= ~(LOCK);
-       }
-       //TODO Update location lookup table
-
-       /* Send ack to coordinator */
-       control = TRANS_SUCESSFUL;
-       send_data((int)acceptfd, &control, sizeof(char));
-       return 0;
+  objheader_t *header;
+  objheader_t *newheader;
+  int i = 0, offset = 0;
+  char control;
+  int tmpsize;
+  
+  /* Process each modified object saved in the mainobject store */
+  for(i = 0; i < nummod; i++) {
+    if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
+      printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
+      return 1;
+    }
+    GETSIZE(tmpsize,header);
+    memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
+    header->version += 1; 
+    /* If threads are waiting on this object to be updated, notify them */
+    if(header->notifylist != NULL) {
+      notifyAll(&header->notifylist, OID(header), header->version);
+    }
+    offset += sizeof(objheader_t) + tmpsize;
+  }
+  
+  if (nummod > 0)
+    free(modptr);
+  
+  /* Unlock locked objects */
+  for(i = 0; i < numlocked; i++) {
+    if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
+      printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
+      return 1;
+    }
+    UnLock(STATUSPTR(header));
+  }
+  //TODO Update location lookup table
+  
+  /* Send ack to coordinator */
+  control = TRANS_SUCESSFUL;
+  send_data((int)acceptfd, &control, sizeof(char));
+  return 0;
 }
 
 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
@@ -717,74 +750,70 @@ void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
 }
 
 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
-       objheader_t *header;
-       unsigned int oid;
-       unsigned short newversion;
-       char msg[1+  2 * sizeof(unsigned int) + sizeof(unsigned short)];
-       int sd;
-       struct sockaddr_in remoteAddr;
-       int bytesSent;
-       int size;
-       int i = 0;
-
-       while(i < numoid) {
-               oid = *(oidarry + i);
-               if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
-                       printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
-                       return;
-               } else {
-                       /* Check to see if versions are same */
-checkversion:
-                       if ((STATUS(header) & LOCK) != LOCK) {          
-                pthread_mutex_lock(&notifymutex);
-                               STATUS(header) |= LOCK;
-                               newversion = header->version;
-                               if(newversion == *(versionarry + i)) {
-                                       //Add to the notify list 
-                                       if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
-                                               printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__); 
-                        pthread_mutex_unlock(&notifymutex);
-                                               return;
-                                       }
-                                       STATUS(header) &= ~(LOCK);              
-                    pthread_mutex_unlock(&notifymutex);
-                               } else {
-                                       STATUS(header) &= ~(LOCK);              
-                    pthread_mutex_unlock(&notifymutex);
-                                       if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
-                                               perror("processReqNotify():socket()");
-                                               return;
-                                       }
-                                       bzero(&remoteAddr, sizeof(remoteAddr));
-                                       remoteAddr.sin_family = AF_INET;
-                                       remoteAddr.sin_port = htons(LISTEN_PORT);
-                                       remoteAddr.sin_addr.s_addr = htonl(mid);
-
-                                       if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
-                                               printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
-                                                               inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
-                                               close(sd);
-                                               return;
-                                       } else {
-                                               //Send Update notification
-                                               msg[0] = THREAD_NOTIFY_RESPONSE;
-                                               *((unsigned int *)&msg[1]) = oid;
-                                               size = sizeof(unsigned int);
-                                               *((unsigned short *)(&msg[1]+size)) = newversion;
-                                               size += sizeof(unsigned short);
-                                               *((unsigned int *)(&msg[1]+size)) = threadid;
-                                               size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
-                                               send_data(sd, msg, size);
-                                       }
-                                       close(sd);
-                               }
-                       } else {
-                               randomdelay();
-                               goto checkversion;
-                       }
-               }
-               i++;
+  objheader_t *header;
+  unsigned int oid;
+  unsigned short newversion;
+  char msg[1+  2 * sizeof(unsigned int) + sizeof(unsigned short)];
+  int sd;
+  struct sockaddr_in remoteAddr;
+  int bytesSent;
+  int size;
+  int i = 0;
+  
+  while(i < numoid) {
+    oid = *(oidarry + i);
+    if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
+      printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
+      return;
+    } else {
+      /* Check to see if versions are same */
+    checkversion:
+      if (test_and_set(STATUSPTR(header))==0) {
+       //have lock
+       newversion = header->version;
+       if(newversion == *(versionarry + i)) {
+         //Add to the notify list 
+         if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
+           printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__); 
+           return;
+         }
+         UnLock(STATUSPTR(header));
+       } else {
+         UnLock(STATUSPTR(header));
+         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
+           perror("processReqNotify():socket()");
+           return;
+         }
+         bzero(&remoteAddr, sizeof(remoteAddr));
+         remoteAddr.sin_family = AF_INET;
+         remoteAddr.sin_port = htons(LISTEN_PORT);
+         remoteAddr.sin_addr.s_addr = htonl(mid);
+         
+         if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
+           printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
+                  inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
+           close(sd);
+           return;
+         } else {
+           //Send Update notification
+           msg[0] = THREAD_NOTIFY_RESPONSE;
+           *((unsigned int *)&msg[1]) = oid;
+           size = sizeof(unsigned int);
+           *((unsigned short *)(&msg[1]+size)) = newversion;
+           size += sizeof(unsigned short);
+           *((unsigned int *)(&msg[1]+size)) = threadid;
+           size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
+           send_data(sd, msg, size);
+         }
+         close(sd);
        }
-       free(oidarry);
-       free(versionarry);
+      } else {
+       randomdelay();
+       goto checkversion;
+      }
+    }
+    i++;
+  }
+  free(oidarry);
+  free(versionarry);
 }