//----------------------------------------------------------------------------- // 描述: 在事件循环线程中立即执行指定的仿函数 // 备注: 线程安全 //----------------------------------------------------------------------------- void EventLoop::executeInLoop(const Functor& functor) { if (isInLoopThread()) functor(); else delegateToLoop(functor); }
void Evloop::runInLoop(PendFuncType &&cb) { if(isInLoopThread()) cb(); else queueInLoop(cb); }
void Evloop::queueInLoop(PendFuncType &&cb) { { std::lock_guard<std::mutex> guard(mutex_); pendfctrList_.push_back(cb); } if(!isInLoopThread() || inHandingPendingFctrs_) wake(); }
uint64_t EventLoop::runAfter(double interval, event_callback_fn cb_func, void *arg) { if (isInLoopThread()) { uint64_t timerId = newTimerId(); installTimerEvent(timerId, interval, cb_func, 1, arg); return timerId; } else { uint64_t timerId = newTimerId(); Closure<void> *pCb = NewClosure(this, &EventLoop::installTimerEvent, uint64_t(timerId), \ double(interval), event_callback_fn(cb_func), int(1), (void*)(arg)); { SpinLockGuard lock(lock_); pendingFunctors_.push_back(pCb); } wakeup(); return timerId; } }
//----------------------------------------------------------------------------- // 描述: 确保当前调用在事件循环线程内 //----------------------------------------------------------------------------- void EventLoop::assertInLoopThread() { ISE_ASSERT(isInLoopThread()); }