/src/brpc/src/bthread/stack.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | // bthread - An M:N threading library to make applications more concurrent. |
19 | | |
20 | | // Date: Sun Sep 7 22:37:39 CST 2014 |
21 | | |
22 | | #ifndef BTHREAD_ALLOCATE_STACK_H |
23 | | #define BTHREAD_ALLOCATE_STACK_H |
24 | | |
25 | | #include <assert.h> |
26 | | #include <gflags/gflags.h> // DECLARE_int32 |
27 | | #include "bthread/types.h" |
28 | | #include "bthread/context.h" // bthread_fcontext_t |
29 | | #include "butil/object_pool.h" |
30 | | |
31 | | namespace bthread { |
32 | | |
33 | | struct StackStorage { |
34 | | unsigned stacksize; |
35 | | unsigned guardsize; |
36 | | // Assume stack grows upwards. |
37 | | // http://www.boost.org/doc/libs/1_55_0/libs/context/doc/html/context/stack.html |
38 | | void* bottom; |
39 | | unsigned valgrind_stack_id; |
40 | | |
41 | | // Clears all members. |
42 | 0 | void zeroize() { |
43 | 0 | stacksize = 0; |
44 | 0 | guardsize = 0; |
45 | 0 | bottom = NULL; |
46 | 0 | valgrind_stack_id = 0; |
47 | 0 | } |
48 | | }; |
49 | | |
50 | | // Allocate a piece of stack. |
51 | | int allocate_stack_storage(StackStorage* s, int stacksize, int guardsize); |
52 | | // Deallocate a piece of stack. Parameters MUST be returned or set by the |
53 | | // corresponding allocate_stack_storage() otherwise behavior is undefined. |
54 | | void deallocate_stack_storage(StackStorage* s); |
55 | | |
56 | | enum StackType { |
57 | | STACK_TYPE_MAIN = 0, |
58 | | STACK_TYPE_PTHREAD = BTHREAD_STACKTYPE_PTHREAD, |
59 | | STACK_TYPE_SMALL = BTHREAD_STACKTYPE_SMALL, |
60 | | STACK_TYPE_NORMAL = BTHREAD_STACKTYPE_NORMAL, |
61 | | STACK_TYPE_LARGE = BTHREAD_STACKTYPE_LARGE |
62 | | }; |
63 | | |
64 | | struct ContextualStack { |
65 | 0 | virtual ~ContextualStack() = default; |
66 | | bthread_fcontext_t context; |
67 | | StackType stacktype; |
68 | | StackStorage storage; |
69 | | }; |
70 | | |
71 | | // Get a stack in the `type' and run `entry' at the first time that the |
72 | | // stack is jumped. |
73 | | ContextualStack* get_stack(StackType type, void (*entry)(intptr_t)); |
74 | | // Recycle a stack. NULL does nothing. |
75 | | void return_stack(ContextualStack*); |
76 | | // Jump from stack `from' to stack `to'. `from' must be the stack of callsite |
77 | | // (to save contexts before jumping) |
78 | | void jump_stack(ContextualStack* from, ContextualStack* to); |
79 | | |
80 | | } // namespace bthread |
81 | | |
82 | | #include "bthread/stack_inl.h" |
83 | | |
84 | | #endif // BTHREAD_ALLOCATE_STACK_H |