Is there a way to execute code after 'initialization' and before 'main', i. e. AddExitProc complement?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a way to execute code after 'initialization' and before 'main', i. e. AddExitProc complement?

Free Pascal - General mailing list
'Initialization' sections are executed in poorly predictable order, so I
want to partially order them according to some form of explicit
prioritization and execute before control flow enters main block (so I
won't need to run all that initialization by hand, neither I would be
able to forget to do that). Like:

unit UnitA;
...
procedure Init;

initialization
   RegisterUnit('UnitA', @Init);
end;

unit UnitB;
...
procedure Init;

initialization
   RegisterUnit('UnitB', @Init, {priority} +1);
end;

...
procedure SortAndInitialize;
...

Can I run SortAndInitialize before 'main' without additional user actions?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to execute code after 'initialization' and before 'main', i. e. AddExitProc complement?

Free Pascal - General mailing list

Am 28.11.2017 20:04 schrieb "Роман via fpc-pascal" <[hidden email]>:
'Initialization' sections are executed in poorly predictable order, so I want to partially order them according to some form of explicit prioritization and execute before control flow enters main block (so I won't need to run all that initialization by hand, neither I would be able to forget to do that).

If unit A uses unit B in the interface section then unit B's initialization section is always run before A's. For the implementation section the same holds true if there isn't a cycle otherwise it depends on the unit load order. 

Like:

unit UnitA;
...
procedure Init;

initialization
  RegisterUnit('UnitA', @Init);
end;

unit UnitB;
...
procedure Init;

initialization
  RegisterUnit('UnitB', @Init, {priority} +1);
end;

...
procedure SortAndInitialize;
...

Can I run SortAndInitialize before 'main' without additional user actions?

No, you can't.

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to execute code after 'initialization' and before 'main', i. e. AddExitProc complement?

Christo Crause
In reply to this post by Free Pascal - General mailing list

On 28 Nov 2017 9:04 PM, "Роман via fpc-pascal" <[hidden email]> wrote:
> Can I run SortAndInitialize before 'main' without additional user actions?

Maybe not exactly what you want but you could probably add the SortAndInitialize procedure to the initialization section of a new unit and add this unit as the last unit to your program.

This use case is probably a good candidate for using the "section" modifier to specify initialization sequence. Unfortunately not available in the compiler for code sections AFAIK.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal