File: test_thread.py

package info (click to toggle)
nanobind 2.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,060 kB
  • sloc: cpp: 11,838; python: 5,862; ansic: 4,820; makefile: 22; sh: 15
file content (102 lines) | stat: -rw-r--r-- 2,506 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import test_thread_ext as t
from test_thread_ext import Counter, GlobalData, ClassWithProperty, ClassWithClassProperty
from common import parallelize

def test01_object_creation(n_threads=8):
    # This test hammers 'inst_c2p' from multiple threads, and
    # checks that the locking of internal data structures works

    n = 100000
    def f():
        r = [None]*n
        for i in range(n):
            c = Counter()
            c.inc_unsafe()
            r[i] = c
        for i in range(n):
            assert t.return_self(r[i]) is r[i]
        return r

    v = parallelize(f, n_threads=n_threads)
    assert len(v) == n_threads
    for v2 in v:
        assert len(v2) == n
        for v3 in v2:
            assert v3.value == 1

def test02_global_lock(n_threads=8):
    # Test that a global PyMutex protects the counter
    n = 100000
    c = Counter()
    def f():
        for _ in range(n):
            t.inc_global(c)

    parallelize(f, n_threads=n_threads)
    assert c.value == n * n_threads


def test03_locked_method(n_threads=8):
    # Checks that nb::lock_self() protects an internal counter
    n = 100000
    c = Counter()
    def f():
        for i in range(n):
            c.inc_safe()

    parallelize(f, n_threads=n_threads)
    assert c.value == n * n_threads


def test04_locked_function(n_threads=8):
    # Checks that nb::lock_self() protects an internal counter
    n = 100000
    c = Counter()
    def f():
        for _ in range(n):
            t.inc_safe(c)

    parallelize(f, n_threads=n_threads)
    assert c.value == n * n_threads


def test05_locked_twoargs(n_threads=8):
    # Check two-argument locking
    n = 100000
    c = Counter()
    def f():
        c2 = Counter()
        for i in range(n):
            c2.inc_unsafe()
            if i & 1 == 0:
                c2.merge_safe(c)
            else:
                c.merge_safe(c2)

    parallelize(f, n_threads=n_threads)
    assert c.value == n * n_threads


def test06_global_wrapper(n_threads=8):
    # Check wrapper lookup racing with wrapper deallocation
    n = 10000
    def f():
        for _ in range(n):
            GlobalData.get()
            GlobalData.get()
            GlobalData.get()
            GlobalData.get()

    parallelize(f, n_threads=n_threads)


def test07_access_attributes(n_threads=8):
    n = 1000
    c1 = ClassWithProperty(123)
    c2 = ClassWithClassProperty(c1)

    def f():
        for i in range(n):
            _ = c2.prop1.prop2

    parallelize(f, n_threads=n_threads)