Mercurial > projects > mde
annotate mde/setup/Init.d @ 85:56c0ddd90193
Intermediate commit (not stable). Changes to init system.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Thu, 11 Sep 2008 11:33:51 +0100 |
parents | 3dfd934100f7 |
children | 79d816b3e2d2 |
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 /************************************************************************************************** | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
17 * Initialisation and cleanup (shutdown) module. |
20 | 18 * |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
19 * Program startup follows this sequence: static this() functions, pre-init, init. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
20 * Shutdown consists of: cleanup, post-cleanup, static ~this() functions. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
21 * |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
22 * static this and ~this functions should not use any external resources, such as dynamically |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
23 * loaded libraries or files, and should not interact with other modules. They should be almost |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
24 * guaranteed not to fail. Preferably, they shouldn't involve large amounts of memory or |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
25 * processing, but this is not a requirement. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
26 * |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
27 * Pre-init: init code written in this module. Generally only prerequisets of most other stages |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
28 * go here. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
29 * |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
30 * Init: This is where init code from external modules gets hooked in. Each stage consists of an |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
31 * initialization function, a corresponding cleanup function, a state, and any dependencies (other |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
32 * init functions which must be run before this one). Init functions are run according to these |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
33 * dependencies, potentially threaded simultaeneously with other init functions. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
34 * |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
35 * Cleanup: Cleanup happens similarly to init for all stages requiring shutdown (according to their |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
36 * state). The init dependencies are applied in reverse order (so if X depended on Y, Y's cleanup |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
37 * will not run until X's cleanup has completed), and again the functions may be threaded. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
38 * |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
39 * Post-cleanup: like pre-init, this is code written in Init. |
20 | 40 *************************************************************************************************/ |
63
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
56
diff
changeset
|
41 module mde.setup.Init; |
20 | 42 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
43 import mde.setup.InitStage; // Controls external delegates run by init |
63
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
56
diff
changeset
|
44 import mde.setup.exception; |
20 | 45 |
63
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
56
diff
changeset
|
46 import mde.lookup.Options; |
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
56
diff
changeset
|
47 import paths = mde.setup.paths; |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
48 import mde.exception; // optionsLoadException |
20 | 49 |
50 // tango imports | |
51 import tango.core.Thread; | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
52 import tango.core.sync.Condition; |
20 | 53 import tango.core.Exception; |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
54 import tango.util.container.LinkedList; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
55 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
56 //import tango.stdc.stringz : fromStringz; |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
57 import tango.io.Console; // for printing command-line usage |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
58 import TimeStamp = tango.text.convert.TimeStamp, tango.time.WallClock; // output date in log file |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
59 import tango.util.Arguments; |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
60 import tango.util.log.Log; |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
61 import tango.util.log.AppendConsole; |
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
62 import tango.util.log.AppendFiles; |
20 | 63 |
29
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
64 // Derelict imports |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
65 import derelict.opengl.gl; |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
66 import derelict.sdl.sdl; |
56
f9f5e04f20b2
Bit of polish for a public release, including some more documentation.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
55
diff
changeset
|
67 //import derelict.sdl.image; Was to be used... for now isn't |
30
467c74d4804d
Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
29
diff
changeset
|
68 import derelict.freetype.ft; |
29
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
69 import derelict.util.exception; |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
70 |
20 | 71 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
72 /************************************************************************************************** |
20 | 73 * Init class |
74 * | |
75 * A scope class created at beginning of the program and destroyed at the end; thus the CTOR | |
76 * handles program initialisation and the DTOR handles program cleanup. | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
77 *************************************************************************************************/ |
20 | 78 scope class Init |
79 { | |
80 static this() { | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
81 // Set up the logger temporarily (until pre-init): |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
82 Logger root = Log.root; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
83 debug root.level(Logger.Trace); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
84 else root.level(Logger.Info); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
85 root.add(new AppendConsole); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
86 |
63
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
56
diff
changeset
|
87 logger = Log.getLogger ("mde.setup.Init"); |
20 | 88 } |
89 | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
90 /** this() − pre-init and init */ |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
91 this(char[][] cmdArgs) |
20 | 92 { |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
93 /****************************************************************************************** |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
94 * Pre-init - init code written in this module. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
95 *****************************************************************************************/ |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
96 debug logger.trace ("Init: starting pre-init"); |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
97 //FIXME: warn on invalid arguments, including base-path on non-Windows |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
98 // But Arguments doesn't support this (in tango 0.99.6 and in r3563). |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
99 Arguments args; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
100 try { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
101 args = new Arguments(); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
102 args.define("base-path").parameters(1); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
103 args.define("data-path").parameters(1,-1); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
104 args.define("conf-path").parameters(1,-1); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
105 args.define("paths"); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
106 args.define("q").aliases(["quick-exit"]); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
107 args.define("help").aliases(["h"]); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
108 args.parse(cmdArgs); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
109 if (args.contains("help")) // lazy way to print help |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
110 throw new InitException ("Help requested"); // and stop |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
111 } catch (Exception e) { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
112 printUsage(cmdArgs[0]); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
113 throw new InitException ("Parsing arguments failed: "~e.msg); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
114 } |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
115 |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
116 // Find/create paths: |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
117 try { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
118 if (args.contains("data-path")) |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
119 paths.extraDataPath = args["data-path"]; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
120 if (args.contains("conf-path")) |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
121 paths.extraConfPath = args["conf-path"]; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
122 if (args.contains("base-path")) |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
123 paths.resolvePaths (args["base-path"]); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
124 else |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
125 paths.resolvePaths(); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
126 } catch (Exception e) { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
127 throw new InitException ("Resolving paths failed: " ~ e.msg); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
128 } |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
129 if (args.contains("paths")) { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
130 paths.mdeDirectory.printPaths; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
131 throw new InitException ("Paths requested"); // lazy way to stop |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
132 } |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
133 debug logger.trace ("Init: resolved paths successfully"); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
134 |
20 | 135 /* Load options now. Don't load in a thread since: |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
136 * Loading should be fast & most work is probably disk access |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
137 * It enables logging to be controlled by options |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
138 * It's a really good idea to let the options apply to all other loading */ |
20 | 139 try { |
140 Options.load(); | |
141 } catch (optionsLoadException e) { | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
142 throw new InitException ("Loading options failed: " ~ e.msg); |
20 | 143 } |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
144 debug logger.trace ("Init: loaded options successfully"); |
20 | 145 |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
146 // Set up the logger: |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
147 Logger root; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
148 try { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
149 enum LOG { |
55
f3d8c0441408
Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
150 LEVEL = 0xF, // mask for log level |
f3d8c0441408
Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
151 CONSOLE = 0x1000, // log to console? |
f3d8c0441408
Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
152 ROLLFILE= 0x2000 // use Rolling/Switching File Appender? |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
153 } |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
154 |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
155 // Where logging is done to is determined at compile-time, currently just via static ifs. |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
156 root = Log.root; |
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
157 root.clear; // we may no longer want to log to the console |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
158 |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
159 // Now re-set the logging level, using the value from the config file: |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
160 root.level (cast(Level) (miscOpts.logOptions & LOG.LEVEL), true); |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
161 |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
162 // Log to a file (first appender so root seperator messages don't show on console): |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
163 if (miscOpts.logOptions & LOG.ROLLFILE) { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
164 // Use 2 log files with a maximum size of 1 MB: |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
165 root.add (new AppendFiles (paths.logDir~"/log-.txt", 2, 1024*1024)); |
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
166 root.info (""); // some kind of separation between runs |
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
167 root.info (""); |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
168 } else if (!(miscOpts.logOptions & LOG.CONSOLE)) { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
169 // make sure at least one logger is enabled |
64
cc3763817b8a
Overhauled Options so that it now uses templates and mixins for type-specific internals, and supported types can be adjusted via just one list.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
63
diff
changeset
|
170 miscOpts.set!(int) ("logOptions", miscOpts.logOptions | LOG.CONSOLE); |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
171 } |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
172 if (miscOpts.logOptions & LOG.CONSOLE) { // Log to the console |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
173 root.add(new AppendConsole); |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
174 } |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
175 logger.info ("Starting mde [no version] on " ~ TimeStamp.toString(WallClock.now)); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
176 } catch (Exception e) { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
177 // Presumably it was only adding a file appender which failed; set up a new console |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
178 // logger and if that fails let the exception kill the program. |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
179 root.clear; |
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
64
diff
changeset
|
180 root.add (new AppendConsole); |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
181 logger.warn ("Exception while setting up the logger; logging to the console instead."); |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
182 } |
20 | 183 |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
184 // a debugging option: |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
185 imde.run = !args.contains("q") && !miscOpts.exitImmediately; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
186 debug logger.trace ("Init: applied pre-init options"); |
29
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
187 |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
188 //BEGIN Load dynamic libraries |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
189 /* Can be done by init functions but much neater to do here. |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
190 * Also means that init functions aren't run if a library fails to load. */ |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
191 try { |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
192 DerelictSDL.load(); |
56
f9f5e04f20b2
Bit of polish for a public release, including some more documentation.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
55
diff
changeset
|
193 // SDLImage was going to be used... for now it isn't because of gl texturing problems |
f9f5e04f20b2
Bit of polish for a public release, including some more documentation.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
55
diff
changeset
|
194 //DerelictSDLImage.load(); |
29
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
195 DerelictGL.load(); |
30
467c74d4804d
Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
29
diff
changeset
|
196 DerelictFT.load(); |
29
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
197 } catch (DerelictException de) { |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
198 logger.fatal ("Loading dynamic library failed:"); |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
199 logger.fatal (de.msg); |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
200 |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
201 throw new InitException ("Loading dynamic libraries failed (see above)."); |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
202 } |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
203 debug logger.trace ("Init: dynamic libraries loaded"); |
29
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
204 //END Load dynamic libraries |
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
28
diff
changeset
|
205 |
20 | 206 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
207 /****************************************************************************************** |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
208 * Init − where init code from external modules gets hooked in. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
209 *****************************************************************************************/ |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
210 debug logger.trace ("Init: done pre-init, starting init stages"); |
20 | 211 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
212 // Calculate reverse dependencies of stages: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
213 foreach (key,stage_p; stages) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
214 foreach (name; stage_p.depends) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
215 stages[name].rdepends ~= key; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
216 if (miscOpts.numThreads < 1 || miscOpts.numThreads > 64) // limit to a sensible number of threads |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
217 miscOpts.set!(int)("numThreads", 4); // FIXME enforce limit in Options |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
218 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
219 runStages!(true); // startup delegates |
20 | 220 |
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
|
221 debug logger.trace ("Init: done"); |
20 | 222 } |
223 | |
224 /** DTOR - runs cleanup functions. */ | |
225 ~this() | |
226 { | |
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
|
227 debug logger.trace ("Cleanup: starting"); |
20 | 228 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
229 runStages!(false); // cleanup delegates |
20 | 230 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
231 Options.save(); // save options... do so here for now |
20 | 232 |
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
|
233 debug logger.trace ("Cleanup: done"); |
20 | 234 } |
235 | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
236 // run init stages or cleanup if startup is false |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
237 private static void runStages(bool startup) () { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
238 LinkedList!(InitStage*) toRun = new LinkedList!(InitStage*); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
239 foreach (v; stages) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
240 // Filter only stages with the relevant delegate. It is not checked later that the |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
241 // delegate exists! |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
242 // If no init/cleanup function exists, implicit activation/deactivation can occur so |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
243 // that dependents/dependencies may activate/deactivate. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
244 static if (startup) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
245 if (v.state == StageState.INACTIVE) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
246 if ((*v).init) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
247 toRun.append (v); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
248 else |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
249 v.state = StageState.ACTIVE; |
20 | 250 } |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
251 } else { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
252 if (v.state == StageState.ACTIVE) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
253 if (v.cleanup) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
254 toRun.append (v); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
255 else |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
256 v.state = StageState.INACTIVE; |
20 | 257 } |
258 } | |
259 } | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
260 debug logger.trace ("Added {} init/cleanup stages", toRun.size); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
261 int numWorking = miscOpts.numThreads; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
262 bool abortInit = false; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
263 Mutex toRunM = new Mutex; // synchronization on toRun, numWorking |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
264 Condition toRunC = new Condition(toRunM); // used by threads waiting for remaining stages' dependencies to be met |
20 | 265 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
266 /* This is a threadable member function to run init stages. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
267 * Operation follows: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
268 * 1 Look for a stage to run: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
269 * if found: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
270 * notify waiting threads work may be available |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
271 * init stage |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
272 * goto 1 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
273 * if not found: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
274 * if any other threads are working, wait (dependencies may not be met yet) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
275 * if no other threads are working, notify waiting threads and terminate (finished) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
276 * When notified, threads start at 1. */ |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
277 void initThreadFct () { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
278 debug logger.trace ("initThreadFct: starting"); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
279 try { // created as a thread - must not throw exceptions |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
280 InitStage* stage; |
20 | 281 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
282 threadLoop: while (true) { // thread loops until a problem occurs or nothing else can be done |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
283 // Look for a job: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
284 synchronized (toRunM) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
285 --numWorking; // stopped working: looking/waiting for a job |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
286 if (abortInit) break threadLoop; // something went wrong in another thread |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
287 static if (startup) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
288 int num_rdepends = (stage is null) ? 0 : stage.rdepends.length; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
289 else |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
290 int num_rdepends = (stage is null) ? 0 : stage.depends.length; |
20 | 291 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
292 getStage: while (true) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
293 auto it = toRun.iterator; // asserts if toRun is empty |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
294 itStages: while (it.next (stage)) { // get next element of toRun |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
295 static if (startup) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
296 foreach (d; stage.depends) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
297 if (stages[d].state != StageState.ACTIVE) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
298 continue itStages; // dependency isn't met (yet) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
299 } else { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
300 foreach (d; stage.rdepends) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
301 if (stages[d].state != StageState.INACTIVE) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
302 continue itStages; // reverse dependency isn't unmet (yet) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
303 } |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
304 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
305 // All dependencies met |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
306 it.remove; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
307 break getStage; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
308 } |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
309 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
310 // No stage remaining with all dependencies met |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
311 if (!toRun.isEmpty && numWorking) // still some working so more dependencies may be met later |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
312 toRunC.wait; // wait until another thread finishes |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
313 else // no thread is working, so none of what's left is doable, or nothing's left |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
314 break threadLoop; |
20 | 315 } |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
316 ++numWorking; // got a job! |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
317 if (num_rdepends > 2) // how many stages depended on the last one run? |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
318 toRunC.notifyAll(); // tell all waiting threads there may be work now |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
319 else if (num_rdepends == 2) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
320 toRunC.notify(); // there's potentially work for this thread and one other |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
321 // else there won't be additional work so don't notify |
20 | 322 } |
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
|
323 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
324 // Do a job: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
325 try { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
326 // FIXME - old stage start&finish trace messages - we don't have a name! |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
327 debug logger.trace ("InitStage {}: starting", stage); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
328 static if (startup) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
329 stage.state = (*stage).init(); // init is a property of a pointer (oh no!) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
330 else |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
331 stage.state = stage.cleanup(); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
332 debug logger.trace ("InitStage {}: completed; state: {}", stage, stage.state); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
333 } catch (InitStageException e) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
334 debug logger.trace ("InitStage {}: failed", stage); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
335 stage.state = e.state; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
336 abortInit = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
337 break threadLoop; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
338 } catch (Exception e) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
339 debug logger.trace ("InitStage {}: failed", stage); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
340 abortInit = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
341 break threadLoop; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
342 } |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
343 } |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
344 } catch (Exception e) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
345 logger.fatal ("Exception in initThreadFct: "~e.msg); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
346 abortInit = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
347 } |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
348 toRunC.notifyAll(); // Most likely if we're exiting, we should make sure others aren't waiting. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
349 debug logger.trace ("initThreadFct: returning"); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
350 return; |
20 | 351 } |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
352 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
353 // Start miscOpts.NumThreads - 1 threads: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
354 try { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
355 ThreadGroup g = new ThreadGroup; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
356 for (int i = miscOpts.numThreads; i > 1; --i) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
357 g.create (&initThreadFct); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
358 initThreadFct(); // also run in current thread |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
359 g.joinAll (false); // don't rethrow exceptions - there SHOULD NOT be any |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
360 } catch (ThreadException e) { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
361 logger.error ("Exception while using threads: "~e.msg); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
362 logger.error ("Disabling threads and attempting to continue."); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
363 miscOpts.set!(int)("NumThreads", 1); // count includes current thread |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
364 initThreadFct(); // try with just this thread |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
365 } // any other exception will be caught in main() and abort program |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
366 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
367 if (abortInit) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
368 throw new InitException ("An init/cleanup function failed (see above message(s))"); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
369 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
370 foreach (stage; toRun) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
371 logger.warn ("InitStage {}: was not run due to unmet dependencies", stage); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
372 } |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
373 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
374 private static { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
375 Logger logger; |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
376 |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
377 void printUsage (char[] progName) { |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
378 Cout ("mde [no version]").newline; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
379 Cout ("Usage:").newline; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
380 Cout (progName ~ ` [options]`).newline; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
381 version(Windows) |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
382 Cout ( |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
383 ` --base-path path Use path as the base (install) path (Windows only). It |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
384 should contain the "data" directory.`).newline; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
385 Cout ( |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
386 ` --data-path path(s) Add path(s) as a potential location for data files. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
387 First path argument becomes the preffered location to |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
388 load data files from. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
389 --conf-path path(s) Add path(s) as a potential location for config files. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
390 Configuration in the first path given take highest |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
391 priority. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
392 --paths Print all paths found and exit. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
393 --quick-exit, -q Exit immediately, without entering main loop. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
394 --help, -h Print this message.`).newline; |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
45
diff
changeset
|
395 } |
20 | 396 } |
397 | |
398 debug (mdeUnitTest) unittest { | |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
399 logger.trace ("Starting unittest"); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
400 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
401 auto realInit = stages; // backup the real init stages |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
402 stages = new typeof(stages); // an empty test-bed |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
403 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
404 bool init1, init2, init3 = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
405 bool failed = false; // set if anything goes wrong |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
406 StageState s1InitReturns = StageState.ACTIVE; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
407 addInitStage ("stg1", delegate StageState() { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
408 init1 = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
409 return s1InitReturns; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
410 }, delegate StageState() { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
411 init1 = false; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
412 return StageState.INACTIVE; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
413 }); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
414 addInitStage ("stg2", delegate StageState() { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
415 if (!init1) failed = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
416 init2 = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
417 return StageState.ACTIVE; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
418 }, delegate StageState() { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
419 if (!init1) failed = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
420 init2 = false; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
421 return StageState.INACTIVE; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
422 }, ["stg1"]); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
423 InitStage s3; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
424 s3.init = delegate StageState() { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
425 throw new InitStageException (cast(StageState)7); // not a normal state, but good for a test |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
426 return StageState.ERROR; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
427 }; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
428 s3.cleanup = delegate StageState() { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
429 if (!init1) failed = true; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
430 init3 = false; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
431 return StageState.INACTIVE; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
432 }; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
433 s3.depends = [ toStageName("stg1") ]; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
434 s3.state = StageState.ACTIVE; // already active, so s3.init should not run (first time) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
435 addInitStage ("stg3", &s3); |
20 | 436 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
437 // Stuff normally done in Init.this(): |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
438 // Calculate reverse dependencies of stages: |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
439 foreach (key,stage_p; stages) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
440 foreach (name; stage_p.depends) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
441 stages[name].rdepends ~= key; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
442 if (miscOpts.numThreads < 1 || miscOpts.numThreads > 64) // limit to a sensible number of threads |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
443 miscOpts.set!(int)("numThreads", 4); // FIXME enforce limit in Options |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
444 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
445 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
446 // Run the above. |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
447 runStages!(true); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
448 assert (init1); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
449 assert (init2); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
450 assert (!failed); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
451 foreach (s; stages) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
452 assert (s.state == StageState.ACTIVE); |
20 | 453 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
454 runStages!(false); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
455 assert (!init1); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
456 assert (!init2); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
457 assert (!init3); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
458 assert (!failed); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
459 foreach (s; stages) |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
460 assert (s.state == StageState.INACTIVE); |
20 | 461 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
462 s1InitReturns = StageState.ERROR; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
463 // Run again. S2/S3 shouldn't run, S1 won't shut down |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
464 runStages!(true); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
465 assert (init1); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
466 assert (!init2); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
467 assert (!init3); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
468 assert (!failed); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
469 runStages!(false); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
470 assert (init1); // S1 cleanup won't run |
20 | 471 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
472 stages[toStageName("stg1")].state = StageState.INACTIVE; // hack it back so we can still test |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
473 s1InitReturns = StageState.ACTIVE; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
474 init1 = false; |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
475 try { |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
476 runStages!(true); |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
477 assert (false); // runStages should throw because s3.init runs now |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
478 } catch (Exception) {} |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
479 assert (init1); // s1.init should run first |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
480 assert (stages[toStageName("stg3")].state == cast(StageState)7); // set by the exception |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
481 |
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
77
diff
changeset
|
482 stages = realInit; // restore the real init stages |
20 | 483 logger.info ("Unittest complete."); |
484 } | |
485 } |