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