Commit d0142327 authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook Github Bot

Introduce GILAwareExecutor

Summary: A simpler manual executor that can be executed by a python thread, and release the GIL while waiting.

Reviewed By: andriigrynenko

Differential Revision: D8087779

fbshipit-source-id: a75ecbda3804a53f1b705aa04546b74368738b7b
parent 5981f1bb
......@@ -115,10 +115,12 @@ list(REMOVE_ITEM files
${FOLLY_DIR}/experimental/JSONSchemaTester.cpp
${FOLLY_DIR}/experimental/io/HugePageUtil.cpp
${FOLLY_DIR}/experimental/symbolizer/ElfUtil.cpp
${FOLLY_DIR}/python/GILAwareManualExecutor.cpp
)
list(REMOVE_ITEM hfiles
${FOLLY_DIR}/detail/SlowFingerprint.h
${FOLLY_DIR}/detail/FingerprintPolynomial.h
${FOLLY_DIR}/python/GILAwareManualExecutor.h
)
# Explicitly include utility library code from inside
......
/*
* Copyright 2018-present 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.
*/
#include <folly/python/GILAwareManualExecutor.h>
#include <Python.h>
#include <folly/ScopeGuard.h>
namespace folly {
namespace python {
void GILAwareManualExecutor::add(Func callback) {
{
std::lock_guard<std::mutex> lock(lock_);
funcs_.emplace(std::move(callback));
}
cv_.notify_one();
}
void GILAwareManualExecutor::waitBeforeDrive() {
std::unique_lock<std::mutex> lock(lock_);
if (!funcs_.empty()) {
return;
}
// Release GIL before waiting on lock
auto* pyThreadState = PyEval_SaveThread();
SCOPE_EXIT {
PyEval_RestoreThread(pyThreadState);
};
cv_.wait(lock, [&] { return !funcs_.empty(); });
}
void GILAwareManualExecutor::driveImpl() {
Func func;
while (true) {
{
std::lock_guard<std::mutex> lock(lock_);
if (funcs_.empty()) {
break;
}
func = std::move(funcs_.front());
funcs_.pop();
}
func();
}
}
} // namespace python
} // namespace folly
/*
* Copyright 2018-present 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.
*/
#pragma once
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
#include <folly/executors/DrivableExecutor.h>
#include <folly/executors/SequencedExecutor.h>
namespace folly {
namespace python {
/**
* A simple ManualExecutor intended to be run directly on a Python thread.
* It releases Python GIL while waiting for tasks to execute.
*/
class GILAwareManualExecutor : public DrivableExecutor,
public SequencedExecutor {
public:
void add(Func) override;
void drive() override {
waitBeforeDrive();
driveImpl();
}
private:
void waitBeforeDrive();
void driveImpl();
std::mutex lock_;
std::queue<Func> funcs_;
std::condition_variable cv_;
};
} // namespace python
} // namespace folly
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