Replace SIZEOF_REGISTER with sizeof(mgreg_t) for consistency with sizeof(gpointer)
[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                         if (Source.IsCancellationRequested)
77                                 throw new OperationCanceledException (this);
78                 }
79
80                 public bool Equals (CancellationToken other)
81                 {
82                         return this.Source == other.Source;
83                 }
84
85                 public override bool Equals (object obj)
86                 {
87                         return (obj is CancellationToken) ? Equals ((CancellationToken)obj) : false;
88                 }
89
90                 public override int GetHashCode ()
91                 {
92                         return Source.GetHashCode ();
93                 }
94
95                 public static bool operator == (CancellationToken lhs, CancellationToken rhs)
96                 {
97                         return lhs.Equals (rhs);
98                 }
99
100                 public static bool operator != (CancellationToken lhs, CancellationToken rhs)
101                 {
102                         return !lhs.Equals (rhs);
103                 }
104
105                 public bool CanBeCanceled {
106                         get {
107                                 return true;
108                         }
109                 }
110
111                 public bool IsCancellationRequested {
112                         get {
113                                 return Source.IsCancellationRequested;
114                         }
115                 }
116
117                 public WaitHandle WaitHandle {
118                         get {
119                                 return Source.WaitHandle;
120                         }
121                 }
122
123                 internal CancellationTokenSource Source {
124                         get;
125                         set;
126                 }
127         }
128 }
129 #endif