void EventBase::cancelTimeout(AsyncTimeout* obj) { assert(isInEventBaseThread()); struct event* ev = obj->getEvent(); if (EventUtil::isEventRegistered(ev)) { event_del(ev); } }
void EventBase::setName(const std::string& name) { assert(isInEventBaseThread()); name_ = name; if (isRunning()) { setThreadName(loopThread_.load(std::memory_order_relaxed), name_); } }
void EventBase::setName(const std::string& name) { assert(isInEventBaseThread()); name_ = name; #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12) if (isRunning()) { pthread_setname_np(loopThread_.load(std::memory_order_relaxed), name_.c_str()); } #endif }
void EventBase::runInLoop(const Cob& cob, bool thisIteration) { DCHECK(isInEventBaseThread()); Tr1FunctionLoopCallback* wrapper = new Tr1FunctionLoopCallback(cob); wrapper->context_ = RequestContext::saveContext(); if (runOnceCallbacks_ != nullptr && thisIteration) { runOnceCallbacks_->push_back(*wrapper); } else { loopCallbacks_.push_back(*wrapper); } }
void EventBase::runInLoop(LoopCallback* callback, bool thisIteration) { DCHECK(isInEventBaseThread()); callback->cancelLoopCallback(); callback->context_ = RequestContext::saveContext(); if (runOnceCallbacks_ != nullptr && thisIteration) { runOnceCallbacks_->push_back(*callback); } else { loopCallbacks_.push_back(*callback); } }
bool EventBase::scheduleTimeout(AsyncTimeout* obj, std::chrono::milliseconds timeout) { assert(isInEventBaseThread()); // Set up the timeval and add the event struct timeval tv; tv.tv_sec = timeout.count() / 1000LL; tv.tv_usec = (timeout.count() % 1000LL) * 1000LL; struct event* ev = obj->getEvent(); if (event_add(ev, &tv) < 0) { LOG(ERROR) << "EventBase: failed to schedule timeout: " << strerror(errno); return false; } return true; }
const std::string& EventBase::getName() { assert(isInEventBaseThread()); return name_; }
void EventBase::runBeforeLoop(LoopCallback* callback) { DCHECK(isInEventBaseThread()); callback->cancelLoopCallback(); runBeforeLoopCallbacks_.push_back(*callback); }
void EventBase::runOnDestruction(LoopCallback* callback) { DCHECK(isInEventBaseThread()); callback->cancelLoopCallback(); onDestructionCallbacks_.push_back(*callback); }
void RequestChannel::sendRequestAsync( apache::thrift::RpcOptions& rpcOptions, std::unique_ptr<apache::thrift::RequestCallback> callback, std::unique_ptr<apache::thrift::ContextStack> ctx, std::unique_ptr<folly::IOBuf> buf, std::shared_ptr<apache::thrift::transport::THeader> header, RpcKind kind) { auto eb = getEventBase(); if (!eb || eb->isInEventBaseThread()) { switch (kind) { case RpcKind::SINGLE_REQUEST_NO_RESPONSE: // Calling asyncComplete before sending because // sendOnewayRequest moves from ctx and clears it. ctx->asyncComplete(); sendOnewayRequest( rpcOptions, std::move(callback), std::move(ctx), std::move(buf), std::move(header)); break; case RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE: sendRequest( rpcOptions, std::move(callback), std::move(ctx), std::move(buf), std::move(header)); break; case RpcKind::SINGLE_REQUEST_STREAMING_RESPONSE: sendStreamRequest( rpcOptions, std::move(callback), std::move(ctx), std::move(buf), std::move(header)); break; default: folly::assume_unreachable(); break; } } else { switch (kind) { case RpcKind::SINGLE_REQUEST_NO_RESPONSE: eb->runInEventBaseThread([this, rpcOptions, callback = std::move(callback), ctx = std::move(ctx), buf = std::move(buf), header = std::move(header)]() mutable { // Calling asyncComplete before sending because // sendOnewayRequest moves from ctx and clears it. ctx->asyncComplete(); sendOnewayRequest( rpcOptions, std::move(callback), std::move(ctx), std::move(buf), std::move(header)); }); break; case RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE: eb->runInEventBaseThread([this, rpcOptions, callback = std::move(callback), ctx = std::move(ctx), buf = std::move(buf), header = std::move(header)]() mutable { sendRequest( rpcOptions, std::move(callback), std::move(ctx), std::move(buf), std::move(header)); }); break; case RpcKind::SINGLE_REQUEST_STREAMING_RESPONSE: eb->runInEventBaseThread([this, rpcOptions, callback = std::move(callback), ctx = std::move(ctx), buf = std::move(buf), header = std::move(header)]() mutable { sendStreamRequest( rpcOptions, std::move(callback), std::move(ctx), std::move(buf), std::move(header)); }); break; default: folly::assume_unreachable(); break; } } }