Mercurial > projects > mde
annotate mde/scheduler/Init.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 | 32eff0e01c05 |
children | 611f7b9063c6 |
rev | line source |
---|---|
20 | 1 /* LICENSE BLOCK |
2 Part of mde: a Modular D game-oriented Engine | |
3 Copyright © 2007-2008 Diggory Hardy | |
4 | |
5 This program is free software; you can redistribute it and/or modify it under the terms of | |
6 the GNU General Public License, version 2, as published by the Free Software Foundation. | |
7 | |
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
10 See the GNU General Public License for more details. | |
11 | |
12 You should have received a copy of the GNU General Public License along | |
13 with this program; if not, write to the Free Software Foundation, Inc., | |
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ | |
15 | |
16 /************************************************************************************************** | |
17 * Initialisation setup and exit cleanup module. | |
18 * | |
19 * This module provides an infrastructure for handling much of the initialisation and | |
20 * deinitialisation of the program. It does not, however, provide much of the (de)initialisation | |
21 * code; with the exception of that for the logger. | |
22 *************************************************************************************************/ | |
23 module mde.scheduler.Init; | |
24 | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
25 import mde.scheduler.InitFunctions; |
20 | 26 import mde.scheduler.exception; |
27 | |
28 import mde.options; | |
29 import paths = mde.resource.paths; | |
30 import mde.exception; | |
31 | |
32 // tango imports | |
33 import tango.core.Thread; | |
34 import tango.core.Exception; | |
35 import tango.stdc.stringz : fromStringz; | |
36 import tango.util.log.Log : Log, Logger; | |
37 import tango.util.log.ConsoleAppender : ConsoleAppender; | |
38 | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
39 //version = SwitchAppender; |
20 | 40 version (SwitchAppender) { // My own variation, currently just a test |
41 import tango.util.log.SwitchingFileAppender : SwitchingFileAppender; | |
42 } else { | |
43 import tango.util.log.RollingFileAppender : RollingFileAppender; | |
44 } | |
45 | |
46 /** | |
47 * Static CTOR | |
48 * | |
49 * This should handle a minimal amount of functionality where useful. For instance, configuring the | |
50 * logger here and not in Init allows unittests to use the logger. | |
51 */ | |
52 static this() | |
53 { | |
54 // Find/create paths: | |
55 try { | |
56 paths.resolvePaths(); | |
57 } catch (Exception e) { | |
58 throw new InitException ("Resolving paths failed: " ~ e.msg); | |
59 } | |
60 | |
61 // Set up the logger: | |
62 { | |
63 // Where logging is done to is determined at compile-time, currently just via static ifs. | |
64 Logger root = Log.getRootLogger(); | |
65 | |
66 static if (true ) { // Log to the console | |
67 root.addAppender(new ConsoleAppender); | |
68 } | |
69 static if (true ) { // Log to files | |
70 version (SwitchAppender) { | |
71 root.addAppender (new SwitchingFileAppender (paths.logDir~"/log-.txt", 5)); | |
72 } else { | |
73 // Use 2 log files with a maximum size of 1 MB: | |
74 root.addAppender (new RollingFileAppender (paths.logDir~"/log-.txt", 2, 1024*1024)); | |
75 } | |
76 } | |
77 | |
78 // Set the level here, but set it again once options have been loaded: | |
79 debug root.setLevel(root.Level.Trace); | |
80 else root.setLevel(root.Level.Info); | |
81 } | |
82 } | |
83 static ~this() | |
84 { | |
85 } | |
86 | |
87 /** | |
88 * Init class | |
89 * | |
90 * A scope class created at beginning of the program and destroyed at the end; thus the CTOR | |
91 * handles program initialisation and the DTOR handles program cleanup. | |
92 */ | |
93 scope class Init | |
94 { | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
95 //private bool failure = false; // set true on failure during init, so that clean |
20 | 96 private static Logger logger; |
97 static this() { | |
98 logger = Log.getLogger ("mde.scheduler.Init.Init"); | |
99 } | |
100 | |
101 /** CTOR − initialisation | |
102 * | |
103 * Runs general initialisation code, in a threaded manner where this isn't difficult. | |
104 * | |
105 * If any init fails, it must run necessary cleanup first since the DTOR cannot(?) be run. */ | |
106 /* In a single-threaded function this could be done with: | |
107 * scope(failure) cleanup; | |
108 * This won't work with a threaded init function since any threads completing succesfully will | |
109 * not clean-up, and a fixed list of clean-up functions cannot be used since clean-up functions | |
110 * must not run where the initialisation functions have failed. | |
111 * Hence a list of clean-up functions is built similarly to scope(failure) --- see addCleanupFct. | |
112 */ | |
113 this() | |
114 { | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
115 debug logger.trace ("Init: starting"); |
20 | 116 |
117 //BEGIN Pre-init (stage init0) | |
118 /* Load options now. Don't load in a thread since: | |
119 * Loading should be fast | |
120 * Most work is probably disk access | |
121 * It's a really good idea to let the options apply to all other loading. */ | |
122 try { | |
123 Options.load(); | |
124 } catch (optionsLoadException e) { | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
125 throw new InitException ("Loading options failed: " ~ e.msg); |
20 | 126 } |
127 | |
128 // Now re-set the logging level: | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
129 Log.getRootLogger.setLevel (cast(Log.Level) miscOpts.logLevel, true); // set the stored log level |
20 | 130 //END Pre-init |
131 | |
132 | |
133 //BEGIN Init (stages init2, init4) | |
134 /* Call init functions. | |
135 * | |
136 * Current method is to try using threads, and on failure assume no threads were actually | |
137 * created and run functions in a non-threaded manner. */ | |
138 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
139 try { |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
140 if (runStageThreaded (init)) runStageForward (init); |
20 | 141 } |
142 catch (InitStageException) { // This init stage failed. | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
143 // FIXME: check DTOR still runs |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
144 throw new InitException ("An init function failed (see above message(s))"); |
20 | 145 } |
146 //END Init | |
147 | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
148 debug logger.trace ("Init: done"); |
20 | 149 } |
150 | |
151 /** DTOR - runs cleanup functions. */ | |
152 ~this() | |
153 { | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
154 debug logger.trace ("Cleanup: starting"); |
20 | 155 |
156 Options.save(); // save options... do so here for now | |
157 | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
158 // General cleanup: |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
159 try { |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
160 if (runStageThreaded (cleanup)) runStageReverse (cleanup); |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
161 } |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
162 catch (InitStageException) { |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
163 // Nothing else to do but report: |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
164 logger.error ("One or more cleanup functions failed!"); |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
165 } |
20 | 166 |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
167 debug logger.trace ("Cleanup: done"); |
20 | 168 } |
169 | |
170 | |
171 //BEGIN runStage... | |
172 private static { | |
173 /* The following three functions, runStage*, each run all functions in a stage in some order, | |
174 * catching any exceptions thrown by the functions (although this isn't guaranteed for threads), | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
175 * and throw an InitStageException on initFailure. */ |
20 | 176 |
177 const UFE = "Unhandled exception from Init function:"; | |
178 /* Runs all functions consecutively, first-to-last. | |
179 * If any function fails, halts immediately. */ | |
180 void runStageForward (InitStage s) { | |
181 foreach (func; s.funcs) { | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
182 if (initFailure) break; |
20 | 183 try { |
184 func(); | |
185 } catch (Exception e) { | |
186 logger.fatal (UFE); | |
187 logger.fatal (e.msg); | |
188 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
189 setInitFailure(); |
20 | 190 } |
191 } | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
192 |
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
193 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 194 } |
195 /* Runs all functions consecutively, last-to-first. | |
196 * If any function fails, continue until all have been run. */ | |
197 void runStageReverse (InitStage s) { | |
198 foreach_reverse (func; s.funcs) { | |
199 try { | |
200 func(); | |
201 } catch (Exception e) { | |
202 logger.fatal (UFE); | |
203 logger.fatal (e.msg); | |
204 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
205 setInitFailure(); |
20 | 206 } |
207 } | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
208 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 209 } |
210 /* Tries running functions in a threaded way. Returns false if successful, true if not but | |
211 * functions should be run without threads. */ | |
212 bool runStageThreaded (InitStage s) { | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
213 if (!miscOpts.useThreads) return true; // Use unthreaded route instead |
20 | 214 |
215 ThreadGroup tg; | |
216 try { // creating/starting threads could fail | |
217 tg = new ThreadGroup; | |
218 foreach (func; s.funcs) tg.create(func); // Start all threads | |
219 } catch (ThreadException e) { // Problem with threading; try without threads | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
220 logger.error ("Caught ThreadException while trying to create threads:"); |
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
221 logger.error (e.msg); |
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
222 logger.info ("Will disable threads and continue."); |
20 | 223 |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
224 Options.setBool("misc", "useThreads", false); // Disable threads entirely |
20 | 225 return true; // Try again without threads |
226 } | |
227 | |
228 /* Wait for all threads to complete. | |
229 * | |
230 * If something went wrong, we still need to do this before cleaning up. | |
231 */ | |
232 foreach (t; tg) { | |
233 try { | |
234 t.join (true); | |
235 } catch (Exception e) { | |
236 // Relying on catching exceptions thrown by other threads is a bad idea. | |
237 // Hence all threads should catch their own exceptions and return safely. | |
238 | |
239 logger.fatal ("Unhandled exception from Init function:"); | |
240 logger.fatal (e.msg); | |
241 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
242 setInitFailure (); // abort (but join other threads first) |
20 | 243 } |
244 } | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
245 |
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
246 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 247 return false; // Done successfully |
248 } | |
249 } | |
250 //END runStage... | |
251 | |
252 debug (mdeUnitTest) unittest { | |
253 /* Fake init and cleanup. Use unittest-specific init and cleanup InitStages to avoid | |
254 * messing other init/cleanup up. */ | |
255 static InitStage initUT, cleanupUT; | |
256 | |
257 static bool initialised = false; | |
258 static void cleanupFunc1 () { | |
259 initialised = false; | |
260 } | |
261 static void cleanupFunc2 () { | |
262 assert (initialised == true); | |
263 } | |
264 | |
265 static void initFunc () { | |
266 initialised = true; | |
267 cleanupUT.addFunc (&cleanupFunc1); | |
268 cleanupUT.addFunc (&cleanupFunc2); | |
269 } | |
270 | |
271 initUT.addFunc (&initFunc); | |
272 | |
273 runStageForward (initUT); | |
274 assert (initialised); | |
275 | |
276 runStageReverse (cleanupUT); | |
277 assert (!initialised); | |
278 | |
279 logger.info ("Unittest complete."); | |
280 } | |
281 } |