summaryrefslogtreecommitdiff
path: root/includes/TaskMan.h
blob: fd00172f9a3ea7e10602e44c5192e234458bb502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once

#include <stdint.h>
#include <coro.h>
#include <ev++.h>
#include <ext/hash_set>
#include <vector>
#include <Threads.h>

namespace gnu = __gnu_cxx;

namespace Balau {

class Task;

class TaskMan {
  public:
      TaskMan();
      ~TaskMan();
    void mainLoop();
    void stop() { m_stopped = true; }
    static TaskMan * getTaskMan();
    struct ev_loop * getLoop() { return m_loop; }
    void signalTask(Task * t);

  private:
    void registerTask(Task * t);
    void unregisterTask(Task * t);
    coro_context m_returnContext;
    friend class Task;
    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, m_signaledTasks;
    taskList_t m_pendingAdd;
    Lock m_pendingLock;
    volatile bool m_stopped;
    struct ev_loop * m_loop;
};

};