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

235 lines
5.1 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);
/**
* 线线 hook API
* hook API
* @param on {bool}
*/
static void hook_api(bool on);
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);
};
} // 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