1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
|
#if NET_4_0
// Task.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Threading;
using System.Collections.Concurrent;
namespace System.Threading.Tasks
{
public class Task : IDisposable, IAsyncResult, ICancelableOperation
{
// With this attribute each thread has its own value so that it's correct for our Schedule code
// and for Parent property.
[System.ThreadStatic]
static Task current;
[System.ThreadStatic]
static Action<Task> childWorkAdder;
static int id = -1;
static TaskFactory defaultFactory = new TaskFactory ();
CountdownEvent childTasks = new CountdownEvent (1);
Task parent = current;
int taskId;
bool respectParentCancellation;
TaskCreationOptions taskCreationOptions;
IScheduler scheduler;
TaskScheduler taskScheduler;
volatile Exception exception;
volatile bool exceptionObserved;
volatile TaskStatus status;
Action<object> action;
object state;
EventHandler completed;
CancellationTokenSource src = new CancellationTokenSource ();
public Task (Action action) : this (action, TaskCreationOptions.None)
{
}
public Task (Action action, TaskCreationOptions options) : this ((o) => action (), null, options)
{
}
public Task (Action<object> action, object state) : this (action, state, TaskCreationOptions.None)
{
}
public Task (Action<object> action, object state, TaskCreationOptions options)
{
this.taskCreationOptions = options;
this.action = action == null ? EmptyFunc : action;
this.state = state;
this.taskId = Interlocked.Increment (ref id);
this.status = TaskStatus.Created;
// Process taskCreationOptions
if (CheckTaskOptions (taskCreationOptions, TaskCreationOptions.DetachedFromParent))
parent = null;
else if (parent != null)
parent.AddChild ();
respectParentCancellation =
CheckTaskOptions (taskCreationOptions, TaskCreationOptions.RespectParentCancellation);
}
~Task ()
{
if (exception != null && !exceptionObserved)
throw exception;
}
bool CheckTaskOptions (TaskCreationOptions opt, TaskCreationOptions member)
{
return (opt & member) == member;
}
static void EmptyFunc (object o)
{
}
#region Start
public void Start ()
{
Start (TaskScheduler.Current);
}
public void Start (TaskScheduler tscheduler)
{
this.taskScheduler = tscheduler;
Start (ProxifyScheduler (tscheduler));
}
void Start (IScheduler scheduler)
{
this.scheduler = scheduler;
status = TaskStatus.WaitingForActivation;
Schedule ();
}
IScheduler ProxifyScheduler (TaskScheduler tscheduler)
{
IScheduler sched = tscheduler as IScheduler;
return sched != null ? sched : new SchedulerProxy (tscheduler);
}
public void RunSynchronously ()
{
RunSynchronously (TaskScheduler.Current);
}
public void RunSynchronously (TaskScheduler tscheduler)
{
// Adopt this scheme for the moment
ThreadStart ();
}
#endregion
#region ContinueWith
public Task ContinueWith (Action<Task> a)
{
return ContinueWith (a, TaskContinuationOptions.None);
}
public Task ContinueWith (Action<Task> a, TaskContinuationOptions kind)
{
return ContinueWith (a, kind, TaskScheduler.Current);
}
public Task ContinueWith (Action<Task> a, TaskScheduler scheduler)
{
return ContinueWith (a, TaskContinuationOptions.None, scheduler);
}
public Task ContinueWith (Action<Task> a, TaskContinuationOptions kind, TaskScheduler scheduler)
{
Task continuation = new Task ((o) => a ((Task)o), this, GetCreationOptions (kind));
ContinueWithCore (continuation, kind, scheduler);
return continuation;
}
public Task<TResult> ContinueWith<TResult> (Func<Task, TResult> a)
{
return ContinueWith<TResult> (a, TaskContinuationOptions.None);
}
public Task<TResult> ContinueWith<TResult> (Func<Task, TResult> a, TaskContinuationOptions options)
{
return ContinueWith<TResult> (a, options, TaskScheduler.Current);
}
public Task<TResult> ContinueWith<TResult> (Func<Task, TResult> a, TaskScheduler scheduler)
{
return ContinueWith<TResult> (a, TaskContinuationOptions.None, scheduler);
}
public Task<TResult> ContinueWith<TResult> (Func<Task, TResult> a, TaskContinuationOptions kind, TaskScheduler scheduler)
{
Task<TResult> t = new Task<TResult> ((o) => a ((Task)o), this, GetCreationOptions (kind));
ContinueWithCore (t, kind, scheduler);
return t;
}
internal void ContinueWithCore (Task continuation, TaskContinuationOptions kind, TaskScheduler scheduler)
{
ContinueWithCore (continuation, kind, scheduler, () => true);
}
internal void ContinueWithCore (Task continuation, TaskContinuationOptions kind,
TaskScheduler scheduler, Func<bool> predicate)
{
// Already set the scheduler so that user can call Wait and that sort of stuff
continuation.taskScheduler = scheduler;
continuation.scheduler = ProxifyScheduler (scheduler);
AtomicBoolean launched = new AtomicBoolean ();
EventHandler action = delegate {
if (!predicate ()) return;
if (!launched.Value && !launched.Exchange (true)) {
if (!ContinuationStatusCheck (kind)) {
continuation.Cancel ();
continuation.CancelReal ();
continuation.Dispose ();
return;
}
CheckAndSchedule (continuation, kind, scheduler);
}
};
if (IsCompleted) {
action (this, EventArgs.Empty);
return;
}
completed += action;
// Retry in case completion was achieved but event adding was too late
if (IsCompleted)
action (this, EventArgs.Empty);
}
bool ContinuationStatusCheck (TaskContinuationOptions kind)
{
if (kind == TaskContinuationOptions.None)
return true;
int kindCode = (int)kind;
if (kindCode >= ((int)TaskContinuationOptions.NotOnRanToCompletion)) {
if (status == TaskStatus.Canceled) {
if (kind == TaskContinuationOptions.NotOnCanceled)
return false;
if (kind == TaskContinuationOptions.OnlyOnFaulted)
return false;
if (kind == TaskContinuationOptions.OnlyOnRanToCompletion)
return false;
} else if (status == TaskStatus.Faulted) {
if (kind == TaskContinuationOptions.NotOnFaulted)
return false;
if (kind == TaskContinuationOptions.OnlyOnCanceled)
return false;
if (kind == TaskContinuationOptions.OnlyOnRanToCompletion)
return false;
} else if (status == TaskStatus.RanToCompletion) {
if (kind == TaskContinuationOptions.NotOnRanToCompletion)
return false;
if (kind == TaskContinuationOptions.OnlyOnFaulted)
return false;
if (kind == TaskContinuationOptions.OnlyOnCanceled)
return false;
}
}
return true;
}
void CheckAndSchedule (Task continuation, TaskContinuationOptions options, TaskScheduler scheduler)
{
if (options == TaskContinuationOptions.None || (options & TaskContinuationOptions.ExecuteSynchronously) > 0) {
continuation.ThreadStart ();
} else {
continuation.Start (scheduler);
}
}
static TaskCreationOptions GetCreationOptions (TaskContinuationOptions kind)
{
TaskCreationOptions options = TaskCreationOptions.None;
if ((kind & TaskContinuationOptions.DetachedFromParent) > 0)
options |= TaskCreationOptions.DetachedFromParent;
if ((kind & TaskContinuationOptions.RespectParentCancellation) > 0)
options |= TaskCreationOptions.RespectParentCancellation;
return options;
}
#endregion
#region Internal and protected thingies
internal void Schedule ()
{
status = TaskStatus.WaitingToRun;
// If worker is null it means it is a local one, revert to the old behavior
if (current == null || childWorkAdder == null || parent == null
|| CheckTaskOptions (taskCreationOptions, TaskCreationOptions.PreferFairness)) {
scheduler.AddWork (this);
} else {
/* Like the semantic of the ABP paper describe it, we add ourselves to the bottom
* of our Parent Task's ThreadWorker deque. It's ok to do that since we are in
* the correct Thread during the creation
*/
childWorkAdder (this);
}
}
void ThreadStart ()
{
current = this;
TaskScheduler.Current = taskScheduler;
if (!src.IsCancellationRequested
&& (!respectParentCancellation || (respectParentCancellation && parent != null && !parent.IsCanceled))) {
status = TaskStatus.Running;
try {
InnerInvoke ();
} catch (Exception e) {
exception = e;
status = TaskStatus.Faulted;
}
} else {
AcknowledgeCancellation ();
}
Finish ();
}
internal void Execute (Action<Task> childAdder)
{
childWorkAdder = childAdder;
ThreadStart ();
}
internal void AddChild ()
{
childTasks.AddCount ();
}
internal void ChildCompleted ()
{
childTasks.Signal ();
if (childTasks.IsSet && status == TaskStatus.WaitingForChildrenToComplete)
status = TaskStatus.RanToCompletion;
}
internal virtual void InnerInvoke ()
{
if (action != null)
action (state);
// Set action to null so that the GC can collect the delegate and thus
// any big object references that the user might have captured in an anonymous method
action = null;
state = null;
}
internal void Finish ()
{
// If there wasn't any child created in the task we set the CountdownEvent
childTasks.Signal ();
// Don't override Canceled or Faulted
if (status == TaskStatus.Running) {
if (childTasks.IsSet )
status = TaskStatus.RanToCompletion;
else
status = TaskStatus.WaitingForChildrenToComplete;
}
// Call the event in the correct style
EventHandler tempCompleted = completed;
if (tempCompleted != null)
tempCompleted (this, EventArgs.Empty);
// Reset the current thingies
current = null;
TaskScheduler.Current = null;
// Tell parent that we are finished
if (!CheckTaskOptions (taskCreationOptions, TaskCreationOptions.DetachedFromParent) && parent != null){
parent.ChildCompleted ();
}
Dispose ();
}
#endregion
#region Cancel and Wait related methods
public void AcknowledgeCancellation ()
{
if (this != current)
throw new InvalidOperationException ("The Task object is different from the currently executing"
+ "task or the current task hasn't been "
+ "marked for cancellation.");
CancelReal ();
}
internal void CancelReal ()
{
exception = new TaskCanceledException (this);
status = TaskStatus.Canceled;
}
public void Cancel ()
{
src.Cancel ();
}
public void CancelAndWait ()
{
Cancel ();
Wait ();
}
public void CancelAndWait (CancellationToken token)
{
Cancel ();
Wait (token);
}
public bool CancelAndWait (TimeSpan ts)
{
Cancel ();
return Wait (ts);
}
public bool CancelAndWait (int millisecondsTimeout)
{
Cancel ();
return Wait (millisecondsTimeout);
}
public bool CancelAndWait (int millisecondsTimeout, CancellationToken token)
{
Cancel ();
return Wait (millisecondsTimeout, token);
}
public void Wait ()
{
if (scheduler == null)
throw new InvalidOperationException ("The Task hasn't been Started and thus can't be waited on");
scheduler.ParticipateUntil (this);
if (exception != null && !(exception is TaskCanceledException))
throw exception;
}
public void Wait (CancellationToken token)
{
Wait (null, token);
}
public bool Wait (TimeSpan ts)
{
return Wait ((int)ts.TotalMilliseconds);
}
public bool Wait (int millisecondsTimeout)
{
Watch sw = Watch.StartNew ();
return Wait (() => sw.ElapsedMilliseconds >= millisecondsTimeout, null);
}
public bool Wait (int millisecondsTimeout, CancellationToken token)
{
Watch sw = Watch.StartNew ();
return Wait (() => sw.ElapsedMilliseconds >= millisecondsTimeout, token);
}
bool Wait (Func<bool> stopFunc, CancellationToken? token)
{
if (scheduler == null)
throw new InvalidOperationException ("The Task hasn't been Started and thus can't be waited on");
bool result = scheduler.ParticipateUntil (this, delegate {
if (token.HasValue && token.Value.IsCancellationRequested)
throw new OperationCanceledException ("The CancellationToken has had cancellation requested.");
return (stopFunc != null) ? stopFunc () : false;
});
if (exception != null && !(exception is TaskCanceledException))
throw exception;
return !result;
}
public static void WaitAll (params Task[] tasks)
{
if (tasks == null)
throw new ArgumentNullException ("tasks");
if (tasks.Length == 0)
throw new ArgumentException ("tasks is empty", "tasks");
foreach (var t in tasks)
t.Wait ();
}
public static void WaitAll (Task[] tasks, CancellationToken token)
{
if (tasks == null)
throw new ArgumentNullException ("tasks");
if (tasks.Length == 0)
throw new ArgumentException ("tasks is empty", "tasks");
foreach (var t in tasks)
t.Wait (token);
}
public static bool WaitAll (Task[] tasks, TimeSpan ts)
{
if (tasks == null)
throw new ArgumentNullException ("tasks");
if (tasks.Length == 0)
throw new ArgumentException ("tasks is empty", "tasks");
bool result = true;
foreach (var t in tasks)
result &= t.Wait (ts);
return result;
}
public static bool WaitAll (Task[] tasks, int millisecondsTimeout)
{
if (tasks == null)
throw new ArgumentNullException ("tasks");
if (tasks.Length == 0)
throw new ArgumentException ("tasks is empty", "tasks");
bool result = true;
foreach (var t in tasks)
result &= t.Wait (millisecondsTimeout);
return result;
}
public static bool WaitAll (Task[] tasks, int millisecondsTimeout, CancellationToken token)
{
if (tasks == null)
throw new ArgumentNullException ("tasks");
if (tasks.Length == 0)
throw new ArgumentException ("tasks is empty", "tasks");
bool result = true;
foreach (var t in tasks)
result &= t.Wait (millisecondsTimeout, token);
return result;
}
public static int WaitAny (params Task[] tasks)
{
return WaitAny (tasks, null, null);
}
static int WaitAny (Task[] tasks, Func<bool> stopFunc, CancellationToken? token)
{
if (tasks == null)
throw new ArgumentNullException ("tasks");
if (tasks.Length == 0)
throw new ArgumentException ("tasks is empty", "tasks");
int numFinished = 0;
int indexFirstFinished = -1;
int index = 0;
foreach (Task t in tasks) {
t.ContinueWith (delegate {
int indexResult = index;
int result = Interlocked.Increment (ref numFinished);
// Check if we are the first to have finished
if (result == 1)
indexFirstFinished = indexResult;
});
index++;
}
// One task already finished
if (indexFirstFinished != -1)
return indexFirstFinished;
// All tasks are supposed to use the same TaskManager
tasks[0].scheduler.ParticipateUntil (delegate {
if (stopFunc != null && stopFunc ())
return true;
if (token.HasValue && token.Value.IsCancellationRequested)
throw new OperationCanceledException ("The CancellationToken has had cancellation requested.");
return numFinished >= 1;
});
return indexFirstFinished;
}
public static int WaitAny (Task[] tasks, TimeSpan ts)
{
return WaitAny (tasks, (int)ts.TotalMilliseconds);
}
public static int WaitAny (Task[] tasks, int millisecondsTimeout)
{
if (millisecondsTimeout < -1)
throw new ArgumentOutOfRangeException ("millisecondsTimeout");
if (millisecondsTimeout == -1)
return WaitAny (tasks);
Watch sw = Watch.StartNew ();
return WaitAny (tasks, () => sw.ElapsedMilliseconds > millisecondsTimeout, null);
}
public static int WaitAny (Task[] tasks, int millisecondsTimeout, CancellationToken token)
{
if (millisecondsTimeout < -1)
throw new ArgumentOutOfRangeException ("millisecondsTimeout");
if (millisecondsTimeout == -1)
return WaitAny (tasks);
Watch sw = Watch.StartNew ();
return WaitAny (tasks, () => sw.ElapsedMilliseconds > millisecondsTimeout, token);
}
public static int WaitAny (Task[] tasks, CancellationToken token)
{
return WaitAny (tasks, null, token);
}
#endregion
#region Dispose
public void Dispose ()
{
Dispose (true);
}
protected virtual void Dispose (bool disposeManagedRes)
{
// Set action to null so that the GC can collect the delegate and thus
// any big object references that the user might have captured in a anonymous method
if (disposeManagedRes) {
action = null;
completed = null;
state = null;
}
}
#endregion
#region Properties
public static TaskFactory Factory {
get {
return defaultFactory;
}
}
public static Task Current {
get {
return current;
}
}
public CancellationToken CancellationToken {
get {
return src.Token;
}
}
public Exception Exception {
get {
exceptionObserved = true;
return exception;
}
internal set {
exception = value;
}
}
public bool IsCanceled {
get {
return status == TaskStatus.Canceled;
}
}
public bool IsCancellationRequested {
get {
return src.IsCancellationRequested;
}
}
public bool IsCompleted {
get {
return status == TaskStatus.RanToCompletion ||
status == TaskStatus.Canceled || status == TaskStatus.Faulted;
}
}
public bool IsFaulted {
get {
return status == TaskStatus.Faulted;
}
}
public Task Parent {
get {
return parent;
}
}
public TaskCreationOptions CreationOptions {
get {
return taskCreationOptions;
}
}
public TaskStatus Status {
get {
return status;
}
internal set {
status = value;
}
}
public object AsyncState {
get {
return state;
}
}
bool IAsyncResult.CompletedSynchronously {
get {
return true;
}
}
WaitHandle IAsyncResult.AsyncWaitHandle {
get {
return null;
}
}
public int Id {
get {
return taskId;
}
}
#endregion
}
}
#endif
|