Mercurial > projects > mde
comparison 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 |
comparison
equal
deleted
inserted
replaced
24:32eff0e01c05 | 25:2c28ee04a4ed |
---|---|
1 /** This module is responsible for calling all init functions. | |
2 * | |
3 * It is also responsible for setting up all scheduled functions for now. */ | |
4 module mde.scheduler.InitFunctions; | |
5 | |
6 static import mde.gl; | |
7 | |
8 import tango.util.log.Log : Log, Logger; | |
9 static this() { | |
10 logger = Log.getLogger ("mde.scheduler.InitFunctions"); | |
11 } | |
12 | |
13 /** Should be called by an init function when a failure occurs. */ | |
14 void setInitFailure () { | |
15 initFailure = true; | |
16 } | |
17 | |
18 package: | |
19 | |
20 /** Represents all functions to be called for a particular init stage. */ | |
21 struct InitStage | |
22 { | |
23 alias void function() InitFunction; /// Alias | |
24 | |
25 /** Add a function to be called during this init stage. | |
26 * | |
27 * Called in order added when not threaded (reverse order for cleanup). | |
28 * | |
29 * Exceptions should never be thrown, since each function may run as a thread, and catching | |
30 * thread exceptions is not guaranteed to work. Log a message, call setFailure() and return | |
31 * instead. */ | |
32 void addFunc (InitFunction f) { | |
33 funcs ~= f; | |
34 } | |
35 | |
36 InitFunction[] funcs = []; | |
37 } | |
38 | |
39 InitStage init; // all functions called during init (all should be thread-safe) | |
40 InitStage cleanup; // all functions called during cleanup (all should be thread-safe) | |
41 | |
42 bool initFailure = false; // set on failure (throwing through threads isn't a good idea) | |
43 | |
44 private: | |
45 Logger logger; | |
46 const FAIL_MSG = "Init function failed: "; | |
47 // Template to call function, catching exceptions: | |
48 void initInput(alias Func) () { | |
49 try { | |
50 Func(); | |
51 } catch (Exception e) { | |
52 logger.fatal (FAIL_MSG ~ e.msg); | |
53 initFailure = true; | |
54 } | |
55 } |