summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/LocalAppContextSwitches.Common.cs
diff options
context:
space:
mode:
authorJan Kotas <jkotas@microsoft.com>2018-11-24 06:59:54 -0800
committerGitHub <noreply@github.com>2018-11-24 06:59:54 -0800
commit55d1363ac36735008dee4d11e10934b8ee94e80b (patch)
tree4e71d783ccf7edb501c4b17dc7cc2ac708fe3d75 /src/System.Private.CoreLib/shared/System/LocalAppContextSwitches.Common.cs
parent65f88672f888e893a44f21b59ecfd87f4d17e499 (diff)
downloadcoreclr-55d1363ac36735008dee4d11e10934b8ee94e80b.tar.gz
coreclr-55d1363ac36735008dee4d11e10934b8ee94e80b.tar.bz2
coreclr-55d1363ac36735008dee4d11e10934b8ee94e80b.zip
Move AppDomain local data store to AppContext and cleanup AppContext (#21180)
Contributes to #21028
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/LocalAppContextSwitches.Common.cs')
-rw-r--r--src/System.Private.CoreLib/shared/System/LocalAppContextSwitches.Common.cs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/System.Private.CoreLib/shared/System/LocalAppContextSwitches.Common.cs b/src/System.Private.CoreLib/shared/System/LocalAppContextSwitches.Common.cs
new file mode 100644
index 0000000000..521848f2ef
--- /dev/null
+++ b/src/System.Private.CoreLib/shared/System/LocalAppContextSwitches.Common.cs
@@ -0,0 +1,39 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.CompilerServices;
+
+namespace System
+{
+ // Helper method for local caching of compatibility quirks. Keep this lean and simple - this file is included into
+ // every framework assembly that implements any compatibility quirks.
+ internal static partial class LocalAppContextSwitches
+ {
+ // Returns value of given switch using provided cache.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static bool GetCachedSwitchValue(string switchName, ref int cachedSwitchValue)
+ {
+ // The cached switch value has 3 states: 0 - unknown, 1 - true, -1 - false
+ if (cachedSwitchValue < 0) return false;
+ if (cachedSwitchValue > 0) return true;
+
+ return GetCachedSwitchValueInternal(switchName, ref cachedSwitchValue);
+ }
+
+ private static bool GetCachedSwitchValueInternal(string switchName, ref int cachedSwitchValue)
+ {
+ bool isSwitchEnabled;
+ AppContext.TryGetSwitch(switchName, out isSwitchEnabled);
+
+ AppContext.TryGetSwitch(@"TestSwitch.LocalAppContext.DisableCaching", out bool disableCaching);
+ if (!disableCaching)
+ {
+ cachedSwitchValue = isSwitchEnabled ? 1 /*true*/ : -1 /*false*/;
+ }
+
+ return isSwitchEnabled;
+ }
+ }
+}