| 1 |
import weakref |
| 2 |
from rpython.rlib import jit |
| 3 |
from pypy.interpreter.baseobjspace import W_Root |
| 4 |
from pypy.interpreter.error import oefmt |
| 5 |
from pypy.interpreter.executioncontext import ExecutionContext |
| 6 |
from pypy.interpreter.typedef import (TypeDef, interp2app, GetSetProperty, |
| 7 |
descr_get_dict, make_weakref_descr) |
| 8 |
from rpython.rlib.rshrinklist import AbstractShrinkList |
| 9 |
|
| 10 |
|
| 11 |
class WRefShrinkList(AbstractShrinkList): |
| 12 |
def must_keep(self, wref): |
| 13 |
return wref() is not None |
| 14 |
|
| 15 |
|
| 16 |
ExecutionContext._thread_local_objs = None |
| 17 |
ExecutionContext._sentinel_lock = None |
| 18 |
|
| 19 |
|
| 20 |
class Local(W_Root): |
| 21 |
"""Thread-local data""" |
| 22 |
|
| 23 |
@jit.dont_look_inside |
| 24 |
def __init__(self, space, initargs): |
| 25 |
self.initargs = initargs |
| 26 |
self.dicts = {} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ec = space.getexecutioncontext() |
| 33 |
w_dict = space.newdict(instance=True) |
| 34 |
self.dicts[ec] = w_dict |
| 35 |
self._register_in_ec(ec) |
| 36 |
|
| 37 |
self.last_dict = w_dict |
| 38 |
self.last_ec = ec |
| 39 |
|
| 40 |
def _register_in_ec(self, ec): |
| 41 |
if not ec.space.config.translation.rweakref: |
| 42 |
return |
| 43 |
if ec._thread_local_objs is None: |
| 44 |
ec._thread_local_objs = WRefShrinkList() |
| 45 |
ec._thread_local_objs.append(weakref.ref(self)) |
| 46 |
|
| 47 |
@jit.dont_look_inside |
| 48 |
def create_new_dict(self, ec): |
| 49 |
|
| 50 |
space = ec.space |
| 51 |
w_dict = space.newdict(instance=True) |
| 52 |
self.dicts[ec] = w_dict |
| 53 |
|
| 54 |
try: |
| 55 |
w_type = space.type(self) |
| 56 |
w_init = space.getattr(w_type, space.newtext("__init__")) |
| 57 |
space.call_obj_args(w_init, self, self.initargs) |
| 58 |
except: |
| 59 |
|
| 60 |
del self.dicts[ec] |
| 61 |
raise |
| 62 |
|
| 63 |
self._register_in_ec(ec) |
| 64 |
return w_dict |
| 65 |
|
| 66 |
def getdict(self, space): |
| 67 |
ec = space.getexecutioncontext() |
| 68 |
if ec is self.last_ec: |
| 69 |
return self.last_dict |
| 70 |
try: |
| 71 |
w_dict = self.dicts[ec] |
| 72 |
except KeyError: |
| 73 |
w_dict = self.create_new_dict(ec) |
| 74 |
self.last_ec = ec |
| 75 |
self.last_dict = w_dict |
| 76 |
return w_dict |
| 77 |
|
| 78 |
def descr_local__new__(space, w_subtype, __args__): |
| 79 |
from pypy.objspace.std.typeobject import _precheck_for_new |
| 80 |
w_subtype = _precheck_for_new(space, w_subtype) |
| 81 |
if __args__.arguments_w or __args__.keyword_names_w: |
| 82 |
w_parent_init, _ = space.lookup_in_type_where(w_subtype, '__init__') |
| 83 |
if w_parent_init is space.w_object: |
| 84 |
raise oefmt(space.w_TypeError, |
| 85 |
"Initialization arguments are not supported") |
| 86 |
local = space.allocate_instance(Local, w_subtype) |
| 87 |
Local.__init__(local, space, __args__) |
| 88 |
return local |
| 89 |
|
| 90 |
Local.typedef = TypeDef("_thread._local", |
| 91 |
__doc__ = "Thread-local data", |
| 92 |
__new__ = interp2app(Local.descr_local__new__.im_func), |
| 93 |
__dict__ = GetSetProperty(descr_get_dict, cls=Local), |
| 94 |
__weakref__ = make_weakref_descr(Local), |
| 95 |
) |
| 96 |
|
| 97 |
def thread_is_stopping(ec): |
| 98 |
sentinel_lock = ec._sentinel_lock |
| 99 |
if sentinel_lock is not None: |
| 100 |
if sentinel_lock.lock.is_acquired(): |
| 101 |
sentinel_lock.descr_lock_release(ec.space) |
| 102 |
tlobjs = ec._thread_local_objs |
| 103 |
if tlobjs is None: |
| 104 |
return |
| 105 |
ec._thread_local_objs = None |
| 106 |
for wref in tlobjs.items(): |
| 107 |
local = wref() |
| 108 |
if local is not None: |
| 109 |
del local.dicts[ec] |
| 110 |
local.last_dict = None |
| 111 |
local.last_ec = None |