-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Internal Code
-
None
Static initializers don't work in the end, because there's no way to specify ordering dependences. The code is slowly amassing a hodgepodge of alternatives, including singleton objects initialized at first access, using a hand-written double-check locking pattern.
A better solution would be to allow programmers to declare "init functions" for modules, and to list their dependencies in their declaration. Then, at the top of main(), mongo programs would call InitMongo(), which would execute the init functions in top-sort order.
Implementations I've used in the past have declared init functions using a macro:
INIT_FN(fn_name, (input_dep_name_0, ...), (output_dep_name_0, ...)) { code that initializes some logical module, but cannot run until after (input_dep_name_0, ..., input_dep_name_n), and must run before (output_dep_name_0, ..., output_dep_name_n). }
Further, you can declare nodes in the dependency graph to aggregate things. For example, if you need to run some code after all global foo objects are initialized, you can have a macro for declaring global foo objects that makes their initialization code out-depend on "foos_initialized", a noop node. Then, your code that depends on "all foos are initialized" can just have an input dependency on "foos_initialized", and doesn't need to explicitly be able to name all instances of "foo".
- is depended on by
-
SERVER-6452 Replace time(0) calls
- Backlog
-
CXX-90 Crash due to static initialization order fiasco in BSON implementation
- Closed
- related to
-
SERVER-7332 Separate command line processing from server startup in mongod and mongos.
- Closed