diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/Local.h | 6 | ||||
-rw-r--r-- | includes/Main.h | 25 | ||||
-rw-r--r-- | includes/Task.h | 44 | ||||
-rw-r--r-- | includes/TaskMan.h | 17 |
4 files changed, 67 insertions, 25 deletions
diff --git a/includes/Local.h b/includes/Local.h index e3d08a4..79862a6 100644 --- a/includes/Local.h +++ b/includes/Local.h @@ -11,7 +11,7 @@ class TLSManager { void * createTLS(); }; -extern TLSManager * tlsManager; +extern TLSManager * g_tlsManager; class Local : public AtStart { public: @@ -27,8 +27,8 @@ class Local : public AtStart { int getIndex() { return m_idx; } private: static void * create() { void * r = malloc(s_size * sizeof(void *)); return r; } - static void * getTLS() { return tlsManager->getTLS(); } - static void * setTLS(void * val) { return tlsManager->setTLS(val); } + static void * getTLS() { return g_tlsManager->getTLS(); } + static void * setTLS(void * val) { return g_tlsManager->setTLS(val); } virtual void doStart(); int m_idx; static int s_size; diff --git a/includes/Main.h b/includes/Main.h index eee7ff7..44764a3 100644 --- a/includes/Main.h +++ b/includes/Main.h @@ -37,9 +37,17 @@ class Exit : public GeneralException { }; #include <Printer.h> +#include <Task.h> +#include <TaskMan.h> namespace Balau { +class MainTask : public Task { + public: + virtual const char * getName() { return "Main Task"; } + virtual void Do(); +}; + class Main { public: enum Status { @@ -49,11 +57,10 @@ class Main { STOPPING, STOPPED, }; - Main() : m_status(UNKNOWN) { application = this; } - virtual int startup() throw (GeneralException) = 0; - static Status status() { return application->m_status; } + Main() : m_status(UNKNOWN) { Assert(s_application == 0); s_application = this; } + static Status status() { return s_application->m_status; } int bootstrap(int _argc, char ** _argv) { - int r; + int r = 0; m_status = STARTING; argc = _argc; @@ -65,7 +72,8 @@ class Main { try { m_status = RUNNING; - r = startup(); + MainTask * mainTask = new MainTask(); + TaskMan::getTaskMan()->mainLoop(); m_status = STOPPING; } catch (Exit e) { @@ -96,7 +104,7 @@ class Main { char ** enve; private: Status m_status; - static Main * application; + static Main * s_application; }; #define BALAU_STARTUP \ @@ -106,12 +114,11 @@ class Application : public Balau::Main { \ virtual int startup() throw (Balau::GeneralException); \ }; \ \ -static Application application; \ -\ extern "C" { \ int main(int argc, char ** argv) { \ setlocale(LC_ALL, ""); \ - return application.bootstrap(argc, argv); \ + Balau::Main mainClass; \ + return mainClass.bootstrap(argc, argv); \ } \ } diff --git a/includes/Task.h b/includes/Task.h index fb210a2..c2777fe 100644 --- a/includes/Task.h +++ b/includes/Task.h @@ -2,10 +2,37 @@ #include <stdlib.h> #include <coro.h> +#include <Exceptions.h> +#include <vector> namespace Balau { class TaskMan; +class Task; + +namespace Events { + +class BaseEvent { + public: + BaseEvent() : m_signal(false), m_task(NULL) { } + bool gotSignal() { return m_signal; } + void doSignal() { m_signal = true; } + Task * taskWaiting() { Assert(m_task); return m_task; } + void registerOwner(Task * task) { Assert(m_task == NULL); m_task = task; } + private: + bool m_signal; + Task * m_task; +}; + +class TaskEvent : public BaseEvent { + public: + TaskEvent(Task * taskWaited); + Task * taskWaited() { return m_taskWaited; } + private: + Task * m_taskWaited; +}; + +}; class Task { public: @@ -19,20 +46,25 @@ class Task { Task(); virtual ~Task(); virtual const char * getName() = 0; - Status getStatus() { return status; } + Status getStatus() { return m_status; } + static Task * getCurrentTask(); protected: void suspend(); virtual void Do() = 0; + void waitFor(Events::BaseEvent * event); private: size_t stackSize() { return 128 * 1024; } void switchTo(); static void coroutine(void *); - void * stack; - coro_context ctx; - TaskMan * taskMan; - Status status; - void * tls; + void * m_stack; + coro_context m_ctx; + TaskMan * m_taskMan; + Status m_status; + void * m_tls; friend class TaskMan; + friend class Events::TaskEvent; + typedef std::vector<Events::TaskEvent *> waitedByList_t; + waitedByList_t m_waitedBy; }; }; diff --git a/includes/TaskMan.h b/includes/TaskMan.h index ac95f71..585fb7f 100644 --- a/includes/TaskMan.h +++ b/includes/TaskMan.h @@ -1,8 +1,9 @@ #pragma once +#include <stdint.h> #include <coro.h> #include <ext/hash_set> -#include <stdint.h> +#include <vector> namespace gnu = __gnu_cxx; @@ -15,18 +16,20 @@ class TaskMan { TaskMan(); ~TaskMan(); void mainLoop(); - void stop() { stopped = true; } + void stop() { m_stopped = true; } static TaskMan * getTaskMan(); private: void registerTask(Task * t); void unregisterTask(Task * t); - coro_context returnContext; + coro_context m_returnContext; friend class Task; - struct taskHash { size_t operator()(const Task * t) const { return reinterpret_cast<uintptr_t>(t); } }; - typedef gnu::hash_set<Task *, taskHash> taskList; - taskList tasks, pendingAdd; - volatile bool stopped; + struct taskHasher { size_t operator()(const Task * t) const { return reinterpret_cast<uintptr_t>(t); } }; + typedef gnu::hash_set<Task *, taskHasher> taskHash_t; + typedef std::vector<Task *> taskList_t; + taskHash_t m_tasks; + taskList_t m_pendingAdd; + volatile bool m_stopped; }; }; |