示例#1
0
        internal void Acquire(Transaction transaction, LockType requestedType)
        {
            string tname   = "T" + transaction.Id;
            string message = "";

            if (requestedType == Kind && OwnerIds.Contains(transaction))
            {
                return;
            }
            switch (Kind)
            {
            //When a lock is UNLOCKED the transaction always gets the requested lock
            case LockType.UNLOCKED:
                switch (requestedType)
                {
                case LockType.SHARED:
                    GetShared(transaction);
                    break;

                case LockType.EXCLUSIVE:
                    GetExclusive(transaction);
                    break;

                default:
                    break;
                }
                break;

            case LockType.SHARED:
                switch (requestedType)
                {
                //A SHARED lock can always be acquired, if the current state of the lock is SHARED
                case LockType.SHARED:
                    GetShared(transaction);
                    break;

                case LockType.EXCLUSIVE:
                    //There is more than one transaction using the SHARED lock. In this case the EXCLUSIVE lock is denied and the
                    //requesting transaction is placed in queue.
                    if (OwnerIds.Exists(t => t.Id != transaction.Id))
                    {
                        message     = tname + " <-- " + "Wait(" + ObjectName + ")\n";
                        Logger.Log += message;
                        Console.Write(message);
                        AddToQueue(transaction, requestedType);
                    }
                    //If there exists no other transaction owning the SHARED lock, then the transaction can upgrade its SHARED lock
                    //to an EXCLUSIVE lock.
                    else
                    {
                        GetExclusive(transaction);
                    }
                    break;
                }
                break;

            case LockType.EXCLUSIVE:
                //if the lock is EXCLUSIVE, then any request to acquire it is denied an the requesting transaction is placed in queue
                switch (requestedType)
                {
                case LockType.SHARED:
                    message     = tname + " <-- " + "Wait(" + ObjectName + ")\n";
                    Logger.Log += message;
                    Console.Write(message);
                    AddToQueue(transaction, requestedType);
                    break;

                case LockType.EXCLUSIVE:
                    message     = tname + " <-- " + "Wait(" + ObjectName + ")\n";
                    Logger.Log += message;
                    Console.Write(message);
                    AddToQueue(transaction, requestedType);
                    break;
                }
                break;
            }
        }
示例#2
0
 public TransactionExecution(Scheduler scheduler, Transaction transaction)
 {
     this.scheduler   = scheduler;
     this.transaction = transaction;
 }
示例#3
0
 //add a transaction to queue and pause it
 private void AddToQueue(Transaction transaction, LockType lockType)
 {
     TransactionQueue.Enqueue(new Tuple <Transaction, LockType>(transaction, lockType));
     transaction.Paused = true;
 }
示例#4
0
 //When releasing the lock, the current transaction is removed from the owners. This function reports
 //whether the queue is empty or not.
 internal bool Release(Transaction transaction)
 {
     OwnerIds.Remove(transaction);
     return(OwnerIds.Count == 0);
 }