Debian Codegraph @

rpython/rlib/test/test_rgil.py

raw symbols SCIP
1 from rpython.rlib import rgil
2 from rpython.rlib.debug import debug_print
3 from rpython.translator.c.test.test_standalone import StandaloneTests
4 from rpython.config.translationoption import get_combined_translation_config
5
6
7 class BaseTestGIL(StandaloneTests):
8
9 def test_simple(self):
10 def main(argv):
11 rgil.release()
12 # don't have the GIL here
13 rgil.acquire()
14 rgil.yield_thread()
15 print "OK" # there is also a release/acquire pair here
16 return 0
17
18 main([])
19
20 t, cbuilder = self.compile(main)
21 data = cbuilder.cmdexec('')
22 assert data == "OK\n"
23
24 def test_after_thread_switch(self):
25 class Foo:
26 pass
27 foo = Foo()
28 foo.counter = 0
29 def seeme():
30 foo.counter += 1
31 def main(argv):
32 rgil.invoke_after_thread_switch(seeme)
33 print "Test" # one release/acquire pair here
34 print foo.counter
35 print foo.counter
36 return 0
37
38 t, cbuilder = self.compile(main)
39 data = cbuilder.cmdexec('')
40 assert data == "Test\n1\n2\n"
41
42 def test_am_I_holding_the_GIL(self):
43 def check(name, expected=True):
44 # we may not have the GIL here, don't use "print"
45 debug_print(name)
46 if rgil.am_I_holding_the_GIL() != expected:
47 debug_print('assert failed at point', name)
48 debug_print('rgil.gil_get_holder() ==', rgil.gil_get_holder())
49 assert False
50
51 def main(argv):
52 check('1')
53 rgil.release()
54 # don't have the GIL here
55 check('2', False)
56 rgil.acquire()
57 check('3')
58 rgil.yield_thread()
59 check('4')
60 print "OK" # there is also a release/acquire pair here
61 check('5')
62 return 0
63
64 main([])
65
66 t, cbuilder = self.compile(main)
67 data = cbuilder.cmdexec('')
68 assert data == "OK\n"
69
70 def test_multiple_threads(self):
71 import time, random
72 from rpython.rlib import rthread
73
74 def check(name, nextop, expected=True):
75 # we may not have the GIL here, don't use "print"
76 if rgil.am_I_holding_the_GIL() != expected:
77 debug_print('assert failed at point', name, 'at', nextop)
78 debug_print('rgil.gil_get_holder() ==', rgil.gil_get_holder())
79 assert False
80
81 seed = int(time.time())
82 print "Random seed:", seed
83 random.seed(seed)
84
85 # This is just a complicated way of simulating random work.
86 # We randomly release the GIL in various ways from 4 different threads
87 # and check that at least rgil.am_I_holding_the_GIL() is sane.
88
89 OP_YIELD = 0
90 OP_RELEASE_AND_ACQUIRE = 1
91 OP_BUSY = 2 # without releasing the GIL
92 OP_SLEEP = 3 # time.sleep() always releases the GIL
93 OPS = [OP_YIELD, OP_RELEASE_AND_ACQUIRE, OP_BUSY, OP_SLEEP]
94
95 N_THREADS = 4
96 ops_by_thread = []
97 for i in range(N_THREADS):
98 ops = []
99 for j in range(10000):
100 op = random.choice(OPS)
101 ops.append(op)
102 if op >= 2:
103 ops.append(random.randint(0, 1000))
104 ops_by_thread.append(ops)
105
106 class Glob:
107 def __init__(self):
108 self.my_locks = []
109 self.n_threads = 0
110 glob = Glob()
111
112 def do_random_work():
113 thread_index = glob.n_threads
114 glob.n_threads += 1
115 ops = ops_by_thread[thread_index]
116 nextop = 0
117 while nextop < len(ops):
118 op = ops[nextop]
119 nextop += 1
120 if op == OP_YIELD:
121 rgil.yield_thread()
122 check("after yield", nextop)
123 elif op == OP_RELEASE_AND_ACQUIRE:
124 rgil.release()
125 check("after release_gil", nextop, expected=False)
126 rgil.acquire()
127 check("after acquire_gil", nextop)
128 else:
129 arg = ops[nextop]
130 nextop += 1
131 if op == OP_BUSY:
132 end_time = time.time() + arg * 1e-6
133 while time.time() < end_time:
134 pass
135 check("after busy work", nextop)
136 else:
137 time.sleep(arg * 1e-6)
138 check("after time.sleep()", nextop)
139 finish_lock = glob.my_locks[thread_index]
140 finish_lock.release()
141
142 def main(argv):
143 for j in range(N_THREADS):
144 lock = rthread.allocate_lock()
145 lock.acquire(True)
146 glob.my_locks.append(lock)
147
148 for j in range(N_THREADS):
149 rthread.start_new_thread(do_random_work, ())
150
151 for j in range(N_THREADS):
152 glob.my_locks[j].acquire(True)
153
154 print "OK"
155 return 0
156
157 self.config = get_combined_translation_config(
158 overrides={"translation.thread": True})
159 t, cbuilder = self.compile(main)
160 data = cbuilder.cmdexec('')
161 assert data == "OK\n"
162
163
164 class TestGILShadowStack(BaseTestGIL):
165 gc = 'minimark'
166 gcrootfinder = 'shadowstack'