The Labs \ Source Viewer \ SSCLI \ System.Threading \ Interlocked

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace System.Threading
  16. {
  17.     using System;
  18.     using System.Security.Permissions;
  19.     using System.Runtime.CompilerServices;
  20.     using System.Runtime.ConstrainedExecution;
  21.    
  22.     // After much discussion, we decided the Interlocked class doesn't need
  23.     // any HPA's for synchronization or external threading. They hurt C#'s
  24.     // codegen for the yield keyword, and arguably they didn't protect much.
  25.     // Instead, they penalized people (and compilers) for writing threadsafe
  26.     // code.
  27.     public static class Interlocked
  28.     {
  29. /******************************
  30.         * Increment
  31.         *  Implemented: int
  32.         *                        long
  33.         *****************************/       
  34.        
  35.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  36.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  37.         public static extern int Increment(ref int location);
  38.        
  39.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  40.         public static extern long Increment(ref long location);
  41.        
  42. /******************************
  43.         * Decrement
  44.         *  Implemented: int
  45.         *                        long
  46.         *****************************/       
  47.        
  48.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  49.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  50.         public static extern int Decrement(ref int location);
  51.        
  52.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  53.         public static extern long Decrement(ref long location);
  54.        
  55. /******************************
  56.         * Exchange
  57.         *  Implemented: int
  58.         *                        long
  59.         *                        float
  60.         *                        double
  61.         *                        Object
  62.         *                        IntPtr
  63.         *****************************/       
  64.        
  65.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  66.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  67.         public static extern int Exchange(ref int location1, int value);
  68.        
  69.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  70.         public static extern long Exchange(ref long location1, long value);
  71.        
  72.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  73.         public static extern float Exchange(ref float location1, float value);
  74.        
  75.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  76.         public static extern double Exchange(ref double location1, double value);
  77.        
  78.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  79.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  80.         public static extern object Exchange(ref object location1, object value);
  81.        
  82.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  83.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  84.         public static extern IntPtr Exchange(ref IntPtr location1, IntPtr value);
  85.        
  86.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  87.         [System.Runtime.InteropServices.ComVisible(false)]
  88.         public static T Exchange<T>(ref T location1, T value) where T : class
  89.         {
  90.             _Exchange(__makeref(location1), __makeref(value));
  91.             //Since value is a local we use trash its data on return
  92.             // The Exchange replaces the data with new data
  93.             // so after the return "value" contains the original location1
  94.             //See ExchangeGeneric for more details
  95.             return value;
  96.         }
  97.        
  98.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  99.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  100.         private static extern void _Exchange(TypedReference location1, TypedReference value);
  101.        
  102. /******************************
  103.         * CompareExchange
  104.         *    Implemented: int
  105.         *                        long
  106.         *                        float
  107.         *                        double
  108.         *                        Object
  109.         *                        IntPtr
  110.         *****************************/       
  111.        
  112.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  113.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  114.         public static extern int CompareExchange(ref int location1, int value, int comparand);
  115.        
  116.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  117.         public static extern long CompareExchange(ref long location1, long value, long comparand);
  118.        
  119.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  120.         public static extern float CompareExchange(ref float location1, float value, float comparand);
  121.        
  122.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  123.         public static extern double CompareExchange(ref double location1, double value, double comparand);
  124.        
  125.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  126.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  127.         public static extern object CompareExchange(ref object location1, object value, object comparand);
  128.        
  129.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  130.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  131.         public static extern IntPtr CompareExchange(ref IntPtr location1, IntPtr value, IntPtr comparand);
  132.        
  133.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  134.         [System.Runtime.InteropServices.ComVisible(false)]
  135.         public static T CompareExchange<T>(ref T location1, T value, T comparand) where T : class
  136.         {
  137.             _CompareExchange(__makeref(location1), __makeref(value), comparand);
  138.             //Since value is a local we use trash its data on return
  139.             // The Exchange replaces the data with new data
  140.             // so after the return "value" contains the original location1
  141.             //See CompareExchangeGeneric for more details
  142.             return value;
  143.         }
  144.        
  145.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  146.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  147.         private static extern void _CompareExchange(TypedReference location1, TypedReference value, object comparand);
  148.        
  149. /******************************
  150.         * Add
  151.         *    Implemented: int
  152.         *                        long
  153.         *****************************/       
  154.        
  155.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  156.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  157.         static internal extern int ExchangeAdd(ref int location1, int value);
  158.        
  159.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  160.         static internal extern long ExchangeAdd(ref long location1, long value);
  161.        
  162.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  163.         public static int Add(ref int location1, int value)
  164.         {
  165.             return ExchangeAdd(ref location1, value) + value;
  166.         }
  167.        
  168.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  169.         public static long Add(ref long location1, long value)
  170.         {
  171.             return ExchangeAdd(ref location1, value) + value;
  172.         }
  173.        
  174. /******************************
  175.         * Read
  176.         *****************************/       
  177.        
  178.         public static long Read(ref long location)
  179.         {
  180.             return Interlocked.CompareExchange(ref location, 0, 0);
  181.         }
  182.     }
  183. }

Developer Fusion