Debian Codegraph @

rpython/rlib/rstacklet.py

raw symbols SCIP
1 import sys
2 from rpython.rlib import _rffi_stacklet as _c
3 from rpython.rlib import jit
4 from rpython.rlib.objectmodel import fetch_translated_config
5 from rpython.rtyper.lltypesystem import lltype, llmemory
6 from rpython.rlib import rvmprof
7
8 DEBUG = False
9
10
11 class StackletThread(object):
12
13 @jit.dont_look_inside
14 def __init__(self, _argument_ignored_for_backward_compatibility=None):
15 self._gcrootfinder = _getgcrootfinder(fetch_translated_config())
16 self._thrd = _c.newthread()
17 if not self._thrd:
18 raise MemoryError
19 self._thrd_deleter = StackletThreadDeleter(self._thrd)
20 if DEBUG:
21 assert debug.sthread is None, "multithread debug support missing"
22 debug.sthread = self
23
24 @jit.dont_look_inside
25 def new(self, callback, arg=llmemory.NULL):
26 if DEBUG:
27 callback = _debug_wrapper(callback)
28 x = rvmprof.save_stack()
29 try:
30 rvmprof.empty_stack()
31 h = self._gcrootfinder.new(self, callback, arg)
32 finally:
33 rvmprof.restore_stack(x)
34 if DEBUG:
35 debug.add(h)
36 return h
37 new._annspecialcase_ = 'specialize:arg(1)'
38
39 @jit.dont_look_inside
40 def switch(self, stacklet):
41 if DEBUG:
42 debug.remove(stacklet)
43 x = rvmprof.save_stack()
44 try:
45 h = self._gcrootfinder.switch(stacklet)
46 finally:
47 rvmprof.restore_stack(x)
48 if DEBUG:
49 debug.add(h)
50 return h
51
52 def is_empty_handle(self, stacklet):
53 # note that "being an empty handle" and being equal to
54 # "get_null_handle()" may be the same, or not; don't rely on it
55 return self._gcrootfinder.is_empty_handle(stacklet)
56
57 def get_null_handle(self):
58 return self._gcrootfinder.get_null_handle()
59
60 def _freeze_(self):
61 raise Exception("StackletThread instances must not be seen during"
62 " translation.")
63
64
65 class StackletThreadDeleter(object):
66 # quick hack: the __del__ is on another object, so that
67 # if the main StackletThread ends up in random circular
68 # references, on pypy deletethread() is only called
69 # when all that circular reference mess is gone.
70 def __init__(self, thrd):
71 self._thrd = thrd
72 def __del__(self):
73 thrd = self._thrd
74 if thrd:
75 self._thrd = lltype.nullptr(_c.thread_handle.TO)
76 _c.deletethread(thrd)
77
78 # ____________________________________________________________
79
80 def _getgcrootfinder(config):
81 if config is None and '__pypy__' in sys.builtin_module_names:
82 import py
83 py.test.skip("cannot run the stacklet tests on top of pypy: "
84 "calling directly the C function stacklet_switch() "
85 "will crash, depending on details of your config")
86 if config is not None:
87 assert config.translation.continuation, (
88 "stacklet: you have to translate with --continuation")
89 if (config is None or
90 config.translation.gc in ('ref', 'boehm', 'none')): # for tests
91 gcrootfinder = 'n/a'
92 else:
93 gcrootfinder = config.translation.gcrootfinder
94 gcrootfinder = gcrootfinder.replace('/', '_')
95 module = __import__('rpython.rlib._stacklet_%s' % gcrootfinder,
96 None, None, ['__doc__'])
97 return module.gcrootfinder
98 _getgcrootfinder._annspecialcase_ = 'specialize:memo'
99
100
101 class StackletDebugError(Exception):
102 pass
103
104 class Debug(object):
105 def __init__(self):
106 self.sthread = None
107 self.active = []
108 def _cleanup_(self):
109 self.__init__()
110 def add(self, h):
111 if not self.sthread.is_empty_handle(h):
112 if h == self.sthread.get_null_handle():
113 raise StackletDebugError("unexpected null handle")
114 self.active.append(h)
115 def remove(self, h):
116 try:
117 i = self.active.index(h)
118 except ValueError:
119 if self.sthread.is_empty_handle(h):
120 msg = "empty stacklet handle"
121 elif h == self.sthread.get_null_handle():
122 msg = "unexpected null handle"
123 else:
124 msg = "double usage of handle %r" % (h,)
125 raise StackletDebugError(msg)
126 del self.active[i]
127 debug = Debug()
128
129 def _debug_wrapper(callback):
130 def wrapper(h, arg):
131 debug.add(h)
132 h = callback(h, arg)
133 debug.remove(h)
134 return h
135 return wrapper
136 _debug_wrapper._annspecialcase_ = 'specialize:memo'