acl  3.5.3.0
go_fiber.hpp
浏览该文件的文档.
1 #pragma once
2 #include "fiber_cpp_define.hpp"
3 #include <thread>
4 #include <functional>
5 #include "fiber.hpp"
6 #include "fiber_tbox.hpp"
7 
8 struct ACL_FIBER;
9 
10 namespace acl {
11 
13 {
14 public:
15  fiber_ctx(std::function<void()> fn) {
16  fn_ = std::move(fn);
17  }
18 
19  ~fiber_ctx() = default;
20 
21  std::function<void()> fn_;
22 };
23 
24 #define go acl::go_fiber()>
25 #define go_stack(size) acl::go_fiber(size)>
26 
27 #define go_wait_fiber acl::go_fiber()<
28 #define go_wait_thread acl::go_fiber()<<
29 #define go_wait go_wait_thread
30 
32 {
33 public:
34  go_fiber(void) {}
35  go_fiber(size_t stack_size) : stack_size_(stack_size) {}
36 
37  void operator > (std::function<void()> fn)
38  {
39  fiber_ctx* ctx = new fiber_ctx(fn);
40  fiber::fiber_create(fiber_main, (void*) ctx, stack_size_);
41  }
42 
43  void operator < (std::function<void()> fn)
44  {
45  fiber_tbox<int> box;
46 
47  go[&] {
48  fn();
49  box.push(NULL);
50  };
51  (void) box.pop();
52  }
53 
54  void operator << (std::function<void()> fn)
55  {
56  fiber_tbox<int> box;
57 
58  std::thread thread([&]() {
59  fn();
60  box.push(NULL);
61  });
62 
63  thread.detach();
64  (void) box.pop();
65  }
66 
67 private:
68  size_t stack_size_ = 320000;
69 
70  static void fiber_main(ACL_FIBER*, void* ctx)
71  {
72  fiber_ctx* fc = (fiber_ctx *) ctx;
73  std::function<void()> fn = fc->fn_;
74  delete fc;
75 
76  fn();
77  }
78 };
79 
80 } // namespace acl
81 
82 /**
83  * static void fiber1(void)
84  * {
85  * printf("fiber: %d\r\n", acl::fiber::self());
86  * }
87  *
88  * static void fiber2(acl::string& buf)
89  * {
90  * printf("in fiber: %d, buf: %s\r\n", acl::fiber::self(), buf.c_str());
91  * buf = "world";
92  * }
93  *
94  * static void fiber3(const acl::string& buf)
95  * {
96  * printf("in fiber: %d, buf: %s\r\n", acl::fiber::self(), buf.c_str());
97  * }
98  *
99  * static void incr(int& n)
100  * {
101  * n++;
102  * }
103  *
104  * static void waiter(void)
105  * {
106  * int n = 100;
107  *
108  * // run in thread and wait for result
109  * go_wait_thread[&] { incr(n); };
110  * // here: n should be 101
111  *
112  * n = 200;
113  *
114  * // run in fiber and wait for result
115  * go_wait_fiber[&] { incr(n); };
116  * // here: n should be 201
117  * }
118  *
119  * static test(void)
120  * {
121  * go fiber1;
122  *
123  * acl::string buf("hello");
124  *
125  * go[&] {
126  * fiber2(buf);
127  * };
128  *
129  * go[=] {
130  * fiber3(buf);
131  * };
132  *
133  * go[&] {
134  * fiber3(buf);
135  * };
136  * }
137  */
#define go
Definition: go_fiber.hpp:24
go_fiber(size_t stack_size)
Definition: go_fiber.hpp:35
go_fiber(void)
Definition: go_fiber.hpp:34
T * pop(int wait_ms=-1, bool *found=NULL)
Definition: fiber_tbox.hpp:127
#define FIBER_CPP_API
static void fiber_create(void(*fn)(ACL_FIBER *, void *), void *ctx, size_t size)
fiber_ctx(std::function< void()> fn)
Definition: go_fiber.hpp:15
std::function< void()> fn_
Definition: go_fiber.hpp:21
bool push(T *t, bool notify_first=true)
Definition: fiber_tbox.hpp:84
struct ACL_FIBER ACL_FIBER
Definition: fiber_define.h:100