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

390 lines
8.9 KiB
C++
Raw Normal View History

#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;
/**
*
2018-11-30 14:38:22 +08:00
*/
class FIBER_CPP_API fiber : public noncopyable
2018-11-30 14:38:22 +08:00
{
public:
/**
*
* @param running {bool} true
* 便 ACL_FIBER
* start ; false start
*
2018-11-30 14:38:22 +08:00
*/
fiber(bool running = false);
virtual ~fiber(void);
/**
* running false
* run running true
* start
* @param stack_size {size_t}
2018-11-30 14:38:22 +08:00
*/
void start(size_t stack_size = 320000);
/**
* 退
* @return {bool} false 退
2018-11-30 14:38:22 +08:00
*/
bool kill(void);
/**
* 退
* @return {bool} 退
2018-11-30 14:38:22 +08:00
*/
bool killed(void) const;
/**
* 退 killed
* killed acl::fiber
* self_killed acl::fiber
*
2018-11-30 14:38:22 +08:00
* @return {bool}
*/
static bool self_killed(void);
/**
* ID
2018-11-30 14:38:22 +08:00
* @return {unsigned int}
*/
unsigned int get_id(void) const;
/**
* ID
2018-11-30 14:38:22 +08:00
* @return {unsigned int}
*/
static unsigned int self(void);
/**
* API
2018-11-30 14:38:22 +08:00
* return {int}
*/
int get_errno(void) const;
/**
*
2018-11-30 14:38:22 +08:00
* @param errnum {int}
*/
void set_errno(int errnum);
2018-11-30 18:26:46 +08:00
/**
*
2018-11-30 18:26:46 +08:00
* @return {const char*}
*/
static const char* last_serror(void);
/**
*
2018-11-30 18:26:46 +08:00
* @return {int}
*/
static int last_error(void);
/**
*
* @param errnum {int}
* @param buf {char*}
* @param size {size_t} buf
* @return {const char*} buf
2018-11-30 18:26:46 +08:00
*/
static const char* strerror(int errnum, char* buf, size_t size);
/**
*
* @param on {bool} true
2018-11-30 18:26:46 +08:00
*/
static void stdout_open(bool on);
2018-11-30 14:38:22 +08:00
/**
*
* schedule schedule_with
* @param type {fiber_event_t} FIBER_EVENT_T_XXX
* @param schedule_auto {bool} true
* schedule/schedule_with
* schedule
* schedule_with false
2018-11-30 14:38:22 +08:00
*/
static void init(fiber_event_t type, bool schedule_auto = false);
/**
*
2018-11-30 14:38:22 +08:00
*/
static void schedule(void);
/**
* schedule_init
* schedule
* @param type {fiber_event_t} FIBER_EVENT_T_XXX
2018-11-30 14:38:22 +08:00
*/
static void schedule_with(fiber_event_t type);
/**
* 线
2018-11-30 14:38:22 +08:00
* @return {bool}
*/
static bool scheduled(void);
/**
*
2018-11-30 14:38:22 +08:00
*/
static void schedule_stop(void);
public:
/**
* ()
2018-11-30 14:38:22 +08:00
*/
static void yield(void);
/**
*
2018-11-30 14:38:22 +08:00
*/
static void switch_to_next(void);
/**
*
2018-11-30 14:38:22 +08:00
* @param f {fiber&}
*/
static void ready(fiber& f);
/**
* 使
* @param milliseconds {unsigned int}
* @return {unsigned int}
2018-11-30 14:38:22 +08:00
*/
static unsigned int delay(unsigned int milliseconds);
/**
*
* @return {unsigned}
*/
static unsigned alive_number(void);
/**
* 退
* @return {unsigned}
*/
static unsigned dead_number(void);
2018-11-30 14:38:22 +08:00
/**
* 线线 hook API
* hook API
2018-11-30 14:38:22 +08:00
* @param on {bool}
*/
static void hook_api(bool on);
/**
* 使 acl IO UNIX
* HOOK IO API
2018-11-30 14:38:22 +08:00
*/
static void acl_io_hook(void);
/**
* acl基础库中的 IO
2018-11-30 14:38:22 +08:00
*/
static void acl_io_unlock(void);
/**
*
2018-11-30 14:38:22 +08:00
* @return {int}
*/
static int get_sys_errno(void);
/**
*
2018-11-30 14:38:22 +08:00
* @param errnum {int}
*/
static void set_sys_errno(int errnum);
public:
/**
* C
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER* }
*/
ACL_FIBER* get_fiber(void) const;
protected:
/**
* start
* ;
* running true start
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);
};
/**
*
2018-11-30 14:38:22 +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) {}
/**
*
* @param milliseconds {unsigned int}
* @param stack_size {size_t}
2018-11-30 14:38:22 +08:00
*/
void start(unsigned int milliseconds, size_t stack_size = 320000);
protected:
/**
*
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);
};
/**
*
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