{"id":166,"date":"2020-08-06T11:44:09","date_gmt":"2020-08-06T11:44:09","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=166"},"modified":"2020-12-26T04:34:49","modified_gmt":"2020-12-26T04:34:49","slug":"concurrencycheck-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/08\/06\/concurrencycheck-cs\/","title":{"rendered":"ConcurrencyCheck.cs"},"content":{"rendered":"\n<p>Check and Store Concurrency Usage<\/p>\n\n\n\n<p>Updated: Dec-25,2020<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Diagnostics;\nusing System.Threading;\npublic class ConcurrencyCheck\n{\n    public volatile ConcurrencyInfo ConcurrencyInformation = new ConcurrencyInfo();\n    private static  int             ProcessorCount => Environment.ProcessorCount;\n    public bool OverrideAutoConcurrency\n    {\n        get;\n        set;\n    }\n    public bool QuickCheck\n    {\n        get;\n        set;\n    } = false;\n    public bool UsingConcurrency\n    {\n        get;\n        private set;\n    }\n    public bool CheckState()\n    {\n        if (OverrideAutoConcurrency)\n            return false;\n        if (QuickCheck &amp;&amp; ConcurrencyInformation.Calls > ProcessorCount &amp;&amp; ConcurrencyInformation.LockState == 0)\n            return false;\n        if (ConcurrencyInformation.StatusThreadId != Thread.CurrentThread.ManagedThreadId)\n        {\n            ConcurrencyInformation.StatusThreadId = Thread.CurrentThread.ManagedThreadId;\n            ConcurrencyInformation.Add(ConcurrencyInformation.StatusThreadId);\n        }\n        if (ConcurrencyInformation.LockState == 1)\n            return true;\n        if (ConcurrencyInformation.BeginningThreadId == 0 &amp;&amp; ConcurrencyInformation.LockState == 0 &amp;&amp; Thread.CurrentThread.ManagedThreadId != 0)\n            ConcurrencyInformation.BeginningThreadId = Thread.CurrentThread.ManagedThreadId;\n        if (ConcurrencyInformation.LockState == 0)\n            if (ConcurrencyInformation.BeginningThreadId != Thread.CurrentThread.ManagedThreadId)\n            {\n                ConcurrencyInformation.LockState = 1;\n                UsingConcurrency                 = true;\n                return true;\n            }\n        Interlocked.Increment(ref ConcurrencyInformation.Calls);\n        return false;\n    }\n    [DebuggerDisplay(\"UniqueThreadIds = {ActiveThreads}\")]\n    public class ConcurrencyInfo\n    {\n        public volatile int     ActiveThreads;\n        public volatile int     BeginningThreadId;\n        public volatile int     Calls;\n        public volatile int     LockState;\n        public volatile int     StatusThreadId;\n        public volatile bool[]  UniqueThreadIds = new bool[32768];\n        public void Add(int value)\n        {\n            if (!UniqueThreadIds[value])\n            {\n                UniqueThreadIds[value] = true;\n                Interlocked.Increment(ref ActiveThreads);\n                return;\n            }\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Check and Store Concurrency Usage Updated: Dec-25,2020<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[78,81,82,30],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/166"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":3,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}