#pragma once #include #ifndef _WIN32 #include #endif #include #include #include #include #include namespace gnu = __gnu_cxx; namespace Balau { class Task; class TaskScheduler; namespace Events { class Async; }; class TaskMan { public: TaskMan(); ~TaskMan(); void mainLoop(); static TaskMan * getDefaultTaskMan(); struct ev_loop * getLoop() { return m_loop; } void signalTask(Task * t); static void stop(); void stopMe() { m_stopped = true; } private: static void registerTask(Task * t, Task * stick); void addToPending(Task * t); #ifndef _WIN32 coro_context m_returnContext; #else void * m_fiber; #endif friend class Task; friend class TaskScheduler; template friend T * createTask(T * t, Task * stick = NULL); struct taskHasher { size_t operator()(const Task * t) const { return reinterpret_cast(t); } }; typedef gnu::hash_set taskHash_t; taskHash_t m_tasks, m_signaledTasks; Queue m_pendingAdd; bool m_stopped; struct ev_loop * m_loop; bool m_allowedToSignal; ev::async m_evt; }; template T * createTask(T * t, Task * stick = NULL) { TaskMan::registerTask(t, stick); Assert(dynamic_cast(t)); return t; } };