+#include "qui/threading/ThreadManager.hpp"
+#include "qui/threading/InterpreterThread.hpp"
+#include <QMessageBox>
+#include <QMutex>
+#include <QThreadStorage>
+
+ThreadManager& ThreadManagerFactory() {
+ static ThreadManager *mgr;
+ if (!mgr) {
+ mgr = new ThreadManager;
+ }
+ return *mgr;
+}
+
+ThreadManager::ThreadManager()
+{
+ // initialize thread variable
+ get_gui_proxy();
+}
+
+const InterpreterThread *ThreadManager::create_interpreter() {
+
+ static QMutex interpreter_lock;
+ static InterpreterThread *interpreter_thread;
+ if (interpreter_lock.tryLock()) {
+ // we're the first thread
+ interpreter_thread = new InterpreterThread();
+ interpreter_thread->start();
+ }
+ return interpreter_thread;
+ // someone already started a copy of the interpreter
+}
+
+GuiProxy *ThreadManager::get_gui_proxy()
+{
+ static GuiProxy *master;
+ static QThreadStorage <GuiProxy *> storage;
+ if (!master) {
+ // we don't have a master object so we probably should make one
+ assert (storage.hasLocalData() == false);
+ master = new GuiProxy;
+ if (!master) {
+ QMessageBox::critical(0, "Memory error", "Out of memory");
+ }
+ storage.setLocalData(master);
+ return master;
+ } else if (storage.hasLocalData()) {
+ // we've been initialized properly
+ return storage.localData();
+ } else {
+ // we have a master, but not a local proxy,
+ GuiProxy *client = new GuiProxy(master);
+ if (!client) {
+ QMessageBox::critical(0, "Memory error", "Out of memory");
+ }
+ storage.setLocalData(client);
+ return client;
+ }
+}
\ No newline at end of file