diff options
author | Pixel <pixel@nobis-crew.org> | 2011-10-07 09:27:04 -0700 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2011-10-07 09:28:53 -0700 |
commit | 5a283e5b2b523d53e3504292c386b534dc74386a (patch) | |
tree | 779a2f275fd68db81e5927c6b2c190782e6f11f8 /src | |
parent | ad4fddc454407fe154e87564472332e5c421452a (diff) |
Preliminary work for the Task manager.
Added libcoro submodule.
Also refactored some pieces of code.
Diffstat (limited to 'src')
-rw-r--r-- | src/Local.cc | 4 | ||||
-rw-r--r-- | src/Printer.cc | 29 | ||||
-rw-r--r-- | src/Task.cc | 46 | ||||
-rw-r--r-- | src/TaskMan.cc | 21 |
4 files changed, 76 insertions, 24 deletions
diff --git a/src/Local.cc b/src/Local.cc index 1262179..c354db9 100644 --- a/src/Local.cc +++ b/src/Local.cc @@ -13,6 +13,10 @@ void * Balau::TLSManager::setTLS(void * val) { return r; } +void * Balau::TLSManager::createTLS() { + return Local::create(); +} + static Balau::TLSManager dummyTLSManager; Balau::TLSManager * Balau::tlsManager = &dummyTLSManager; diff --git a/src/Printer.cc b/src/Printer.cc index fda9952..cb94084 100644 --- a/src/Printer.cc +++ b/src/Printer.cc @@ -2,14 +2,8 @@ #include "Main.h" #include "Local.h" -class PrinterLocal : public Balau::Local { - public: - PrinterLocal() { } - Balau::Printer * getGlobal() { return reinterpret_cast<Balau::Printer *>(Local::getGlobal()); } - Balau::Printer * get() { return reinterpret_cast<Balau::Printer *>(Local::get()); } - void setGlobal(Balau::Printer * printer) { Local::setGlobal(printer); } - void set(Balau::Printer * printer) { Local::set(printer); } -} printerLocal; +static Balau::DefaultTmpl<Balau::Printer> defaultPrinter(10); +static Balau::LocalTmpl<Balau::Printer> localPrinter; static const char * prefixes[] = { "(DD) ", @@ -21,11 +15,11 @@ static const char * prefixes[] = { }; Balau::Printer::Printer() : m_verbosity(M_STATUS | M_WARNING | M_ERROR) { - if (!printerLocal.getGlobal()) - printerLocal.setGlobal(this); + if (!localPrinter.getGlobal()) + localPrinter.setGlobal(this); } -Balau::Printer * Balau::Printer::getPrinter() { return printerLocal.get(); } +Balau::Printer * Balau::Printer::getPrinter() { return localPrinter.get(); } void Balau::Printer::_log(uint32_t level, const char * fmt, va_list ap) { if (!(level & m_verbosity)) @@ -45,16 +39,3 @@ void Balau::Printer::_log(uint32_t level, const char * fmt, va_list ap) { void Balau::Printer::_print(const char * fmt, va_list ap) { vfprintf(stderr, fmt, ap); } - -class DefaultPrinter : public Balau::AtStart { - public: - DefaultPrinter() : AtStart(10) { } - protected: - virtual void doStart(); -}; - -static DefaultPrinter defaultPrinter; - -void DefaultPrinter::doStart() { - new Balau::Printer(); -} diff --git a/src/Task.cc b/src/Task.cc new file mode 100644 index 0000000..d41f088 --- /dev/null +++ b/src/Task.cc @@ -0,0 +1,46 @@ +#include "Task.h" +#include "TaskMan.h" +#include "Exceptions.h" +#include "Printer.h" +#include "Local.h" + +Balau::Task::Task() { + size_t size = stackSize(); + stack = malloc(size); + coro_create(&ctx, coroutine, this, stack, size); + taskMan = TaskMan::getTaskMan(); + tls = tlsManager->createTLS(); + status = STARTING; +} + +void Balau::Task::coroutine(void * arg) { + Task * task = reinterpret_cast<Task *>(arg); + Assert(task); + try { + task->status = RUNNING; + task->Do(); + task->status = STOPPED; + } + catch (GeneralException & e) { + Printer::log(M_WARNING, "Task %s caused an exception: `%s' - stopping.", task->getName(), e.getMsg()); + task->status = FAULTED; + } + catch (...) { + Printer::log(M_WARNING, "Task %s caused an unknown exception - stopping.", task->getName()); + task->status = FAULTED; + } + coro_transfer(&task->ctx, &task->taskMan->returnContext); +} + +void Balau::Task::switchTo() { + status = RUNNING; + void * oldTLS = tlsManager->getTLS(); + tlsManager->setTLS(tls); + coro_transfer(&taskMan->returnContext, &ctx); + tlsManager->setTLS(oldTLS); + status = IDLE; +} + +void Balau::Task::suspend() { + coro_transfer(&ctx, &taskMan->returnContext); +} diff --git a/src/TaskMan.cc b/src/TaskMan.cc new file mode 100644 index 0000000..783c683 --- /dev/null +++ b/src/TaskMan.cc @@ -0,0 +1,21 @@ +#include "TaskMan.h" +#include "Main.h" +#include "Local.h" + +static Balau::DefaultTmpl<Balau::TaskMan> defaultTaskMan(50); +static Balau::LocalTmpl<Balau::TaskMan> localTaskMan; + +Balau::TaskMan::TaskMan() { + coro_create(&returnContext, 0, 0, 0, 0); + if (!localTaskMan.getGlobal()) + localTaskMan.setGlobal(this); +} + +Balau::TaskMan * Balau::TaskMan::getTaskMan() { return localTaskMan.get(); } + +Balau::TaskMan::~TaskMan() { + Assert(localTaskMan.getGlobal() != this); +} + +void Balau::TaskMan::mainLoop() { +} |