예제 #1
0
        /// <summary>
        /// Attempt to lock the specified lock.
        /// If the lock is acquired, add the lock to the accumulated list of locks.
        /// Return the gotLock result.
        /// </summary>
        /// <param name="LockToLock"></param>
        /// <returns></returns>
        public bool Lock(IGenericLock LockToLock)
        {
            bool gotLock = LockToLock.Lock();

            if (gotLock == true)
            {
                AccumulatedLocks.Add(LockToLock);
            }
            else
            {
                this.GotAllLocks = false;
            }
            return(gotLock);
        }
예제 #2
0
        private bool DoYieldUntilLock()
        {
            bool gotLock    = false;
            int  yieldCount = 0;

            while (true)
            {
                // either get the single lock. or get all the many locks.
                {
                    if (_Lock != null)
                    {
                        gotLock = _Lock.Lock();
                    }
                    else if (_ManyLocks != null)
                    {
                        gotLock = _ManyLocks.LockAll();
                    }
                    else
                    {
                        throw new ApplicationException("nothing to lock");
                    }
                }

                // got the lock. return to caller.
                if (gotLock == true)
                {
                    break;
                }

                // did not get the lock. Yield to other threads. Then loop back and
                // try to lock again.
                Thread.Yield();

                // too many yields.
                {
                    yieldCount += 1;
                    if (yieldCount > 1000000)
                    {
                        throw new ApplicationException("too many yields. could be deadlock.");
                    }
                }
            }
            return(gotLock);
        }