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