protected static void StaticLeaveCriticalSection(Concurrent <TSubClass> _this = null) { if (Monitor.IsEntered(_this?.CriticalSection ?? StaticCriticalSection)) { Monitor.Exit(_this?.CriticalSection ?? StaticCriticalSection); } }
protected static bool StaticAtomic( Func <bool> test, Action action, Action actionElse = null, Concurrent <TSubClass> _this = null) { bool success = false; lock (_this?.CriticalSection ?? StaticCriticalSection) { success = test(); if (success) { action(); } else if (actionElse != null) { actionElse(); } } return(success); }
protected static T StaticThreadSafeInit <T>( Func <T> getValue, Action init, Concurrent <TSubClass> _this = null) where T : class { // prevent global lock at every call T value = getValue(); if (value != null) { return(value); } lock (_this?.CriticalSection ?? StaticCriticalSection) { // prevent race conditions value = getValue(); if (value == null) { init(); value = getValue(); } return(value); } }
protected static void StaticEnterCriticalSection(Concurrent <TSubClass> _this = null) { Monitor.Enter(_this?.CriticalSection ?? StaticCriticalSection); }
protected static T StaticThreadSafe <T>(Func <T> func, Concurrent <TSubClass> _this = null) { lock (_this?.CriticalSection ?? StaticCriticalSection) { return(func()); } }
protected static void StaticThreadSafe(Action action, Concurrent <TSubClass> _this = null) { lock (_this?.CriticalSection ?? StaticCriticalSection) { action(); } }