-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuc_coroutine.h
More file actions
186 lines (141 loc) · 4.6 KB
/
uc_coroutine.h
File metadata and controls
186 lines (141 loc) · 4.6 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* =====================================================================================
*
* Filename: uc_coroutine.h
*
* Description:
*
* Version: 1.0
* Created: 2017年01月10日 18时11分34秒
* Revision: none
* Compiler: gcc
*
* Author: anonymalias (anonym_alias@163.com),
* Organization:
*
* =====================================================================================
*/
#include <ucontext.h>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <functional>
class UCCoroutine
{
public:
typedef std::function<void(uint32_t)> callback_t;
friend class UCCoroutinePool;
public:
UCCoroutine(callback_t callback, uint32_t _stack_size = 102400) :
cb(callback), private_stack_size(_stack_size), private_stack_sp(NULL), private_buf_size(0),
share_stack_sp(NULL), share_stack_size(0)
{//private stack model
getcontext(&m_ctx);
private_stack_sp = new char[private_stack_size];
m_ctx.uc_stack.ss_sp = private_stack_sp;
m_ctx.uc_stack.ss_size = private_stack_size;
m_ctx.uc_link = &m_main_ctx;
//printf("%s:makecontext:stack%p\n", __FUNCTION__, m_ctx.uc_stack.ss_sp);
//makecontext(&m_ctx, Run, 1, this);
}
UCCoroutine(callback_t callback, char * _stack_sp, uint32_t _stack_size) :
cb(callback), share_stack_sp(_stack_sp), share_stack_size(_stack_size),
private_stack_sp(NULL), private_stack_size(0), private_buf_size(0)
{//share stack model
getcontext(&m_ctx);
m_ctx.uc_stack.ss_sp = share_stack_sp;
m_ctx.uc_stack.ss_size = share_stack_size;
m_ctx.uc_link = &m_main_ctx;
//printf("%s:makecontext:stack%p\n", __FUNCTION__, m_ctx.uc_stack.ss_sp);
//makecontext(&m_ctx, Run, 1, this);
}
void Resume_P()
{
//setcontext(&m_ctx);
swapcontext(&m_main_ctx, &m_ctx);
}
void Resume_S()
{
printf("[TRACE:%s:IN]\n", __FUNCTION__);
if(private_stack_sp)
{
char * start_sp = share_stack_sp + share_stack_size - private_stack_size;
memmove(start_sp, private_stack_sp, private_stack_size);
}
swapcontext(&m_main_ctx, &m_ctx);
printf("[TRACE:%s:OUT]\n", __FUNCTION__);
}
void Yield_P()
{
//getcontext(&m_ctx);
swapcontext(&m_ctx, &m_main_ctx);
}
void Yield_S()
{
getcontext(&m_ctx);
uint32_t share_stack_used = (uint64_t)share_stack_sp + share_stack_size - m_ctx.uc_mcontext.gregs[REG_RSP];
if(!private_stack_sp)
{
private_stack_size = share_stack_used;
private_stack_sp = new char[private_stack_size];
private_buf_size = private_stack_size;
memmove(private_stack_sp, (void*)(m_ctx.uc_mcontext.gregs[REG_RSP]), private_stack_size);
}
else
{//used share stack size, may change bigger or smaller than last yeild
if(private_stack_size < share_stack_used)
{
if(private_buf_size < share_stack_used)
{
delete private_stack_sp;
private_stack_sp = new char[share_stack_used];
private_buf_size = share_stack_used;
}
private_stack_size = share_stack_used;
memmove(private_stack_sp, (void*)(m_ctx.uc_mcontext.gregs[REG_RSP]), private_stack_size);
}
else
{
private_stack_size = share_stack_used;
memmove(private_stack_sp, (void *)(m_ctx.uc_mcontext.gregs[REG_RSP]), private_stack_size);
}
}
//setcontext(&m_main_ctx);
swapcontext(&m_ctx, &m_main_ctx);
}
uint32_t GetTaskID()
{
return task_id;
}
private:
void Init()
{
makecontext(&m_ctx, Run, 1, this);
}
void SetTaskID(uint32_t _task_id)
{
task_id = _task_id;
}
private:
UCCoroutine(const UCCoroutine & uc_context)
{}
UCCoroutine & operator=(const UCCoroutine & uc_context)
{}
static void Run(void * arg)
{
UCCoroutine * coro = (UCCoroutine *)arg;
printf("%s:[coro:%p,task_id:%u]\n", __FUNCTION__, arg, coro->task_id);
coro->cb(coro->task_id);
}
private:
ucontext_t m_ctx;
ucontext_t m_main_ctx;
callback_t cb;
char * private_stack_sp;
uint32_t private_stack_size;
uint32_t private_buf_size;
char * share_stack_sp;
uint32_t share_stack_size;
uint32_t task_id;
};