acl/lib_fiber/cpp/include/fiber/fiber.hpp

378 lines
7.6 KiB
C++
Raw Normal View History

2018-11-30 14:38:22 +08:00
#pragma once
#include <stddef.h>
#include "fiber_cpp_define.hpp"
struct ACL_FIBER;
namespace acl {
typedef enum
{
FIBER_EVENT_T_KERNEL, // Linux: epoll, FreeBSD: kquque, Windows: iocp
FIBER_EVENT_T_POLL,
FIBER_EVENT_T_SELECT,
FIBER_EVENT_T_WMSG,
} fiber_event_t;
/**
* Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳в<EFBFBD>ʵ<EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
class FIBER_CPP_API fiber
{
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param running {bool} <EFBFBD><EFBFBD>Ϊ true ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ǰЭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѣ<EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ACL_FIBER <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󶨣<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> start <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>; <EFBFBD><EFBFBD>Ϊ false ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> start <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>
*/
fiber(bool running = false);
virtual ~fiber(void);
/**
* <EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> running Ϊ false ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Э<EFBFBD>̣<EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD> run <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> running Ϊ true ʱ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> start <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param stack_size {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>С
*/
void start(size_t stack_size = 320000);
/**
* <EFBFBD>ڱ<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>˳<EFBFBD>
*/
bool kill(void);
/**
* <EFBFBD>жϵ<EFBFBD>ǰЭ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>˳<EFBFBD>
* @return {bool} <EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>˳<EFBFBD>
*/
bool killed(void) const;
/**
* <EFBFBD>жϵ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> killed <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
* killed <EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> acl::fiber <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD>Ҹ<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
* Ҳ<EFBFBD>п<EFBFBD><EFBFBD>ܱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>𣬶<EFBFBD> self_killed <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ acl::fiber <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
* <EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Э<EFBFBD><EFBFBD>
* @return {bool}
*/
static bool self_killed(void);
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ID <EFBFBD><EFBFBD>
* @return {unsigned int}
*/
unsigned int get_id(void) const;
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ID <EFBFBD><EFBFBD>
* @return {unsigned int}
*/
static unsigned int self(void);
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰЭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>ϵͳ API <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* return {int}
*/
int get_errno(void) const;
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰЭ<EFBFBD>̵Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param errnum {int}
*/
void set_errno(int errnum);
2018-11-30 18:26:46 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @return {const char*}
*/
static const char* last_serror(void);
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {int}
*/
static int last_error(void);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param errnum {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param buf {char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param size {size_t} buf <EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>С
* @return {const char*} buf <EFBFBD><EFBFBD>ַ
*/
static const char* strerror(int errnum, char* buf, size_t size);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param on {bool} Ϊ true ʱ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
static void stdout_open(bool on);
2018-11-30 14:38:22 +08:00
/**
* <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̺󲻱<EFBFBD>
* <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> schedule <EFBFBD><EFBFBD> schedule_with <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param type {fiber_event_t} <EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD>μ<EFBFBD><EFBFBD><EFBFBD>FIBER_EVENT_T_XXX
* @param schedule_auto {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ true<EFBFBD><EFBFBD><EFBFBD>򴴽<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD>Э<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󲻱<EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> schedule/schedule_with <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Э<EFBFBD>̹<EFBFBD><EFBFBD>̣<EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̺󣬱<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD> schedule <EFBFBD><EFBFBD>
* schedule_with <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̹<EFBFBD><EFBFBD>̣<EFBFBD><EFBFBD>ڲ<EFBFBD>ȱʡ״̬Ϊ false
*/
static void init(fiber_event_t type, bool schedule_auto = false);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĵ<EFBFBD><EFBFBD>ȹ<EFBFBD><EFBFBD><EFBFBD>
*/
static void schedule(void);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD>ʱָ<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> schedule_init
* <EFBFBD><EFBFBD> schedule <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param type {fiber_event_t} <EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD>μ<EFBFBD><EFBFBD><EFBFBD>FIBER_EVENT_T_XXX
*/
static void schedule_with(fiber_event_t type);
/**
* <EFBFBD>жϵ<EFBFBD>ǰ<EFBFBD>߳<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD>״̬
* @return {bool}
*/
static bool scheduled(void);
/**
* ֹͣЭ<EFBFBD>̵<EFBFBD><EFBFBD>ȹ<EFBFBD><EFBFBD><EFBFBD>
*/
static void schedule_stop(void);
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Э<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>) <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
static void yield(void);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰЭ<EFBFBD>̣<EFBFBD>ִ<EFBFBD>еȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>
*/
static void switch_to_next(void);
/**
* <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param f {fiber&}
*/
static void ready(fiber& f);
/**
* ʹ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param milliseconds {unsigned int} ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ߵĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {unsigned int} <EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߺ<EFBFBD><EFBFBD>ٴα<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѻ<EFBFBD>ʣ<EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
static unsigned int delay(unsigned int milliseconds);
/**
* <EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD>߳<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>Ҫ hook ϵͳ API<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ȱʡ
* <EFBFBD><EFBFBD> hook ϵͳ API
* @param on {bool}
*/
static void hook_api(bool on);
/**
* <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ acl <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IO <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UNIX ƽ̨<EFBFBD>²<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD> HOOK IO API
*/
static void acl_io_hook(void);
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD> acl<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> IO Э<EFBFBD>̻<EFBFBD>
*/
static void acl_io_unlock(void);
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰϵͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {int}
*/
static int get_sys_errno(void);
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰϵͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param errnum {int}
*/
static void set_sys_errno(int errnum);
public:
/**
* <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> C <EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD>
* @return {ACL_FIBER* }
*/
ACL_FIBER* get_fiber(void) const;
protected:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> start <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̺󣬱<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD>Ӷ<EFBFBD>֪ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еIJ<EFBFBD><EFBFBD><EFBFBD>
* running Ϊ true <EFBFBD><EFBFBD><EFBFBD><EFBFBD> start <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
virtual void run(void);
private:
ACL_FIBER* f_;
static void fiber_callback(ACL_FIBER* f, void* ctx);
};
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
class FIBER_CPP_API fiber_timer
{
public:
fiber_timer(void);
virtual ~fiber_timer(void) {}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD>ʱ<EFBFBD><EFBFBD>
* @param milliseconds {unsigned int} <EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
* @param stack_size {size_t} Э<EFBFBD>̵<EFBFBD>ջ<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>С
*/
void start(unsigned int milliseconds, size_t stack_size = 320000);
protected:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ָô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD>
*/
virtual void run(void) = 0;
private:
ACL_FIBER* f_;
static void timer_callback(ACL_FIBER* f, void* ctx);
};
/**
* <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>
*/
template <typename T>
class fiber_trigger : public fiber
{
public:
fiber_trigger(timer_trigger<T>& timer)
: delay_(100)
, stop_(false)
, timer_(timer)
{
}
virtual ~fiber_trigger(void) {}
void add(T* o)
{
mbox_.push(o);
}
void del(T* o)
{
timer_.del(o);
}
timer_trigger<T>& get_trigger(void)
{
return timer_;
}
// @override
void run(void)
{
while (!stop_) {
T* o = mbox_.pop(delay_);
if (o)
timer_.add(o);
long long next = timer_.trigger();
long long curr = get_curr_stamp();
if (next == -1)
delay_ = 100;
else {
delay_ = next - curr;
if (delay_ <= 0)
delay_ = 1;
}
}
}
private:
long long delay_;
bool stop_;
timer_trigger<T>& timer_;
mbox<T> mbox_;
};
} // namespace acl
#if defined(__GNUC__) && (__GNUC__ > 6 ||(__GNUC__ == 6 && __GNUC_MINOR__ >= 0))
# ifndef ACL_USE_CPP11
# define ACL_USE_CPP11
# endif
#endif
#ifdef ACL_USE_CPP11
#include <functional>
namespace acl
{
class FIBER_CPP_API go_fiber
{
public:
go_fiber(void) {}
go_fiber(size_t stack_size) : stack_size_(stack_size) {}
void operator=(std::function<void()> fn);
private:
size_t stack_size_ = 320000;
};
} // namespace acl
#define go acl::go_fiber()=
#define go_stack(size) acl::go_fiber(size)=
/**
* static void fiber1(void)
* {
* printf("fiber: %d\r\n", acl::fiber::self());
* }
*
* static void fiber2(acl::string& buf)
* {
* printf("in fiber: %d, buf: %s\r\n", acl::fiber::self(), buf.c_str());
* buf = "world";
* }
*
* static void fiber3(const acl::string& buf)
* {
* printf("in fiber: %d, buf: %s\r\n", acl::fiber::self(), buf.c_str());
* }
*
* static test(void)
* {
* go fiber1;
*
* acl::string buf("hello");
*
* go[&] {
* fiber2(buf);
* };
*
* go[=] {
* fiber3(buf);
* };
*
* go[&] {
* fiber3(buf);
* };
* }
*/
#endif