detecting or preventing re-entrant functions
Timothee Cour via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jan 2 22:41:18 PST 2016
some weird bugs can occur when, for example, an error or signal handler is
re-entrant.
Is there a way to detect this?
this works but is cumbersome to use [see below], is there a simpler way to
detect in calling code (i want to simplify SNIPPET in calling code)
---
module util.reentrance_check;
import std.stdio;
void reentrance_check(ref int depth, string file=__FILE__, int
line=__LINE__){
depth++;
if(depth>1){
// make sure code here is not reentrant
writeln(file, ":", line, " reentrant ", depth, " ");
// print stack trace...
import core.stdc.stdlib: exit;
exit(1);// because assert(0) could in turn call a handler which could
re-enter here
return;
}
}
unittest{
example_reentrance_check;
example_reentrance_check;
}
// USAGE example
void example_reentrance_check(){
// NOTE: can't use RAII because $depth a static var
// TODO: how can we simplify this, to a 1-liner?
// insert this block in your non-re-entrant function
import util.reentrance_check;
static int depth=0;
reentrance_check(depth);
scope(success) depth--;
/// code after
version(bad) example_reentrance_check;// should crash with -version=bad
}
---
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20160102/4e12e730/attachment.html>
More information about the Digitalmars-d
mailing list