保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
#include <node.h>
#include <stdlib.h>
#include <vector>
#include "libcoro/coro.h"
 
class Coroutine {
    public:
        typedef void(entry_t)(void*);
 
    private:
#ifdef CORO_FIBER
        void* stack_base;
#endif
        coro_context context;
        coro_stack stack;
        std::vector<void*> fls_data;
        entry_t* entry;
        void* arg;
 
        ~Coroutine();
 
        /**
         * Constructor for currently running "fiber". This is really just original thread, but we
         * need a way to get back into the main thread after yielding to a fiber. Basically this
         * shouldn't be called from anywhere.
         */
        Coroutine();
 
        /**
         * This constructor will actually create a new fiber context. Execution does not begin
         * until you call run() for the first time.
         */
        Coroutine(entry_t& entry, void* arg);
 
        /**
         * Resets the context of this coroutine from the start. Used to recyle old coroutines.
         */
        void reset(entry_t* entry, void* arg);
 
        static void trampoline(void* that);
        void transfer(Coroutine& next);
 
    public:
        static size_t pool_size;
 
        /**
         * Returns the currently-running fiber.
         */
        static Coroutine& current();
 
        /**
         * Create a new fiber.
         */
        static Coroutine* create_fiber(entry_t* entry, void* arg = NULL);
 
        /**
         * Initialize the library.
         */
        static void init(v8::Isolate* isolate);
 
        /**
         * Set the size of coroutines created by this library. Since coroutines are pooled the stack
         * size is global instead of per-coroutine. Stack is measured in sizeof(void*), so
         * set_stack_size(128) -> 512 bytes or 1kb
         */
        static void set_stack_size(unsigned int size);
 
        /**
         * Get the number of coroutines that have been created.
         */
        static size_t coroutines_created();
 
        /**
         * Start or resume execution in this fiber. Note there is no explicit yield() function,
         * you must manually run another fiber.
         */
        void run();
 
        /**
         * Finish this coroutine.. This will halt execution of this coroutine and resume execution
         * of `next`. If you do not call this function, and instead just return from `entry` the
         * application will exit. This function may or may not actually return.
         */
        void finish(Coroutine& next, v8::Isolate* isolate);
 
        /**
         * Returns address of the lowest usable byte in this Coroutine's stack.
         */
        void* bottom() const;
 
        /**
         * Returns the size this Coroutine takes up in the heap.
         */
        size_t size() const;
};