-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuc_coroutine_pool.h
More file actions
127 lines (104 loc) · 3.07 KB
/
uc_coroutine_pool.h
File metadata and controls
127 lines (104 loc) · 3.07 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* =====================================================================================
*
* Filename: uc_coroutine_pool.h
*
* Description:
*
* Version: 1.0
* Created: 03/23/2017 07:29:16 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include "uc_coroutine.h"
#include <list>
#include <unordered_map>
class UCCoroutinePool
{
public:
typedef std::function<void(void)> task_t;
struct TaskCoro
{
task_t task;
UCCoroutine * coro;
};
public:
UCCoroutinePool(uint32_t max_coro = 1024, uint32_t max_free_coro = 512,
uint32_t stack_size = 102400, bool is_shared_model = false) :
m_max_coro(max_coro), m_max_free_coro(max_free_coro), m_stack_size(stack_size)
{
if(is_shared_model)
{
m_shared_buf_ptr = new char[m_stack_size];
}
for(uint32_t i = 0; i < max_free_coro; ++i)
{
UCCoroutine * coro = NULL;
if(!is_shared_model)
coro = new UCCoroutine(
std::bind(&UCCoroutinePool::Execute, this, std::placeholders::_1),
m_stack_size
);
else
coro = new UCCoroutine(
std::bind(&UCCoroutinePool::Execute, this, std::placeholders::_1),
m_shared_buf_ptr, m_stack_size
);
m_free_coro_list.push_back(coro);
}
}
uint32_t GetTaskID()
{
static uint32_t task_id = 0;
return ++task_id == 0 ? ++task_id : task_id;
}
void Execute(uint32_t task_id)
{
printf("%s:%u\n", __FUNCTION__, task_id);
if(!m_coro_map.count(task_id))
{
return;
}
m_coro_map[task_id].task();
m_free_coro_list.push_back(m_coro_map[task_id].coro);
}
UCCoroutine * Post(task_t cb)
{
printf("\n%s:\n", __FUNCTION__);
UCCoroutine * coro = NULL;
if(!m_free_coro_list.empty())
{
coro = m_free_coro_list.back();
m_free_coro_list.pop_back();
coro->Init();
}
else
{
if(m_coro_map.size() >= m_max_coro)
{
printf("coroutine num is up to %lu >= m_max_coro:%u\n", m_coro_map.size(), m_max_coro);
return NULL;
}
coro = new UCCoroutine(
std::bind(&UCCoroutinePool::Execute, this, std::placeholders::_1),
m_stack_size);
coro->Init();
}
uint32_t task_id = GetTaskID();
m_coro_map[task_id] = {cb, coro};
coro->SetTaskID(task_id);
return coro;
}
private:
uint32_t m_max_coro;
uint32_t m_max_free_coro;
uint32_t m_stack_size;
char * m_shared_buf_ptr;
std::list<UCCoroutine *> m_free_coro_list;
std::unordered_map<uint32_t, TaskCoro> m_coro_map;
};