This repository was archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Add BackgroundTaskManager and WaitForBackgroundTasks #6
Open
dellis1972
wants to merge
1
commit into
xamarin:main
Choose a base branch
from
dellis1972:taskmanager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Collections.Concurrent; | ||
| using System.Threading; | ||
| using Microsoft.Build.Utilities; | ||
| using Microsoft.Build.Framework; | ||
| using System.Collections; | ||
| using TPL = System.Threading.Tasks; | ||
|
|
||
| namespace Xamarin.Build | ||
| { | ||
| /// <summary> | ||
| /// A class for keeping track of any Task instances which are run as part of | ||
| /// a build. | ||
| /// Call `manager.RegisterTask` to register the task with the manager. You can | ||
| /// provide a `Category` for each task. This is so they can be waited on later | ||
| /// using the `WaitForBackgroundTasks` MSBuild task. | ||
| /// </summary> | ||
| public class BackgroundTaskManager : IDisposable | ||
| { | ||
| internal const string DefaultCategory = "default"; | ||
| ConcurrentDictionary<string, ConcurrentBag<AsyncTask>> tasks = new ConcurrentDictionary<string, ConcurrentBag<AsyncTask>> (); | ||
| CancellationTokenSource tcs = new CancellationTokenSource (); | ||
| IBuildEngine4 buildEngine; | ||
|
|
||
| /// <summary> | ||
| /// Get an Instance of the TaskManager. | ||
| /// NOTE This MUST be called from the main thread in a Task, it cannot be called on a background thread. | ||
| /// </summary> | ||
| /// <param name="buildEngine4">An instance of the IBuildEngine4 interface.</param> | ||
| /// <returns>An instance of a TaskManager</returns> | ||
| public static BackgroundTaskManager GetTaskManager (IBuildEngine4 buildEngine4) | ||
| { | ||
| var manager = (BackgroundTaskManager)buildEngine4.GetRegisteredTaskObject (typeof (BackgroundTaskManager).FullName, RegisteredTaskObjectLifetime.Build); | ||
| if (manager == null) | ||
| { | ||
| manager = new BackgroundTaskManager (buildEngine4); | ||
| buildEngine4.RegisterTaskObject (typeof (BackgroundTaskManager).FullName, manager, RegisteredTaskObjectLifetime.Build, allowEarlyCollection: false); | ||
| } | ||
| return manager; | ||
| } | ||
|
|
||
| public BackgroundTaskManager(IBuildEngine4 buildEngine) | ||
| { | ||
| this.buildEngine = buildEngine; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Register a new Task which will be running in the background. | ||
| /// If you have multiple tasks running you can split them up into | ||
| /// different categories. This can then we used to wait in differnt | ||
| /// parts of the build later on. | ||
| /// </summary> | ||
| /// <param name="task">The task you are running</param> | ||
| /// <param name="category">The category this task is in. </param> | ||
| public void RegisterTask (AsyncTask task, string category = DefaultCategory) | ||
| { | ||
| var bag = tasks.GetOrAdd (category, new ConcurrentBag<AsyncTask> ()); | ||
| bag.Add (task); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns an array of Tasks for that category. | ||
| /// </summary> | ||
| /// <param name="category">The category you want to get the list for.</param> | ||
| /// <returns>Either the array of task or an empty array if the category does not exist.</returns> | ||
| public AsyncTask [] this [string category] | ||
| { | ||
| get | ||
| { | ||
| ConcurrentBag<AsyncTask> result; | ||
| if (!tasks.TryGetValue (category, out result)) | ||
| return Array.Empty<AsyncTask> (); | ||
| return result.ToArray (); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose () | ||
| { | ||
| // wait for all tasks to complete. | ||
| foreach (var bag in tasks) { | ||
| foreach (AsyncTask t in bag.Value) { | ||
| t.Wait (buildEngine); | ||
| } | ||
| } | ||
| tcs.Cancel (); | ||
| } | ||
|
|
||
| public CancellationToken CancellationToken { get { return tcs.Token; } } | ||
|
|
||
| /// <summary> | ||
| /// The number of registered categories. | ||
| /// </summary> | ||
| public int Count => tasks.Count; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.