Jump to page: 1 2
Thread overview
What happens when you launch a D application ?
May 22, 2015
Suliman
May 22, 2015
John Colvin
May 22, 2015
Suliman
May 22, 2015
John Colvin
May 22, 2015
John Colvin
May 22, 2015
Suliman
May 22, 2015
Adam D. Ruppe
May 23, 2015
Mike Parker
May 23, 2015
Mike
May 23, 2015
Mike
May 23, 2015
Suliman
May 23, 2015
Rikki Cattermole
May 23, 2015
Mike
May 23, 2015
Maxim Fomin
May 22, 2015
On SO[1] I got next answer:

"What happens when you launch a D application ? The entry point is a C main inside the runtime, which initialize it (the runtime), including module constructor, run the unittest (if you've compiled with -unittest), then call your main (which name is "_Dmain" - useful to know if you want to set a breakpoint with GDB). When Vibe.d's main is called, it parses command line argument, an optional config file, and finally, starts the event loop. Any code that wish to run once the event loop has started should use runTask and similar, or createTimer. They should not call the code directly from the static constructor (it's actually one of the most common mistake when starting with Vibe.d)."

Could you explain what mean "C main inside the runtime". I thought that is only one main is possible. And why it's named "*С* main" D is not C-translated language.

Same question is about _Dmain -- what is it?

If I will call this() before main? What it will be? Will it run before main?

[1] http://stackoverflow.com/questions/30302161/cant-connect-to-mysql-mariadb-database-from-vibed-app
May 22, 2015
On Friday, 22 May 2015 at 06:36:27 UTC, Suliman wrote:
> On SO[1] I got next answer:
>
> "What happens when you launch a D application ? The entry point is a C main inside the runtime, which initialize it (the runtime), including module constructor, run the unittest (if you've compiled with -unittest), then call your main (which name is "_Dmain" - useful to know if you want to set a breakpoint with GDB). When Vibe.d's main is called, it parses command line argument, an optional config file, and finally, starts the event loop. Any code that wish to run once the event loop has started should use runTask and similar, or createTimer. They should not call the code directly from the static constructor (it's actually one of the most common mistake when starting with Vibe.d)."
>
> Could you explain what mean "C main inside the runtime". I thought that is only one main is possible. And why it's named "*С* main" D is not C-translated language.
>
> Same question is about _Dmain -- what is it?
>
> If I will call this() before main? What it will be? Will it run before main?
>
> [1] http://stackoverflow.com/questions/30302161/cant-connect-to-mysql-mariadb-database-from-vibed-app

Glossing over a lot of the detail:
"_Dmain" is the symbol that is generated in the object file for the function called "main" in your source code. A symbol "main" is also generated, which is where the OS starts the whole program*. It sets up the runtime and module constructors etc. and then calls _Dmain.

*It's sometimes called the "C main" because it's equivalent to "main" in C.
May 22, 2015
Am I right understand that that:
1. every App start from main()
2. Dmain is function that run after main is started and it's run GC, unit-tests and so on?
May 22, 2015
On Friday, 22 May 2015 at 11:13:43 UTC, Suliman wrote:
> Am I right understand that that:
> 1. every App start from main()
> 2. Dmain is function that run after main is started and it's run GC, unit-tests and so on?

Not really, it depends what you mean by main, the function called main that you write in your source code or the function called main that ends up in the actual program?

Here's an example:

int a;

static this()
{
  a = 4;
}

void main() //
{
  a = 5;
}

Conceptually speaking, the compiler turns this in to:

int a;

extern(C) int main()
{
    a = 4;
    return _Dmain();
}

int _Dmain()
{
    a = 5;
    return 0;
}

I think that in practice it's more like this:

int a;

void staticConstructor1()
{
    a = 4;
}

//actually in druntime
int _d_run_main(int function() sourceCodeMain)
{
    staticConstructor1();
    // and all the other necessary setup
    // for the program and runtime

    sourceCodeMain();
}

extern(C) int main()
{
    _d_run_main(&_Dmain);
}

int _Dmain
{
    a = 5;
    return 0;
}
May 22, 2015
On Friday, 22 May 2015 at 11:51:01 UTC, John Colvin wrote:
> On Friday, 22 May 2015 at 11:13:43 UTC, Suliman wrote:
>> Am I right understand that that:
>> 1. every App start from main()
>> 2. Dmain is function that run after main is started and it's run GC, unit-tests and so on?
>
> Not really, it depends what you mean by main, the function called main that you write in your source code or the function called main that ends up in the actual program?
>
> Here's an example:
>
> int a;
>
> static this()
> {
>   a = 4;
> }
>
> void main() //
> {
>   a = 5;
> }
>
> Conceptually speaking, the compiler turns this in to:
>
> int a;
>
> extern(C) int main()
> {
>     a = 4;
>     return _Dmain();
> }
>
> int _Dmain()
> {
>     a = 5;
>     return 0;
> }
>
> I think that in practice it's more like this:
>
> int a;
>
> void staticConstructor1()
> {
>     a = 4;
> }
>
> //actually in druntime
> int _d_run_main(int function() sourceCodeMain)
> {
>     staticConstructor1();
>     // and all the other necessary setup
>     // for the program and runtime
>
>     sourceCodeMain();
> }
>
> extern(C) int main()
> {
>     _d_run_main(&_Dmain);
> }
>
> int _Dmain
> {
>     a = 5;
>     return 0;
> }

Give or take a few return statements, not that it matters.
May 22, 2015
Really hard to understand...

So what what would call at first ?
extern(C) int main()
or
int _Dmain()
May 22, 2015
On Friday, 22 May 2015 at 13:26:32 UTC, Suliman wrote:
> So what what would call at first ?

The operating system starts the program

Then the extern(C) int main() gets called.

Then the _Dmain gets called.
May 22, 2015
On 5/22/15 2:36 AM, Suliman wrote:
> On SO[1] I got next answer:
>
> "What happens when you launch a D application ? The entry point is a C
> main inside the runtime, which initialize it (the runtime), including
> module constructor, run the unittest (if you've compiled with
> -unittest), then call your main (which name is "_Dmain" - useful to know
> if you want to set a breakpoint with GDB). When Vibe.d's main is called,
> it parses command line argument, an optional config file, and finally,
> starts the event loop. Any code that wish to run once the event loop has
> started should use runTask and similar, or createTimer. They should not
> call the code directly from the static constructor (it's actually one of
> the most common mistake when starting with Vibe.d)."
>
> Could you explain what mean "C main inside the runtime". I thought that
> is only one main is possible. And why it's named "*С* main" D is not
> C-translated language.
>
> Same question is about _Dmain -- what is it?
>
> If I will call this() before main? What it will be? Will it run before
> main?

Druntime defines the main function that C runtime will call. C runtime intializes its own things (e.g. stdin/stdout/stderr) and then calls C main.

Druntime's version of C main then intializes all things that D needs (and there's quite a bit) not in any specific order:

- set up the main thread
- initialize the GC
- detect module constructor/destructor cycles
- run module constructors (both shared and thread-local)
- run unittests if enabled
- run your main function (defined in D as 'main')

After main exits, it unwinds all this stuff.

FYI, I didn't realize this (but just figured it out), C main *used* to be in druntime, but it's now generated by the compiler. See here:

https://github.com/D-Programming-Language/dmd/blob/master/src/mars.c#L236

The function it calls (_d_run_main) is now the entry point into druntime, and it is here:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L290

It can give you more clues as to how it works.

-Steve
May 23, 2015
On 5/22/2015 10:26 PM, Suliman wrote:
> Really hard to understand...
>
> So what what would call at first ?
> extern(C) int main()
> or
> int _Dmain()

Your D programs have multiple layers. There is the C runtime, DRuntime, and your program code.

The C runtime is at the bottom. When the program launches, it gets control first. When it has done its work, it looks for a main function and calls it. Normally, in a C program, that would be the main function that you implement. But in D, it is a function implemented by DRuntime.

The DRuntime main, because it is called by C, must be declared as extern(C) in order for the C runtime to recognize it. When it is called by the C runtime, then DRuntime does some housekeeping work (calling module constructors, intializing the garbage collecter, and so on). When it is done with that, it then calls the main function that you implemented in your program.

The exception to this is when a program is compiled on Windows with WinMain enabled. In this case, the C main is not the entry point, but WinMain is instead. DRuntime does not implement WinMain, so when compiling a D program like this, we have to call initialize DRuntime manually.
May 23, 2015
> FYI, I didn't realize this (but just figured it out), C main *used* to be in druntime, but it's now generated by the compiler. See here:
>
> https://github.com/D-Programming-Language/dmd/blob/master/src/mars.c#L236
>

True. But it is compiler-dependent.  GDC actually still defines "C main" in the runtime:  https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/__entrypoint.di#L60

Mike

« First   ‹ Prev
1 2