@@ -4,7 +4,6 @@ from __future__ import absolute_import
4
4
import sys
5
5
import threading
6
6
import typing
7
- import weakref
8
7
9
8
import attr
10
9
import six
@@ -403,74 +402,10 @@ cdef stack_collect(ignore_profiler, thread_time, max_nframes, interval, wall_tim
403
402
return stack_events, exc_events
404
403
405
404
406
- # cython does not play well with mypy
407
405
if typing.TYPE_CHECKING:
408
- _T = typing.TypeVar(" _T" )
409
- _thread_link_base = typing.Generic[_T]
410
- _weakref_type = weakref.ReferenceType[_T]
406
+ _thread_span_links_base = _threading._ThreadLink[ddspan.Span]
411
407
else :
412
- _thread_link_base = object
413
- _weakref_type = typing.Any
414
-
415
-
416
- @ attr.s (slots = True , eq = False )
417
- class _ThreadLink (_thread_link_base ):
418
- """ Link a thread with an object.
419
-
420
- Object is removed when the thread disappears.
421
- """
422
-
423
- # Key is a thread_id
424
- # Value is a weakref to an object
425
- _thread_id_to_object = attr.ib(factory = dict , repr = False , init = False , type = typing.Dict[int , _weakref_type])
426
- _lock = attr.ib(factory = nogevent.Lock, repr = False , init = False , type = nogevent.Lock)
427
-
428
- def link_object (
429
- self ,
430
- obj # type: _T
431
- ):
432
- # type: (...) -> None
433
- """ Link an object to the current running thread."""
434
- # Since we're going to iterate over the set, make sure it's locked
435
- with self ._lock:
436
- self ._thread_id_to_object[nogevent.thread_get_ident()] = weakref.ref(obj)
437
-
438
- def clear_threads (self ,
439
- existing_thread_ids , # type: typing.Set[int]
440
- ):
441
- """ Clear the stored list of threads based on the list of existing thread ids.
442
-
443
- If any thread that is part of this list was stored, its data will be deleted.
444
-
445
- :param existing_thread_ids: A set of thread ids to keep.
446
- """
447
- with self ._lock:
448
- # Iterate over a copy of the list of keys since it's mutated during our iteration.
449
- for thread_id in list (self ._thread_id_to_object.keys()):
450
- if thread_id not in existing_thread_ids:
451
- del self ._thread_id_to_object[thread_id]
452
-
453
- def get_object (
454
- self ,
455
- thread_id # type: int
456
- ):
457
- # type: (...) -> _T
458
- """ Return the object attached to thread.
459
-
460
- :param thread_id: The thread id.
461
- :return: The attached object.
462
- """
463
-
464
- with self ._lock:
465
- obj_ref = self ._thread_id_to_object.get(thread_id)
466
- if obj_ref is not None :
467
- return obj_ref()
468
-
469
-
470
- if typing.TYPE_CHECKING:
471
- _thread_span_links_base = _ThreadLink[ddspan.Span]
472
- else :
473
- _thread_span_links_base = _ThreadLink
408
+ _thread_span_links_base = _threading._ThreadLink
474
409
475
410
476
411
@ attr.s (slots = True , eq = False )
0 commit comments