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

313 lines
6.4 KiB
C++
Raw Normal View History

#pragma once
2016-12-08 13:13:30 +08:00
#include <stddef.h>
struct ACL_FIBER;
namespace acl {
/**
*
*/
class fiber
{
public:
/**
*
* @param running {bool} true
* 便 ACL_FIBER
* start ; false start
*
*/
fiber(bool running = false);
virtual ~fiber(void);
/**
* running false
* run running true
* start
* @param stack_size {size_t}
*/
2016-08-19 22:31:05 +08:00
void start(size_t stack_size = 320000);
/**
* 退
* @return {bool} false 退
*/
bool kill(void);
/**
* 退
* @return {bool} 退
*/
bool killed(void) const;
/**
* 退 killed
* killed acl::fiber
* self_killed acl::fiber
*
* @return {bool}
*/
static bool self_killed(void);
/**
* ID
* @return {unsigned int}
*/
unsigned int get_id(void) const;
/**
* ID
* @return {unsigned int}
*/
static unsigned int self(void);
/**
* API
* return {int}
*/
int get_errno(void) const;
/**
*
* @param errnum {int}
*/
void set_errno(int errnum);
/**
*
*/
static void schedule(void);
2017-04-11 23:14:39 +08:00
/**
* 线
* @return {bool}
*/
static bool scheduled(void);
/**
*
*/
2016-09-22 22:50:45 +08:00
static void schedule_stop(void);
public:
/**
* ()
*/
static void yield(void);
/**
*
*/
static void switch_to_next(void);
/**
*
* @param f {fiber&}
*/
static void ready(fiber& f);
/**
* 使
* @param milliseconds {unsigned int}
* @return {unsigned int}
*/
static unsigned int delay(unsigned int milliseconds);
/**
* 线线 hook API
* hook API
* @param on {bool}
*/
static void hook_api(bool on);
/**
*
* @return {int}
*/
static int get_sys_errno(void);
/**
*
* @param errnum {int}
*/
static void set_sys_errno(int errnum);
public:
/**
* C
2017-08-25 13:34:48 +08:00
* @return {ACL_FIBER* }
*/
2017-08-25 13:34:48 +08:00
ACL_FIBER* get_fiber(void) const;
protected:
/**
* start
* ;
* running true start
*/
virtual void run(void);
private:
2017-08-25 13:34:48 +08:00
ACL_FIBER* f_;
2017-08-25 13:34:48 +08:00
static void fiber_callback(ACL_FIBER* f, void* ctx);
};
/**
*
*/
class fiber_timer
{
public:
fiber_timer(void);
virtual ~fiber_timer(void) {}
/**
*
* @param milliseconds {unsigned int}
* @param stack_size {size_t}
*/
void start(unsigned int milliseconds, size_t stack_size = 320000);
protected:
/**
*
*/
virtual void run(void) = 0;
private:
ACL_FIBER* f_;
static void timer_callback(ACL_FIBER* f, void* ctx);
};
/**
*
*/
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 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