Mercurial > projects > mde
annotate mde/scheduler/Init.d @ 26:611f7b9063c6
Changed the licensing and removed a few dead files.
Changed licensing to "GPL version 2 or later" to avoid future compatibility issues.
Also a unittest fix to the previous commit.
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Thu, 03 Apr 2008 18:15:02 +0100 |
parents | 2c28ee04a4ed |
children | b5fadd8d930b |
rev | line source |
---|---|
20 | 1 /* LICENSE BLOCK |
2 Part of mde: a Modular D game-oriented Engine | |
3 Copyright © 2007-2008 Diggory Hardy | |
4 | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify it under the terms |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
6 of the GNU General Public License as published by the Free Software Foundation, either |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
7 version 2 of the License, or (at your option) any later version. |
20 | 8 |
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
11 See the GNU General Public License for more details. | |
12 | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
13 You should have received a copy of the GNU General Public License |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
20 | 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(); | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
65 |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
66 static if (true ) { // Log to files (first appender so root seperator messages don't show on console) |
20 | 67 version (SwitchAppender) { |
68 root.addAppender (new SwitchingFileAppender (paths.logDir~"/log-.txt", 5)); | |
69 } else { | |
70 // Use 2 log files with a maximum size of 1 MB: | |
71 root.addAppender (new RollingFileAppender (paths.logDir~"/log-.txt", 2, 1024*1024)); | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
72 root.info (""); // some kind of separation between runs |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
73 root.info (""); |
20 | 74 } |
75 } | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
76 static if (true ) { // Log to the console |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
77 root.addAppender(new ConsoleAppender); |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
78 } |
20 | 79 |
80 // Set the level here, but set it again once options have been loaded: | |
81 debug root.setLevel(root.Level.Trace); | |
82 else root.setLevel(root.Level.Info); | |
83 } | |
84 } | |
85 static ~this() | |
86 { | |
87 } | |
88 | |
89 /** | |
90 * Init class | |
91 * | |
92 * A scope class created at beginning of the program and destroyed at the end; thus the CTOR | |
93 * handles program initialisation and the DTOR handles program cleanup. | |
94 */ | |
95 scope class Init | |
96 { | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
97 //private bool failure = false; // set true on failure during init, so that clean |
20 | 98 private static Logger logger; |
99 static this() { | |
100 logger = Log.getLogger ("mde.scheduler.Init.Init"); | |
101 } | |
102 | |
103 /** CTOR − initialisation | |
104 * | |
105 * Runs general initialisation code, in a threaded manner where this isn't difficult. | |
106 * | |
107 * If any init fails, it must run necessary cleanup first since the DTOR cannot(?) be run. */ | |
108 /* In a single-threaded function this could be done with: | |
109 * scope(failure) cleanup; | |
110 * This won't work with a threaded init function since any threads completing succesfully will | |
111 * not clean-up, and a fixed list of clean-up functions cannot be used since clean-up functions | |
112 * must not run where the initialisation functions have failed. | |
113 * Hence a list of clean-up functions is built similarly to scope(failure) --- see addCleanupFct. | |
114 */ | |
115 this() | |
116 { | |
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
|
117 debug logger.trace ("Init: starting"); |
20 | 118 |
119 //BEGIN Pre-init (stage init0) | |
120 /* Load options now. Don't load in a thread since: | |
121 * Loading should be fast | |
122 * Most work is probably disk access | |
123 * It's a really good idea to let the options apply to all other loading. */ | |
124 try { | |
125 Options.load(); | |
126 } catch (optionsLoadException e) { | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
127 throw new InitException ("Loading options failed: " ~ e.msg); |
20 | 128 } |
129 | |
130 // 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
|
131 Log.getRootLogger.setLevel (cast(Log.Level) miscOpts.logLevel, true); // set the stored log level |
20 | 132 //END Pre-init |
133 | |
134 | |
135 //BEGIN Init (stages init2, init4) | |
136 /* Call init functions. | |
137 * | |
138 * Current method is to try using threads, and on failure assume no threads were actually | |
139 * created and run functions in a non-threaded manner. */ | |
140 | |
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
|
141 try { |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
142 if (runStageThreaded (init)) runStageForward (init); |
20 | 143 } |
144 catch (InitStageException) { // This init stage failed. | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
145 // FIXME: check DTOR still runs |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
146 throw new InitException ("An init function failed (see above message(s))"); |
20 | 147 } |
148 //END Init | |
149 | |
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
|
150 debug logger.trace ("Init: done"); |
20 | 151 } |
152 | |
153 /** DTOR - runs cleanup functions. */ | |
154 ~this() | |
155 { | |
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
|
156 debug logger.trace ("Cleanup: starting"); |
20 | 157 |
158 Options.save(); // save options... do so here for now | |
159 | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
160 // General cleanup: |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
161 try { |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
162 if (runStageThreaded (cleanup)) runStageReverse (cleanup); |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
163 } |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
164 catch (InitStageException) { |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
165 // Nothing else to do but report: |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
166 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
|
167 } |
20 | 168 |
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
|
169 debug logger.trace ("Cleanup: done"); |
20 | 170 } |
171 | |
172 | |
173 //BEGIN runStage... | |
174 private static { | |
175 /* The following three functions, runStage*, each run all functions in a stage in some order, | |
176 * 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
|
177 * and throw an InitStageException on initFailure. */ |
20 | 178 |
179 const UFE = "Unhandled exception from Init function:"; | |
180 /* Runs all functions consecutively, first-to-last. | |
181 * If any function fails, halts immediately. */ | |
182 void runStageForward (InitStage s) { | |
183 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
|
184 if (initFailure) break; |
20 | 185 try { |
186 func(); | |
187 } catch (Exception e) { | |
188 logger.fatal (UFE); | |
189 logger.fatal (e.msg); | |
190 | |
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
|
191 setInitFailure(); |
20 | 192 } |
193 } | |
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
|
194 |
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
|
195 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 196 } |
197 /* Runs all functions consecutively, last-to-first. | |
198 * If any function fails, continue until all have been run. */ | |
199 void runStageReverse (InitStage s) { | |
200 foreach_reverse (func; s.funcs) { | |
201 try { | |
202 func(); | |
203 } catch (Exception e) { | |
204 logger.fatal (UFE); | |
205 logger.fatal (e.msg); | |
206 | |
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
|
207 setInitFailure(); |
20 | 208 } |
209 } | |
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
|
210 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 211 } |
212 /* Tries running functions in a threaded way. Returns false if successful, true if not but | |
213 * functions should be run without threads. */ | |
214 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
|
215 if (!miscOpts.useThreads) return true; // Use unthreaded route instead |
20 | 216 |
217 ThreadGroup tg; | |
218 try { // creating/starting threads could fail | |
219 tg = new ThreadGroup; | |
220 foreach (func; s.funcs) tg.create(func); // Start all threads | |
221 } 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
|
222 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
|
223 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
|
224 logger.info ("Will disable threads and continue."); |
20 | 225 |
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
|
226 Options.setBool("misc", "useThreads", false); // Disable threads entirely |
20 | 227 return true; // Try again without threads |
228 } | |
229 | |
230 /* Wait for all threads to complete. | |
231 * | |
232 * If something went wrong, we still need to do this before cleaning up. | |
233 */ | |
234 foreach (t; tg) { | |
235 try { | |
236 t.join (true); | |
237 } catch (Exception e) { | |
238 // Relying on catching exceptions thrown by other threads is a bad idea. | |
239 // Hence all threads should catch their own exceptions and return safely. | |
240 | |
241 logger.fatal ("Unhandled exception from Init function:"); | |
242 logger.fatal (e.msg); | |
243 | |
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
|
244 setInitFailure (); // abort (but join other threads first) |
20 | 245 } |
246 } | |
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
|
247 |
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
|
248 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 249 return false; // Done successfully |
250 } | |
251 } | |
252 //END runStage... | |
253 | |
254 debug (mdeUnitTest) unittest { | |
255 /* Fake init and cleanup. Use unittest-specific init and cleanup InitStages to avoid | |
256 * messing other init/cleanup up. */ | |
257 static InitStage initUT, cleanupUT; | |
258 | |
259 static bool initialised = false; | |
260 static void cleanupFunc1 () { | |
261 initialised = false; | |
262 } | |
263 static void cleanupFunc2 () { | |
264 assert (initialised == true); | |
265 } | |
266 | |
267 static void initFunc () { | |
268 initialised = true; | |
269 cleanupUT.addFunc (&cleanupFunc1); | |
270 cleanupUT.addFunc (&cleanupFunc2); | |
271 } | |
272 | |
273 initUT.addFunc (&initFunc); | |
274 | |
275 runStageForward (initUT); | |
276 assert (initialised); | |
277 | |
278 runStageReverse (cleanupUT); | |
279 assert (!initialised); | |
280 | |
281 logger.info ("Unittest complete."); | |
282 } | |
283 } |