/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
+ * Copyright 2014 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "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
*
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
#ifndef __STDC_FORMAT_MACROS
using folly::Cob;
using folly::EventBase;
-class Tr1FunctionLoopCallback : public EventBase::LoopCallback {
+template <typename Callback>
+class FunctionLoopCallback : public EventBase::LoopCallback {
public:
- explicit Tr1FunctionLoopCallback(const Cob& function)
- : function_(function) {}
+ explicit FunctionLoopCallback(Cob&& function)
+ : function_(std::move(function)) {}
+
+ explicit FunctionLoopCallback(const Cob& function)
+ : function_(function) {}
virtual void runLoopCallback() noexcept {
function_();
}
private:
- Cob function_;
+ Callback function_;
};
}
maxLatencyLoopTime_.reset(value);
}
-static int64_t getTimeDelta(int64_t *prev) {
- int64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::steady_clock::now().time_since_epoch()).count();
- int64_t delta = now - *prev;
- *prev = now;
- return delta;
+static std::chrono::milliseconds
+getTimeDelta(std::chrono::steady_clock::time_point* prev) {
+ auto result = std::chrono::steady_clock::now() - *prev;
+ *prev = std::chrono::steady_clock::now();
+
+ return std::chrono::duration_cast<std::chrono::milliseconds>(result);
}
void EventBase::waitUntilRunning() {
}
#endif
- int64_t prev = std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::steady_clock::now().time_since_epoch()).count();
+ auto prev = std::chrono::steady_clock::now();
int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
}
- 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
stop_ = false;
void EventBase::runInLoop(const Cob& cob, bool thisIteration) {
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();
if (runOnceCallbacks_ != nullptr && thisIteration) {
runOnceCallbacks_->push_back(*wrapper);
return false;
}
- if (!runInEventBaseThread(&EventBase::runTr1FunctionPtr, fnCopy)) {
+ if (!runInEventBaseThread(&EventBase::runFunctionPtr, fnCopy)) {
delete fnCopy;
return false;
}
}
/* static */
-void EventBase::runTr1FunctionPtr(Cob* fn) {
+void EventBase::runFunctionPtr(Cob* fn) {
// The function should never throw an exception, because we have no
// way of knowing what sort of error handling to perform.
//
/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
+ * Copyright 2014 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "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
*
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
#pragma once
*/
void runInLoop(const Cob& c, bool thisIteration = false);
+ void runInLoop(Cob&& c, bool thisIteration = false);
+
/**
* Run the specified function in the EventBase's thread.
*
// --------- 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
// appropriate client-provided Cob