[corlib] Ensure Thread.ManagedThreadId is unique per thread.
[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 || MOBILE
31 namespace System.Threading
32 {
33         [System.Diagnostics.DebuggerDisplay ("IsCancellationRequested = {IsCancellationRequested}")]
34         public struct CancellationToken
35         {
36                 public CancellationToken (bool canceled)
37                         : this ()
38                 {
39                         // dummy, this is actually set by CancellationTokenSource when the token is created
40                         Source = null;
41                 }
42
43                 public static CancellationToken None {
44                         get {
45                                 return CancellationTokenSource.NoneSource.Token;
46                         }
47                 }
48
49                 public CancellationTokenRegistration Register (Action callback)
50                 {
51                         return Register (callback, false);
52                 }
53
54                 public CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
55                 {
56                         if (callback == null)
57                                 throw new ArgumentNullException ("callback");
58
59                         return Source.Register (callback, useSynchronizationContext);
60                 }
61
62                 public CancellationTokenRegistration Register (Action<object> callback, object state)
63                 {
64                         return Register (callback, state, false);
65                 }
66
67                 public CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext)
68                 {
69                         if (callback == null)
70                                 throw new ArgumentNullException ("callback");
71
72                         return Register (() => callback (state), useSynchronizationContext);
73                 }
74
75                 public void ThrowIfCancellationRequested ()
76                 {
77                         if (Source.IsCancellationRequested)
78                                 throw new OperationCanceledException (this);
79                 }
80
81                 public bool Equals (CancellationToken other)
82                 {
83                         return this.Source == other.Source;
84                 }
85
86                 public override bool Equals (object other)
87                 {
88                         return (other is CancellationToken) ? Equals ((CancellationToken)other) : false;
89                 }
90
91                 public override int GetHashCode ()
92                 {
93                         return Source.GetHashCode ();
94                 }
95
96                 public static bool operator == (CancellationToken left, CancellationToken right)
97                 {
98                         return left.Equals (right);
99                 }
100
101                 public static bool operator != (CancellationToken left, CancellationToken right)
102                 {
103                         return !left.Equals (right);
104                 }
105
106                 public bool CanBeCanceled {
107                         get {
108                                 return true;
109                         }
110                 }
111
112                 public bool IsCancellationRequested {
113                         get {
114                                 return Source.IsCancellationRequested;
115                         }
116                 }
117
118                 public WaitHandle WaitHandle {
119                         get {
120                                 return Source.WaitHandle;
121                         }
122                 }
123
124                 internal CancellationTokenSource Source {
125                         get;
126                         set;
127                 }
128         }
129 }
130 #endif