| 1 |
import weakref |
| 2 |
from rpython.rlib import rthread, rshrinklist |
| 3 |
from rpython.rlib.objectmodel import we_are_translated |
| 4 |
from rpython.rlib.rarithmetic import r_ulonglong |
| 5 |
from pypy.module.thread.error import wrap_thread_error |
| 6 |
from pypy.interpreter.executioncontext import ExecutionContext |
| 7 |
|
| 8 |
|
| 9 |
ExecutionContext._signals_enabled = 0 |
| 10 |
ExecutionContext._sentinel_lock = None |
| 11 |
|
| 12 |
|
| 13 |
class OSThreadLocals: |
| 14 |
"""Thread-local storage for OS-level threads. |
| 15 |
For memory management, this version depends on explicit notification when |
| 16 |
a thread finishes. This works as long as the thread was started by |
| 17 |
os_thread.bootstrap().""" |
| 18 |
|
| 19 |
def __init__(self, space): |
| 20 |
"NOT_RPYTHON" |
| 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 |
self.space = space |
| 51 |
self._valuedict = {} |
| 52 |
self._cleanup_() |
| 53 |
self.raw_thread_local = rthread.ThreadLocalReference(ExecutionContext, |
| 54 |
loop_invariant=True) |
| 55 |
|
| 56 |
def can_optimize_with_weaklist(self): |
| 57 |
config = self.space.config |
| 58 |
return (config.translation.rweakref and |
| 59 |
rthread.ThreadLocalReference.automatic_keepalive(config)) |
| 60 |
|
| 61 |
def _cleanup_(self): |
| 62 |
self._valuedict.clear() |
| 63 |
self._weaklist = None |
| 64 |
self._mainthreadident = 0 |
| 65 |
|
| 66 |
def enter_thread(self, space): |
| 67 |
"Notification that the current thread is about to start running." |
| 68 |
self._set_ec(space.createexecutioncontext()) |
| 69 |
|
| 70 |
def try_enter_thread(self, space): |
| 71 |
|
| 72 |
if self.raw_thread_local.get() is not None: |
| 73 |
return False |
| 74 |
|
| 75 |
|
| 76 |
ec = space.createexecutioncontext() |
| 77 |
if not self.can_optimize_with_weaklist(): |
| 78 |
self._set_ec(ec) |
| 79 |
return True |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if self._weaklist is None: |
| 88 |
self._weaklist = ListECWrappers() |
| 89 |
self._weaklist.append(weakref.ref(AutoFreeECWrapper(ec))) |
| 90 |
self._set_ec(ec, register_in_valuedict=False) |
| 91 |
return False |
| 92 |
|
| 93 |
def _set_ec(self, ec, register_in_valuedict=True): |
| 94 |
ident = rthread.get_ident() |
| 95 |
if self._mainthreadident == 0 or self._mainthreadident == ident: |
| 96 |
ec._signals_enabled = 1 |
| 97 |
self._mainthreadident = ident |
| 98 |
if register_in_valuedict: |
| 99 |
self._valuedict[ident] = ec |
| 100 |
self.raw_thread_local.set(ec) |
| 101 |
|
| 102 |
def leave_thread(self, space): |
| 103 |
"Notification that the current thread is about to stop." |
| 104 |
from pypy.module.thread.os_local import thread_is_stopping |
| 105 |
ec = self.get_ec() |
| 106 |
if ec is not None: |
| 107 |
try: |
| 108 |
thread_is_stopping(ec) |
| 109 |
finally: |
| 110 |
self.raw_thread_local.set(None) |
| 111 |
ident = rthread.get_ident() |
| 112 |
try: |
| 113 |
del self._valuedict[ident] |
| 114 |
except KeyError: |
| 115 |
pass |
| 116 |
|
| 117 |
def get_ec(self): |
| 118 |
ec = self.raw_thread_local.get() |
| 119 |
if not we_are_translated(): |
| 120 |
assert ec is self._valuedict.get(rthread.get_ident(), None) |
| 121 |
return ec |
| 122 |
|
| 123 |
def signals_enabled(self): |
| 124 |
ec = self.get_ec() |
| 125 |
return ec is not None and ec._signals_enabled |
| 126 |
|
| 127 |
def enable_signals(self, space): |
| 128 |
ec = self.get_ec() |
| 129 |
assert ec is not None |
| 130 |
ec._signals_enabled += 1 |
| 131 |
|
| 132 |
def disable_signals(self, space): |
| 133 |
ec = self.get_ec() |
| 134 |
assert ec is not None |
| 135 |
new = ec._signals_enabled - 1 |
| 136 |
if new < 0: |
| 137 |
raise wrap_thread_error(space, |
| 138 |
"cannot disable signals in thread not enabled for signals") |
| 139 |
ec._signals_enabled = new |
| 140 |
|
| 141 |
def getallvalues(self): |
| 142 |
if self._weaklist is None: |
| 143 |
return self._valuedict |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
result = {} |
| 152 |
for h in self._weaklist.items(): |
| 153 |
wrapper = h() |
| 154 |
if wrapper is not None and not wrapper.deleted: |
| 155 |
result[wrapper.ident] = wrapper.ec |
| 156 |
|
| 157 |
result.update(self._valuedict) |
| 158 |
return result |
| 159 |
|
| 160 |
def reinit_threads(self, space): |
| 161 |
"Called in the child process after a fork()" |
| 162 |
ident = rthread.get_ident() |
| 163 |
ec = self.get_ec() |
| 164 |
assert ec is not None |
| 165 |
old_sig = ec._signals_enabled |
| 166 |
if ident != self._mainthreadident: |
| 167 |
old_sig += 1 |
| 168 |
self._cleanup_() |
| 169 |
self._mainthreadident = ident |
| 170 |
self._set_ec(ec) |
| 171 |
ec._signals_enabled = old_sig |
| 172 |
|
| 173 |
|
| 174 |
class AutoFreeECWrapper(object): |
| 175 |
deleted = False |
| 176 |
|
| 177 |
def __init__(self, ec): |
| 178 |
|
| 179 |
|
| 180 |
self.ec = ec |
| 181 |
ec._threadlocals_auto_free = self |
| 182 |
self.ident = rthread.get_ident() |
| 183 |
|
| 184 |
def __del__(self): |
| 185 |
from pypy.module.thread.os_local import thread_is_stopping |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
self.deleted = True |
| 191 |
thread_is_stopping(self.ec) |
| 192 |
|
| 193 |
class ListECWrappers(rshrinklist.AbstractShrinkList): |
| 194 |
def must_keep(self, wref): |
| 195 |
return wref() is not None |