-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoroutine_mgr.h
71 lines (56 loc) · 1.24 KB
/
coroutine_mgr.h
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef __COROUTINE_MGR__
#define __COROUTINE_MGR__
#include <algorithm>
#include <map>
#include <vector>
#include <stdint.h>
#include <sys/time.h>
#include "ctx_swap.h"
static inline uint64_t coroutine_time_ms()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
struct CoTask;
void coroutine_sleep(CoTask *task, int ms);
bool coroutine_sleep2(int ms);
struct CoTaskTimerElem
{
CoTaskTimerElem() : mCallbackID(-1), mTimeOut(0) {}
CoTaskTimerElem(int callbackid, uint64_t timeout_ms) : mCallbackID(callbackid), mTimeOut(timeout_ms) {}
uint64_t GetTimeOut() const
{
return mTimeOut;
}
int mCallbackID;
uint64_t mTimeOut;
};
class CoroutineAsyncMap
{
public:
~CoroutineAsyncMap();
CoTask *Alloc();
void Free(CoTask *&task);
CoTask *Find(int callbackid);
int Tick();
static CoroutineAsyncMap &GetInstance()
{
static CoroutineAsyncMap ins;
return ins;
}
uint64_t GetCurMs()
{
return mCurMs;
}
int AddTimer(const CoTaskTimerElem &elem);
private:
CoroutineAsyncMap();
int GetCallBackID();
std::map<int32_t, CoTask *> mCoTaskMap;
int32_t mCallbackID;
uint64_t mCurMs;
std::vector<CoTaskTimerElem> mTaskTimerHeap;
uint32_t mTaskTimerCnt;
};
#endif