Click here to Skip to main content
15,893,161 members
Articles / Programming Languages / C#

Applied Use of LinFu/Cecil and Aspect-Oriented Programming Concepts - A Library

Rate me:
Please Sign up or sign in to vote.
4.88/5 (8 votes)
4 Sep 2008CPOL17 min read 35.2K   160   32  
A library of useful functionality using Aspect-Oriented Programming concepts, and implemented using the LinFu and Cecil.Mono projects/frameworks.
#define TrackConflicts

using System;
using System.ComponentModel;
using Hyper.ComponentModel;
using System.Runtime;
using System.Configuration;
using System.Threading;

namespace BrainTechLLC.ThreadSafeObjects
{
	public delegate bool Match<TListType>(TListType item);

    /// <summary>
    /// Basic support for simple spin-lock - for locking tiny sections of code only, avoiding lock()
    /// Uses atomic CompareExchange operation.  NOTE: no deadlock detection, same thread cannot lock an already-locked object    
    /// </summary>
    [TypeDescriptionProvider(typeof(HyperTypeDescriptionProvider)), Serializable]
    public class Lockable : ILockable
    {
        [NonSerialized]
        private readonly static int SpinCycles = 20;//Properties.Settings.Default.SpinCycles;

        [NonSerialized]
        protected static long _conflicts;

        [NonSerialized]
        protected int _lock;

        /// <summary>
        /// Returns the number of lock conflicts that have occurred
        /// </summary>
        public static long Conflicts { get { return _conflicts; } }

        /// <summary>
        /// Aquire the lock
        /// </summary>
        public void AquireLock()
        {
            // Assume that we will grab the lock - call CompareExchange
            if (Interlocked.CompareExchange(ref _lock, 1, 0) == 1)
            {
                int n = 0;

                // Could not grab the lock - spin/wait until the lock looks obtainable
                while (_lock == 1)
                {
                    if (n++ > SpinCycles)
                    {
#if TrackConflicts
                        Interlocked.Increment(ref _conflicts);
#endif
                        n = 0;
                        Thread.Sleep(0);
                    }
                }

                // Try to grab the lock - call CompareExchange
                while (Interlocked.CompareExchange(ref _lock, 1, 0) == 1)
                {
                    n = 0;

                    // Someone else grabbed the lock.  Continue to spin/wait until the lock looks obtainable
                    while (_lock == 1)
                    {
                        if (n++ > SpinCycles)
                        {
#if TrackConflicts
                            Interlocked.Increment(ref _conflicts);
#endif
                            n = 0;
                            Thread.Sleep(0);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Release the lock
        /// </summary>
        public void ReleaseLock()
        {
            _lock = 0;
        }

        public bool Locked { get { return (_lock != 0); } }

        public bool TryAquireLock()
        {
			if (_lock == 1) return false;
            return (Interlocked.CompareExchange(ref _lock, 1, 0) != 1);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Troppus Software
United States United States
Currently working as a Senior Silverlight Developer with Troppus Software in Superior, CO. I enjoy statistics, programming, new technology, playing the cello, and reading codeproject articles. Smile | :)

Comments and Discussions