view mde/scheduler/InitStage.d @ 20:838577503598

Reworked much of Init. Moved mde.Init to mde.scheduler.Init and largely cleaned up the code. Implemented mde.scheduler.InitStage to reduce dependancies of modules running Init functions. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 22 Mar 2008 16:22:59 +0000
parents
children a60cbb7359dd
line wrap: on
line source

/* LICENSE BLOCK
Part of mde: a Modular D game-oriented Engine
Copyright © 2007-2008 Diggory Hardy

This program is free software; you can redistribute it and/or modify it under the terms of
the GNU General Public License, version 2, as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

/** This module is used to create a list of all functions required to run for initialisation and
* cleanup.
*
* It has been separated out of Init.d to massively reduce dependancies of modules adding init
* functions.
*/
module mde.scheduler.InitStage;

/** Represents all functions to be called for a particular init stage.
*
* No code is included here to run the functions intentionally, to keep dependancies minimal.
*/
struct InitStage
{
    alias void function() InitFunction; /// Alias
    
    /** Add a function to be called during this init stage.
    *
    * Called in order added when not threaded (reverse order for cleanup).
    *
    * Exceptions should never be thrown, since each function may run as a thread, and catching
    * thread exceptions is not guaranteed to work. Log a message, call setFailure() and return
    * instead. */
    void addFunc (InitFunction f) {
        funcs ~= f;
    }
    
    /** Should be called by an init function when a failure occurs. */
    void setFailure () {
        synchronized failure = true;
    }
    
    package InitFunction[]  funcs   = [];
    package bool            failure = false;
}

/** Init can be divided up into these stages, each run in order:
*
* init0:
*   static this() (not represented here)
*
* init1:
*   Also known as pre-init; is where options get loaded by Init (not represented here).
*
* init2:
*   Main symbol and config loading and general low-level stage. This stage is threaded, so all
*   called functions need to be thread-safe.
*
* init3:
*   Reserved as an unthreaded stage.
*
* init4:
*   Main loading stage for data files and setup for higher-level elements, e.g. windows and input
*   devices. This stage is threaded, so all called functions need to be thread-safe.
*
* cleanupX:
*   Corresponding cleanup stage to initX. Called in reverse order (e.g. cleanup2 called before
*   cleanup1). Cleanup is never threaded.
*
* The following functions get called (update list as appropriate):
*
* init2:
*   mde.SDL.initSdlAndGl, mde.events.initInput
*
* init4:
*   mde.SDL.setupWindow
*
* cleanup2:
*   (Potentially): mde.SDL.cleanupSDL, mde.init.joystick.closeJoysticks
*/
InitStage init2;
InitStage init4;    /// ditto
InitStage cleanup2; /// ditto
InitStage cleanup4; /// ditto