-/* ================================================================
- * transCommit
- * - This function initiates the transaction commit process
- * - goes through the transaction cache and decides
- * - a final response
- * ================================================================
- */
-int transCommit() {
- do {
- /* Look through all the objects in the transaction hash table */
- int finalResponse = traverseCache();
- if(finalResponse == TRANS_ABORT) {
-#ifdef TRANSSTATS
- numTransAbort++;
-#endif
- freenewobjs();
- objstrDelete(t_cache);
- t_chashDelete();
- return TRANS_ABORT;
- }
- if(finalResponse == TRANS_COMMIT) {
-#ifdef TRANSSTATS
- numTransCommit++;
-#endif
- freenewobjs();
- objstrDelete(t_cache);
- t_chashDelete();
- return 0;
- }
- /* wait a random amount of time before retrying to commit transaction*/
- if(finalResponse == TRANS_SOFT_ABORT) {
-#ifdef TRANSSTATS
- nSoftAbort++;
-#endif
- randomdelay();
- } else {
- printf("Error: in %s() Unknown outcome", __func__);
- exit(-1);
- }
- } while (1);
-}
-
-/* ==================================================
- * traverseCache
- * - goes through the transaction cache and
- * - decides if a transaction should commit or abort
- * ==================================================
- */
-int traverseCache() {
- /* Create info to keep track of objects that can be locked */
- int numoidrdlocked=0;
- int numoidwrlocked=0;
- unsigned int oidrdlocked[c_numelements];
- unsigned int oidwrlocked[c_numelements];
- int softabort=0;
- int i;
- chashlistnode_t *ptr = c_table;
- /* Represents number of bins in the chash table */
- unsigned int size = c_size;
- for(i = 0; i<size; i++) {
- chashlistnode_t *curr = &ptr[i];
- /* Inner loop to traverse the linked list of the cache lookupTable */
- while(curr != NULL) {
- //if the first bin in hash table is empty
- if(curr->key == 0)
- break;
- objheader_t * headeraddr=&((objheader_t *) curr->val)[-1];
-
- unsigned int version = headeraddr->version;
- objheader_t *header=(objheader_t *) (((char *)curr->key)-sizeof(objheader_t));
-
- if(STATUS(headeraddr) & DIRTY) {
- /* Read from the main heap and compare versions */
- if(write_trylock(&header->lock)) { //can aquire write lock
- if (version == header->version) {/* versions match */
- /* Keep track of objects locked */
- oidwrlocked[numoidwrlocked++] = OID(header);
- } else {
- oidwrlocked[numoidwrlocked++] = OID(header);
- transAbortProcess(oidrdlocked, &numoidrdlocked, oidwrlocked, &numoidwrlocked);
- return TRANS_ABORT;
- }
- } else { /* cannot aquire lock */
- if(version == header->version) /* versions match */
- softabort=1;
- else {
- transAbortProcess(oidrdlocked, &numoidrdlocked, oidwrlocked, &numoidwrlocked);
- return TRANS_ABORT;
- }
- }
- } else {
- /* Read from the main heap and compare versions */
- if(read_trylock(&header->lock)) { //can further aquire read locks
- if(version == header->version) {/* versions match */
- oidrdlocked[numoidrdlocked++] = OID(header);
- } else {
- oidrdlocked[numoidrdlocked++] = OID(header);
- transAbortProcess(oidrdlocked, &numoidrdlocked, oidwrlocked, &numoidwrlocked);
- return TRANS_ABORT;
- }
- } else { /* cannot aquire lock */
- if(version == header->version)
- softabort=1;
- else {
- transAbortProcess(oidrdlocked, &numoidrdlocked, oidwrlocked, &numoidwrlocked);
- return TRANS_ABORT;
- }
- }
- }
-
- curr = curr->next;
- }
- } //end of for
-
- /* Decide the final response */
- if (softabort) {
- transAbortProcess(oidrdlocked, &numoidrdlocked, oidwrlocked, &numoidwrlocked);
- return TRANS_SOFT_ABORT;
- } else {
- transCommitProcess(oidrdlocked, &numoidrdlocked, oidwrlocked, &numoidwrlocked);
- return TRANS_COMMIT;
- }
-}
-
-/* ===========================================================================
- * decideResponse
- * - increments counters that keep track of objects read, modified or locked
- * - updates the oids locked and oids newly created
- * ===========================================================================
- */
-
-
-/* ==================================
- * transAbortProcess
- *
- * =================================
- */
-int transAbortProcess(unsigned int *oidrdlocked, int *numoidrdlocked, unsigned int *oidwrlocked, int *numoidwrlocked) {
- int i;
- objheader_t *header;
- /* Release read locks */
- for(i=0; i< *numoidrdlocked; i++) {
- /* Read from the main heap */
- header = (objheader_t *)(((char *)(oidrdlocked[i])) - sizeof(objheader_t));
- read_unlock(&header->lock);
- }
-
- /* Release write locks */
- for(i=0; i< *numoidwrlocked; i++) {
- /* Read from the main heap */
- header = (objheader_t *)(((char *)(oidwrlocked[i])) - sizeof(objheader_t));
- write_unlock(&header->lock);
- }
-}
-
-/* ==================================
- * transCommitProcess
- *
- * =================================
- */
-int transCommitProcess(unsigned int *oidrdlocked, int *numoidrdlocked,
- unsigned int *oidwrlocked, int *numoidwrlocked) {
- objheader_t *header;
- void *ptrcreate;
- int i;
- struct objlist *ptr=newobjs;
- while(ptr!=NULL) {
- int max=ptr->offset;
- for(i=0;i<max;i++) {
- //clear the new flag
- ((struct ___Object___ *)ptr->objs[i])->___objstatus___=0;
- }
- ptr=ptr->next;
- }
-
- /* Copy from transaction cache -> main object store */
- for (i = 0; i < *numoidwrlocked; i++) {
- /* Read from the main heap */
- header = (objheader_t *)(((char *)(oidwrlocked[i])) - sizeof(objheader_t));
- int tmpsize;
- GETSIZE(tmpsize, header);
- struct ___Object___ *dst=(struct ___Object___*)oidwrlocked[i];
- struct ___Object___ *src=t_chashSearch(oidwrlocked[i]);
- dst->___cachedCode___=src->___cachedCode___;
- dst->___cachedHash___=src->___cachedHash___;
- memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
- header->version += 1;
- }
-
- /* Release read locks */
- for(i=0; i< *numoidrdlocked; i++) {
- /* Read from the main heap */
- header = (objheader_t *)(((char *)(oidrdlocked[i])) - sizeof(objheader_t));
- read_unlock(&header->lock);
- }
-
- /* Release write locks */
- for(i=0; i< *numoidwrlocked; i++) {
- header = (objheader_t *)(((char *)(oidwrlocked[i])) - sizeof(objheader_t));
- write_unlock(&header->lock);
- }
-
- return 0;
-}
-