| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import py |
| 4 |
from rpython.translator import cdir |
| 5 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 6 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 7 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 8 |
from rpython.rlib.objectmodel import not_rpython, we_are_translated |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
translator_c_dir = py.path.local(cdir) |
| 13 |
|
| 14 |
eci = ExternalCompilationInfo( |
| 15 |
includes = ['src/thread.h'], |
| 16 |
separate_module_files = [translator_c_dir / 'src' / 'thread.c'], |
| 17 |
include_dirs = [translator_c_dir], |
| 18 |
post_include_bits = ['#define RPY_WITH_GIL']) |
| 19 |
|
| 20 |
llexternal = rffi.llexternal |
| 21 |
|
| 22 |
|
| 23 |
_gil_allocate = llexternal('RPyGilAllocate', [], lltype.Void, |
| 24 |
_nowrapper=True, sandboxsafe=True, |
| 25 |
compilation_info=eci) |
| 26 |
|
| 27 |
_gil_yield_thread = llexternal('RPyGilYieldThread', [], lltype.Signed, |
| 28 |
_nowrapper=True, sandboxsafe=True, |
| 29 |
compilation_info=eci) |
| 30 |
|
| 31 |
_gil_release = llexternal('RPyGilRelease', [], lltype.Void, |
| 32 |
_nowrapper=True, sandboxsafe=True, |
| 33 |
compilation_info=eci) |
| 34 |
|
| 35 |
_gil_acquire = llexternal('RPyGilAcquire', [], lltype.Void, |
| 36 |
_nowrapper=True, sandboxsafe=True, |
| 37 |
compilation_info=eci) |
| 38 |
|
| 39 |
gil_fetch_fastgil = llexternal('RPyFetchFastGil', [], llmemory.Address, |
| 40 |
_nowrapper=True, sandboxsafe=True, |
| 41 |
compilation_info=eci) |
| 42 |
|
| 43 |
_gil_get_holder = llexternal('RPyGilGetHolder', [], lltype.Signed, |
| 44 |
_nowrapper=True, sandboxsafe=True, |
| 45 |
compilation_info=eci) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
def invoke_after_thread_switch(callback): |
| 51 |
"""Invoke callback() after a thread switch. |
| 52 |
|
| 53 |
This is a hook used by pypy.module.signal. Several callbacks should |
| 54 |
be easy to support (but not right now). |
| 55 |
|
| 56 |
This function should be called from the translated RPython program |
| 57 |
(i.e. *not* at module level!), but registers the callback |
| 58 |
statically. The exact point at which invoke_after_thread_switch() |
| 59 |
is called has no importance: the callback() will be called anyway. |
| 60 |
""" |
| 61 |
print("NOTE: invoke_after_thread_switch() is meant to be translated ") |
| 62 |
print("and not called directly. Using some emulation.") |
| 63 |
global _emulated_after_thread_switch |
| 64 |
_emulated_after_thread_switch = callback |
| 65 |
|
| 66 |
_emulated_after_thread_switch = None |
| 67 |
|
| 68 |
@not_rpython |
| 69 |
def _after_thread_switch(): |
| 70 |
if _emulated_after_thread_switch is not None: |
| 71 |
_emulated_after_thread_switch() |
| 72 |
|
| 73 |
|
| 74 |
class Entry(ExtRegistryEntry): |
| 75 |
_about_ = invoke_after_thread_switch |
| 76 |
|
| 77 |
def compute_result_annotation(self, s_callback): |
| 78 |
assert s_callback.is_constant() |
| 79 |
callback = s_callback.const |
| 80 |
bk = self.bookkeeper |
| 81 |
translator = bk.annotator.translator |
| 82 |
if hasattr(translator, '_rgil_invoke_after_thread_switch'): |
| 83 |
assert translator._rgil_invoke_after_thread_switch == callback, ( |
| 84 |
"not implemented yet: several invoke_after_thread_switch()") |
| 85 |
else: |
| 86 |
translator._rgil_invoke_after_thread_switch = callback |
| 87 |
bk.emulate_pbc_call("rgil.invoke_after_thread_switch", s_callback, []) |
| 88 |
|
| 89 |
def specialize_call(self, hop): |
| 90 |
|
| 91 |
hop.exception_cannot_occur() |
| 92 |
|
| 93 |
class Entry(ExtRegistryEntry): |
| 94 |
_about_ = _after_thread_switch |
| 95 |
|
| 96 |
def compute_result_annotation(self): |
| 97 |
|
| 98 |
pass |
| 99 |
|
| 100 |
def specialize_call(self, hop): |
| 101 |
translator = hop.rtyper.annotator.translator |
| 102 |
if hasattr(translator, '_rgil_invoke_after_thread_switch'): |
| 103 |
func = translator._rgil_invoke_after_thread_switch |
| 104 |
graph = translator._graphof(func) |
| 105 |
llfn = hop.rtyper.getcallable(graph) |
| 106 |
c_callback = hop.inputconst(lltype.typeOf(llfn), llfn) |
| 107 |
hop.exception_is_here() |
| 108 |
hop.genop("direct_call", [c_callback]) |
| 109 |
else: |
| 110 |
hop.exception_cannot_occur() |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
class EmulatedGilHolder: |
| 115 |
def __init__(self): |
| 116 |
try: |
| 117 |
import thread |
| 118 |
except ImportError: |
| 119 |
import dummy_thread as thread |
| 120 |
self._tid = self._get_ident() |
| 121 |
self._lock = thread.allocate_lock() |
| 122 |
self._lock.acquire() |
| 123 |
|
| 124 |
def _get_ident(self): |
| 125 |
from rpython.rlib import rthread |
| 126 |
tid = rthread.get_ident() |
| 127 |
assert tid != 0 |
| 128 |
return tid |
| 129 |
|
| 130 |
def release(self): |
| 131 |
assert self._tid == self._get_ident() |
| 132 |
self._tid = 0 |
| 133 |
self._lock.release() |
| 134 |
|
| 135 |
def acquire(self): |
| 136 |
assert self._tid != self._get_ident() |
| 137 |
self._lock.acquire() |
| 138 |
assert self._tid == 0 |
| 139 |
self._tid = self._get_ident() |
| 140 |
|
| 141 |
def get_holder(self): |
| 142 |
return self._tid |
| 143 |
|
| 144 |
|
| 145 |
_emulated_gil_holder = None |
| 146 |
|
| 147 |
def _reset_emulated_gil_holder(): |
| 148 |
|
| 149 |
if _emulated_gil_holder is not None and _emulated_gil_holder._tid == 0: |
| 150 |
_emulated_gil_holder.acquire() |
| 151 |
|
| 152 |
|
| 153 |
def allocate(): |
| 154 |
global _emulated_gil_holder |
| 155 |
if we_are_translated(): |
| 156 |
_gil_allocate() |
| 157 |
elif _emulated_gil_holder is None: |
| 158 |
_emulated_gil_holder = EmulatedGilHolder() |
| 159 |
|
| 160 |
def release(): |
| 161 |
|
| 162 |
|
| 163 |
if we_are_translated(): |
| 164 |
_gil_release() |
| 165 |
else: |
| 166 |
allocate() |
| 167 |
_emulated_gil_holder.release() |
| 168 |
release._gctransformer_hint_cannot_collect_ = True |
| 169 |
release._dont_reach_me_in_del_ = True |
| 170 |
|
| 171 |
def acquire(): |
| 172 |
if we_are_translated(): |
| 173 |
from rpython.rlib import rthread |
| 174 |
_gil_acquire() |
| 175 |
rthread.gc_thread_run() |
| 176 |
else: |
| 177 |
allocate() |
| 178 |
_emulated_gil_holder.acquire() |
| 179 |
_after_thread_switch() |
| 180 |
acquire._gctransformer_hint_cannot_collect_ = True |
| 181 |
acquire._dont_reach_me_in_del_ = True |
| 182 |
|
| 183 |
def acquire_maybe_in_new_thread(): |
| 184 |
if not we_are_translated(): |
| 185 |
return acquire() |
| 186 |
from rpython.rlib import rthread |
| 187 |
rthread.get_or_make_ident() |
| 188 |
_gil_acquire() |
| 189 |
rthread.gc_thread_run() |
| 190 |
_after_thread_switch() |
| 191 |
acquire_maybe_in_new_thread._gctransformer_hint_cannot_collect_ = True |
| 192 |
acquire_maybe_in_new_thread._dont_reach_me_in_del_ = True |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
def yield_thread(): |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if we_are_translated(): |
| 206 |
if _gil_yield_thread(): |
| 207 |
from rpython.rlib import rthread |
| 208 |
rthread.gc_thread_run() |
| 209 |
_after_thread_switch() |
| 210 |
else: |
| 211 |
release() |
| 212 |
acquire() |
| 213 |
yield_thread._gctransformer_hint_close_stack_ = True |
| 214 |
yield_thread._dont_reach_me_in_del_ = True |
| 215 |
yield_thread._dont_inline_ = True |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
def gil_get_holder(): |
| 222 |
if we_are_translated(): |
| 223 |
return _gil_get_holder() |
| 224 |
else: |
| 225 |
allocate() |
| 226 |
return _emulated_gil_holder.get_holder() |
| 227 |
|
| 228 |
def am_I_holding_the_GIL(): |
| 229 |
from rpython.rlib import rthread |
| 230 |
my_tid = rthread.get_or_make_ident() |
| 231 |
return gil_get_holder() == my_tid |