view mde/scheduler/InitFunctions.d @ 25:2c28ee04a4ed

Some minor and some futile efforts. Played around with init functions, had problems, gave up and put them back. Removed idea for multiple init stages; it's not good for performance or simplicity. Adjusted exception messages. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 03 Apr 2008 17:26:52 +0100
parents
children 611f7b9063c6
line wrap: on
line source

/** This module is responsible for calling all init functions.
*
* It is also responsible for setting up all scheduled functions for now. */
module mde.scheduler.InitFunctions;

static import mde.gl;

import tango.util.log.Log : Log, Logger;
static this() {
    logger = Log.getLogger ("mde.scheduler.InitFunctions");
}

/** Should be called by an init function when a failure occurs. */
void setInitFailure () {
    initFailure = true;
}

package:

/** Represents all functions to be called for a particular init stage. */
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;
    }
    
    InitFunction[] funcs = [];
}

InitStage init;     // all functions called during init (all should be thread-safe)
InitStage cleanup;  // all functions called during cleanup (all should be thread-safe)

bool initFailure = false;   // set on failure (throwing through threads isn't a good idea)

private:
Logger logger;
const FAIL_MSG = "Init function failed: ";
// Template to call function, catching exceptions:
void initInput(alias Func) () {
    try {
        Func();
    } catch (Exception e) {
        logger.fatal (FAIL_MSG ~ e.msg);
        initFailure = true;
    }
}