μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_thread.cpp
Go to the documentation of this file.
1/*
2 tests/test_thread.cpp -- call pybind11 bound methods in threads
3
4 Copyright (c) 2021 Laramie Leavitt (Google LLC) <lar@google.com>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#include <pybind11/cast.h>
11#include <pybind11/pybind11.h>
12
13#include "pybind11_tests.h"
14
15#include <chrono>
16#include <thread>
17
18namespace py = pybind11;
19
20namespace {
21
22struct IntStruct {
23 explicit IntStruct(int v) : value(v){};
24 ~IntStruct() { value = -value; }
25 IntStruct(const IntStruct &) = default;
26 IntStruct &operator=(const IntStruct &) = default;
27
28 int value;
29};
30
31} // namespace
32
33TEST_SUBMODULE(thread, m) {
34
35 py::class_<IntStruct>(m, "IntStruct").def(py::init([](const int i) { return IntStruct(i); }));
36
37 // implicitly_convertible uses loader_life_support when an implicit
38 // conversion is required in order to lifetime extend the reference.
39 //
40 // This test should be run with ASAN for better effectiveness.
41 py::implicitly_convertible<int, IntStruct>();
42
43 m.def("test", [](int expected, const IntStruct &in) {
44 {
45 py::gil_scoped_release release;
46 std::this_thread::sleep_for(std::chrono::milliseconds(5));
47 }
48
49 if (in.value != expected) {
50 throw std::runtime_error("Value changed!!");
51 }
52 });
53
54 m.def(
55 "test_no_gil",
56 [](int expected, const IntStruct &in) {
57 std::this_thread::sleep_for(std::chrono::milliseconds(5));
58 if (in.value != expected) {
59 throw std::runtime_error("Value changed!!");
60 }
61 },
62 py::call_guard<py::gil_scoped_release>());
63
64 // NOTE: std::string_view also uses loader_life_support to ensure that
65 // the string contents remain alive, but that's a C++ 17 feature.
66}
#define TEST_SUBMODULE(name, variable)