Debian Codegraph @

rpython/jit/backend/llsupport/test/zrpy_releasegil_test.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
2 from rpython.rlib.jit import dont_look_inside
3 from rpython.jit.metainterp.optimizeopt import ALL_OPTS_NAMES
4 from rpython.translator.tool.cbuild import ExternalCompilationInfo
5 from rpython.rlib import rposix
6 from rpython.rlib import rgil
7 from rpython.rlib.debug import debug_print
8
9 from rpython.rtyper.annlowlevel import llhelper
10
11 from rpython.jit.backend.llsupport.test.zrpy_gc_test import BaseFrameworkTests
12 from rpython.jit.backend.llsupport.test.zrpy_gc_test import check
13 from rpython.tool.udir import udir
14
15
16 class ReleaseGILTests(BaseFrameworkTests):
17 compile_kwds = dict(enable_opts=ALL_OPTS_NAMES, thread=True)
18
19 def define_simple(self):
20 c_strchr = rffi.llexternal('strchr', [rffi.CCHARP, lltype.Signed],
21 rffi.CCHARP)
22
23 def before(n, x):
24 return (n, None, None, None, None, None,
25 None, None, None, None, None, None)
26 #
27
28 @dont_look_inside
29 def check_gil(name):
30 # we may not have the GIL here, don't use "print"
31 debug_print(name)
32 if not rgil.am_I_holding_the_GIL():
33 debug_print('assert failed at point', name)
34 debug_print('rgil.gil_get_holder() ==', rgil.gil_get_holder())
35 assert False
36
37 def f(n, x, *args):
38 a = rffi.str2charp(str(n))
39 check_gil('before c_strchr')
40 c_strchr(a, ord('0'))
41 check_gil('after c_strchr')
42 lltype.free(a, flavor='raw')
43 n -= 1
44 return (n, x) + args
45 return before, f, None
46
47 def test_simple(self):
48 self.run('simple')
49 assert 'call_release_gil' in udir.join('TestCompileFramework.log').read()
50
51 def define_close_stack(self):
52 #
53 class Glob(object):
54 pass
55 glob = Glob()
56 class X(object):
57 pass
58 #
59 def callback(p1, p2):
60 for i in range(100):
61 glob.lst.append(X())
62 return rffi.cast(rffi.INT, 1)
63 CALLBACK = lltype.Ptr(lltype.FuncType([lltype.Signed,
64 lltype.Signed], rffi.INT))
65 #
66 @dont_look_inside
67 def alloc1():
68 return llmemory.raw_malloc(16)
69 @dont_look_inside
70 def free1(p):
71 llmemory.raw_free(p)
72
73 c_qsort = rffi.llexternal('qsort', [rffi.VOIDP, rffi.SIZE_T,
74 rffi.SIZE_T, CALLBACK], lltype.Void)
75 #
76 def f42(n):
77 length = len(glob.lst)
78 raw = alloc1()
79 wrapper = rffi._make_wrapper_for(CALLBACK, callback, None, True)
80 fn = llhelper(CALLBACK, wrapper)
81 if n & 1: # to create a loop and a bridge, and also
82 pass # to run the qsort() call in the blackhole interp
83 c_qsort(rffi.cast(rffi.VOIDP, raw), rffi.cast(rffi.SIZE_T, 2),
84 rffi.cast(rffi.SIZE_T, 8), fn)
85 free1(raw)
86 check(len(glob.lst) > length)
87 del glob.lst[:]
88 #
89 def before(n, x):
90 glob.lst = []
91
92 return (n, None, None, None, None, None,
93 None, None, None, None, None, None)
94 #
95 def f(n, x, *args):
96 f42(n)
97 n -= 1
98 return (n, x) + args
99 return before, f, None
100
101 def test_close_stack(self):
102 self.run('close_stack')
103 assert 'call_release_gil' in udir.join('TestCompileFramework.log').read()
104
105 # XXX this should also test get/set_alterrno ?
106 def define_get_set_errno(self):
107 eci = ExternalCompilationInfo(
108 post_include_bits=[r'''
109 #include <errno.h>
110 static int test_get_set_errno(void) {
111 int r = errno;
112 //fprintf(stderr, "read saved errno: %d\n", r);
113 errno = 42;
114 return r;
115 }
116 '''])
117
118 c_test = rffi.llexternal('test_get_set_errno', [], rffi.INT,
119 compilation_info=eci,
120 save_err=rffi.RFFI_FULL_ERRNO)
121
122 def before(n, x):
123 return (n, None, None, None, None, None,
124 None, None, None, None, None, None)
125 #
126 def f(n, x, *args):
127 rposix.set_saved_errno(24)
128 result1 = c_test()
129 result2 = rposix.get_saved_errno()
130 assert result1 == 24
131 assert result2 == 42
132 n -= 1
133 return (n, x) + args
134 return before, f, None
135
136 def test_get_set_errno(self):
137 self.run('get_set_errno')
138 assert 'call_release_gil' in udir.join('TestCompileFramework.log').read()