State of interfacing with c++

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Fri Mar 18 12:21:08 PDT 2016


On Friday, 18 March 2016 at 15:29:55 UTC, jmh530 wrote:
> On Friday, 18 March 2016 at 14:26:31 UTC, ZombineDev wrote:
>> The important thing to remember is that the Druntime must be 
>> initialized (see http://wiki.dlang.org/Runtime_internals and 
>> http://dlang.org/phobos/core_runtime.html#.rt_init for more 
>> info), before you call D functions that use the garbage 
>> collector. If all your D code is @nogc nothrow, you shouldn't 
>> need to do any initialization.
>
> You mean if you call D functions in C++, you have to use 
> rt_init to enable the garbage collector.

I'm sorry, what I said about rt_init() [1] is wrong. It does a 
lot of stuff and it's not just enabling the garbage collector. 
This means you have to be more careful if you decide to not call 
it. Here's what it does:

1. Initializes the global mutex that guards the creation of per 
object monitors, needed for the synchronized (obj) statement.
2. Initializes the first mutex in the critical section linked 
list, needed for the synchronized {} statement.
3. Locates the sections from the binary where the module info & 
module ctor / dtors are located
4. Queries the OS about the clock resolution needed in order for 
MonoTime to be usable.
5. Initializes the internal structures of the GC and reserves 
some initial amount of memory as per the GC config.
6. Adds the static data and bss segments to the GC ranges, so 
they can be scanned.
7. Initializes the callStructDtorsDuringGC flag by reading the GC 
config.
8. Runs shared module ctors
9. Run non-shared module ctors

[1]: 
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L158

> I don't see that mentioned on the Interfacing to C++ page. 
> Probably worth adding.
>
> Other suggestions:
> 1) Split up the C++ templates section of that page, instead of 
> just being big blocks of code.
> 2) In the memory allocation section, it mentions for ways to 
> ensure D garbage is collected properly by C++. Might provide a 
> simple example of each technique.

I agree.





More information about the Digitalmars-d mailing list