-
Notifications
You must be signed in to change notification settings - Fork 91
/
coroutine.h
144 lines (117 loc) · 3.88 KB
/
coroutine.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
// Copyright (c) 2018-2020 The EFramework Project
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef _COROUTINE_HEADER_
#define _COROUTINE_HEADER_
#include <unistd.h>
#include <sys/time.h>
#include "fiber.h"
#include "util/list.h"
#define ERROR_CO_EXITED ERROR_FIBER_EXITED
#define ERROR_CO_NOT_INITED ERROR_FIBER_NOT_INITED
typedef struct _ef_coroutine {
/*
* nested fiber header struct
*/
ef_fiber_t fiber;
/*
* chain the coroutine to the pool
*/
ef_list_entry_t full_entry;
/*
* after the coroutine exited, chain it to the pool's free list
*/
ef_list_entry_t free_entry;
/*
* coroutine last run time, checked when doing pool shrink
*/
struct timeval last_run_time;
/*
* run count of the coroutine
*/
unsigned int run_count;
} ef_coroutine_t;
typedef struct _ef_coroutine_pool {
/*
* nested fiber sched struct
*/
ef_fiber_sched_t fiber_sched;
/*
* the stack size of the coroutines created in current pool
*/
size_t stack_size;
/*
* the minimum number of coroutines kept when doing pool shrink
*/
int limit_min;
/*
* the maximum number of coroutines this pool can hold
*/
int limit_max;
/*
* chain all coroutines in this pool together
*/
ef_list_entry_t full_list;
/*
* the chain of exited coroutines, can be reused
*/
ef_list_entry_t free_list;
/*
* the number of coroutines in this pool
*/
int full_count;
/*
* the number of exited coroutines in this pool
*/
int free_count;
/*
* total run count of coroutines in the pool
*/
unsigned long run_count;
} ef_coroutine_pool_t;
typedef ef_fiber_proc_t ef_coroutine_proc_t;
/*
* init the pool with stack_size, the min/max number of coroutines
*/
int ef_coroutine_pool_init(ef_coroutine_pool_t *pool, size_t stack_size, int limit_min, int limit_max);
/*
* create a coroutine in the pool and init it, may take one from free_list
*/
ef_coroutine_t *ef_coroutine_create(ef_coroutine_pool_t *pool, size_t header_size, ef_coroutine_proc_t fiber_proc, void *param);
/*
* resume or first run the coroutine in the pool
*/
long ef_coroutine_resume(ef_coroutine_pool_t *pool, ef_coroutine_t *co, long to_yield);
/*
* shrink the pool, free(delete) at most max_count coroutines whose idle time exceed idle_millisecs
*/
int ef_coroutine_pool_shrink(ef_coroutine_pool_t *pool, int idle_millisecs, int max_count);
/*
* get the current "running" coroutine use pool
*/
inline ef_coroutine_t *ef_coroutine_current(ef_coroutine_pool_t *pool) __attribute__((always_inline));
inline ef_coroutine_t *ef_coroutine_current(ef_coroutine_pool_t *pool)
{
ef_fiber_sched_t *rt = &pool->fiber_sched;
if (rt->current_fiber == &rt->thread_fiber) {
return NULL;
}
return (ef_coroutine_t*)rt->current_fiber;
}
#endif