|
| 1 | +#include "stdafx.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +class wait_box |
| 6 | +{ |
| 7 | +public: |
| 8 | + wait_box(void) { |
| 9 | + refer_ = 2; |
| 10 | + } |
| 11 | + |
| 12 | + void done(void) { |
| 13 | + box_.push(NULL); |
| 14 | + if (--refer_ == 0) { |
| 15 | + delete this; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + void wait(void) { |
| 20 | + box_.pop(); |
| 21 | + if (--refer_ == 0) { |
| 22 | + delete this; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | +private: |
| 27 | + acl::atomic_long refer_; |
| 28 | + acl::fiber_tbox<bool> box_; |
| 29 | + |
| 30 | + ~wait_box(void) {} |
| 31 | +}; |
| 32 | + |
| 33 | +class fiber_waiter : public acl::fiber |
| 34 | +{ |
| 35 | +public: |
| 36 | + fiber_waiter(acl::fiber_tbox<bool>& box) : box_(box) {} |
| 37 | + ~fiber_waiter(void) {} |
| 38 | + |
| 39 | +private: |
| 40 | + acl::fiber_tbox<bool>& box_; |
| 41 | + |
| 42 | + // @override |
| 43 | + void run(void) { |
| 44 | + box_.pop(); |
| 45 | + printf("fiber_waiter got one stop messasge\r\n"); |
| 46 | + acl::fiber::schedule_stop(); |
| 47 | + delete this; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +class thread_runner : public acl::thread |
| 52 | +{ |
| 53 | +public: |
| 54 | + thread_runner(acl::fiber_event_t event_type) |
| 55 | + : event_type_(event_type) |
| 56 | + { |
| 57 | + wait_box_ = new wait_box; |
| 58 | + } |
| 59 | + |
| 60 | + void stop(void) { |
| 61 | + printf("send stop message to stop_box\r\n"); |
| 62 | + stop_box_.push(NULL); |
| 63 | + printf("wait for runner finish\r\n"); |
| 64 | + wait_box_->wait(); |
| 65 | + printf("runner finished\r\n"); |
| 66 | + } |
| 67 | + |
| 68 | +protected: |
| 69 | + ~thread_runner(void) {} |
| 70 | + |
| 71 | + // @override |
| 72 | + void* run(void) { |
| 73 | + acl::fiber* fb = new fiber_waiter(stop_box_); |
| 74 | + fb->start(); |
| 75 | + |
| 76 | + acl::fiber::schedule_with(event_type_); |
| 77 | + printf("fiber schedule stopped\r\n"); |
| 78 | + wait_box_->done(); |
| 79 | + printf("runner will exit\r\n"); |
| 80 | + |
| 81 | + delete this; |
| 82 | + return NULL; |
| 83 | + } |
| 84 | + |
| 85 | +private: |
| 86 | + acl::fiber_event_t event_type_; |
| 87 | + acl::fiber_tbox<bool> stop_box_; |
| 88 | + wait_box* wait_box_; |
| 89 | +}; |
| 90 | + |
| 91 | +class thread_stop : public acl::thread |
| 92 | +{ |
| 93 | +public: |
| 94 | + thread_stop(thread_runner& runner) : runner_(runner) {} |
| 95 | + ~thread_stop(void) {} |
| 96 | + |
| 97 | +private: |
| 98 | + thread_runner& runner_; |
| 99 | + |
| 100 | + // @override |
| 101 | + void* run(void) { |
| 102 | + printf("thread_stop: sleep one second\r\n"); |
| 103 | + sleep(1); |
| 104 | + printf("thread_stop: stop the runner thread\r\n"); |
| 105 | + runner_.stop(); |
| 106 | + printf("thread_stop: stop the runner ok\r\n"); |
| 107 | + return NULL; |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +////////////////////////////////////////////////////////////////////////////// |
| 112 | + |
| 113 | +static void usage(const char* procname) |
| 114 | +{ |
| 115 | + printf("usage: %s -h [help]\r\n" |
| 116 | + " -e event_type[kernel|poll|select|io_uring]\r\n" |
| 117 | + , procname); |
| 118 | +} |
| 119 | + |
| 120 | +int main(int argc, char *argv[]) |
| 121 | +{ |
| 122 | + int ch; |
| 123 | + acl::fiber_event_t event_type = acl::FIBER_EVENT_T_KERNEL; |
| 124 | + |
| 125 | + acl::acl_cpp_init(); |
| 126 | + acl::log::stdout_open(true); |
| 127 | + acl::fiber::stdout_open(true); |
| 128 | + |
| 129 | + while ((ch = getopt(argc, argv, "he:")) > 0) { |
| 130 | + switch (ch) { |
| 131 | + case 'h': |
| 132 | + usage(argv[0]); |
| 133 | + return 0; |
| 134 | + case 'e': |
| 135 | + if (strcasecmp(optarg, "kernel") == 0) { |
| 136 | + event_type = acl::FIBER_EVENT_T_KERNEL; |
| 137 | + } else if (strcasecmp(optarg, "select") == 0) { |
| 138 | + event_type = acl::FIBER_EVENT_T_SELECT; |
| 139 | + } else if (strcasecmp(optarg, "poll") == 0) { |
| 140 | + event_type = acl::FIBER_EVENT_T_POLL; |
| 141 | + } else if (strcasecmp(optarg, "io_uring") == 0) { |
| 142 | + event_type = acl::FIBER_EVENT_T_IO_URING; |
| 143 | + } |
| 144 | + break; |
| 145 | + default: |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + thread_runner* runner = new thread_runner(event_type); |
| 151 | + runner->set_detachable(true); |
| 152 | + runner->start(); |
| 153 | + |
| 154 | + acl::thread* thr = new thread_stop(*runner); |
| 155 | + thr->set_detachable(false); |
| 156 | + thr->start(); |
| 157 | + thr->wait(); |
| 158 | + delete thr; |
| 159 | + |
| 160 | + return 0; |
| 161 | +} |
0 commit comments