+
    Bi+                     f   R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RI	H
t
 ^ RIHtHtHt ^ RIHt  ! R R4      t ! R	 R
4      t]
P"                  ! 4        ! R R]]4      4       t]
P"                  ! 4        ! R R]]]4      4       t]
P"                  ! 4        ! R R]]4      4       t]
P"                  ! 4        ! R R]]4      4       t]
P"                  ! 4        ! R R]4      4       t ! R R4      t]
P"                  ! 4        ! R R]]4      4       t]R8X  d   ]P4                  ! 4        R# R# )zTests monitoring, sys.settrace, and sys.setprofile in a multi-threaded
environment to verify things are thread-safe in a free-threaded buildN)contextmanager)
monitoring)threading_helper)Thread_PyRLockBarrier)TestCasec                   P   a  ] tR t^t o ^
t^2t^tR tR tR t	R t
R tR tRtV tR# )	!InstrumentationMultiThreadedMixinc                    R# )z,Runs once after all the threads have startedN selfs   &?/usr/lib/python3.14/test/test_free_threading/test_monitoring.pyafter_threads/InstrumentationMultiThreadedMixin.after_threads           c                    R# )z3Runs repeatedly while the threads are still runningNr   r   s   &r   during_threads0InstrumentationMultiThreadedMixin.during_threads   r   r   c                    V F
  pV! 4        K  	  V^8  d   V# V P                  V^,
          V4      V P                  V^,
          V4      ,           # )z?Fibonacci function which also calls a bunch of random functions)work)r   nfuncsfuncs   &&& r   r   &InstrumentationMultiThreadedMixin.work   sF    DF q5HyyQ&1q5%)@@@r   c                T    \         P                  ! R 4       V P                  W4       R# )g?N)timesleepr   )r   r   r   s   &&&r   
start_work,InstrumentationMultiThreadedMixin.start_work&   s     	

3		!r   c                    R# )z Runs once after the test is doneNr   r   s   &r   
after_test,InstrumentationMultiThreadedMixin.after_test,   r   r   c                &   . p\        V P                  4       F)  p/ p\        R V4       VP                  VR,          4       K+  	  . p\        V P                  4       FP  p\        V P                  V P                  \        V4      3R7      pVP                  4        VP                  V4       KR  	  V P                  4         RpV F  pVP                  4       '       g   K  Rp M	  V'       g   MV P                  4        KA  V P                  4        R# )zdef f(): passftargetargsTFN)range
func_countexecappendthread_countr   r    fibliststartr   is_aliver   r#   )r   r   ixthreadst	any_alives   &      r   test_instrumentation6InstrumentationMultiThreadedMixin.test_instrumentation0   s    t'AA!$LL3  (
 t(()AdooTXXtE{4KLAGGINN1	 * 	I::<< $I 
 !r   r   N)__name__
__module____qualname____firstlineno__r.   r+   r/   r   r   r   r    r#   r8   __static_attributes____classdictcell____classdict__s   @r   r
   r
      s8     LJ
CA r   r
   c                   ,   a  ] tR t^Pt o R tR tRtV tR# )MonitoringTestMixinc                    \        ^4       FN  p\        P                  ! V4      e   K  Wn        \        P                  ! WP
                  P                  4        R# 	  R# )   N)r*   r   get_tooltool_iduse_tool_id	__class__r:   )r   r3   s   & r   setUpMonitoringTestMixin.setUpQ   sA    qA""1%- &&q..*A*AB	 r   c                F    \         P                  ! V P                  4       R # N)r   free_tool_idrG   r   s   &r   tearDownMonitoringTestMixin.tearDownX   s    -r   )rG   N)r:   r;   r<   r=   rJ   rO   r>   r?   r@   s   @r   rC   rC   P   s     . .r   rC   c                   H   a a ] tR t^\t oRtV 3R ltR tR tR tRt	Vt
V ;t# )SetPreTraceMultiThreadedz4Sets tracing one time after the threads have startedc                2   < \         SV `  4        R V n        R# FN)superrJ   calledr   rI   s   &r   rJ   SetPreTraceMultiThreaded.setUp`   s    r   c                <    V P                  V P                  4       R # rM   
assertTruerV   r   s   &r   r#   #SetPreTraceMultiThreaded.after_testd       $r   c                (    R V n         V P                  # TrV   
trace_funcr   frameeventargs   &&&&r   ra   #SetPreTraceMultiThreaded.trace_funcg       r   c                F    \         P                  ! V P                  4       R # rM   )syssettracera   r   s   &r   r   &SetPreTraceMultiThreaded.after_threadsk   s    T__%r   rV   )r:   r;   r<   r=   __doc__rJ   r#   ra   r   r>   r?   __classcell__rI   rA   s   @@r   rR   rR   \   s!     >%& &r   rR   c                   T   a a ] tR t^ot oRtV 3R ltV 3R ltR tR tR t	Rt
VtV ;t# )MonitoringMultiThreadedzEUses sys.monitoring and repeatedly toggles instrumentation on and offc                   < \         SV `  4        R V n        R V n        \        P
                  ! V P                  \        P                  P                  V P                  4       R# rT   )
rU   rJ   setrV   r   register_callbackrG   eventsLINEcallbackrW   s   &r   rJ   MonitoringMultiThreaded.setUpu   sC    $$LL*++00$--	
r   c                f   < \         P                  ! V P                  ^ 4       \        SV `  4        R#     N)r   
set_eventsrG   rU   rO   rW   s   &r   rO    MonitoringMultiThreaded.tearDown}   s"    dllA.r   c                    R V n         R# )TNrl   )r   r)   s   &*r   rw    MonitoringMultiThreaded.callback   s	    r   c                <    V P                  V P                  4       R # rM   rZ   r   s   &r   r#   "MonitoringMultiThreaded.after_test   r]   r   c                J   V P                   '       dZ   \        P                  ! V P                  \        P                  P
                  \        P                  P                  ,          4       M!\        P                  ! V P                  ^ 4       V P                   '       * V n         R# rz   )rs   r   r|   rG   ru   CALLrv   r   s   &r   r   &MonitoringMultiThreaded.during_threads   sb    888!!j//44z7H7H7M7MM !!$,,2xx<r   rV   rs   )r:   r;   r<   r=   rm   rJ   rO   rw   r#   r   r>   r?   rn   ro   s   @@r   rq   rq   o   s)      P
%   r   rq   c                   B   a  ] tR t^t o RtR tR tR tR tR t	Rt
V tR# )	SetTraceMultiThreadedzCUses sys.settrace and repeatedly toggles instrumentation on and offc                "    R V n         R V n        R# rT   rs   rV   r   s   &r   rJ   SetTraceMultiThreaded.setUp       r   c                <    V P                  V P                  4       R # rM   rZ   r   s   &r   r#    SetTraceMultiThreaded.after_test   r]   r   c                2    \         P                  ! R 4       R # rM   ri   rj   r   s   &r   rO   SetTraceMultiThreaded.tearDown   s    Tr   c                (    R V n         V P                  # r_   r`   rb   s   &&&&r   ra    SetTraceMultiThreaded.trace_func   rg   r   c                    V P                   '       d"   \        P                  ! V P                  4       M\        P                  ! R 4       V P                   '       * V n         R # rM   )rs   ri   rj   ra   r   s   &r   r   $SetTraceMultiThreaded.during_threads   s4    888LL)LLxx<r   r   Nr:   r;   r<   r=   rm   rJ   r#   rO   ra   r   r>   r?   r@   s   @r   r   r      s&     M%   r   r   c                   B   a  ] tR t^t o RtR tR tR tR tR t	Rt
V tR# )	SetProfileMultiThreadedzEUses sys.setprofile and repeatedly toggles instrumentation on and offc                "    R V n         R V n        R# rT   r   r   s   &r   rJ   SetProfileMultiThreaded.setUp   r   r   c                <    V P                  V P                  4       R # rM   rZ   r   s   &r   r#   "SetProfileMultiThreaded.after_test   r]   r   c                2    \         P                  ! R 4       R # rM   ri   
setprofiler   s   &r   rO    SetProfileMultiThreaded.tearDown   s    tr   c                (    R V n         V P                  # r_   r`   rb   s   &&&&r   ra   "SetProfileMultiThreaded.trace_func   rg   r   c                    V P                   '       d"   \        P                  ! V P                  4       M\        P                  ! R 4       V P                   '       * V n         R # rM   )rs   ri   r   ra   r   s   &r   r   &SetProfileMultiThreaded.during_threads   s4    888NN4??+NN4 xx<r   r   Nr   r@   s   @r   r   r      s&     O%   r   r   c                   &   a  ] tR t^t o R tRtV tR# )SetProfileAllMultiThreadedc                  aa \         P                  ! 4       oR  oVV3R lpR p. p\        ^
4       F;  p\         P                  ! VR7      pVP	                  4        VP                  V4       K=  	  \        ^d4       F/  p\         P                  ! V4       \         P                  ! R4       K1  	  SP                  4        V F  pVP                  4        K  	  R# )c                      R # rM   r   r   r   r   r   ASetProfileAllMultiThreaded.test_profile_all_threads.<locals>.func       r   c                  ~   < S P                  4       '       g&   S! 4        S! 4        S! 4        S! 4        S! 4        K;  R # rM   )is_set)doner   s   r   	bg_threadFSetProfileAllMultiThreaded.test_profile_all_threads.<locals>.bg_thread   s,    kkmm $r   c                     R # rM   r   )rc   rd   re   s   &&&r   
my_profileGSetProfileAllMultiThreaded.test_profile_all_threads.<locals>.my_profile   s    r   r(   N)		threadingEventr*   r   r1   r-   setprofile_all_threadsrs   join)r   r   r   
bg_threadsr3   r6   r   r   s   &     @@r   test_profile_all_threads3SetProfileAllMultiThreaded.test_profile_all_threads   s     			 
rA  	2AGGIa  
 sA,,Z8,,T2  	
AFFH r   r   N)r:   r;   r<   r=   r   r>   r?   r@   s   @r   r   r      s      r   r   c                   ,   a  ] tR t^t o R tR tRtV tR# )TraceBufc                H    . V n         \        P                  ! 4       V n        R # rM   )tracesr   Locktraces_lockr   s   &r   __init__TraceBuf.__init__   s    $>>+r   c                    V P                   ;_uu_ 4        V P                  P                  V4       R R R 4       R #   + '       g   i     R # ; irM   )r   r   r-   )r   traces   &&r   r-   TraceBuf.append   s/    KKu% s	   ;A	)r   r   N)r:   r;   r<   r=   r   r-   r>   r?   r@   s   @r   r   r      s     ,& &r   r   c                   P   a  ] tR t^t o R tR tR tR tR tR t	R t
R tR	tV tR
# )MonitoringMiscc                "   VP                  4        R  p\        ^4       F=  p\        P                  ! V P                  \        P
                  P                  V4       K?  	  V P                  P                  \        P                  ! V4      4       R# )c                      R # rM   r   )r)   s   *r   rw   2MonitoringMisc.register_callback.<locals>.callback   r   r   N)waitr*   r   rt   rG   ru   rv   refsr-   weakrefref)r   barrierrw   r3   s   &&  r   rt    MonitoringMisc.register_callback   s_    	 sA((z7H7H7M7MxX  			X./r   c                   . V n         . p\        ^4      p\        ^4       F<  p\        V P                  V3R7      pVP                  4        VP                  V4       K>  	  V F  pVP                  4        K  	  \        P                  ! V P                  \        P                  P                  R4       V P                    F  pV P                  V! 4       R4       K  	  R# )   r'   N)r   r   r*   r   rt   r1   r-   r   r   rG   ru   rv   assertEqual)r   r5   r   r3   r6   threadr   s   &      r   test_register_callback%MonitoringMisc.test_register_callback   s    	!*qAd44G:FAGGINN1 
 FKKM  	$$T\\:3D3D3I3I4P99CSUD) r   c                  aaa V3R  loRo\         P                  ! S4        \        4       oVV3R lp\        VR7      pVP	                  4        \        S4       F  pS;_uu_ 4         RRR4       K  	  VP                  4        \         P                  ! R4       R#   + '       g   i     KT  ; i  \         P                  ! R4       i ; i)c                    < R V n         S# r_   )f_trace_opcodes)rc   rd   re   r   s   &&&r   r   :MonitoringMisc.test_set_local_trace_opcodes.<locals>.trace  s    $(E!Lr   i  c                  |   < \        S4       F  p S;_uu_ 4         R R R 4       K  	  R #   + '       g   i     K.  ; irM   )r*   )r3   lloopss    r   r&   6MonitoringMisc.test_set_local_trace_opcodes.<locals>.f  s(    uA  &s   *;r   N)ri   rj   r   r   r1   r*   r   )r   r&   r6   r3   r   r   r   s   &   @@@r   test_set_local_trace_opcodes+MonitoringMisc.test_set_local_trace_opcodes  s    	 U	
A
 a AGGI5\Q Q " FFHLL	 QQ LLs$   AB2 (B*B2 B/)	B2 2C
c           	       aaaa . oV3R  loV3R loR o\         P                  ! S4        S! ^^4       \         P                  ! R4       SP                  4        VV3R lp\        VR
R7      pVP	                  4        VP                  4        RRRR\        3RR\        3RRR	R\         P                  3.pV P                  SV4       R#   \         P                  ! R4       i ; i)c                 V   < SP                  V P                  P                  W34       R # rM   r-   f_codeco_name)rc   rd   re   r   s   &&&r   profilerEMonitoringMisc.test_toggle_setprofile_no_new_events.<locals>.profiler-  s    MM5<<//<=r   c                    < S! W4      # rM   r   )r4   ybs   &&r   a>MonitoringMisc.test_toggle_setprofile_no_new_events.<locals>.a0  s    Q7Nr   c                     \        W4      # rM   )max)r4   r   s   &&r   r   >MonitoringMisc.test_toggle_setprofile_no_new_events.<locals>.b3  s    q9r   Nc                    < \         P                  ! S4        S! W4       \         P                  ! R 4       R #   \         P                  ! R 4       i ; irM   r   )r4   r   r   r   s   &&r   thread_mainHMonitoringMisc.test_toggle_setprofile_no_new_events.<locals>.thread_main=  s4    NN8$%!t$t$s	   9 Ar'   r   c_callc_returnr   )d      )r   callN)r   r   N)r   returnr   )r   r   r   )ri   r   clearr   r1   r   r   r   )r   r   r6   expectedr   r   r   r   s   &   @@@@r   $test_toggle_setprofile_no_new_events3MonitoringMisc.test_toggle_setprofile_no_new_events'  s     	>		 	x 	!aGNN4 	% +J7			  (C *c"  Hcnn5
 	*- NN4 s   	C C$c                  aa	a
a R  o
V
V3R lo	R oV	VV3R lp^p\        V4      p. p\        V4       F2  p\        W5V3R7      pVP                  4        VP	                  V4       K4  	  V F  pVP                  4        K  	  \        V4       FA  pV P                  RRV3VP                  4       V P                  RRV3VP                  4       KC  	  R# )	c                     V # rM   r   idents   &r   in_child0MonitoringMisc.observe_threads.<locals>.in_childS      Lr   c                 p   < S! 4       ;_uu_ 4        S! V 4       R R R 4       R #   + '       g   i     R # ; irM   r   )r   r   observers   &r   child-MonitoringMisc.observe_threads.<locals>.childV  s     s   	$5	c                     V # rM   r   r   s   &r   	in_parent1MonitoringMisc.observe_threads.<locals>.in_parentZ  r  r   c                    < V P                  4        S! 4       ;_uu_ 4        \        SV3R 7      pVP                  4        VP                  4        S! V4       RRR4       R#   + '       g   i     R# ; i)r'   N)r   r   r1   r   )r   r   r6   r  r  r  s   && r   parent.MonitoringMisc.observe_threads.<locals>.parent]  sG    LLN%uh7	% 	 s   7A""A3	r'   r  r   r   N)r   r*   r   r1   r-   r   assertInr   )r   r  bufr  num_threadsr   r5   r3   r6   r  r   r  s   &f&      @@@r   observe_threadsMonitoringMisc.observe_threadsR  s    		 		! +&{#AfQ<8AGGINN1 $ AFFH  {#AMM;!4cjjAMM:x3SZZ@ $r   c                l   aa \        4       oV3R  lo\        V3R l4       pV P                  VS4       R# )c                 V   < SP                  V P                  P                  W34       R # rM   r   )rc   rd   re   r  s   &&&r   r   5MonitoringMisc.test_profile_threads.<locals>.profilerv  s    JJ,,e9:r   c               3      <"   \         P                  ! S 4        R x  \         P                  ! R 4       R #   \         P                  ! R 4       i ; i5irM   r   )r   s   r   profile4MonitoringMisc.test_profile_threads.<locals>.profiley  s2     NN8$%t$t$   A7 AAANr   r   r  )r   r  r  r   s   & @@r   test_profile_threads#MonitoringMisc.test_profile_threadss  s5    j	; 
	% 
	% 	Wc*r   c                n   aa \        4       oVV3R  lo\        V3R l4       pV P                  VS4       R# )c                 V   < SP                  V P                  P                  W34       S# rM   r   )rc   rd   re   r  tracers   &&&r   r  1MonitoringMisc.test_trace_threads.<locals>.tracer  s#    JJ,,e9:Mr   c               3      <"   \         P                  ! S 4        R x  \         P                  ! R 4       R #   \         P                  ! R 4       i ; i5irM   r   )r  s   r   r   0MonitoringMisc.test_trace_threads.<locals>.trace  s2     LL #T"T"r  Nr  )r   r   r  r  s   & @@r   test_trace_threads!MonitoringMisc.test_trace_threads  s5    j	 
	# 
	# 	UC(r   c                J  a \        4       oV3R  lp\        P                  ! V P                  \        P                  P
                  V4       \        P                  ! V P                  \        P                  P
                  4       \        R 4       pV P                  VS4       R# )c                 D   < SP                  V P                  R V34       R# )r   N)r-   r   )codeoffretvalr  s   &&&r   monitor_py_return>MonitoringMisc.test_monitor_threads.<locals>.monitor_py_return  s    JJh78r   c               3      "   R x  R # 5irM   r   r   r   r   noop1MonitoringMisc.test_monitor_threads.<locals>.noop  s     s   	N)	r   r   rt   rG   ru   	PY_RETURNr|   r   r  )r   r)  r,  r  s   &  @r   test_monitor_threads#MonitoringMisc.test_monitor_threads  s    j	9 	$$LL*++557H	
 	LL*++55	
 
	 
	 	T3'r   )r   N)r:   r;   r<   r=   rt   r   r   r   r  r  r"  r/  r>   r?   r@   s   @r   r   r      s5     	0* 2)+VAB+ )"( (r   r   __main__)rm   ri   r   r   unittestr   
contextlibr   r   test.supportr   r   r   r   r   r
   rC   requires_working_threadingrR   rq   r   r   r   r   r   r:   mainr   r   r   <module>r7     sU  I      %  ) / / < <~	. 	. ,,.&@( & /&$ ,,. :H  / B ,,. =x   / 2 ,,. ?   / 2 ,,.  /B& & ,,.t((( t( /t(n zMMO r   