[webkit-dev] Introduce RecursionGuard to prevent recursive calls to a function

KwangYul Seo kseo at webkit.org
Wed Jul 24 19:34:53 PDT 2013


Hello,

I am working on https://bugs.webkit.org/show_bug.cgi?id=119070, which
introduces a new helper class, RecursionGuard in WTF to prevent recursive
calls to a function.

We usually repeat the following pattern to prevent recursive calls to a
function

void Foo::bar()
{
    ASSERT(!m_inBar);
    m_inBar = true;

    ...

    m_inBar = false;
}

This is error prone because we can accidentally add a return statement in
the body of bar and forget to set m_isBar  back to false. TemporaryChange
is used to address this issue in some places, but it is way too general as
it restores old value whatever it was and doesn't provide an assertion we
need. So we need to find a tighter way to express the idiom of "this
boolean variable is only true while executing this code".

So I suggest we should introduce RecusrionGuard, a RAII idiom to prevent
this kind of error by asserting if the flag is false and setting it to true
in the constructor and then resetting the flag to false in the destructor.
With RecursionGuard, the code is simplified as in the following:

void Foo::bar()
{
    RecursionGuard guard(m_inBar);

    ...
}

I'd like to hear opinions on this new helper class.

Regards,
Kwang Yul Seo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20130725/26c52038/attachment.html>


More information about the webkit-dev mailing list