Created
January 12, 2021 14:08
-
-
Save mjs3339/825d9b5805ed3d2cf1b6cfde2eb6c461 to your computer and use it in GitHub Desktop.
Monitor Lock Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
public class MonitorActionFuncWrapper : ConcurrencyCheck | |
{ | |
public void Lock(object localObject, Action action) | |
{ | |
if (action == null) | |
throw new Exception("Action argument cannot be null"); | |
if (CheckState()) | |
{ | |
var lockTaken = false; | |
try | |
{ | |
Monitor.Enter(localObject, ref lockTaken); | |
action(); | |
} | |
finally | |
{ | |
if (lockTaken) | |
Monitor.Exit(localObject); | |
} | |
} | |
else | |
{ | |
action(); | |
} | |
} | |
public TResult Lock<TResult>(object localObject, Func<TResult> func) | |
{ | |
if (func == null) | |
throw new Exception("Func argument cannot be null"); | |
if (CheckState()) | |
{ | |
var lockTaken = false; | |
try | |
{ | |
Monitor.Enter(localObject, ref lockTaken); | |
return func(); | |
} | |
finally | |
{ | |
if (lockTaken) | |
Monitor.Exit(localObject); | |
} | |
} | |
return func(); | |
} | |
public (TResult1, TResult2) Lock<TResult1, TResult2>(object localObject, Func<(TResult1, TResult2)> func) | |
{ | |
if (func == null) | |
throw new Exception("Func argument cannot be null"); | |
if (CheckState()) | |
{ | |
var lockTaken = false; | |
try | |
{ | |
Monitor.Enter(localObject, ref lockTaken); | |
return func(); | |
} | |
finally | |
{ | |
if (lockTaken) | |
Monitor.Exit(localObject); | |
} | |
} | |
return func(); | |
} | |
public (TResult1, TResult2) Lock<TArg, TResult1, TResult2>(object localObject, Func<TArg, (TResult1, TResult2)> func, TArg arg) | |
{ | |
if (func == null) | |
throw new Exception("Func argument cannot be null"); | |
if (CheckState()) | |
{ | |
var lockTaken = false; | |
try | |
{ | |
Monitor.Enter(localObject, ref lockTaken); | |
return func(arg); | |
} | |
finally | |
{ | |
if (lockTaken) | |
Monitor.Exit(localObject); | |
} | |
} | |
return func(arg); | |
} | |
public TResult Lock<TArg, TResult>(object localObject, Func<TArg, TResult> func, TArg arg) | |
{ | |
if (func == null) | |
throw new Exception("Func argument cannot be null"); | |
if (CheckState()) | |
{ | |
var lockTaken = false; | |
try | |
{ | |
Monitor.Enter(localObject, ref lockTaken); | |
return func(arg); | |
} | |
finally | |
{ | |
if (lockTaken) | |
Monitor.Exit(localObject); | |
} | |
} | |
return func(arg); | |
} | |
public void Lock<TArg>(object localObject, Action<TArg> action, TArg arg) | |
{ | |
if (action == null) | |
throw new Exception("Action argument cannot be null"); | |
if (CheckState()) | |
{ | |
var lockTaken = false; | |
try | |
{ | |
Monitor.Enter(localObject, ref lockTaken); | |
action(arg); | |
} | |
finally | |
{ | |
if (lockTaken) | |
Monitor.Exit(localObject); | |
} | |
} | |
else | |
{ | |
action(arg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment