acl  3.5.3.0
fiber_base.h
浏览该文件的文档.
1 #ifndef FIBER_BASE_INCLUDE_H
2 #define FIBER_BASE_INCLUDE_H
3 
4 #include <stdarg.h>
5 #include "fiber_define.h"
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 typedef ACL_FIBER *((*FIBER_ALLOC_FN)(void (*)(ACL_FIBER *), size_t));
12 typedef ACL_FIBER *((*FIBER_ORIGIN_FN)(void));
13 
15  FIBER_ORIGIN_FN origin_fn);
16 
17 FIBER_API ACL_FIBER *acl_fiber_alloc(size_t size, void **pptr);
18 
19 /**
20  * set flag if the system API should be hooked, default value is 1 internal
21  * @param onoff {int} if need to hook the system API
22  */
23 FIBER_API void acl_fiber_hook_api(int onoff);
24 
25 /**
26  * create and start one fiber
27  * @param fn {void (*)(ACL_FIBER*, void*)} the callback of fiber running
28  * @param arg {void*} the second parameter of the callback fn
29  * @param size {size_t} the virual memory size of the fiber created
30  * @return {ACL_FIBER*}
31  */
32 FIBER_API ACL_FIBER* acl_fiber_create(void (*fn)(ACL_FIBER*, void*),
33  void* arg, size_t size);
34 
35 /**
36  * get the fibers count in deading status
37  * @return {unsigned}
38  */
39 FIBER_API unsigned acl_fiber_ndead(void);
40 
41 /**
42  * get the fibers count in aliving status
43  * @return {unsigned}
44  */
45 FIBER_API unsigned acl_fiber_number(void);
46 
47 /**
48  * create one fiber in background for freeing the dead fibers, specify the
49  * maximum fibers in every recyling process
50  * @param max {size_t} the maximum fibers to freed in every recyling process
51  */
52 FIBER_API void acl_fiber_check_timer(size_t max);
53 
54 /**
55  * get the current running fiber
56  * @retur {ACL_FIBER*} if no running fiber NULL will be returned
57  */
59 
60 /**
61  * get the fiber ID of the specified fiber
62  * @param fiber {const ACL_FIBER*} the specified fiber object
63  * @return {unsigned int} return the fiber ID
64  */
65 FIBER_API unsigned int acl_fiber_id(const ACL_FIBER* fiber);
66 
67 /**
68  * get the current running fiber's ID
69  * @return {unsigned int} the current fiber's ID
70  */
71 FIBER_API unsigned int acl_fiber_self(void);
72 
73 /**
74  * set the error number to the specified fiber object
75  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
76  * fiber will be used
77  * @param errnum {int} the error number
78  */
79 FIBER_API void acl_fiber_set_errno(ACL_FIBER* fiber, int errnum);
80 
81 /**
82  * get the error number of assosiated fiber
83  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
84  * @return {int} get the error number of assosiated fiber
85  */
87 
88 /**
89  * @deprecated
90  * @param fiber {ACL_FIBER*}
91  * @param yesno {int}
92  */
93 FIBER_API void acl_fiber_keep_errno(ACL_FIBER* fiber, int yesno);
94 
95 /**
96  * get the assosiated fiber's status
97  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
98  * @return {int}
99  */
100 FIBER_API int acl_fiber_status(const ACL_FIBER* fiber);
101 
102 /**
103  * kill the suspended fiber and notify it to exit
104  * @param fiber {const ACL_FIBER*} the specified fiber, NOT NULL
105  */
106 FIBER_API void acl_fiber_kill(ACL_FIBER* fiber);
107 
108 /**
109  * check if the specified fiber has been killed
110  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
111  * @return {int} non zero returned if been killed
112  */
114 
115 /**
116  * check if the specified fiber has been signaled
117  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
118  * @return {int} non zero returned if been signed
119  */
121 
122 /**
123  * check if the specified fiber's socket has been closed by another fiber
124  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
125  * @return {int} non zero returned if been closed
126  */
128 
129 /**
130  * check if the specified fiber has been canceled
131  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
132  * @return {int} non zero returned if been canceled
133  */
135 
136 /**
137  * wakeup the suspended fiber with the assosiated signal number
138  * @param fiber {const ACL_FIBER*} the specified fiber, NOT NULL
139  * @param signum {int} SIGINT, SIGKILL, SIGTERM ... refer to bits/signum.h
140  */
141 FIBER_API void acl_fiber_signal(ACL_FIBER* fiber, int signum);
142 
143 /**
144  * get the signal number got from other fiber
145  * @param fiber {ACL_FIBER*} the specified fiber, if NULL the current running
146  * @retur {int} the signal number got
147  */
149 
150 /**
151  * suspend the current running fiber
152  * @return {int}
153  */
154 FIBER_API int acl_fiber_yield(void);
155 
156 /**
157  * add the suspended fiber into resuming queue
158  * @param fiber {ACL_FIBER*} the fiber, NOT NULL
159  */
160 FIBER_API void acl_fiber_ready(ACL_FIBER* fiber);
161 
162 /**
163  * suspend the current fiber and switch to run the next ready fiber
164  */
165 FIBER_API void acl_fiber_switch(void);
166 
167 /**
168  * set the fiber schedule process with automatically, in this way, when one
169  * fiber was created, the schedule process will start automatically, but only
170  * the first fiber was started, so you can create the other fibers in this
171  * fiber. The default schedule mode is non-automatically, you should call the
172  * acl_fiber_schedule or acl_fiber_schedule_with explicit
173  */
175 
176 /**
177  * start the fiber schedule process, the fibers in the ready quque will be
178  * started in sequence.
179  */
180 FIBER_API void acl_fiber_schedule(void);
181 
182 /**
183  * start the fiber schedule process with the specified event type, the default
184  * event type is FIBER_EVENT_KERNEL. acl_fiber_schedule using the default
185  * event type. FIBER_EVENT_KERNEL is diffrent for diffrent OS platform:
186  * Linux: epoll; BSD: kqueue; Windows: iocp.
187  * @param event_mode {int} the event type, defined as FIBER_EVENT_XXX
188  */
189 #define FIBER_EVENT_KERNEL 0 /* epoll/kqueue/iocp */
190 #define FIBER_EVENT_POLL 1 /* poll */
191 #define FIBER_EVENT_SELECT 2 /* select */
192 #define FIBER_EVENT_WMSG 3 /* win message */
193 FIBER_API void acl_fiber_schedule_with(int event_mode);
194 
195 /**
196  * set the event type, the default type is FIBER_EVENT_KERNEL, this function
197  * must be called before acl_fiber_schedule.
198  * @param event_mode {int} the event type, defined as FIBER_EVENT_XXX
199  */
200 FIBER_API void acl_fiber_schedule_set_event(int event_mode);
201 
202 /**
203  * check if the current thread is in fiber schedule status
204  * @return {int} non zero returned if in fiber schedule status
205  */
207 
208 /**
209  * stop the fiber schedule process, all fibers will be stopped
210  */
212 
213 /**
214  * let the current fiber sleep for a while
215  * @param milliseconds {unsigned int} the milliseconds to sleep
216  * @return {unsigned int} the rest milliseconds returned after wakeup
217  */
218 FIBER_API unsigned int acl_fiber_delay(unsigned int milliseconds);
219 
220 /**
221  * let the current fiber sleep for a while
222  * @param seconds {unsigned int} the seconds to sleep
223  * @return {unsigned int} the rest seconds returned after wakeup
224  */
225 FIBER_API unsigned int acl_fiber_sleep(unsigned int seconds);
226 
227 /**
228  * create one fiber timer
229  * @param milliseconds {unsigned int} the timer wakeup milliseconds
230  * @param size {size_t} the virtual memory of the created fiber
231  * @param fn {void (*)(ACL_FIBER*, void*)} the callback when fiber wakeup
232  * @param ctx {void*} the second parameter of the callback fn
233  * @return {ACL_FIBER*} the new created fiber returned
234  */
235 FIBER_API ACL_FIBER* acl_fiber_create_timer(unsigned int milliseconds,
236  size_t size, void (*fn)(ACL_FIBER*, void*), void* ctx);
237 
238 /**
239  * reset the timer milliseconds time before the timer fiber wakeup
240  * @param timer {ACL_FIBER*} the fiber created by acl_fiber_create_timer
241  * @param milliseconds {unsigned int} the new timer wakeup milliseconds
242  */
243 FIBER_API void acl_fiber_reset_timer(ACL_FIBER* timer, unsigned int milliseconds);
244 
245 /**
246  * set the DNS service addr
247  * @param ip {const char*} ip of the DNS service
248  * @param port {int} port of the DNS service
249  */
250 FIBER_API void acl_fiber_set_dns(const char* ip, int port);
251 
252 /* for fiber specific */
253 
254 /**
255  * set the current fiber's local object
256  * @param key {int*} the addr of indexed key, its initial value should <= 0,
257  * and one integer which > 0 will be set for it; the fiber local object will
258  * be assosiated with the indexed key.
259  * @param ctx {void *} the fiber local object
260  * @param free_fn {void (*)(void*)} the callback will be called before the
261  * current fiber exiting
262  * @return {int} the integer value(>0) of indexed key returned, value less than
263  * 0 will be returned if no running fiber
264  */
265 FIBER_API int acl_fiber_set_specific(int* key, void* ctx, void (*free_fn)(void*));
266 
267 /**
268  * get the current fiber's local object assosiated with the specified indexed key
269  * @param key {int} the integer value returned by acl_fiber_set_specific
270  * @retur {void*} NULL returned if no fiber local object with the specified key
271  */
272 FIBER_API void* acl_fiber_get_specific(int key);
273 
274 /****************************************************************************/
275 
276 /**
277  * log function type used in fiber logging process, should be set by the
278  * function acl_fiber_msg_pre_write
279  * @param ctx {void*} the user's context
280  * @param fmt {const char*} format of parameters
281  * @param ap {va_list} list of parameters
282  */
283 typedef void (*FIBER_MSG_PRE_WRITE_FN)(void *ctx, const char *fmt, va_list ap);
284 
285 /**
286  * log function type used in fiber logging process, should be set by the
287  * function acl_fiber_msg_register. This can be used by user for get the
288  * logging information of fiber
289  * @param ctx {void*} the user's context
290  * @param fmt {const char*} format of parameters
291  * @param ap {va_list} list of parameters
292  */
293 typedef void (*FIBER_MSG_WRITE_FN) (void *ctx, const char *fmt, va_list ap);
294 
295 /**
296  * set the user's log saving function when process started
297  * @param write_fn {MSG_WRITE_FN} log function defined by the user
298  * @param ctx {void*} parameter will be transfered to write_fn
299  */
300 FIBER_API void acl_fiber_msg_register(FIBER_MSG_WRITE_FN write_fn, void *ctx);
301 
302 /**
303  * cleanup the registered log callback by acl_fiber_msg_register
304  */
306 
307 /**
308  * register the user's callback
309  * @param pre_write {MSG_PRE_WRITE_FN}
310  * @param ctx {void*}
311  */
312 FIBER_API void acl_fiber_msg_pre_write(FIBER_MSG_PRE_WRITE_FN pre_write, void *ctx);
313 
314 /**
315  * if showing the fiber schedule process's log to stdout
316  * @param onoff {int} log will be showed to stdout if onoff isn't 0
317  */
318 FIBER_API void acl_fiber_msg_stdout_enable(int onoff);
319 
320 /**
321  * get the system error number of last system API calling
322  * @return {int} error number
323  */
325 
326 /**
327  * get the error information of last system API calling
328  * @return {const char*}
329  */
330 FIBER_API const char *acl_fiber_last_serror(void);
331 
332 /**
333  * convert errno to string
334  * @param errnum {int}
335  * @param buf {char*} hold the result
336  * @param size {size_t} buf's size
337  * @retur {const char*} the addr of buf
338  */
339 FIBER_API const char *acl_fiber_strerror(int errnum, char *buf, size_t size);
340 
341 /**
342  * set the system error number
343  * @param errnum {int} the error number
344  */
345 FIBER_API void acl_fiber_set_error(int errnum);
346 
347 /**
348  * set the fd limit for the current process
349  * @param limit {int} the fd limit to be set
350  * @return {int} the real fd limit will be returned
351  */
352 FIBER_API int acl_fiber_set_fdlimit(int limit);
353 
354 #if !defined(_WIN32) || !defined(_WIN64)
355 FIBER_API int acl_fiber_gettimeofday(struct timeval *tv, struct timezone *tz);
356 #endif
357 
358 FIBER_API void acl_fiber_memstat(void);
359 
360 /****************************************************************************/
361 
362 #ifdef __cplusplus
363 }
364 #endif
365 
366 #endif
FIBER_API int acl_fiber_closed(ACL_FIBER *fiber)
FIBER_API unsigned int acl_fiber_id(const ACL_FIBER *fiber)
FIBER_API ACL_FIBER * acl_fiber_create_timer(unsigned int milliseconds, size_t size, void(*fn)(ACL_FIBER *, void *), void *ctx)
FIBER_API void acl_fiber_msg_stdout_enable(int onoff)
FIBER_API int acl_fiber_killed(ACL_FIBER *fiber)
FIBER_API int acl_fiber_yield(void)
void(* FIBER_MSG_WRITE_FN)(void *ctx, const char *fmt, va_list ap)
Definition: fiber_base.h:293
FIBER_API void acl_fiber_keep_errno(ACL_FIBER *fiber, int yesno)
FIBER_API unsigned int acl_fiber_self(void)
FIBER_API int acl_fiber_errno(ACL_FIBER *fiber)
FIBER_API void acl_fiber_register(FIBER_ALLOC_FN alloc_fn, FIBER_ORIGIN_FN origin_fn)
FIBER_API void acl_fiber_schedule(void)
FIBER_API int acl_fiber_set_fdlimit(int limit)
FIBER_API void acl_fiber_set_dns(const char *ip, int port)
#define FIBER_API
Definition: fiber_define.h:94
FIBER_API const char * acl_fiber_last_serror(void)
FIBER_API int acl_fiber_scheduled(void)
FIBER_API ACL_FIBER * acl_fiber_alloc(size_t size, void **pptr)
FIBER_API void acl_fiber_msg_unregister(void)
FIBER_API void * acl_fiber_get_specific(int key)
FIBER_API void acl_fiber_ready(ACL_FIBER *fiber)
ACL_FIBER *(* FIBER_ORIGIN_FN)(void))
Definition: fiber_base.h:12
FIBER_API int acl_fiber_gettimeofday(struct timeval *tv, struct timezone *tz)
FIBER_API void acl_fiber_msg_register(FIBER_MSG_WRITE_FN write_fn, void *ctx)
FIBER_API void acl_fiber_msg_pre_write(FIBER_MSG_PRE_WRITE_FN pre_write, void *ctx)
FIBER_API int acl_fiber_canceled(ACL_FIBER *fiber)
FIBER_API void acl_fiber_set_error(int errnum)
FIBER_API int acl_fiber_status(const ACL_FIBER *fiber)
FIBER_API unsigned int acl_fiber_delay(unsigned int milliseconds)
FIBER_API int acl_fiber_set_specific(int *key, void *ctx, void(*free_fn)(void *))
FIBER_API void acl_fiber_set_errno(ACL_FIBER *fiber, int errnum)
FIBER_API void acl_fiber_schedule_set_event(int event_mode)
FIBER_API const char * acl_fiber_strerror(int errnum, char *buf, size_t size)
FIBER_API void acl_fiber_check_timer(size_t max)
ACL_FIBER *(* FIBER_ALLOC_FN)(void(*)(ACL_FIBER *), size_t))
Definition: fiber_base.h:11
FIBER_API void acl_fiber_kill(ACL_FIBER *fiber)
void(* FIBER_MSG_PRE_WRITE_FN)(void *ctx, const char *fmt, va_list ap)
Definition: fiber_base.h:283
FIBER_API ACL_FIBER * acl_fiber_running(void)
struct ACL_FIBER ACL_FIBER
Definition: fiber_define.h:100
FIBER_API int acl_fiber_signum(ACL_FIBER *fiber)
FIBER_API void acl_fiber_signal(ACL_FIBER *fiber, int signum)
FIBER_API int acl_fiber_last_error(void)
FIBER_API unsigned acl_fiber_ndead(void)
FIBER_API unsigned acl_fiber_number(void)
FIBER_API void acl_fiber_schedule_with(int event_mode)
ACL_API void const char * fmt
Definition: acl_aio.h:771
FIBER_API ACL_FIBER * acl_fiber_create(void(*fn)(ACL_FIBER *, void *), void *arg, size_t size)
FIBER_API int acl_fiber_signaled(ACL_FIBER *fiber)
FIBER_API void acl_fiber_switch(void)
FIBER_API void acl_fiber_memstat(void)
FIBER_API void acl_fiber_hook_api(int onoff)
FIBER_API void acl_fiber_reset_timer(ACL_FIBER *timer, unsigned int milliseconds)
FIBER_API void acl_fiber_schedule_stop(void)
FIBER_API unsigned int acl_fiber_sleep(unsigned int seconds)
FIBER_API void acl_fiber_schedule_init(int on)