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

  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. /*============================================================
  16. **
  17. ** Class:  CancellationSignal
  18. **
  19. **
  20. ** Purpose: Defines an identifier to ensure you can only
  21. ** cancel synchronous IO requests that you were expecting to
  22. ** cancel.
  23. **
  24. **
  25. ===========================================================*/
  26. using System.IO;
  27. using System.Security.Permissions;
  28. using System.Collections.Generic;
  29. using System.Runtime.ConstrainedExecution;
  30. using Microsoft.Win32;
  31. namespace System.Threading
  32. {
  33.    
  34.    
  35.     public sealed class CancellationSignal
  36.     {
  37.         // Warning - Thread contains a stack of CancellationSignals, which
  38.         // places a restriction on this class. All types used within a
  39.         // CancellationSignal must be AppDomain-agile. Otherwise, we will
  40.         // bleed objects across appdomains, just as we do with CultureInfo
  41.         // today. Be very careful about adding new members here. We've
  42.         // sealed the class to prevent users from accidentally causing this
  43.         // problem for themselves in subclasses of CancellationSignal.
  44.         private Thread _thread;
  45.         private bool _cancelable;
  46.         private bool _wasCancelRequested;
  47.        
  48.         private static bool SupportsCancelSyncIOChecked;
  49.         private static bool OSSupportsCancelSyncIO;
  50.        
  51.         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  52.         public CancellationSignal()
  53.         {
  54.             _cancelable = true;
  55.         }
  56.        
  57.         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  58.         internal CancellationSignal(bool cancelable)
  59.         {
  60.             _cancelable = cancelable;
  61.         }
  62.        
  63.         [HostProtection(ExternalThreading = true)]
  64.         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  65.         public void Cancel()
  66.         {
  67.             _wasCancelRequested = true;
  68.             throw new PlatformNotSupportedException();
  69.         }
  70.        
  71.         private static void CheckSupportsCancelSyncIO()
  72.         {
  73.             OSSupportsCancelSyncIO = false;
  74.             SupportsCancelSyncIOChecked = true;
  75.         }
  76.        
  77.         // To allow CPU-intensive tasks to use our cancellation model to
  78.         // opt into cancellation via polling. We could also use this in
  79.         // select places to help avoid races inherent in the OS's
  80.         // cancellation model, such as attempting to cancel CopyFile while
  81.         // it is in the middle of some CPU-intensive calculation instead of
  82.         // blocked waiting for IO.
  83.         // Note that if you cancel one region, it should cancel any nested
  84.         // cancellable regions. See Cancel for this logic.
  85.         public bool CancelRequested {
  86.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  87.             get { return _wasCancelRequested; }
  88.         }
  89.        
  90.         internal Thread Thread {
  91.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  92.             get { return _thread; }
  93.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  94.             set {
  95.                 if (_thread != null && value != null)
  96.                     throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CancellationSignalReuse"));
  97.                 _thread = value;
  98.             }
  99.         }
  100.     }
  101. }

Developer Fusion