Merge pull request #93 from konrad-kruczynski/dispatcher_timer_fix
[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                 bool canBeCanceled;
37                 bool initialized;
38                 CancellationTokenSource source;
39
40                 public CancellationToken (bool canceled)
41                         : this ()
42                 {
43                         initialized = true;
44                         canBeCanceled = canceled;
45                         // This is correctly set later if token originates from a Source
46                         source = canceled ? CancellationTokenSource.CanceledSource : CancellationTokenSource.NoneSource;
47                 }
48
49                 public static CancellationToken None {
50                         get {
51                                 return CancellationTokenSource.NoneSource.Token;
52                         }
53                 }
54
55                 public CancellationTokenRegistration Register (Action callback)
56                 {
57                         return Register (callback, false);
58                 }
59
60                 public CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
61                 {
62                         if (callback == null)
63                                 throw new ArgumentNullException ("callback");
64
65                         return Source.Register (callback, useSynchronizationContext);
66                 }
67
68                 public CancellationTokenRegistration Register (Action<object> callback, object state)
69                 {
70                         return Register (callback, state, false);
71                 }
72
73                 public CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext)
74                 {
75                         if (callback == null)
76                                 throw new ArgumentNullException ("callback");
77
78                         return Register (() => callback (state), useSynchronizationContext);
79                 }
80
81                 public void ThrowIfCancellationRequested ()
82                 {
83                         if (initialized && Source.IsCancellationRequested)
84                                 throw new OperationCanceledException (this);
85                 }
86
87                 public bool Equals (CancellationToken other)
88                 {
89                         return this.Source == other.Source;
90                 }
91
92                 public override bool Equals (object other)
93                 {
94                         return (other is CancellationToken) ? Equals ((CancellationToken)other) : false;
95                 }
96
97                 public override int GetHashCode ()
98                 {
99                         return Source.GetHashCode ();
100                 }
101
102                 public static bool operator == (CancellationToken left, CancellationToken right)
103                 {
104                         return left.Equals (right);
105                 }
106
107                 public static bool operator != (CancellationToken left, CancellationToken right)
108                 {
109                         return !left.Equals (right);
110                 }
111
112                 public bool CanBeCanceled {
113                         get {
114                                 return canBeCanceled;
115                         }
116                 }
117
118                 public bool IsCancellationRequested {
119                         get {
120                                 return initialized && Source.IsCancellationRequested;
121                         }
122                 }
123
124                 public WaitHandle WaitHandle {
125                         get {
126                                 return Source.WaitHandle;
127                         }
128                 }
129
130                 internal CancellationTokenSource Source {
131                         get {
132                                 if (!initialized)
133                                         CorrectlyInitialize ();
134                                 return source;
135                         }
136                         set {
137                                 source = value;
138                         }
139                 }
140
141                 void CorrectlyInitialize ()
142                 {
143                         Source = CancellationTokenSource.NoneSource;
144                         initialized = true;
145                 }
146         }
147 }
148 #endif