From 1d9def9edae0011eeee8f83dc32fb3797ca2f46b Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 7 Oct 2011 15:36:12 -0700 Subject: More work in the Task manager. Also fixing a few bugs linked with the printer and TLS. Removed flto from compilation flags: this actually creates bad code (!) --- includes/Local.h | 4 ++-- includes/Printer.h | 8 +++++--- includes/Task.h | 4 ++-- includes/TaskMan.h | 13 ++++++++++++- 4 files changed, 21 insertions(+), 8 deletions(-) (limited to 'includes') diff --git a/includes/Local.h b/includes/Local.h index a3405ca..e3d08a4 100644 --- a/includes/Local.h +++ b/includes/Local.h @@ -23,10 +23,10 @@ class Local : public AtStart { void * get() { if (getTLS()) { void * l = getLocal(); return l ? l : getGlobal(); } else return getGlobal(); } void setGlobal(void * obj) { m_globals[m_idx] = obj; } void setLocal(void * obj) { void * r = getTLS(); reinterpret_cast(r)[m_idx] = obj; } - void set(void * obj) { void * r = getTLS(); if (r) setGlobal(obj); else setLocal(obj); } + void set(void * obj) { void * r = getTLS(); if (r) setLocal(obj); else setGlobal(obj); } int getIndex() { return m_idx; } private: - static void * create() { void * r = malloc(s_size * sizeof(void *)); setTLS(r); return r; } + 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); } virtual void doStart(); diff --git a/includes/Printer.h b/includes/Printer.h index b55df22..20b3b6c 100644 --- a/includes/Printer.h +++ b/includes/Printer.h @@ -1,5 +1,6 @@ #pragma once +#include #include namespace Balau { @@ -21,14 +22,15 @@ class Printer { virtual void _print(const char * fmt, va_list ap); private: + void _print(const char * fmt, ...); void _log(uint32_t level, const char * fmt, va_list ap); - static Printer * getPrinter(); public: Printer(); void setLocal(); + static Printer * getPrinter(); static void log(uint32_t level, const String & fmt, ...) { va_list ap; va_start(ap, fmt); log(level, fmt.to_charp(), ap); va_end(ap); } static void log(uint32_t level, const char * fmt, ...) { va_list ap; va_start(ap, fmt); log(level, fmt, ap); va_end(ap); } static void log(uint32_t level, const char * fmt, va_list ap) { getPrinter()->_log(level, fmt, ap); } @@ -36,8 +38,8 @@ class Printer { static void print(const char * fmt, ...) { va_list ap; va_start(ap, fmt); print(fmt, ap); va_end(ap); } static void print(const char * fmt, va_list ap) { getPrinter()->_print(fmt, ap); } - void enable(uint32_t levels = M_ALL) { m_verbosity |= levels; } - void disable(uint32_t levels = M_ALL) { m_verbosity &= ~levels; } + static void enable(uint32_t levels = M_ALL) { getPrinter()->m_verbosity |= levels; } + static void disable(uint32_t levels = M_ALL) { getPrinter()->m_verbosity &= ~levels; } uint32_t m_verbosity; }; diff --git a/includes/Task.h b/includes/Task.h index 314532b..fb210a2 100644 --- a/includes/Task.h +++ b/includes/Task.h @@ -17,12 +17,12 @@ class Task { FAULTED, }; Task(); - ~Task() { free(stack); free(tls); } - virtual void Do() = 0; + virtual ~Task(); virtual const char * getName() = 0; Status getStatus() { return status; } protected: void suspend(); + virtual void Do() = 0; private: size_t stackSize() { return 128 * 1024; } void switchTo(); diff --git a/includes/TaskMan.h b/includes/TaskMan.h index 05e5fa8..ac95f71 100644 --- a/includes/TaskMan.h +++ b/includes/TaskMan.h @@ -1,6 +1,10 @@ #pragma once #include +#include +#include + +namespace gnu = __gnu_cxx; namespace Balau { @@ -11,11 +15,18 @@ class TaskMan { TaskMan(); ~TaskMan(); void mainLoop(); + void stop() { stopped = true; } + static TaskMan * getTaskMan(); private: - static TaskMan * getTaskMan(); + void registerTask(Task * t); + void unregisterTask(Task * t); coro_context returnContext; friend class Task; + struct taskHash { size_t operator()(const Task * t) const { return reinterpret_cast(t); } }; + typedef gnu::hash_set taskList; + taskList tasks, pendingAdd; + volatile bool stopped; }; }; -- cgit v1.2.3