www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I need a detailed document about druntime.

reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
I want to know what happens if either Runtime.initialize or 
terminate is called without matching each other. And why there is 
no ready-to-use thing like initCount which is accessible in the 
code? What I want is to bypass runtime initialization if it is 
already initialized. The below code is my workaround, but it can 
only keep track of my initializations, not the one that 
automatically get fired where there is a dmain.

Should İ use "bool runMain" to differentiate between the 
compilations of dll and executable?

```d
private {
     import core.runtime;
     import core.atomic;

     shared size_t initCount;

     void initRT(){
         if(!atomicLoad!(MemoryOrder.acq)(initCount)){
             Runtime.initialize();
             atomicOp!"+="(initCount, 1);
         }
     }

     void termRT(){
         if(atomicLoad!(MemoryOrder.acq)(initCount) > 0){
             Runtime.terminate();
             atomicOp!"-="(initCount, 1);
         }
     }
}
```
Oct 11 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Monday, 11 October 2021 at 14:56:19 UTC, Ferhat Kurtulmuş 
wrote:
 What I want is to bypass runtime initialization if it is 
 already initialized.
That what it does by itself, you can call it and it has an internal count.
Oct 11 2021
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Monday, 11 October 2021 at 15:09:12 UTC, Adam D Ruppe wrote:
 On Monday, 11 October 2021 at 14:56:19 UTC, Ferhat Kurtulmuş 
 wrote:
 What I want is to bypass runtime initialization if it is 
 already initialized.
That what it does by itself, you can call it and it has an internal count.
Thank you for your response. But this confuses me: "Each call to initialize must be paired by a call to terminate.' https://dlang.org/library/core/runtime/runtime.initialize.html
Oct 11 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Monday, 11 October 2021 at 15:18:11 UTC, Ferhat Kurtulmuş 
wrote:
 "Each call to initialize must be paired by a call to terminate.'
It is so the refcount works out. When you call initialize, it does something like: if(refcount == 0) actually intialize; // calls constructors etc refcount++; When you call terminate, it does: refcount--; if(refcount == 0) actually terminate; // calls destructors etc If you don't pair it, the refcount will be off, so the next call to terminate will still see ref > 0 and not actually terminate. The D main inserts a call to init before main and a call to terminate after main automatically.
Oct 11 2021
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Monday, 11 October 2021 at 15:24:07 UTC, Adam D Ruppe wrote:
 On Monday, 11 October 2021 at 15:18:11 UTC, Ferhat Kurtulmuş 
 wrote:
 "Each call to initialize must be paired by a call to 
 terminate.'
It is so the refcount works out. When you call initialize, it does something like: if(refcount == 0) actually intialize; // calls constructors etc refcount++; When you call terminate, it does: refcount--; if(refcount == 0) actually terminate; // calls destructors etc If you don't pair it, the refcount will be off, so the next call to terminate will still see ref > 0 and not actually terminate. The D main inserts a call to init before main and a call to terminate after main automatically.
That makes sense, now I see, thank you again Adam.
Oct 11 2021