2019-07-28 10:31:56 +08:00
|
|
|
|
#pragma once
|
2018-11-30 14:38:22 +08:00
|
|
|
|
#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;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* Э<EFBFBD><EFBFBD><EFBFBD>ඨ<EFBFBD>壬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>࣬<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳в<EFBFBD>ʵ<EFBFBD>ִ<EFBFBD><EFBFBD>鷽<EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class FIBER_CPP_API fiber : public noncopyable
|
2018-11-30 14:38:22 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
fiber(bool running = false);
|
|
|
|
|
virtual ~fiber(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>С
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
void start(size_t stack_size = 320000);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
bool kill(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD>жϵ<EFBFBD>ǰЭ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>˳<EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>˳<EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
bool killed(void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +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> 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>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
static bool self_killed(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ID <EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @return {unsigned int}
|
|
|
|
|
*/
|
|
|
|
|
unsigned int get_id(void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ID <EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @return {unsigned int}
|
|
|
|
|
*/
|
|
|
|
|
static unsigned int self(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰЭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>ϵͳ API <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* return {int}
|
|
|
|
|
*/
|
|
|
|
|
int get_errno(void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰЭ<EFBFBD>̵Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @param errnum {int}
|
|
|
|
|
*/
|
|
|
|
|
void set_errno(int errnum);
|
|
|
|
|
|
2018-11-30 18:26:46 +08:00
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
2018-11-30 18:26:46 +08:00
|
|
|
|
* @return {const char*}
|
|
|
|
|
*/
|
|
|
|
|
static const char* last_serror(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 18:26:46 +08:00
|
|
|
|
* @return {int}
|
|
|
|
|
*/
|
|
|
|
|
static int last_error(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>ַ
|
2018-11-30 18:26:46 +08:00
|
|
|
|
*/
|
|
|
|
|
static const char* strerror(int errnum, char* buf, size_t size);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2018-11-30 18:26:46 +08:00
|
|
|
|
*/
|
|
|
|
|
static void stdout_open(bool on);
|
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
|
/**
|
2019-07-28 10:31:56 +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
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void init(fiber_event_t type, bool schedule_auto = false);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĵ<EFBFBD><EFBFBD>ȹ<EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void schedule(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +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> 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
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void schedule_with(fiber_event_t type);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD>жϵ<EFBFBD>ǰ<EFBFBD>߳<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD>״̬
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
static bool scheduled(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* ֹͣЭ<EFBFBD>̵<EFBFBD><EFBFBD>ȹ<EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void schedule_stop(void);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +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>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void yield(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰЭ<EFBFBD>̣<EFBFBD>ִ<EFBFBD>еȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void switch_to_next(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @param f {fiber&}
|
|
|
|
|
*/
|
|
|
|
|
static void ready(fiber& f);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* ʹ<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>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static unsigned int delay(unsigned int milliseconds);
|
|
|
|
|
|
2018-12-25 11:32:42 +08:00
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-12-25 11:32:42 +08:00
|
|
|
|
* @return {unsigned}
|
|
|
|
|
*/
|
|
|
|
|
static unsigned alive_number(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>״̬<EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-12-25 11:32:42 +08:00
|
|
|
|
* @return {unsigned}
|
|
|
|
|
*/
|
|
|
|
|
static unsigned dead_number(void);
|
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
|
/**
|
2019-07-28 10:31:56 +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>Ҫ hook ϵͳ API<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ȱʡ
|
|
|
|
|
* <EFBFBD><EFBFBD> hook ϵͳ API
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @param on {bool}
|
|
|
|
|
*/
|
|
|
|
|
static void hook_api(bool on);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void acl_io_hook(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD> acl<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> IO Э<EFBFBD>̻<EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
static void acl_io_unlock(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰϵͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @return {int}
|
|
|
|
|
*/
|
|
|
|
|
static int get_sys_errno(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰϵͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @param errnum {int}
|
|
|
|
|
*/
|
|
|
|
|
static void set_sys_errno(int errnum);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> C <EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
* @return {ACL_FIBER* }
|
|
|
|
|
*/
|
|
|
|
|
ACL_FIBER* get_fiber(void) const;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +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> 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>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual void run(void);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ACL_FIBER* f_;
|
|
|
|
|
|
|
|
|
|
static void fiber_callback(ACL_FIBER* f, void* ctx);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class FIBER_CPP_API fiber_timer : public noncopyable
|
2018-11-30 14:38:22 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
fiber_timer(void);
|
|
|
|
|
virtual ~fiber_timer(void) {}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>С
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
void start(unsigned int milliseconds, size_t stack_size = 320000);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +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>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual void run(void) = 0;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ACL_FIBER* f_;
|
|
|
|
|
|
|
|
|
|
static void timer_callback(ACL_FIBER* f, void* ctx);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
*/
|
|
|
|
|
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
|