9a10,12
> <<<<<<< proc.c
>  *	  $Header: /home/postgres/cvs_root/postgres-r/src/backend/storage/lmgr/d,v 1.1.1.1 2001/05/25 14:29:48 postgres Exp $
> =======
10a14
> >>>>>>> 1.1.1.1.8.1
66a71,73
> <<<<<<< proc.c
>  * $Header: /home/postgres/cvs_root/postgres-r/src/backend/storage/lmgr/d,v 1.1.1.1 2001/05/25 14:29:48 postgres Exp $
> =======
67a75
> >>>>>>> 1.1.1.1.8.1
103c111
< static void ProcAdjustSemaphore(SEMA *sem, int exceedUps);
---
> static void ProcAdjustSemaphore(int exceedUps);
108a117
> 
119d127
< 
194d201
< 
197,200d203
< 	printf("after signal\n");
< 	fflush(stdout);
< 	sleep(1);
< 
203,206d205
< 
< 	printf("after spinaquire\n");
< 	fflush(stdout);
< 	sleep(1);
216,218d214
< 		printf("after shmeminit\n");
< 	fflush(stdout);
< 	sleep(1);
240d235
< 	elog(NOTICE,"init process somewhere in the middle");
259c254
< 		elog(NOTICE,"get new proc");
---
> 
277,282d271
< #ifdef PROC_DEBUG
< 			elog(NOTICE, "[%d]lockqueue", MyProcPid);
< 			dumpQ(&MyProc->lockQueue, "\t before sem init ");
< 			elog(NOTICE, "[%d]shadowList", MyProcPid);
< 			dumpQ(&MyProc->shadowList, "\t before sem init");
< #endif		
301,305d289
< 
< 		printf("before semaphoe 1\n");
< 		fflush(stdout);
< 		sleep(1);
< 		
308,311d291
< 		printf("before semaphoe 2\n");
< 		fflush(stdout);
< 		sleep(1);
< 
319,323d298
< 
< 				printf("before semaphoe 3\n");
< 		fflush(stdout);
< 		sleep(1);
< 
335,364d309
< 		
< 		printf("before sema 2\n");
< 		fflush(stdout);
< 		sleep(1);
< 		
< 		/* bk: now do the same for semaphore for multiple acquisition */
< 		ProcGetNewSemKeyAndNum(&semKey, &semNum);
< 
< 		printf("before sema 3\n");
< 	fflush(stdout);
< 	sleep(1);
< 
< 		semId = IpcSemaphoreCreate(semKey,
< 								   PROC_NSEMS_PER_SET,
< 								   IPCProtection,
< 								   IpcSemaphoreDefaultStartValue,
< 								   0,
< 								   &semstat);
< 
< 		semun.val = IpcSemaphoreDefaultStartValue;
< 		semctl(semId, semNum, SETVAL, semun);
< 
< 
< 
< 		IpcSemaphoreLock(semId, semNum, IpcExclusiveLock);
< 		MyProc->sem_multiple.semId = semId;
< 		MyProc->sem_multiple.semNum = semNum;
< 		MyProc->sem_multiple.semKey = semKey;
< 		
< 
367d311
< 	{
369,370d312
< 		MyProc->sem_multiple.semId = -1;
< 	}
372,375d313
< 
< 		printf("after sema\n");
< 	fflush(stdout);
< 	sleep(1);
460c398
< 	elog(NOTICE, "[%d]inserting into shadowList no %d",MyProcPid, MAKE_OFFSET(result_shdw));
---
> 	elog(NOTICE, "[%d]inserting into shadowList",MyProcPid);
492d429
< 	PROC			*proc;
510d446
< 	proc = (PROC *) MAKE_PTR(result_shdw->proc);
512d447
< 	/* shadow->shadowLinks == proc->shadowList */
514,515c449,450
< 	elog(NOTICE, "[%d]removing from shadowList %d",MyProcPid,MAKE_OFFSET(result_shdw));
< 	dumpQ(&proc->shadowList, "\tbefore:");
---
> 	elog(NOTICE, "[%d]removing from shadowList",MyProcPid);
> 	dumpQ(&MyProc->shadowList, "\tbefore:");
520c455
< 	dumpQ(&proc->shadowList, "\tafter:");
---
> 	dumpQ(&MyProc->shadowList, "\tafter:");
569a505,548
> <<<<<<< proc.c
> 	for(i = 0; i < lock->grantedQueue.size; i++)
> 	{
> 		SHMQueueFirst(queue, (Pointer *) &curr, &curr->grantedLink);
> 		if(curr->abortpid != MyProcPid &&
> 			curr->holders[READ_LOCK] > 0)
> 		{
> 			if(ShmemPIDLookup(curr->abortpid, &proc_offset))
> 			{
> 				proc = (PROC *) MAKE_PTR(proc_offset);
> 				switch (proc->state)
> 				{
> 					case READ_PHASE:
> 					case SEND_PHASE:
> 					case SEND_PHASE_2pc:
> 						// -(ib) used for 2pc
> //						elog(NOTICE, "Setting aborted flag at process %d", 
> //							curr->abortpid);
> 						proc->abortFlag = TRUE;
> 						if(IpcSemaphoreGetCount(proc->sem.semId, 
> 												proc->sem.semNum) > 0)
> 						{
> 							proc->errType = ERR_INT_ABORT;
> 							IpcSemaphoreUnlock(proc->sem.semId, 
> 											   proc->sem.semNum, 
> 											   IpcExclusiveLock);
> 						}
> 						break;
> 
> 					default:
> 						break;
> 				}
> 			}
> 			else
> 			{
> 				proc_exit(600);
> 			}
> 		}
> 		queue = &curr->grantedLink;
> 	}
> }
> 
> 
> =======
598c577
< 						elog(NOTICE,"Setting aborted-flag for txn %x (pid %d)", 
---
> 						printf("Setting aborted-flag for txn %x (pid %d)\n", 
609,610d587
< 						/* bk: uses old semphore */
< 						elog(NOTICE,"before touching semaphore");
620,626d596
< 						/* bk: if proc is in send phase: wake it up so that it aborts */
< 						if (proc->state == SEND_PHASE)
< 						  {
< 							elog(NOTICE,"is really in send phase %d", proc->pid);
< 							kill(proc->pid, SIGQUIT);
< 						  }
< 
779,780d748
< 		/* bk: do not know which semaphore to use here, procedure not used right now */
< 		elog(NOTICE,"before sem 2");
886c854
< 					elog(NOTICE,"Setting Abort flag for txn %x (pid %d)\n", 
---
> 					printf("Setting Abort flag for txn %x (pid %d)\n", 
894,896d861
< 					/* bk: not ProcRemoveWaiting must derecement also the
< 					* nholding counters if the proc was waiting for a lock
< 					*/
898d862
< 					elog(NOTICE,"before sem 3");
902d865
< 
909,914d871
< 					if (proc->state == SEND_PHASE)
< 						  {
< 							elog(NOTICE,"is really in send phase %d", proc->pid);
< 							kill(proc->pid, SIGQUIT);
< 						  }
< 
929a887
> >>>>>>> 1.1.1.1.8.1
945,949c903
< *	ProcRemoveWaiting - removes process shadow from wait queues
< *   (bk) note: this can only happen in read phase. Then, the waiting
< *    process can only wait for one single lock, and hence, there is
< *   only one shadow in the queue. 
< *   hence, the while is turned to a if-statement 
---
> *	ProcRemoveWaiting - removes process shadows from wait queues
956,957d909
< 	XIDLookupEnt *xidentP;
< 	LOCK		 *lock;
959,960c911
< 	/*while(proc->shadowList.next != MAKE_OFFSET(&proc->shadowList))*/
< 	if (proc->shadowList.next != MAKE_OFFSET(&proc->shadowList))
---
> 	while(proc->shadowList.next != MAKE_OFFSET(&proc->shadowList))
962,1018c913,924
< 	  SHMQueueFirst(&proc->shadowList, (Pointer *) &curr_shdw, &curr_shdw->shadowLinks);
< 	  if(curr_shdw->waitLinks.prev != INVALID_OFFSET)
< 	  {
< #ifdef PROC_DEBUG
< 		elog(NOTICE, "[%d]ProcRemoveWaiting procid %d", MyProcPid, proc->pid);
< 		dumpQ(&curr_shdw->waitLock->waitProcs.links, "\tbefore:");
< #endif
< 			
< 		/* delete it from lock->waitProcs.links == shadow->waitLinks */
< 		SHMQueueDelete(&curr_shdw->waitLinks);
< 		SHMQueueElemInit(&curr_shdw->waitLinks);
< 		lock = curr_shdw->waitLock;
< 		--lock->waitProcs.size;
< 		/*--curr_shdw->waitLock->waitProcs.size;*/
< 		/* bk: decrement also the nHolding counters */
< 		/* we want to do this immediately and not when the process wakes up */
< 		/* Decrement the lock nHolding and holders fields as
< 		* we are no longer waiting on this lock.
< 		 */
< 		lock->nHolding--;
< 		/* bk: i know it must be a read lock that will be removed here ! */
< 		lock->holders[READ_LOCK]--;
< 		Assert((lock->nHolding >= 0) && (lock->holders[READ_LOCK] >= 0));
< 		Assert(lock->nActive <= lock->nHolding);
< 			
< #ifdef PROC_DEBUG
< 		dumpQ(&curr_shdw->waitLock->waitProcs.links, "\tafter:");
< #endif	
< 			 
< 		/* bk: before all shadows were deleted (whether still waiting for a lock, i.e. where waitLinks
< 		 * not empty, or not waiting for locks, i.e. in the meanwhile the lock was
< 		 * granting and the shadow is simply still floating around because the
< 		 * process itself has not waken up yet).
< 		 * now, we only delete them when they were not yet granted, hence, the parentheses
< 		 * is not commented out and put later */
< 		/* }*/
< 
< #ifdef PROC_DEBUG
< 		elog(NOTICE, "[%d]ProcRemoveWaiting procid %d from lockqueue", MyProcPid, proc->pid);
< 		dumpQ(&proc->lockQueue,"\tbefore:");
< #endif
< 		/* bk: we also have to delete the xid entry in the proc's lockqueye! */
< 		/* shadow->xidentP->queue == proc->lockQueue */
< 		xidentP = curr_shdw->xidentP;
< 		SHMQueueDelete(&xidentP->queue);
< #ifdef PROC_DEBUG
< 		dumpQ(&proc->lockQueue,"\tafter:");
< #endif
< 		/* bk: new we cannot delete shadow here, because we have to behave
< 		 * same as grant lock, we don't know which will be the last
< 		 * before the process wakes up: ProcLockWakeup or ProcRemoveWaiting
< 		 * and hence the shadow must be deleted in WaitOnLock in any of the cases */
< 		/* and now we have to delete it from the proc's shadowlist and
< 		* we have to delete the shadow itself
< 		* shadow->shadowLinks == proc->shadowList */
< 		/* DeleteShadow(proc->pid, curr_shdw->waitLock);*/
< 	  }
---
> 		SHMQueueFirst(&proc->shadowList, (Pointer *) &curr_shdw, &curr_shdw->shadowLinks);
> 		if(curr_shdw->waitLinks.prev != INVALID_OFFSET)
> 		{
> 			SHMQueueDelete(&curr_shdw->waitLinks);
> 			SHMQueueElemInit(&curr_shdw->waitLinks);
> 			--curr_shdw->waitLock->waitProcs.size;
> 			 //IMPORTANT FOR DeleteShadow to work
> 		}
> //		printf("deleting shadow no.%d(pid %d, lock %d)\t", MAKE_OFFSET(curr_shdw), MyProcPid, MAKE_OFFSET(curr_shdw->waitLock));
> //		fflush(stdout);
> 		
> 		DeleteShadow(proc->pid, curr_shdw->waitLock);
1046,1048d951
< 	/* bk: now also for multiple */
< 	/*ProcFreeSem(proc->sem_multiple.semKey, proc->sem_multiple.semNum);*/
<   ProcFreeSem(proc->sem.semKey, proc->sem.semNum);
1178d1080
< 
1185,1186c1087
< 				  REQ_MODE reqmode,
< 				  XIDLookupEnt *xidentP)
---
> 				  REQ_MODE reqmode)	
1252,1254d1152
< 	my_shdw->xidentP = xidentP;
< 	/* bk: now the shadow has the reqmode strored to unlock the right semaphore */
< 	my_shdw->req_mode = reqmode;
1286c1184
< 	elog(NOTICE, "[%d]inserting myself into lock wait queue shd no %d", MyProcPid, MAKE_OFFSET(my_shdw));
---
> 	elog(NOTICE, "[%d]inserting myself into lock wait queue", MyProcPid);
1325c1223
< 		/* MyProc->abortFlag = FALSE; */
---
> 		MyProc->abortFlag = FALSE;
1356,1358d1253
< 		 /* bk: procsleep is used for request mode REQ_SINGLE, hence the old
< 		  * semaphore is used */
< 		elog(NOTICE,"before sem4");
1391a1287,1288
> 	if(MyProc->errType == NO_ERROR)
> 		DeleteShadow(MyProcPid, lock);
1393,1398d1289
< 	/*if(MyProc->errType == NO_ERROR)
< 		DeleteShadow(MyProcPid, lock); */
< 
< 	/* bk: delete the shadow out of the lists both in NO_ERROR and ERROR case */
< 	DeleteShadow(MyProcPid, lock); 
< 
1459,1462d1349
< 		/* bk: new we take now the multiple semaphore */
< 		elog(NOTICE,"before sem 5");
< 		/*IpcSemaphoreLock(MyProc->sem_multiple.semId, MyProc->sem_multiple.semNum,
< 						 IpcExclusiveLock);*/
1482,1483d1368
< 	/* bk: new we take now the multiple semaphore */
< 	elog(NOTICE,"before sem6");
1485d1369
< 	/*IpcSemaphoreLock(MyProc->sem_multiple.semId, MyProc->sem_multiple.semNum, IpcExclusiveLock);*/
1516,1517c1400
< 	/*ProcAdjustSemaphore(&(MyProc->sem_multiple), i - 1);*/
< ProcAdjustSemaphore(&(MyProc->sem), i - 1);
---
> 	ProcAdjustSemaphore(i - 1);
1534d1416
< 	LOCK			*lock;
1540,1541d1421
< 	lock = shdw->waitLock;
< 	
1553,1562c1433,1434
< 	elog(NOTICE,"before sem 8");
< 	if (shdw->req_mode == REQ_SINGLE)
< 	{
< 	  IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum, IpcExclusiveLock);
< 	}
< 	else
< 	{
< 		  IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum, IpcExclusiveLock);
< 	 /* IpcSemaphoreUnlock(proc->sem_multiple.semId, proc->sem_multiple.semNum, IpcExclusiveLock);*/
< 	}
---
> 	IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum, IpcExclusiveLock);
> 
1586,1587d1457
< 	{
< 		elog(NOTICE,"release: no one waiting");
1589d1458
< 	}
1593d1461
< 
1613,1614d1480
< 		{
< 			elog(NOTICE,"same as last lock type");
1616d1481
< 		}
1628d1492
< 			elog(NOTICE,"conflicts");
1639,1656c1503,1507
< 		/* bk: weird race condition: if the txn belonging to this
< 		 * shadow must be aborted (abortFlag == TRUE) we should
< 		 * not grant this lock but simply ignore it, the corresponding
< 		 * process has been woken up already by the one who set
< 		 * the abort flag to true, when the process finally wakes
< 		 * up it will release it from the wait queue
< 		*/
< 		/* (bk) now i undo this again, the case where the abort
< 		* flag is set should not exist anymore, because whoever
< 		* sets the abort flag of another process removes all entries
< 		* correlated to any waiting lock out of any queue! */
< 		if (!proc->abortFlag)
< 		{
< 		  GrantLock(lock, proc->xid, lockmethod, shdw->token);
< 		  /*
< 		   * ProcWakeup removes proc from the lock waiting process queue and
< 		   * returns the next proc in chain.
< 		   */
---
> 		GrantLock(lock, proc->xid, lockmethod, shdw->token);
> 		/*
> 		 * ProcWakeup removes proc from the lock waiting process queue and
> 		 * returns the next proc in chain.
> 		 */
1658,1659c1509,1510
< 		  count++;
< 		  queue->size--;
---
> 		count++;
> 		queue->size--;
1662,1663c1513,1514
< 		  elog(NOTICE, "[%d]removing from waitQueue shd no %d",MyProcPid,MAKE_OFFSET(shdw));
< 		  dumpQ(&queue->links, "\tbefore:");
---
> 		elog(NOTICE, "[%d]removing from waitQueue",MyProcPid);
> 		dumpQ(&queue->links, "\tbefore:");
1666c1517
< 		  shdw = ProcWakeup(shdw, NO_ERROR);
---
> 		shdw = ProcWakeup(shdw, NO_ERROR);
1669c1520
< 		  dumpQ(&queue->links, "\tafter:");
---
> 		dumpQ(&queue->links, "\tafter:");
1671,1677c1522
< 		}
< 		else
< 		{
< 		  elog(NOTICE,"there should not be a set abortFlag at this place");
< 		}
< 	  }
< 		
---
> 	}
1679c1524
< 	  Assert(queue->size >= 0);
---
> 	Assert(queue->size >= 0);
1681,1695c1526,1536
< 	  if (count){
< 		  return STATUS_OK;
< 	  }
< 	  else
< 	  {
< 		  /* Something is still blocking us.	May have deadlocked. */
< 		  /* bk wakeup might fail because of two correct situations:
< 		   * the process will be woken up by the one who forces it to
< 		   * abort or -- and another reason I just have forgotten
< 		   * but also only in the case of remote txn */
< 		  trace_flag = (lock->tag.lockmethod == USER_LOCKMETHOD) ? \
< 			  TRACE_USERLOCKS : TRACE_LOCKS;
< 		  TPRINTF(trace_flag,
< 			  	"ProcLockWakeup: lock(%x) can't wake up any process",
< 				  MAKE_OFFSET(lock));
---
> 	if (count){
> 		return STATUS_OK;
> 	}
> 	else
> 	{
> 		/* Something is still blocking us.	May have deadlocked. */
> 		trace_flag = (lock->tag.lockmethod == USER_LOCKMETHOD) ? \
> 			TRACE_USERLOCKS : TRACE_LOCKS;
> 		TPRINTF(trace_flag,
> 				"ProcLockWakeup: lock(%x) can't wake up any process",
> 				MAKE_OFFSET(lock));
1697,1698c1538,1539
< 		  if (pg_options[trace_flag] >= 2)
< 			  DumpAllLocks();
---
> 		if (pg_options[trace_flag] >= 2)
> 			DumpAllLocks();
1700,1701c1541,1542
< 		  elog(NOTICE,"wakeup failed:%d (on pid %x / %d, me = %x / %d)\n",count, proc->xid, proc->pid, GetCurrentTransactionId(), MyProcPid );
< 		  return STATUS_NOT_FOUND;
---
> 		printf("wakeup failed:%d (on pid %x / %d, me = %x / %d)\n",count, proc->xid, proc->pid, GetCurrentTransactionId(), MyProcPid );
> 		return STATUS_NOT_FOUND;
1739,1744d1579
< 	else if (txn_type == REPLICATED_LOCAL)
< 	{
< 		HandleDeadLock(sig);
< 		printf("in Handle Alarm: Txn %x has abortflag set, aborts now", GetCurrentTransactionId());
< 		elog(ERROR, "Detected internal abort, aborting(7)");
< 	}
1762c1597
< 			elog(NOTICE,"received nothing");
---
> 			printf("received nothing\n");
1767c1602
< 			elog(NOTICE,"Received commit message in HandleAlarm");
---
> 			printf("Received commit message in HandleAlarm\n");
1783d1617
< 		/* bk: don't know what to do here ! PREDICT_ABORT not used */
1799d1632
< 				/* bk: don't know what to do here ! PREDICT_ABORT not used */
1807d1639
< 		/* bk: don't know what to do here ! PREDICT_ABORT not used */
1846d1677
< 	elog(NOTICE,"before sem9");
1951d1781
< 		elog(NOTICE,"before sem10");
2010c1840
< 	int32	   *freeSemMap; /*= ProcGlobal->freeSemMap;*/
---
> 	int32	   *freeSemMap = ProcGlobal->freeSemMap;
2013d1842
< 
2018,2033d1846
< 	 
< 	 printf("get key \n");
< 				fflush(stdout);
< 				sleep(1);
< 	 
< 	printf("get key proc_nsems %d maxprox %d \n", PROC_NSEMS_PER_SET, MAX_PROC_SEMS);
< 				fflush(stdout);
< 				sleep(1);
< 	
< 	
< 	freeSemMap = ProcGlobal->freeSemMap;
< 	
< 	printf(" 2get key proc_nsems %d maxprox %d \n", PROC_NSEMS_PER_SET, MAX_PROC_SEMS);
< 				fflush(stdout);
< 				sleep(1);
< 				 
2041,2045d1853
< 		{
< 		  printf("non free in this i \n");
< 		  fflush(stdout);
< 		  sleep(1);
< 		  
2047d1854
< 		}
2061,2063d1867
< 				printf("have found\n");
< 				fflush(stdout);
< 				sleep(1);
2067,2069d1870
< 		  printf("non free in this j \n");
< 		  fflush(stdout);
< 		  sleep(1);
2072,2074c1873
< 		printf("cannot allocate 1\n");
< 		fflush(stdout);
< 		sleep(1);
---
> 
2091c1890
< /* bk: new, give as input also the SEMA */
---
> 
2093c1892
< ProcAdjustSemaphore(SEMA *sem, int exceedUps)
---
> ProcAdjustSemaphore(int exceedUps)
2097,2098c1896
< 	  IpcSemaphoreLock(sem->semId, sem->semNum, IpcExclusiveLock);
< 		/*IpcSemaphoreLock(MyProc->sem.semId, MyProc->sem.semNum, IpcExclusiveLock);*/
---
> 		IpcSemaphoreLock(MyProc->sem.semId, MyProc->sem.semNum, IpcExclusiveLock);
2117d1914
< 	elog(NOTICE,"in free sem");
2137,2138d1933
< 	elog(NOTICE,"in all free");
< 
