Example #1
0
    public static void AssertLockIsHeld(object o)
    {
#if DEBUG_LOCK_TRACE
        LockTrace debug = GetTrace(o);
        if (debug == null || debug.Owner != Thread.CurrentThread)
        {
            throw new SynchronizationLockException(string.Format("No lock is held on object {0}.", o.GetType().ToString()));
        }
#endif
    }
Example #2
0
    public static void AssertLockIsHeld(object o, string message)
    {
#if DEBUG_LOCK_TRACE
        LockTrace debug = GetTrace(o);
        if (debug == null || debug.Owner != Thread.CurrentThread)
        {
            throw new SynchronizationLockException(message);
        }
#endif
    }
Example #3
0
    public static Cookie Lock(object o)
    {
        // Create the cookie which will release the lock.
#if DEBUG_LOCK_TRACE
        LockTrace t;
        lock (c_Traces.SyncRoot) {
            t = GetTrace(o);
            if (t == null)
            {
                c_Traces[new HashableWeakReference(o)] = t = new LockTrace(o);
            }
        }
        Cookie cookie = new Cookie(o, t);
#else
        Cookie cookie = new Cookie(o);
#endif
#if DEBUG_LOCK_CONTENTION
        if (!Monitor.TryEnter(o))
        {
            Thread owner = t.Owner;
            Debug.WriteLine(string.Format("Lock contention detected on object of type {0}:",
                                          o.GetType().ToString()), t.GetType().ToString());
            Debug.Indent();
            try {
                Debug.WriteLine(string.Format("Contending thread: {0}; Thread owning lock: {1}",
                                              Thread.CurrentThread.Name, owner == null ? null : owner.Name));
            } finally {
                Debug.Unindent();
            }
            Monitor.Enter(o);
        }
#else
        // Acquire the lock.
        Monitor.Enter(o);
#endif
        try {
#if DEBUG_LOCK_TRACE
            t.TraceEnter();
#endif
            return(cookie);
        } catch {
            // If there's an exception for any reason after the lock is acquired, make sure it's released.
            cookie.Dispose();
            throw;
        }
    }
Example #4
0
    public static Cookie Lock(object o) {
        // Create the cookie which will release the lock.
#if DEBUG_LOCK_TRACE
        LockTrace t;
        lock(c_Traces.SyncRoot) {
            t = GetTrace(o);
            if(t == null)
                c_Traces[new HashableWeakReference(o)] = t = new LockTrace(o);
        }
        Cookie cookie = new Cookie(o, t);
#else
        Cookie cookie = new Cookie(o);
#endif
#if DEBUG_LOCK_CONTENTION
        if(!Monitor.TryEnter(o)) {
            Thread owner = t.Owner;
            Debug.WriteLine(string.Format("Lock contention detected on object of type {0}:",
                o.GetType().ToString()), t.GetType().ToString());
            Debug.Indent();
            try {
                Debug.WriteLine(string.Format("Contending thread: {0}; Thread owning lock: {1}",
                    Thread.CurrentThread.Name, owner == null ? null : owner.Name));
            } finally {
                Debug.Unindent();
            }
            Monitor.Enter(o);
        }
#else
        // Acquire the lock.
        Monitor.Enter(o);
#endif
        try {
#if DEBUG_LOCK_TRACE
            t.TraceEnter();
#endif
            return cookie;
        } catch {
            // If there's an exception for any reason after the lock is acquired, make sure it's released.
            cookie.Dispose();
            throw;
        }
    }
Example #5
0
 internal Cookie(object o, LockTrace t)
 {
     this.t = t;
Example #6
0
 internal Cookie(object o, LockTrace t) {
     this.t = t;