예제 #1
0
        /// <summary>
        /// 释放锁并等待唤醒
        /// </summary>
        /// <param name="value">必须是当前线程已经申请的锁对象, 是引用类型且不能为null</param>
        public static void Wait(object value)
        {
            object newValue = null;

            if (fastCSharp.config.pub.Default.LockCheckMinutes != 0)
            {
                lockInfo lockInfo;
                Monitor.Enter(interlock);
                try
                {
                    if (lockInfos.TryGetValue(new objectReference {
                        Value = value
                    }, out lockInfo))
                    {
                        lockInfo.StackTrace = new StackTrace(true);
                        lockInfos.Add(new objectReference {
                            Value = newValue = new object()
                        }, lockInfo);
                        lockInfos.Remove(new objectReference {
                            Value = value
                        });
                    }
                }
                finally { Monitor.Exit(interlock); }
                if (lockInfo.StackTrace == null)
                {
                    log.Error.Add("没有找到需要释放的锁", null, true);
                }
            }
            Monitor.Wait(value);
            if (newValue != null)
            {
                Monitor.Enter(interlock);
                try
                {
                    lockInfo lockInfo = lockInfos[new objectReference {
                                                      Value = newValue
                                                  }];
                    lockInfos.Add(new objectReference {
                        Value = value
                    }, lockInfo);
                    lockInfos.Remove(new objectReference {
                        Value = newValue
                    });
                }
                finally { Monitor.Exit(interlock); }
            }
        }
예제 #2
0
        /// <summary>
        /// 释放锁并等待唤醒
        /// </summary>
        /// <param name="value">必须是当前线程已经申请的锁对象, 是引用类型且不能为null</param>
        public static void Wait(object value)
        {
            object newValue = null;

            if (fastCSharp.config.pub.Default.LockCheckMinutes != 0)
            {
                while (Interlocked.CompareExchange(ref interlock, 1, 0) != 0)
                {
                    Thread.Sleep(1);
                }
                lockInfo lockInfo;
                if (lockInfos.TryGetValue(value, out lockInfo))
                {
                    lockInfo.StackTrace    = new StackTrace();
                    lockInfos.Add(newValue = new object(), lockInfo);
                    lockInfos.Remove(value);
                }
                else
                {
                    log.Default.Add("没有找到需要释放的锁", true, true);
                }
                interlock = 0;
            }
            Monitor.Wait(value);
            if (newValue != null)
            {
                while (Interlocked.CompareExchange(ref interlock, 1, 0) != 0)
                {
                    Thread.Sleep(1);
                }
                lockInfo lockInfo = lockInfos[newValue];
                lockInfos.Add(value, lockInfo);
                lockInfos.Remove(newValue);
                interlock = 0;
            }
        }