Пример #1
0
        internal void Release(Guid dbGuid, AmDbLockReason lockReason)
        {
            int currentManagedThreadId = Environment.CurrentManagedThreadId;

            AmTrace.Debug("releasing lock for db={0} requester={1} reason={2}", new object[]
            {
                dbGuid,
                currentManagedThreadId,
                lockReason
            });
            lock (this.m_locker)
            {
                AmDbLock.LockData lockData = null;
                if (this.m_lockTable.TryGetValue(dbGuid, out lockData) && lockData.ThreadId == currentManagedThreadId)
                {
                    lockData.RefCount--;
                    if (lockData.RefCount == 0)
                    {
                        this.m_lockTable.Remove(dbGuid);
                    }
                }
            }
            AmTrace.Debug("lock released for db={0} requester={1}", new object[]
            {
                dbGuid,
                currentManagedThreadId
            });
        }
Пример #2
0
        internal bool Lock(Guid dbGuid, AmDbLockReason lockReason)
        {
            bool flag = false;
            int  num  = 0;
            int  currentManagedThreadId = Environment.CurrentManagedThreadId;

            AmTrace.Debug("Trying to acquire lock for db={0} requester={1} reason={2}", new object[]
            {
                dbGuid,
                currentManagedThreadId,
                lockReason
            });
            do
            {
                lock (this.m_locker)
                {
                    AmDbLock.LockData lockData = null;
                    if (this.m_lockTable.TryGetValue(dbGuid, out lockData))
                    {
                        AmTrace.Debug("lock already held for db={0} heldby={1} current={2} reason={3}", new object[]
                        {
                            dbGuid,
                            lockData.ThreadId,
                            currentManagedThreadId,
                            lockData.Reason
                        });
                        if (lockData.ThreadId == currentManagedThreadId)
                        {
                            lockData.RefCount++;
                            flag = true;
                            AmTrace.Debug("same thread has requested lock db={0} heldby={1} refcount={2}", new object[]
                            {
                                dbGuid,
                                lockData.ThreadId,
                                lockData.RefCount
                            });
                        }
                    }
                    else
                    {
                        lockData = new AmDbLock.LockData(currentManagedThreadId, lockReason);
                        this.m_lockTable[dbGuid] = lockData;
                        flag = true;
                        AmTrace.Debug("lock created for db={0} owner={1} reason={2}", new object[]
                        {
                            dbGuid,
                            lockData.ThreadId,
                            lockData.Reason
                        });
                    }
                }
                if (flag)
                {
                    break;
                }
                AmTrace.Debug("sleeping to get the lock again for db={0} requester={1} reason={2} sleptSoFar={3}", new object[]
                {
                    dbGuid,
                    currentManagedThreadId,
                    lockReason,
                    num
                });
                Thread.Sleep(500);
                num += 500;
                AmDbLock.WarnIfSleepingForEver(dbGuid, num, lockReason);
            }while (!this.IsExiting);
            if (this.IsExiting && flag)
            {
                this.Release(dbGuid, lockReason);
                flag = false;
            }
            AmTrace.Debug("Got lock!! db={0} requester={1}", new object[]
            {
                dbGuid,
                currentManagedThreadId
            });
            return(flag);
        }