bool EventBase::runInEventBaseThread(const Cob& fn) { // Short-circuit if we are already in our event base if (inRunningEventBaseThread()) { runInLoop(fn); return true; } Cob* fnCopy; // Allocate a copy of the function so we can pass it to the other thread // The other thread will delete this copy once the function has been run try { fnCopy = new Cob(fn); } catch (const std::bad_alloc& ex) { LOG(ERROR) << "failed to allocate tr::function copy " << "for runInEventBaseThread()"; return false; } if (!runInEventBaseThread(&EventBase::runTr1FunctionPtr, fnCopy)) { delete fnCopy; return false; } return true; }
void McServerSession::queueWrite( std::unique_ptr<McServerTransaction> ptransaction) { DestructorGuard dg(this); if (ptransaction->noReply()) { return; } /* TODO: the subrequests should simply add to the parent's request iovs, so that we only write once */ ptransaction->queueSubRequestsWrites(); auto& transaction = pendingWrites_.pushBack(std::move(ptransaction)); if (options_.singleWrite) { if (!transaction.prepareWrite()) { transport_->close(); return; } transport_->writev(this, transaction.iovs_, transaction.niovs_); if (!pendingWrites_.empty()) { /* We only need to pause if the sendmsg() call didn't write everything in one go */ pause(PAUSE_WRITE); } } else { if (!writeScheduled_) { auto eventBase = transport_->getEventBase(); CHECK(eventBase != nullptr); eventBase->runInLoop(&sendWritesCallback_, /* thisIteration= */ true); writeScheduled_ = true; } } }
bool EventBase::runInEventBaseThread(void (*fn)(void*), void* arg) { // Send the message. // It will be received by the FunctionRunner in the EventBase's thread. // We try not to schedule nullptr callbacks if (!fn) { LOG(ERROR) << "EventBase " << this << ": Scheduling nullptr callbacks is not allowed"; return false; } // Short-circuit if we are already in our event base if (inRunningEventBaseThread()) { runInLoop(new RunInLoopCallback(fn, arg)); return true; } try { queue_->putMessage(std::make_pair(fn, arg)); } catch (const std::exception& ex) { LOG(ERROR) << "EventBase " << this << ": failed to schedule function " << fn << "for EventBase thread: " << ex.what(); return false; } return true; }
void EventBase::runInEventBaseThread(Func fn) noexcept { // Send the message. // It will be received by the FunctionRunner in the EventBase's thread. // We try not to schedule nullptr callbacks if (!fn) { DLOG(FATAL) << "EventBase " << this << ": Scheduling nullptr callbacks is not allowed"; return; } // Short-circuit if we are already in our event base if (inRunningEventBaseThread()) { runInLoop(std::move(fn)); return; } queue_->putMessage(std::move(fn)); }