mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 14:45:12 +00:00
aa98d95717
* chore(deps): update pre-commit hooks updates: - [github.com/pre-commit/mirrors-clang-format: v18.1.2 → v18.1.4](https://github.com/pre-commit/mirrors-clang-format/compare/v18.1.2...v18.1.4) - [github.com/astral-sh/ruff-pre-commit: v0.3.5 → v0.4.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.3.5...v0.4.3) - [github.com/pre-commit/mirrors-mypy: v1.9.0 → v1.10.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.9.0...v1.10.0) - [github.com/pre-commit/pre-commit-hooks: v4.5.0 → v4.6.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.5.0...v4.6.0) - [github.com/python-jsonschema/check-jsonschema: 0.28.1 → 0.28.2](https://github.com/python-jsonschema/check-jsonschema/compare/0.28.1...0.28.2) * style: pre-commit fixes * Update .pre-commit-config.yaml --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
/*
|
|
tests/test_thread.cpp -- call pybind11 bound methods in threads
|
|
|
|
Copyright (c) 2021 Laramie Leavitt (Google LLC) <lar@google.com>
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#include <pybind11/cast.h>
|
|
#include <pybind11/pybind11.h>
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
namespace py = pybind11;
|
|
|
|
namespace {
|
|
|
|
struct IntStruct {
|
|
explicit IntStruct(int v) : value(v) {};
|
|
~IntStruct() { value = -value; }
|
|
IntStruct(const IntStruct &) = default;
|
|
IntStruct &operator=(const IntStruct &) = default;
|
|
|
|
int value;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST_SUBMODULE(thread, m) {
|
|
|
|
py::class_<IntStruct>(m, "IntStruct").def(py::init([](const int i) { return IntStruct(i); }));
|
|
|
|
// implicitly_convertible uses loader_life_support when an implicit
|
|
// conversion is required in order to lifetime extend the reference.
|
|
//
|
|
// This test should be run with ASAN for better effectiveness.
|
|
py::implicitly_convertible<int, IntStruct>();
|
|
|
|
m.def("test", [](int expected, const IntStruct &in) {
|
|
{
|
|
py::gil_scoped_release release;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
|
}
|
|
|
|
if (in.value != expected) {
|
|
throw std::runtime_error("Value changed!!");
|
|
}
|
|
});
|
|
|
|
m.def(
|
|
"test_no_gil",
|
|
[](int expected, const IntStruct &in) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
|
if (in.value != expected) {
|
|
throw std::runtime_error("Value changed!!");
|
|
}
|
|
},
|
|
py::call_guard<py::gil_scoped_release>());
|
|
|
|
// NOTE: std::string_view also uses loader_life_support to ensure that
|
|
// the string contents remain alive, but that's a C++ 17 feature.
|
|
}
|