Repository navigation
Expand file tree
/
Copy pathTaskObjectCache.cs
More file actions
42 lines (35 loc) · 1.54 KB
/
Copy pathTaskObjectCache.cs
File metadata and controls
42 lines (35 loc) · 1.54 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading;
using Microsoft.Build.Framework;
namespace Microsoft.NET.Build.Tasks;
internal static class TaskObjectCache
{
public static T GetOrCreate<T>(
IBuildEngine4 buildEngine,
string key,
RegisteredTaskObjectLifetime lifetime,
Func<T> valueFactory) where T : class
{
// Different SDK task assemblies can share the same MSBuild process.
object cacheKey = (typeof(CacheEntry<T>), key);
var entry = (CacheEntry<T>?)buildEngine.GetRegisteredTaskObject(cacheKey, lifetime);
if (entry is null)
{
var candidate = new CacheEntry<T>();
buildEngine.RegisterTaskObject(cacheKey, candidate, lifetime, allowEarlyCollection: true);
// Equal-key registration retains the first object, which may belong to another caller.
entry = (CacheEntry<T>?)buildEngine.GetRegisteredTaskObject(cacheKey, lifetime) ?? candidate;
}
return entry.GetValue(valueFactory);
}
private sealed class CacheEntry<T> where T : class
{
private T _value = null!;
private bool _initialized;
private object? _syncLock;
// Unlike Lazy<T>, this retries after an exception and does not retain the task's factory.
public T GetValue(Func<T> valueFactory) =>
LazyInitializer.EnsureInitialized(ref _value, ref _initialized, ref _syncLock, valueFactory);
}
}