[D-runtime] static this and extern(C)

Steve Schveighoffer schveiguy at yahoo.com
Wed Dec 22 11:47:45 PST 2010


While working on an update to the array append code that fixes a problem where 
the most recently used cache keeps an array in memory, I thought of this 
possible issue.

If I have a module A.d that looks like this:

module A;

extern(C) int bfunc();
int a;

static this()
{
  a = bfunc() + 1;
}

extern(C) int afunc() { return a; }

and a module B.d that looks like this:

module B;

extern(C) int afunc();
int b;

static this()
{
  b = afunc() + 1;
}

extern(C) int bfunc() { return b; }

This module has an undetectable cycle, because A and B depend on each other but 
they do not import each other.

Now, having extern(C) functions isn't a very common situation, but it does 
happen quite a bit in druntime, since most of the API is done through extern(C) 
functions.  I wonder if there is a way to possibly have the compiler flag 
modules that *define* extern(C) functions so the module construction/destruction 
order can try to run those first?  It won't fix the above problem, but it will 
at least make sure runtime functions are valid before any user/phobos code uses 
them.

Part of the fix that I'm working on depends on static ctors/dtors, so it is very 
relevant to the work I'm doing.

-Steve



      


More information about the D-runtime mailing list