コード例 #1
0
        // 获得锁对象
        RecordLock GetLock(string strID,
                           bool bAutoCreate = true)
        {
            // 加写锁
            this._lockForWrite(m_nLockTimeout);
            try
            {
                RecordLock reclock = (RecordLock)RecordLocks[strID];

                if (reclock == null)
                {
                    if (bAutoCreate == true)
                    {
                        reclock         = new RecordLock(this.m_nLockTimeout);
                        reclock.m_strID = strID;
                        RecordLocks.Add(strID, reclock);
                    }
                    else
                    {
                        return(null);
                    }
                }

                Interlocked.Increment(ref reclock.m_nUseCount);
                // Debug.WriteLine("record lock count " + RecordLocks.Count);
                return(reclock);
            }
            finally
            {
                this._unlockForWrite();
            }
        }
コード例 #2
0
        // 写锁定
        public void LockForWrite(string strID,
                                 int nTimeOut)
        {
            RecordLock reclock = GetLock(strID);

            // Interlocked.Increment(ref reclock.m_nUseCount);

            // 加写锁
            try
            {
                reclock._lockForWrite(nTimeOut);
            }
            catch (Exception ex)
            {
                Interlocked.Decrement(ref reclock.m_nUseCount);
                // 是否还要删除?

                throw ex;
            }
        }
コード例 #3
0
        public void UnlockForWrite(string strID)
        {
            RecordLock reclock = GetLock(strID, false);

            if (reclock == null)
            {
                throw new Exception("id '" + strID + "' 没有找到对应的记录锁");
            }

            try
            {
                reclock._unlockForWrite();
            }
            finally
            {
                Interlocked.Decrement(ref reclock.m_nUseCount);
                Interlocked.Decrement(ref reclock.m_nUseCount);

                TryRemoveLock(reclock);
            }
        }
コード例 #4
0
 // 试图移走锁对象
 // 应当在RecordLock:m_lock解锁以后进行
 void TryRemoveLock(RecordLock reclock)
 {
     // 加写锁
     this._lockForWrite(m_nLockTimeout);
     try
     {
         int nRet = Interlocked.Increment(ref reclock.m_nUseCount);
         if (nRet == 1) // 说明增量以前为0
         {
             this.RecordLocks.Remove(reclock.m_strID);
         }
         else
         {
             Interlocked.Decrement(ref reclock.m_nUseCount);
         }
     }
     finally
     {
         this._unlockForWrite();
     }
 }
コード例 #5
0
        // 输出到文本
        public string DumpText()
        {
            // 加读锁
            this._lockForRead(m_nLockTimeout);
            try
            {
                string strResult = "";

                foreach (string key in RecordLocks.Keys)
                {
                    RecordLock onelock = (RecordLock)this.RecordLocks[key];

                    strResult += "id='" + onelock.m_strID + "' usecount='" + Convert.ToString(onelock.m_nUseCount) + "' hashcode='" + onelock.GetLockHashCode() + "'\r\n";
                }

                return(strResult);
            }
            finally
            {
                this._unlockForRead();
            }
        }