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

394 lines
8.5 KiB
C++
Raw Normal View History

#pragma once
2018-11-30 14:38:22 +08:00
#include <stddef.h>
2023-01-11 10:22:33 +08:00
#include <string>
#include <vector>
2018-11-30 14:38:22 +08:00
#include "fiber_cpp_define.hpp"
struct ACL_FIBER;
namespace acl {
typedef enum
{
2022-10-23 17:36:25 +08:00
FIBER_EVENT_T_KERNEL, // Linux: epoll, FreeBSD: kquque, Windows: iocp
FIBER_EVENT_T_POLL, // Linux, FreeBSD, MacOS, Windows
FIBER_EVENT_T_SELECT, // Linux, FreeBSD, MacOS, Windows
FIBER_EVENT_T_WMSG, // Windows
FIBER_EVENT_T_IO_URING, // Linux
2018-11-30 14:38:22 +08:00
} fiber_event_t;
struct FIBER_CPP_API fiber_frame
2023-01-11 10:22:33 +08:00
{
fiber_frame(void) : pc(0), off(0) {}
~fiber_frame(void) {}
std::string func;
long pc;
long off;
};
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>
2018-11-30 14:38:22 +08:00
*/
class FIBER_CPP_API fiber
2018-11-30 14:38:22 +08:00
{
public:
fiber(void);
fiber(ACL_FIBER *fb);
2018-11-30 14:38:22 +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);
2018-11-30 14:38:22 +08:00
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>С
2022-12-16 13:44:49 +08:00
* @param share_stack {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>ʽ(<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD> libfiber.a ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SHARE_STACK <EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
2018-11-30 14:38:22 +08:00
*/
void start(size_t stack_size = 320000, bool share_stack = false);
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>
* @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);
/**
* <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;
/**
* <EFBFBD>жϵ<EFBFBD>ǰ<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);
/**
* <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;
/**
* <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);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
* @return {unsigned int}
*/
static unsigned int id(const fiber& fb);
2018-11-30 14:38:22 +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;
/**
* <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);
2023-01-11 10:22:33 +08:00
public:
2018-11-30 18:26:46 +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);
/**
* <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);
/**
* <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);
/**
* <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
/**
2020-06-13 10:54:34 +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
2020-06-13 10:54:34 +08:00
* @param schedule_auto {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> 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><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);
/**
* <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);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD><EFBFBD><EFBFBD>ʱָ<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><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);
/**
* <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);
/**
* ֹͣЭ<EFBFBD>̵<EFBFBD><EFBFBD>ȹ<EFBFBD><EFBFBD><EFBFBD>
2018-11-30 14:38:22 +08:00
*/
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>
2018-11-30 14:38:22 +08:00
*/
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>
2018-11-30 14:38:22 +08:00
*/
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>
2018-11-30 14:38:22 +08:00
* @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>
2018-11-30 14:38:22 +08:00
*/
static unsigned int delay(unsigned int milliseconds);
/**
* <EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {unsigned}
*/
static unsigned alive_number(void);
/**
* <EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>״̬<EFBFBD><EFBFBD>Э<EFBFBD>̶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {unsigned}
*/
static unsigned dead_number(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><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Windows)
* @param yes {bool}
*/
static void set_non_blocking(bool yes);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ջģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD>Ĵ<EFBFBD>С,<EFBFBD>ڲ<EFBFBD>ȱʡֵΪ 1024000 <EFBFBD>ֽ<EFBFBD>
* @param size {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>С
*/
static void set_shared_stack_size(size_t size);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ջģʽ<EFBFBD>»<EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>С
* @return {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾδ<EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>ʽ
*/
static size_t get_shared_stack_size(void);
2018-11-30 14:38:22 +08:00
/**
2020-06-13 10:54:34 +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> HOOK IO API
2018-11-30 14:38:22 +08:00
*/
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>
2018-11-30 14:38:22 +08:00
*/
static void acl_io_unlock(void);
/**
* Windows ƽ̨<EFBFBD>¿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>ص<EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD> Hook һЩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ϵͳ API
* @return {bool}
*/
static bool winapi_hook(void);
2018-11-30 14:38:22 +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);
/**
* <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:
/**
* <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;
2020-06-13 14:33:08 +08:00
/**
* <EFBFBD>ײ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> C API <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD>
* @param fn {void (*)(ACL_FIBER*, void*)} Э<EFBFBD>̺<EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param ctx {void*} <EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD>Э<EFBFBD><EFBFBD>ִ<EFBFBD>к<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD>
* @param size {size_t} Э<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>С
*/
static void fiber_create(void (*fn)(ACL_FIBER*, void*),
void* ctx, size_t size);
2023-01-11 10:22:33 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Э<EFBFBD>̵Ķ<EFBFBD>ջ
* @param fb {const fiber&}
* @param out {std::vector<stack_frame>&} <EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param max {size_t} ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
static void stacktrace(const fiber& fb, std::vector<fiber_frame>& out,
size_t max = 50);
2018-11-30 14:38:22 +08:00
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>
2018-11-30 14:38:22 +08:00
*/
virtual void run(void);
private:
ACL_FIBER* f_;
fiber(const fiber&);
void operator = (const fiber&);
2018-11-30 14:38:22 +08:00
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>
2018-11-30 14:38:22 +08:00
*/
class FIBER_CPP_API fiber_timer
2018-11-30 14:38:22 +08:00
{
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>С
2018-11-30 14:38:22 +08:00
*/
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>
2018-11-30 14:38:22 +08:00
*/
virtual void run(void) = 0;
private:
ACL_FIBER* f_;
fiber_timer(const fiber_timer&);
void operator = (const fiber_timer&);
2018-11-30 14:38:22 +08:00
static void timer_callback(ACL_FIBER* f, void* ctx);
};
#if defined(ACL_CPP_API)
2018-11-30 14:38:22 +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_;
};
#endif // ACL_CPP_API
2018-11-30 14:38:22 +08:00
} // namespace acl