forked from apache/brpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_inl.h
192 lines (165 loc) · 5.99 KB
/
stack_inl.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
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
185
186
187
188
189
190
191
192
// bthread - A M:N threading library to make applications more concurrent.
// Copyright (c) 2012 Baidu.com, Inc. All Rights Reserved
// Author: Ge,Jun (gejun@baidu.com)
// Date: Sun Sep 7 22:37:39 CST 2014
#ifndef BAIDU_BTHREAD_ALLOCATE_STACK_INL_H
#define BAIDU_BTHREAD_ALLOCATE_STACK_INL_H
DECLARE_int32(guard_page_size);
DECLARE_int32(tc_stack_small);
DECLARE_int32(tc_stack_normal);
DECLARE_bool(has_valgrind);
namespace bthread {
struct MainStackClass {};
struct SmallStackClass {
static int* stack_size_flag;
const static int stacktype = STACK_TYPE_SMALL;
};
struct NormalStackClass {
static int* stack_size_flag;
const static int stacktype = STACK_TYPE_NORMAL;
};
struct LargeStackClass {
static int* stack_size_flag;
const static int stacktype = STACK_TYPE_LARGE;
};
template <typename StackClass> struct StackContainerFactory {
struct Wrapper : public StackContainer {
explicit Wrapper(void (*entry)(intptr_t)) {
stacksize = *StackClass::stack_size_flag;
guardsize = FLAGS_guard_page_size;
stack = allocate_stack(&stacksize, &guardsize);
valgrind_stack_id = 0;
if (BAIDU_UNLIKELY(NULL == stack)) {
context = NULL;
return;
}
// TODO: Growth direction of stack is arch-dependent(not handled by
// fcontext). We temporarily assume stack grows upwards.
// http://www.boost.org/doc/libs/1_55_0/libs/context/doc/html/context/stack.html
if (FLAGS_has_valgrind) {
valgrind_stack_id = VALGRIND_STACK_REGISTER(
stack, (char*)stack - stacksize);
}
context = bthread_make_fcontext(stack, stacksize, entry);
stacktype = StackClass::stacktype;
}
~Wrapper() {
if (stack) {
if (FLAGS_has_valgrind) {
VALGRIND_STACK_DEREGISTER(valgrind_stack_id);
valgrind_stack_id = 0;
}
deallocate_stack(stack, stacksize, guardsize);
stack = NULL;
}
context = NULL;
}
};
static StackContainer* get_stack(void (*entry)(intptr_t)) {
return base::get_object<Wrapper>(entry);
}
static void return_stack(StackContainer* sc) {
base::return_object(static_cast<Wrapper*>(sc));
}
};
template <> struct StackContainerFactory<MainStackClass> {
static StackContainer* get_stack(void (*)(intptr_t)) {
StackContainer* sc = new (std::nothrow) StackContainer;
if (NULL == sc) {
return NULL;
}
sc->stacksize = 0;
sc->guardsize = 0;
sc->stacktype = STACK_TYPE_MAIN;
return sc;
}
static void return_stack(StackContainer* sc) {
delete sc;
}
};
StackContainer* get_stack(StackType type, void (*entry)(intptr_t)) {
switch (type) {
case STACK_TYPE_PTHREAD:
return NULL;
case STACK_TYPE_SMALL:
return StackContainerFactory<SmallStackClass>::get_stack(entry);
case STACK_TYPE_NORMAL:
return StackContainerFactory<NormalStackClass>::get_stack(entry);
case STACK_TYPE_LARGE:
return StackContainerFactory<LargeStackClass>::get_stack(entry);
case STACK_TYPE_MAIN:
return StackContainerFactory<MainStackClass>::get_stack(entry);
}
return NULL;
}
void return_stack(StackContainer* sc) {
if (NULL == sc) {
return;
}
switch (sc->stacktype) {
case STACK_TYPE_PTHREAD:
assert(false);
return;
case STACK_TYPE_SMALL:
return StackContainerFactory<SmallStackClass>::return_stack(sc);
case STACK_TYPE_NORMAL:
return StackContainerFactory<NormalStackClass>::return_stack(sc);
case STACK_TYPE_LARGE:
return StackContainerFactory<LargeStackClass>::return_stack(sc);
case STACK_TYPE_MAIN:
return StackContainerFactory<MainStackClass>::return_stack(sc);
}
}
} // namespace bthread
namespace base {
template <> struct ObjectPoolFreeChunkMaxItem<
bthread::StackContainerFactory<bthread::LargeStackClass>::Wrapper> {
static const size_t value = 32;
};
template <> struct ObjectPoolFreeChunkMaxItem<
bthread::StackContainerFactory<bthread::NormalStackClass>::Wrapper> {
static const size_t value = 32;
};
template <> struct ObjectPoolFreeChunkMaxItem<
bthread::StackContainerFactory<bthread::SmallStackClass>::Wrapper> {
static const size_t value = 32;
};
template <> struct ObjectPoolFreeChunkMaxItemDynamic<
bthread::StackContainerFactory<bthread::SmallStackClass>::Wrapper> {
inline static size_t value() {
return (FLAGS_tc_stack_small <= 0 ? 0 : FLAGS_tc_stack_small);
}
};
template <> struct ObjectPoolFreeChunkMaxItemDynamic<
bthread::StackContainerFactory<bthread::NormalStackClass>::Wrapper> {
inline static size_t value() {
return (FLAGS_tc_stack_normal <= 0 ? 0 : FLAGS_tc_stack_normal);
}
};
template <> struct ObjectPoolFreeChunkMaxItemDynamic<
bthread::StackContainerFactory<bthread::LargeStackClass>::Wrapper> {
inline static size_t value() { return 1UL; }
};
template <> struct ObjectPoolValidator<
bthread::StackContainerFactory<bthread::LargeStackClass>::Wrapper> {
inline static bool validate(
const bthread::StackContainerFactory<bthread::LargeStackClass>::Wrapper* w) {
return w->stack != NULL;
}
};
template <> struct ObjectPoolValidator<
bthread::StackContainerFactory<bthread::NormalStackClass>::Wrapper> {
inline static bool validate(
const bthread::StackContainerFactory<bthread::NormalStackClass>::Wrapper* w) {
return w->stack != NULL;
}
};
template <> struct ObjectPoolValidator<
bthread::StackContainerFactory<bthread::SmallStackClass>::Wrapper> {
inline static bool validate(
const bthread::StackContainerFactory<bthread::SmallStackClass>::Wrapper* w) {
return w->stack != NULL;
}
};
} // namespace base
#endif // BAIDU_BTHREAD_ALLOCATE_STACK_INL_H