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

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. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » Thread » Custom Lock 
20. 18. 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. 18. Custom Lock
20. 18. 1. A spin-lock
w__ww._j__ava_2__s._co_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.