diff options
-rw-r--r-- | includes/Socket.h | 2 | ||||
-rw-r--r-- | includes/Task.h | 3 | ||||
-rw-r--r-- | includes/TaskMan.h | 9 | ||||
-rw-r--r-- | src/HttpServer.cc | 2 | ||||
-rw-r--r-- | src/LuaTask.cc | 2 | ||||
-rw-r--r-- | src/Main.cc | 2 | ||||
-rw-r--r-- | src/Task.cc | 9 | ||||
-rw-r--r-- | src/TaskMan.cc | 5 | ||||
-rw-r--r-- | tests/test-Http.cc | 6 | ||||
-rw-r--r-- | tests/test-Sockets.cc | 11 | ||||
-rw-r--r-- | tests/test-Tasks.cc | 4 |
11 files changed, 36 insertions, 19 deletions
diff --git a/includes/Socket.h b/includes/Socket.h index f63edf0..b113e43 100644 --- a/includes/Socket.h +++ b/includes/Socket.h @@ -81,7 +81,7 @@ class Listener : public ListenerBase { public: Listener(int port, const char * local = "", void * opaque = NULL) : ListenerBase(port, local, opaque) { } protected: - virtual void factory(IO<Socket> & io, void * opaque) { TaskMan::createTask(new Worker(io, opaque)); } + virtual void factory(IO<Socket> & io, void * opaque) { TaskMan::registerTask(new Worker(io, opaque)); } virtual void setName() { m_name = String(ClassName(this).c_str()) + " - " + m_listener->getName(); } }; diff --git a/includes/Task.h b/includes/Task.h index 343580b..3a02c55 100644 --- a/includes/Task.h +++ b/includes/Task.h @@ -73,11 +73,12 @@ class Timeout : public BaseEvent { class TaskEvent : public BaseEvent { public: - TaskEvent(Task * taskWaited); + TaskEvent(Task * taskWaited = NULL); virtual ~TaskEvent(); void ack(); void signal(); Task * taskWaited() { return m_taskWaited; } + void attachToTask(Task * taskWaited); void evt_cb(ev::async & w, int revents) { doSignal(); } protected: virtual void gotOwner(Task * task); diff --git a/includes/TaskMan.h b/includes/TaskMan.h index 68aaaf6..35e60ed 100644 --- a/includes/TaskMan.h +++ b/includes/TaskMan.h @@ -20,6 +20,7 @@ class TaskScheduler; namespace Events { class Async; +class TaskEvent; }; @@ -54,10 +55,12 @@ class TaskMan { } bool stopped() { return m_stopped; } template<class T> - static T * createTask(T * t, Task * stick = NULL) { TaskMan::registerTask(t, stick); return t; } + static T * registerTask(T * t, Task * stick = NULL) { TaskMan::iRegisterTask(t, stick, NULL); return t; } + template<class T> + static T * registerTask(T * t, Events::TaskEvent * event) { TaskMan::iRegisterTask(t, NULL, event); return t; } private: - static void registerTask(Task * t, Task * stick); + static void iRegisterTask(Task * t, Task * stick, Events::TaskEvent * event); void * getStack(); void freeStack(void * stack); void addToPending(Task * t); @@ -68,8 +71,6 @@ class TaskMan { #endif friend class Task; friend class TaskScheduler; - template<class T> - friend T * createTask(T * t, Task * stick = NULL); struct taskHasher { size_t operator()(const Task * t) const { return reinterpret_cast<uintptr_t>(t); } }; typedef gnu::hash_set<Task *, taskHasher> taskHash_t; taskHash_t m_tasks, m_signaledTasks; diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 44c794e..2e3f703 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -594,7 +594,7 @@ typedef Balau::Listener<Balau::HttpWorker> HttpListener; void Balau::HttpServer::start() { AAssert(!m_started, "Don't start an HttpServer twice"); - m_listenerPtr = TaskMan::createTask(new HttpListener(m_port, m_local.to_charp(), this)); + m_listenerPtr = TaskMan::registerTask(new HttpListener(m_port, m_local.to_charp(), this)); m_started = true; } diff --git a/src/LuaTask.cc b/src/LuaTask.cc index 2641682..687c81a 100644 --- a/src/LuaTask.cc +++ b/src/LuaTask.cc @@ -51,7 +51,7 @@ void Balau::LuaMainTask::Do() { delete cell; return; } - TaskMan::createTask(new LuaTask(L.thread(), cell), this); + TaskMan::registerTask(new LuaTask(L.thread(), cell), this); } } } diff --git a/src/Main.cc b/src/Main.cc index e1eae66..32b7cf8 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -60,7 +60,7 @@ int Balau::Main::bootstrap(int _argc, char ** _argv) { try { m_status = RUNNING; - TaskMan::createTask(new MainTask()); + TaskMan::registerTask(new MainTask()); r = TaskMan::getDefaultTaskMan()->mainLoop(); m_status = STOPPING; } diff --git a/src/Task.cc b/src/Task.cc index 0a213be..f4db057 100644 --- a/src/Task.cc +++ b/src/Task.cc @@ -149,7 +149,14 @@ void Balau::Events::BaseEvent::doSignal() { } } -Balau::Events::TaskEvent::TaskEvent(Task * taskWaited) : m_taskWaited(taskWaited), m_ack(false), m_distant(false) { +Balau::Events::TaskEvent::TaskEvent(Task * taskWaited) : m_taskWaited(NULL), m_ack(false), m_distant(false) { + if (taskWaited) + attachToTask(taskWaited); +} + +void Balau::Events::TaskEvent::attachToTask(Task * taskWaited) { + AAssert(!m_taskWaited, "You can't attach a TaskEvent twice."); + m_taskWaited = taskWaited; ScopeLock lock(m_taskWaited->m_eventLock); m_taskWaited->m_waitedBy.push_back(this); } diff --git a/src/TaskMan.cc b/src/TaskMan.cc index d91581e..1695d39 100644 --- a/src/TaskMan.cc +++ b/src/TaskMan.cc @@ -331,11 +331,14 @@ int Balau::TaskMan::mainLoop() { return m_stopCode; } -void Balau::TaskMan::registerTask(Balau::Task * t, Balau::Task * stick) { +void Balau::TaskMan::iRegisterTask(Balau::Task * t, Balau::Task * stick, Events::TaskEvent * event) { if (stick) { + IAssert(!event, "inconsistent"); TaskMan * tm = stick->getTaskMan(); tm->addToPending(t); } else { + if (event) + event->attachToTask(t); s_scheduler.registerTask(t); } } diff --git a/tests/test-Http.cc b/tests/test-Http.cc index bd0ef49..f54fba0 100644 --- a/tests/test-Http.cc +++ b/tests/test-Http.cc @@ -118,10 +118,10 @@ void MainTask::Do() { yield(); waitFor(&event); - Task * stopper = new Stopper; - Events::TaskEvent stopperEvent(stopper); + + Events::TaskEvent stopperEvent; + Task * stopper = TaskMan::registerTask(new Stopper, &stopperEvent); waitFor(&stopperEvent); - TaskMan::createTask(stopper); bool gotEvent = false, gotStopperEvent = false; int count = 0; diff --git a/tests/test-Sockets.cc b/tests/test-Sockets.cc index c576c97..4ef0a78 100644 --- a/tests/test-Sockets.cc +++ b/tests/test-Sockets.cc @@ -62,11 +62,16 @@ void MainTask::Do() { Printer::enable(M_ALL); Printer::log(M_STATUS, "Test::Sockets running."); - Events::TaskEvent evtSvr(listener = TaskMan::createTask(new Listener<Worker>(1234))); - Events::TaskEvent evtCln(TaskMan::createTask(new Client)); - Printer::log(M_STATUS, "Created %s", listener->getName()); + Events::TaskEvent evtSvr; + Events::TaskEvent evtCln; + + listener = TaskMan::registerTask(new Listener<Worker>(1234), &evtSvr); + TaskMan::registerTask(new Client, &evtCln); + waitFor(&evtSvr); waitFor(&evtCln); + + Printer::log(M_STATUS, "Created %s", listener->getName()); bool svrDone = false, clnDone = false; while (!svrDone || !clnDone) { yield(); diff --git a/tests/test-Tasks.cc b/tests/test-Tasks.cc index 60d1dd3..da5346f 100644 --- a/tests/test-Tasks.cc +++ b/tests/test-Tasks.cc @@ -30,8 +30,8 @@ void MainTask::Do() { customPrinter = new CustomPrinter(); Printer::log(M_STATUS, "Test::Tasks running."); - Task * testTask = TaskMan::createTask(new TestTask()); - Events::TaskEvent taskEvt(testTask); + Events::TaskEvent taskEvt; + Task * testTask = TaskMan::registerTask(new TestTask(), &taskEvt); waitFor(&taskEvt); TAssert(!taskEvt.gotSignal()); yield(); |