Esempio n. 1
0
        //When a transaction gets a shared lock, it is added among the owners and the lock type is set to SHARED
        private void GetShared(Transaction transaction)
        {
            string tname   = "T" + transaction.Id;
            string message = tname + " <-- S(" + ObjectName + ")\n";

            Logger.Log += message;
            Console.Write(message);
            Kind = LockType.SHARED;
            OwnerIds.Add(transaction);
        }
Esempio n. 2
0
        //When a transaction gets an exclusive lock, it is added among the onwers if it is not already there and the lock typ is set to EXCLUSIVE.
        //A transaction might already be in the owner list when requesting an EXCLUSIVE lock when it is upgrading its SHARED lock, i.e.
        //it is the only transaction owning the lock and that lock is SHARED.
        private void GetExclusive(Transaction transaction)
        {
            string tname   = "T" + transaction.Id;
            string message = tname + " <-- X(" + ObjectName + ")\n";

            Logger.Log += message;
            Console.Write(message);
            Kind = LockType.EXCLUSIVE;
            if (!OwnerIds.Contains(transaction))
            {
                OwnerIds.Add(transaction);
            }
        }
Esempio n. 3
0
 public bool IsOwner(IUser u) => OwnerIds.Contains(u.Id);
Esempio n. 4
0
 public bool isOwner(DiscordUser u) => OwnerIds.Contains((long)u.Id);
Esempio n. 5
0
 public bool IsOwner(IUser user)
 {
     return(OwnerIds.Contains(user.Id));
 }
Esempio n. 6
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;
            }
        }
Esempio n. 7
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);
 }
 public bool IsOwner(IUser u)
 => u != null && OwnerIds.Contains(u.Id);