Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add an attribute to a function to prevent reentry?

At the moment, I have some functions which look like this:

private bool inFunction1 = false;
public void function1()
{
    if (inFunction1) return;
    inFunction1 = true;

    // do stuff which might cause function1 to get called
    ...

    inFunction1 = false;
}

I'd like to be able to declare them like this:

[NoReEntry]
public void function1()
{
    // do stuff which might cause function1 to get called
    ...
}

Is there an attribute I can add to a function to prevent reentry? If not, how would I go about making one? I've heard about AOP attributes that can be used to add code before and after function calls; would they be suitable?

like image 713
Simon Avatar asked Nov 24 '08 11:11

Simon


4 Answers

Instead of using a bool and setting it directly, try using a long and the Interlocked class:

long m_InFunction=0;

if(Interlocked.CompareExchange(ref m_InFunction,1,0)==0)
{
  // We're not in the function
  try
  {
  }
  finally
  {
    m_InFunction=0;
  }
}
else
{
  // We're already in the function
}

This will make the check thread safe.

like image 179
Sean Avatar answered Nov 10 '22 10:11

Sean


Without assembly and IL rewriting, there's no way for you to create a custom attribute that modifies the code in the way you describe.

I suggest that you use a delegate-based approach instead, e.g. for functions of a single argument:

static Func<TArg,T> WrapAgainstReentry<TArg,T>(Func<TArg,T> code, Func<TArg,T> onReentry)
{
    bool entered = false;
    return x =>
    {
        if (entered)
            return onReentry(x);
        entered = true;
        try
        {
            return code(x);
        }
        finally
        {
            entered = false;
        }
    };
}

This method takes the function to wrap (assuming it matches Func<TArg,T> - you can write other variants, or a totally generic version with more effort) and an alternate function to call in cases of reentry. (The alternate function could throw an exception, or return immediately, etc.) Then, throughout your code where you would normally be calling the passed method, you call the delegate returned by WrapAgainstReentry() instead.

like image 30
Barry Kelly Avatar answered Nov 10 '22 12:11

Barry Kelly


You could build a PostSharp attribute to check to see if the name of the method is in the current stack trace

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static bool IsReEntry() {
        StackTrace stack = new StackTrace();
        StackFrame[] frames = stack.GetFrames();

        if (frames.Length < 2)
            return false;

        string currentMethod = frames[1].GetMethod().Name;

        for (int i = 2; i < frames.Length; i++) {
            if (frames[i].GetMethod().Name == currentMethod) {
                return true;
            }
        }

        return false;
    }
like image 4
Bob Avatar answered Nov 10 '22 10:11

Bob


You may find that you could use PostSharp to accomplish this - along with the suggestions from Anthony about using try/finally. It's likely to be messy though. Also consider whether you want the re-entrancy to be on a per-thread or per-instance basis. (Could multiple threads call into the method to start with, or not?)

There's nothing like this in the framework itself.

like image 2
Jon Skeet Avatar answered Nov 10 '22 12:11

Jon Skeet