Fiber(): main id = 0 Fiber(): child id = 1 Scheduler::Scheduler() success Schedule::run() starts in thread: 12156 Scheduler::start() success Fiber(): main id = 2 Fiber(): child id = 3 MainFunc start in Fiber id: 3 Scheduler::idle(), sleeping in thread : 12156 idle() : yield() in Fiber id3 Scheduler::idle(), sleeping in thread : 12156
Fiber(): child id = 4 Fiber(): child id = 5 Fiber(): child id = 6 idle() : yield() in Fiber id3 MainFunc start in Fiber id: 4 task 0 is under processing in thread: 12156 Fiber::MainFunc() : yield() in Fiber id : 4 ~Fiber(): id = 4 MainFunc start in Fiber id: 5 task 1 is under processing in thread: 12156 Fiber::MainFunc() : yield() in Fiber id : 5 ~Fiber(): id = 5 ...
post again
Fiber(): child id = 7 Fiber(): child id = 8 Fiber(): child id = 9 idle() : yield() in Fiber id3 MainFunc start in Fiber id: 7 ... Schedule::stop() starts in thread: 12155 MainFunc start in Fiber id: 1 Schedule::run() starts in thread: 12155 Fiber(): child id = 10
MainFunc start in Fiber id: 10 Scheduler::idle(), sleeping in thread : 12155 Fiber::MainFunc() : yield() in Fiber id : 9 ~Fiber(): id = 9 Fiber::MainFunc() : yield() in Fiber id : 3 Schedule::run() ends in thread: 12156 ~Fiber(): id = 3 ~Fiber(): id = 2
idle() : yield() in Fiber id10 Fiber::MainFunc() : yield() in Fiber id : 10 Schedule::run() ends in thread: 12155 ~Fiber(): id = 10 Fiber::MainFunc() : yield() in Fiber id : 1 m_schedulerFiber ends in thread:12155 Schedule::stop() ends in thread:12155 Scheduler::~Scheduler() success ~Fiber(): id = 1 ~Fiber(): id = 0
Fiber(): main id = 0 Fiber(): child id = 1 Scheduler::Scheduler() success Schedule::run() starts in thread: 33052 Fiber(): main id = 2 Fiber(): child id = 3 Scheduler::start() success addevent fd: 6 event: 4 addevent fd: 6 event: 1 event has been posted
IOManaer::tickle() Fiber(): child id = 5 MainFunc start in Fiber id: 5 Fiber::MainFunc() : yield() in Fiber id : 5 ~Fiber(): id = 5 IOManager::idle(), run in thread: 33052 IOManager::idle: receive tickle data IOManager::idle(), run in thread: 33052 triggerEvent() IOManaer::tickle() Fiber(): child id = 6 MainFunc start in Fiber id: 6 HTTP/1.0 200 OK Accept-Ranges: bytes Cache-Control: no-cache Content-Length: 29524 Content-Type: text/html …… ack
Fiber::MainFunc() : yield() in Fiber id : 6 Fiber(): id = 6 IOManager::idle(), run in thread: 33051 name = IOManageridle exits in thread: 33051 Fiber::MainFunc() : yield() in Fiber id : 4 Schedule::run() ends in thread: 33051 ~Fiber(): id = 4 Fiber::MainFunc() : yield() in Fiber id : 1 m_schedulerFiber ends in thread:33051 IOManager::idle: receive tickle data IOManager::idle(), run in thread: 33052 name = IOManageridle exits in thread: 33052 Fiber::MainFunc() : yield() in Fiber id : 3 Schedule::run() ends in thread: 33052 ~Fiber(): id = 3 ~Fiber(): id = 2 Schedule::stop() ends in thread:33051 Scheduler::Scheduler() success ~Fiber(): id = 1 ~Fiber(): id = 0
void *dlsym(void *handle, const char *symbol); /* RTLD_NEXT Find the next occurrence of the desired symbol in the search order after the current object. This allows one to provide a wrapper around a function in another shared object, so that, for example, the definition of a function in a preloaded shared object (see LD_PRELOAD in ld.so(8)) can find and invoke the "real" function provided in another shared object (or for that matter, the "next" definition of the function in cases where there are multiple layers of preloading). RTLD_NEXT找出另一个同一个全局符号的下一个实现*/
Fiber(): main id = 0 Fiber(): child id = 1 Scheduler::Scheduler() success Scheduler::start() success Schedule::stop() starts in thread: 49317 MainFunc start in Fiber id: 1 Schedule::run() starts in thread: 49317 Fiber(): child id = 2 Fiber(): child id = 3 MainFunc start in Fiber id: 3 addevent fd: 6 event: 4 MainFunc start in Fiber id: 2 IOManager::idle(), run in thread: 49317 IOManager::idle(), run in thread: 49317 triggerEvent() IOManaer::tickle() connected send success addevent fd: 6 event: 1 fiber 3yield() IOManager::idle(), run in thread: 49317 IOManager::idle: receive tickle data IOManager::idle(), run in thread: 49317 triggerEvent() IOManaer::tickle() recv success HTTP/1.0 2 Fiber::MainFunc() : yield() in Fiber id : 3 Fiber(): id = 3 IOManager::idle(), run in thread: 49317 name = IOManageridle exits in thread: 49317 Fiber::MainFunc() : yield() in Fiber id : 2 Schedule::run() ends in thread: 49317 ~Fiber(): id = 2 Fiber::MainFunc() : yield() in Fiber id : 1 m_schedulerFiber ends in thread:49317 Schedule::stop() ends in thread:49317 Scheduler::Scheduler() success ~Fiber(): id = 1 ~Fiber(): id = 0
// universal template for read and write function template <typename OriginFun, typename... Args> staticssize_tdo_io(int fd, OriginFun fun, constchar *hook_fun_name, uint32_t event, int timeout_so, Args &&...args) { if (!mycoro::t_hook_enable) { returnfun(fd, std::forward<Args>(args)...); }
std::shared_ptr<mycoro::FdCtx> ctx = mycoro::FdMgr::GetInstance()->get(fd); if (!ctx) { returnfun(fd, std::forward<Args>(args)...); }
if (ctx->isClosed()) { errno = EBADF; return-1; }
if (!ctx->isSocket() || ctx->getUserNonblock()) { returnfun(fd, std::forward<Args>(args)...); }
// get the timeout uint64_t timeout = ctx->getTimeout(timeout_so); // timer condition std::shared_ptr<timer_info> tinfo(new timer_info);
retry: // run the function ssize_t n = fun(fd, std::forward<Args>(args)...);
// EINTR ->Operation interrupted by system ->retry while (n == -1 && errno == EINTR) { n = fun(fd, std::forward<Args>(args)...); }
// 0 resource was temporarily unavailable -> retry until ready if (n == -1 && errno == EAGAIN) { mycoro::IOManager *iom = mycoro::IOManager::GetThis(); // timer std::shared_ptr<mycoro::Timer> timer; std::weak_ptr<timer_info> winfo(tinfo);
// 1 timeout has been set -> add a conditional timer for canceling this operation if (timeout != (uint64_t)-1) { timer = iom->addConditionTimer(timeout, [winfo, fd, iom, event]() { auto t = winfo.lock(); if(!t || t->cancelled) { return; } t->cancelled = ETIMEDOUT; // cancel this event and trigger once to return to this fiber iom->cancelEvent(fd, (mycoro::IOManager::Event)(event)); }, winfo); }
// 2 add event -> callback is this fiber int rt = iom->addEvent(fd, (mycoro::IOManager::Event)(event)); if (rt) { std::cout << hook_fun_name << " addEvent(" << fd << ", " << event << ")"; if (timer) { timer->cancel(); } return-1; } else { mycoro::Fiber::GetThis()->yield();
// 3 resume either by addEvent or cancelEvent if (timer) { timer->cancel(); } // by cancelEvent if (tinfo->cancelled == ETIMEDOUT) { errno = tinfo->cancelled; return-1; } goto retry; } } return n; }