Add pragma to install function as global ctor or dtor. #161

Merged
merged 1 commit into from Dec 22, 2012

Conversation

Projects
None yet
2 participants
Owner

redstar commented Sep 12, 2012

DMD has the obscure functionality to install functions starting with _STI_ as global ctors and funtions starting with _STD_ as global dtors. IMHO a pragma is a better way to specify the behaviour.

This commit adds pragma(ctor) and pragma(dtor). If the pragma is specified on a function or static method then an entry is made in the corresponding list. E.g. in monitor_.d:

extern (C) {
    #pragma(ctor)
    void _STI_monitor_staticctor()
    {
        // ...
    }
}

This works on Linux without problems. On Windows with MS C Runtime only ctors works due to a bug in LLVM.

Owner

klickverbot commented Sep 14, 2012

I'm probably missing something, but what is the real advantage of this over module constructors/destructors?

Owner

redstar commented Sep 15, 2012

The difference is the point of time the code is executed. The function marked with pragma(ctor) is executed during initialization of the C runtime - that's before main is called. In contrast a module constructor is executed from the main function. This may be too late in rare situations.

E.g. _STI_monitor_staticctor must be executed before the module constructors. The Windows version of DMD performed the magic described above. In the Posix version, _STI_monitor_staticctor is called from main function before the module constructors are run. (That's now the only way it is done with a recent commit.)

Without this pragma you have to change main if have have the need to run a function very early.

Owner

klickverbot commented Sep 15, 2012

Hm, I see the point now, but I'm still not quite sure if the functionality is required in the first place. As far as I can tell, the _STI hack was simply something that already existed in the early D1 days from DMC (to be able to use static constructors/destructors from plain C), so it was used when monitors were first implemented in C. There seems to be no reason why the hack can't just be replaced by a call to the C function in the druntime `main' – like done on Posix, also for other low-level initialization stuff.

I'm not sure if there is much use to the pragmas in the general case either. druntime setup should be self-contained (if you find yourself needing to do something before druntime startup in a D program to make druntime work, it's a druntime bug), and if you'd really need to drop down to C level, you could always resort to a small C shim.

I'm not totally against including the pragmas, though, so I'll leave it up to you to decide. In case you opt to include them, please choose a more descriptive name, though (global constructor? low level constructor? C runtime constructor?) – they would be used so rarely that the potential confusion with . Also please don't forget to update the LDC pragma list (which also needs to be moved to GitHub – I'll do it tomorrow if you don't beat me to it).

Owner

redstar commented Dec 22, 2012

I updated this pull request to use new names LDC_global_crt_ctor and LDC_global_crt_dtor for the pragma. This should made it clear that it refers to the C runtime. I also fixed a compile error in V1 code.

Owner

klickverbot commented Dec 22, 2012

Okay, since it apparently is useful for you, I'll merge it once it passed the tester.

Owner

klickverbot commented Dec 22, 2012

Oh, a single last nitpick: Could you update the commit message to use the new names and remove the hash sign? Feel free to just push it to master afterwards.

Owner

redstar commented Dec 22, 2012

I already updated the commit message. Github simply does not update this request.

Owner

redstar commented Dec 22, 2012

Before I merge this request I will silence a clang warning I introduced.

Owner

klickverbot commented Dec 22, 2012

The commit message still says #pragma(global_crt_ctor) instead of pragma(LDC_global_crt_ctor), at least if GitHub didn't mess that up?

Add pragma to install function as global C runtime ctor or dtor.
DMD has the obscure functionality to install functions starting with
_STI_ as global ctors and funtions starting with _STD_ as global
dtors. IMHO a pragma is a better way to specify the behaviour.

This commit adds pragma(LDC_global_crt_ctor) and
pragma(LDC_global_crt_dtor). If the pragma is specified on a function
or static method then an entry is made in the corresponding list. E.g.
in monitor_.d:

    extern (C) {
        #pragma(LDC_global_crt_ctor)
        void _STI_monitor_staticctor()
        {
            // ...
        }
    }

This works on Linux without problems. On Windows with MS C Runtime
ctors work always but dtors are invoked only if linked against the
static C runtime. Dtors on Windows require at least LLVM 3.2.
Owner

redstar commented Dec 22, 2012

Ah, got it. Message is right now after a short misunderstanding with git.

Owner

klickverbot commented Dec 22, 2012

Okay, push at will. ;)

redstar added a commit that referenced this pull request Dec 22, 2012

Merge pull request #161 from redstar/ctordtor
Add pragma to install function as global ctor or dtor.

@redstar redstar merged commit 1600aa9 into ldc-developers:master Dec 22, 2012

1 check passed

default The Travis build passed
Details

@redstar redstar deleted the redstar:ctordtor branch Dec 22, 2012

redstar pushed a commit that referenced this pull request Sep 27, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment