Speed up Thread.ManagedThreadId.
[mono.git] / mcs / class / corlib / System.Threading / CancellationToken.cs
1 //
2 // CancellationToken.cs
3 //
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 //
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.Threading;
29
30 #if NET_4_0 || BOOTSTRAP_NET_4_0
31 namespace System.Threading
32 {
33         public struct CancellationToken
34         {
35                 public CancellationToken (bool canceled)
36                         : this ()
37                 {
38                         // dummy, this is actually set by CancellationTokenSource when the token is created
39                         Source = null;
40                 }
41
42                 public static CancellationToken None {
43                         get {
44                                 return CancellationTokenSource.NoneSource.Token;
45                         }
46                 }
47
48                 public CancellationTokenRegistration Register (Action callback)
49                 {
50                         return Register (callback, false);
51                 }
52
53                 public CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
54                 {
55                         if (callback == null)
56                                 throw new ArgumentNullException ("callback");
57
58                         return Source.Register (callback, useSynchronizationContext);
59                 }
60
61                 public CancellationTokenRegistration Register (Action<object> callback, object state)
62                 {
63                         return Register (callback, state, false);
64                 }
65
66                 public CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext)
67                 {
68                         if (callback == null)
69                                 throw new ArgumentNullException ("callback");
70
71                         return Register (() => callback (state), useSynchronizationContext);
72                 }
73
74                 public void ThrowIfCancellationRequested ()
75                 {
76                         Source.ThrowIfCancellationRequested ();
77                 }
78
79                 public bool Equals (CancellationToken other)
80                 {
81                         return this.Source == other.Source;
82                 }
83
84                 public override bool Equals (object obj)
85                 {
86                         return (obj is CancellationToken) ? Equals ((CancellationToken)obj) : false;
87                 }
88
89                 public override int GetHashCode ()
90                 {
91                         return Source.GetHashCode ();
92                 }
93
94                 public static bool operator == (CancellationToken lhs, CancellationToken rhs)
95                 {
96                         return lhs.Equals (rhs);
97                 }
98
99                 public static bool operator != (CancellationToken lhs, CancellationToken rhs)
100                 {
101                         return !lhs.Equals (rhs);
102                 }
103
104                 public bool CanBeCanceled {
105                         get {
106                                 return true;
107                         }
108                 }
109
110                 public bool IsCancellationRequested {
111                         get {
112                                 return Source.IsCancellationRequested;
113                         }
114                 }
115
116                 public WaitHandle WaitHandle {
117                         get {
118                                 return Source.WaitHandle;
119                         }
120                 }
121
122                 internal CancellationTokenSource Source {
123                         get;
124                         set;
125                 }
126         }
127 }
128 #endif