-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoroutine_pool.cpp
More file actions
151 lines (132 loc) · 3.1 KB
/
Copy pathcoroutine_pool.cpp
File metadata and controls
151 lines (132 loc) · 3.1 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stack>
#include <functional>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include "coroutine_pool.h"
#include "ctx_swap.h"
class CoWorker
{
public:
CoWorker(stroutine *co = NULL) : mpCo(co), mFunc(NULL), mExit(false) {}
stroutine *mpCo;
std::function<void(void)> mFunc;
typedef void(*funType)(void);
bool mExit;
};
class CoWorkerPool
{
public:
CoWorkerPool()
{
mMinStackSize = default_min_stack_size;
mStackSize = default_stack_size;
mFreeSize = default_stack_size - default_min_stack_size;
}
std::stack<CoWorker *> mWorksPool;
int mMinStackSize;
int mStackSize;
int mFreeSize;
};
static CoWorkerPool g_coworker_pool;
void InitCoWorkerPool(int min_stack_size, int stack_size)
{
static bool has_init = false;
if (has_init)
{
return;
}
has_init = true;
if (min_stack_size < default_min_stack_size)
{
min_stack_size = default_min_stack_size;
}
if (stack_size > default_stack_size)
{
stack_size = default_stack_size;
}
if (stack_size < min_stack_size)
{
stack_size = min_stack_size;
}
long pageSize = sysconf(_SC_PAGESIZE);
min_stack_size = (min_stack_size + pageSize - 1) / pageSize * pageSize;
stack_size = (stack_size + pageSize - 1) / pageSize * pageSize;
g_coworker_pool.mMinStackSize = min_stack_size;
g_coworker_pool.mStackSize = stack_size;
g_coworker_pool.mFreeSize = stack_size - min_stack_size;
}
static void freeRss(char *stack_sp)
{
long pageSize = sysconf(_SC_PAGESIZE);
char *tmp = stack_sp + g_coworker_pool.mFreeSize - pageSize;
void *pageStart = (void *)((long long)tmp & ~(pageSize - 1));
unsigned char vec[1] = {0};
int ret = mincore(pageStart, pageSize, vec);
if (ret != 0)
{
//log error
fprintf(stderr, "mincore fail ret = %d, errno = %d\n", ret, errno);
return;
}
if ((vec[0] & 0x1) == 0)
{
// return;
}
pageStart = (void *)((long long)stack_sp & ~(pageSize - 1));
ret = madvise(pageStart, g_coworker_pool.mFreeSize, MADV_DONTNEED);
if (ret != 0)
{
//log error
fprintf(stderr, "miadvice fail ret = %d, errno = %d\n", ret, errno);
return;
}
return;
}
static void *worker_main(void *arg)
{
CoWorker *worker = (CoWorker *)arg;
while(!worker->mExit)
{
if (worker->mFunc)
{
worker->mFunc();
}
worker->mFunc = NULL;
if (g_coworker_pool.mFreeSize > 0)
{
freeRss(worker->mpCo->ctx.ss_sp);
}
g_coworker_pool.mWorksPool.push(worker);
co_yield(worker->mpCo);
}
}
void ProcessByCoWorker(std::function<void(void)> func)
{
CoWorker *worker = NULL;
if (g_coworker_pool.mWorksPool.empty())
{
worker = new CoWorker();
stroutine *worker_routine = co_init(worker_main, worker, default_stack_size);
worker->mpCo = worker_routine;
/*
long pageSize = sysconf(_SC_PAGESIZE);
void* pageStart = (void *)((long long)worker->mpCo->ctx.ss_sp & ~(pageSize - 1));
int ret = madvise(pageStart, g_coworker_pool.mFreeSize, MADV_DONTNEED);
*/
}
else
{
worker = g_coworker_pool.mWorksPool.top();
g_coworker_pool.mWorksPool.pop();
}
if (NULL == worker)
{
//log error
fprintf(stderr, "%s %d get worker fail\n");
return;
}
worker->mFunc = func;
co_resume(worker->mpCo);
}