A spin-lock : Custom Lock « Thread « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Thread » Custom Lock 
20.17.1.A spin-lock
/*
Quote from

Professional .NET Framework 2.0 (Programmer to Programmer) (Paperback)
by Joe Duffy (Author)


# Paperback: 601 pages
# Publisher: Wrox (April 10, 2006)
# Language: English
# ISBN-10: 0764571354
# ISBN-13: 978-0764571350

*/


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading;



class SpinLock
{
    private int state;
    private EventWaitHandle available = new AutoResetEvent(false);

    // This looks at the total number of hardware threads available; if it's
    // only 1, we will use an optimized code path
    private static bool isSingleProc = (Environment.ProcessorCount == 1);

    private const int outerTryCount = 5;
    private const int cexTryCount = 100;

    public void Enter(out bool taken)
    {
        // Taken is an out parameter so that we set it *inside* the critical
        // region, rather than returning it and permitting aborts to creep in.
        // Without this, the caller could take the lock, but not release it
        // because it didn't know it had to.
        taken = false;

        while (!taken)
        {
            if (isSingleProc)
            {
                // Don't busy wait on 1-logical processor machines; try
                // a single swap, and if it fails, drop back to EventWaitHandle.
                Thread.BeginCriticalRegion();
                taken = Interlocked.CompareExchange(ref state, 10== 0;
                if (!taken)
                    Thread.EndCriticalRegion();
            }
            else
            {
                for (int i = 0; !taken && i < outerTryCount; i++)
                {
                    // Tell the CLR we're in a critical region;
                    // interrupting could lead to deadlocks.
                    Thread.BeginCriticalRegion();

                    // Try 'cexTryCount' times to CEX the state variable:
                    int tries = 0;
                    while (!(taken =
                        Interlocked.CompareExchange(ref state, 10== 0&&
                        tries++ < cexTryCount)
                    {
                        Thread.SpinWait(1);
                    }

                    if (!taken)
                    {
                        // We failed to acquire in the busy spin, mark the end
                        // of our critical region and yield to let another
                        // thread make forward progress.
                        Thread.EndCriticalRegion();
                        Thread.Sleep(0);
                    }
                }
            }

            // If we didn't acquire the lock, block.
            if (!takenavailable.WaitOne();
        }

        return;
    }

    public void Enter()
    {
        // Convenience method. Using this could be prone to deadlocks.
        bool b;
        Enter(out b);
    }

    public void Exit()
    {
        if (Interlocked.CompareExchange(ref state, 01== 1)
        
            // We notify the waking threads inside our critical region so
            // that an abort doesn't cause us to lose a pulse, (which could
            // lead to deadlocks).
            available.Set();
            Thread.EndCriticalRegion();
        }
    }
}

public class MainClass
{
    public static void Main()
    {
        SpinLock sl1 = new SpinLock();
        sl1.Enter();
        try
        {
            Console.WriteLine("Acquired the spin lock");
        }
        finally
        {
            sl1.Exit();
            Console.WriteLine("Released the spin lock");
        }

    }
}
Acquired the spin lock
Released the spin lock
20.17.Custom Lock
20.17.1.A spin-lock
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.