PageRenderTime 29ms CodeModel.GetById 11ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/core/main.d

http://github.com/wilkie/djehuty
D | 138 lines | 91 code | 29 blank | 18 comment | 15 complexity | a3ba45225b4357ada2eee7bcf48719fa MD5 | raw file
  1/*
  2 * main.d
  3 *
  4 * This file contains the main logic and the main class for handling
  5 * an Application.
  6 *
  7 * Author: Dave Wilkinson
  8 *
  9 */
 10
 11module core.main;
 12
 13import core.string;
 14import core.arguments;
 15import core.application;
 16import core.locale;
 17import core.system;
 18import core.color;
 19
 20import io.console;
 21
 22import synch.semaphore;
 23import synch.thread;
 24
 25import gui.apploop;
 26
 27// Section: Core
 28
 29// Description: This class is the main class for the framework. It provides base functionality.
 30class Djehuty {
 31static:
 32public:
 33
 34	void application(Application application) {
 35		if (app !is null) {
 36			throw new Exception("Application Already Spawned");
 37		}
 38
 39		app = application;
 40	}
 41
 42	Application application() {
 43		return app;
 44	}
 45
 46package:
 47
 48	void start() {
 49		// Get default locale
 50		Locale.id = System.Locale.id;
 51		
 52		// Allow console output
 53		Console.unlock();
 54
 55		// Can only start the framework once
 56		if (!_hasStarted) {
 57			_hasStarted = true;
 58		}
 59		else {
 60			throw new Exception("Framework Already Started");
 61		}
 62
 63		// Constitute the main thread class
 64		ThreadModuleInit();
 65
 66		// Check to make sure the app provided a suitable class to use
 67		if (app is null) {
 68			throw new Exception("No Application Class");
 69		}
 70		else {
 71			app.onPreApplicationStart();
 72			app.onApplicationStart();
 73		}
 74	}
 75
 76	void end(uint code = 0) {
 77		// Tell all running threads that they should end to allow shutdown to commense
 78		if (_threads !is null) {
 79			_threadRegisterSemaphore.down();
 80
 81			foreach(th; _threads) {
 82				th.pleaseStop();
 83			}
 84
 85			_threadRegisterSemaphore.up();
 86		}
 87
 88		// Reset colors to something sane
 89		Console.forecolor = Color.White;
 90		Console.backcolor = Color.Black;
 91		if (app !is null) {
 92			app.onApplicationEnd();
 93			app.onPostApplicationEnd(code);
 94		}
 95	}
 96
 97	bool _hasStarted = false;
 98
 99	Thread[] _threads;
100
101	Semaphore _threadRegisterSemaphore;
102
103	Application app;
104}
105
106void RegisterThread(ref Thread thread) {
107	if (Djehuty._threadRegisterSemaphore is null) {
108		Djehuty._threadRegisterSemaphore = new Semaphore(1);
109	}
110
111	Djehuty._threadRegisterSemaphore.down();
112	Djehuty._threads ~= thread;
113	Djehuty._threadRegisterSemaphore.up();
114}
115
116void UnregisterThread(ref Thread thread)
117{
118	Djehuty._threadRegisterSemaphore.down();
119
120	if (Djehuty._threads !is null) {
121		foreach(i, th; Djehuty._threads) {
122			if (th is thread) {
123				if (Djehuty._threads.length == 1) {
124					Djehuty._threads = null;
125				}
126				else if (i >= Djehuty._threads.length - 1) {
127					Djehuty._threads = Djehuty._threads[0..i];
128				}
129				else {
130					Djehuty._threads = Djehuty._threads[0..i] ~ Djehuty._threads[i+1..$];
131				}
132				break;
133			}
134		}
135	}
136
137	Djehuty._threadRegisterSemaphore.up();
138}