From: Koundinya Veluri Date: Tue, 23 Apr 2019 06:31:37 +0000 (-0700) Subject: Implement APIs for some threading metrics (CoreRT) (dotnet/corert#7066) X-Git-Tag: accepted/tizen/unified/20190813.215958~42^2~455 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=447b6554f393f3fa02f3d697cdf7c56612a0fa1a;p=platform%2Fupstream%2Fcoreclr.git Implement APIs for some threading metrics (CoreRT) (dotnet/corert#7066) * Implement APIs for some threading metrics (CoreRT) - API review: https://github.com/dotnet/corefx/issues/35500 - May depend on https://github.com/dotnet/coreclr/pull/22754 * Use thread-locals for counting, use finalizer instead of runtime to detect thread exit * Don't let the count properties throw OOM * Remove some flushes Signed-off-by: dotnet-bot --- diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadLocal.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadLocal.cs index 8c19903..276caed 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadLocal.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadLocal.cs @@ -454,16 +454,16 @@ namespace System.Threading /// Gets all of the threads' values in a list. private List? GetValuesAsList() { - List valueList = new List(); + LinkedSlot? linkedSlot = _linkedSlot; int id = ~_idComplement; - if (id == -1) + if (id == -1 || linkedSlot == null) { return null; } // Walk over the linked list of slots and gather the values associated with this ThreadLocal instance. - Debug.Assert(_linkedSlot != null, "Should only be null if the instance was disposed."); - for (LinkedSlot? linkedSlot = _linkedSlot._next; linkedSlot != null; linkedSlot = linkedSlot._next) + var valueList = new List(); + for (linkedSlot = linkedSlot._next; linkedSlot != null; linkedSlot = linkedSlot._next) { // We can safely read linkedSlot.Value. Even if this ThreadLocal has been disposed in the meantime, the LinkedSlot // objects will never be assigned to another ThreadLocal instance. @@ -473,6 +473,32 @@ namespace System.Threading return valueList; } + internal IEnumerable ValuesAsEnumerable + { + get + { + if (!_trackAllValues) + { + throw new InvalidOperationException(SR.ThreadLocal_ValuesNotAvailable); + } + + LinkedSlot? linkedSlot = _linkedSlot; + int id = ~_idComplement; + if (id == -1 || linkedSlot == null) + { + throw new ObjectDisposedException(SR.ThreadLocal_Disposed); + } + + // Walk over the linked list of slots and gather the values associated with this ThreadLocal instance. + for (linkedSlot = linkedSlot._next; linkedSlot != null; linkedSlot = linkedSlot._next) + { + // We can safely read linkedSlot.Value. Even if this ThreadLocal has been disposed in the meantime, the LinkedSlot + // objects will never be assigned to another ThreadLocal instance. + yield return linkedSlot._value; + } + } + } + /// Gets the number of threads that have data in this instance. private int ValuesCountForDebugDisplay {