2024-06-22 04:55:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-07-18 18:50:38 +00:00
|
|
|
import sys
|
2021-09-10 16:29:21 +00:00
|
|
|
import threading
|
|
|
|
|
2024-07-18 18:50:38 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-09-10 16:29:21 +00:00
|
|
|
from pybind11_tests import thread as m
|
|
|
|
|
|
|
|
|
|
|
|
class Thread(threading.Thread):
|
|
|
|
def __init__(self, fn):
|
2022-02-11 02:28:08 +00:00
|
|
|
super().__init__()
|
2021-09-10 16:29:21 +00:00
|
|
|
self.fn = fn
|
|
|
|
self.e = None
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
for i in range(10):
|
|
|
|
self.fn(i, i)
|
|
|
|
except Exception as e:
|
|
|
|
self.e = e
|
|
|
|
|
|
|
|
def join(self):
|
2022-02-11 02:28:08 +00:00
|
|
|
super().join()
|
2021-09-10 16:29:21 +00:00
|
|
|
if self.e:
|
|
|
|
raise self.e
|
|
|
|
|
|
|
|
|
2024-07-18 18:50:38 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads")
|
2021-09-10 16:29:21 +00:00
|
|
|
def test_implicit_conversion():
|
|
|
|
a = Thread(m.test)
|
|
|
|
b = Thread(m.test)
|
|
|
|
c = Thread(m.test)
|
|
|
|
for x in [a, b, c]:
|
|
|
|
x.start()
|
|
|
|
for x in [c, b, a]:
|
|
|
|
x.join()
|
|
|
|
|
|
|
|
|
2024-07-18 18:50:38 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads")
|
2021-09-10 16:29:21 +00:00
|
|
|
def test_implicit_conversion_no_gil():
|
|
|
|
a = Thread(m.test_no_gil)
|
|
|
|
b = Thread(m.test_no_gil)
|
|
|
|
c = Thread(m.test_no_gil)
|
|
|
|
for x in [a, b, c]:
|
|
|
|
x.start()
|
|
|
|
for x in [c, b, a]:
|
|
|
|
x.join()
|