|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | + |
| 6 | +namespace Microsoft.Diagnostics.DataContractReader.Contracts; |
| 7 | + |
| 8 | +// TODO: [cdac] Add other counts / threads |
| 9 | +internal record struct ThreadStoreData( |
| 10 | + int ThreadCount, |
| 11 | + TargetPointer FirstThread); |
| 12 | + |
| 13 | +internal interface IThread : IContract |
| 14 | +{ |
| 15 | + static string IContract.Name { get; } = nameof(Thread); |
| 16 | + static IContract IContract.Create(Target target, int version) |
| 17 | + { |
| 18 | + TargetPointer threadStore = target.ReadGlobalPointer(Constants.Globals.ThreadStore); |
| 19 | + return version switch |
| 20 | + { |
| 21 | + 1 => new Thread_1(target, threadStore), |
| 22 | + _ => default(Thread), |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + public virtual ThreadStoreData GetThreadStoreData() => throw new NotImplementedException(); |
| 27 | +} |
| 28 | + |
| 29 | +internal readonly struct Thread : IThread |
| 30 | +{ |
| 31 | + // Everything throws NotImplementedException |
| 32 | +} |
| 33 | + |
| 34 | +internal readonly struct Thread_1 : IThread |
| 35 | +{ |
| 36 | + private readonly Target _target; |
| 37 | + private readonly TargetPointer _threadStoreAddr; |
| 38 | + |
| 39 | + internal Thread_1(Target target, TargetPointer threadStore) |
| 40 | + { |
| 41 | + _target = target; |
| 42 | + _threadStoreAddr = threadStore; |
| 43 | + } |
| 44 | + |
| 45 | + ThreadStoreData IThread.GetThreadStoreData() |
| 46 | + { |
| 47 | + Data.ThreadStore? threadStore; |
| 48 | + if (!_target.ProcessedData.TryGet(_threadStoreAddr.Value, out threadStore)) |
| 49 | + { |
| 50 | + threadStore = new Data.ThreadStore(_target, _threadStoreAddr); |
| 51 | + |
| 52 | + // Still okay if processed data is already registered by someone else |
| 53 | + _ = _target.ProcessedData.TryRegister(_threadStoreAddr.Value, threadStore); |
| 54 | + } |
| 55 | + |
| 56 | + return new ThreadStoreData(threadStore.ThreadCount, threadStore.FirstThread); |
| 57 | + } |
| 58 | +} |
0 commit comments