Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-5112

Need a better story for startup-time initialization of C++ modules.

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.3.0
    • Affects Version/s: None
    • Component/s: Internal Code
    • Labels:
      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".

            Assignee:
            schwerin@mongodb.com Andy Schwerin
            Reporter:
            schwerin@mongodb.com Andy Schwerin
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: