Commit 7697ddef authored by Dave Watson's avatar Dave Watson

EventBase cleanup

Summary: Moar c++11 features.  marccelani's comments in D1195393, but in a separate diff.

Test Plan:
contbuild
fbconfig thrift/lib/cpp/test/
fbmake runtests

Reviewed By: rajat@fb.com

FB internal diff: D1219158
parent 10a36321
/* /*
* Licensed to the Apache Software Foundation (ASF) under one * Copyright 2014 Facebook, Inc.
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information * Licensed under the Apache License, Version 2.0 (the "License");
* regarding copyright ownership. The ASF licenses this file * you may not use this file except in compliance with the License.
* to you under the Apache License, Version 2.0 (the * You may obtain a copy of the License at
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing, software
* software distributed under the License is distributed on an * distributed under the License is distributed on an "AS IS" BASIS,
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* KIND, either express or implied. See the License for the * See the License for the specific language governing permissions and
* specific language governing permissions and limitations * limitations under the License.
* under the License.
*/ */
#ifndef __STDC_FORMAT_MACROS #ifndef __STDC_FORMAT_MACROS
...@@ -35,9 +32,13 @@ namespace { ...@@ -35,9 +32,13 @@ namespace {
using folly::Cob; using folly::Cob;
using folly::EventBase; using folly::EventBase;
class Tr1FunctionLoopCallback : public EventBase::LoopCallback { template <typename Callback>
class FunctionLoopCallback : public EventBase::LoopCallback {
public: public:
explicit Tr1FunctionLoopCallback(const Cob& function) explicit FunctionLoopCallback(Cob&& function)
: function_(std::move(function)) {}
explicit FunctionLoopCallback(const Cob& function)
: function_(function) {} : function_(function) {}
virtual void runLoopCallback() noexcept { virtual void runLoopCallback() noexcept {
...@@ -46,7 +47,7 @@ class Tr1FunctionLoopCallback : public EventBase::LoopCallback { ...@@ -46,7 +47,7 @@ class Tr1FunctionLoopCallback : public EventBase::LoopCallback {
} }
private: private:
Cob function_; Callback function_;
}; };
} }
...@@ -206,12 +207,12 @@ void EventBase::resetLoadAvg(double value) { ...@@ -206,12 +207,12 @@ void EventBase::resetLoadAvg(double value) {
maxLatencyLoopTime_.reset(value); maxLatencyLoopTime_.reset(value);
} }
static int64_t getTimeDelta(int64_t *prev) { static std::chrono::milliseconds
int64_t now = std::chrono::duration_cast<std::chrono::milliseconds>( getTimeDelta(std::chrono::steady_clock::time_point* prev) {
std::chrono::steady_clock::now().time_since_epoch()).count(); auto result = std::chrono::steady_clock::now() - *prev;
int64_t delta = now - *prev; *prev = std::chrono::steady_clock::now();
*prev = now;
return delta; return std::chrono::duration_cast<std::chrono::milliseconds>(result);
} }
void EventBase::waitUntilRunning() { void EventBase::waitUntilRunning() {
...@@ -235,8 +236,7 @@ bool EventBase::loop() { ...@@ -235,8 +236,7 @@ bool EventBase::loop() {
} }
#endif #endif
int64_t prev = std::chrono::duration_cast<std::chrono::milliseconds>( auto prev = std::chrono::steady_clock::now();
std::chrono::steady_clock::now().time_since_epoch()).count();
int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>( int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count(); std::chrono::steady_clock::now().time_since_epoch()).count();
...@@ -300,7 +300,8 @@ bool EventBase::loop() { ...@@ -300,7 +300,8 @@ bool EventBase::loop() {
} }
} }
VLOG(5) << "EventBase " << this << " loop time: " << getTimeDelta(&prev); VLOG(5) << "EventBase " << this << " loop time: " <<
getTimeDelta(&prev).count();
} }
// Reset stop_ so loop() can be called again // Reset stop_ so loop() can be called again
stop_ = false; stop_ = false;
...@@ -401,7 +402,18 @@ void EventBase::runInLoop(LoopCallback* callback, bool thisIteration) { ...@@ -401,7 +402,18 @@ void EventBase::runInLoop(LoopCallback* callback, bool thisIteration) {
void EventBase::runInLoop(const Cob& cob, bool thisIteration) { void EventBase::runInLoop(const Cob& cob, bool thisIteration) {
DCHECK(isInEventBaseThread()); DCHECK(isInEventBaseThread());
Tr1FunctionLoopCallback* wrapper = new Tr1FunctionLoopCallback(cob); auto wrapper = new FunctionLoopCallback<Cob>(cob);
wrapper->context_ = RequestContext::saveContext();
if (runOnceCallbacks_ != nullptr && thisIteration) {
runOnceCallbacks_->push_back(*wrapper);
} else {
loopCallbacks_.push_back(*wrapper);
}
}
void EventBase::runInLoop(Cob&& cob, bool thisIteration) {
DCHECK(isInEventBaseThread());
auto wrapper = new FunctionLoopCallback<Cob>(std::move(cob));
wrapper->context_ = RequestContext::saveContext(); wrapper->context_ = RequestContext::saveContext();
if (runOnceCallbacks_ != nullptr && thisIteration) { if (runOnceCallbacks_ != nullptr && thisIteration) {
runOnceCallbacks_->push_back(*wrapper); runOnceCallbacks_->push_back(*wrapper);
...@@ -457,7 +469,7 @@ bool EventBase::runInEventBaseThread(const Cob& fn) { ...@@ -457,7 +469,7 @@ bool EventBase::runInEventBaseThread(const Cob& fn) {
return false; return false;
} }
if (!runInEventBaseThread(&EventBase::runTr1FunctionPtr, fnCopy)) { if (!runInEventBaseThread(&EventBase::runFunctionPtr, fnCopy)) {
delete fnCopy; delete fnCopy;
return false; return false;
} }
...@@ -567,7 +579,7 @@ bool EventBase::nothingHandledYet() { ...@@ -567,7 +579,7 @@ bool EventBase::nothingHandledYet() {
} }
/* static */ /* static */
void EventBase::runTr1FunctionPtr(Cob* fn) { void EventBase::runFunctionPtr(Cob* fn) {
// The function should never throw an exception, because we have no // The function should never throw an exception, because we have no
// way of knowing what sort of error handling to perform. // way of knowing what sort of error handling to perform.
// //
......
/* /*
* Licensed to the Apache Software Foundation (ASF) under one * Copyright 2014 Facebook, Inc.
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information * Licensed under the Apache License, Version 2.0 (the "License");
* regarding copyright ownership. The ASF licenses this file * you may not use this file except in compliance with the License.
* to you under the Apache License, Version 2.0 (the * You may obtain a copy of the License at
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing, software
* software distributed under the License is distributed on an * distributed under the License is distributed on an "AS IS" BASIS,
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* KIND, either express or implied. See the License for the * See the License for the specific language governing permissions and
* specific language governing permissions and limitations * limitations under the License.
* under the License.
*/ */
#pragma once #pragma once
...@@ -232,6 +229,8 @@ class EventBase : private boost::noncopyable, public TimeoutManager { ...@@ -232,6 +229,8 @@ class EventBase : private boost::noncopyable, public TimeoutManager {
*/ */
void runInLoop(const Cob& c, bool thisIteration = false); void runInLoop(const Cob& c, bool thisIteration = false);
void runInLoop(Cob&& c, bool thisIteration = false);
/** /**
* Run the specified function in the EventBase's thread. * Run the specified function in the EventBase's thread.
* *
...@@ -451,7 +450,7 @@ class EventBase : private boost::noncopyable, public TimeoutManager { ...@@ -451,7 +450,7 @@ class EventBase : private boost::noncopyable, public TimeoutManager {
// --------- libevent callbacks (not for client use) ------------ // --------- libevent callbacks (not for client use) ------------
static void runTr1FunctionPtr(std::function<void()>* fn); static void runFunctionPtr(std::function<void()>* fn);
// small object used as a callback arg with enough info to execute the // small object used as a callback arg with enough info to execute the
// appropriate client-provided Cob // appropriate client-provided Cob
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment