
    piN                     p   d dl Z d dlZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZmZmZ e j"                  j$                  Ze j&                  e_        g e_        ej,                  dk(  r<e j.                  j0                  j2                  Ze j6                  ge_        de_        dZnZ e j:                  e j<                  j?                  d            j@                  Ze j6                  ge_        e jB                  e_        dZd	 Z"d
Z#d Z$d Z% G d de      Z&e'dk(  r ejP                          yy)    N)jit)errors)TestCasetagskip_if_freethreadingnt   ci  c                 P    |D ]!  }t        dt        z         t               | |<   # y )N
   )sleepsleep_factorPyThread_get_thread_identaindicesidxs      V/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/numba/tests/test_gil.pyfr   "   s,    
  -b< *,#-    zvoid(int64[:], intp[:])c                 d    t                |D ]!  }t        dt        z         t               | |<   # y)z/
    Same as f(), but inside a lifted loop
    r   N)objectr   r   r   r   s      r   lifted_fr   .   s1     H -b< *,#-r   c                 d    |D ]+  }t        dt        z         t                t               | |<   - y)z)
    Same as f(), but in object mode
    r   N)r   r   r   r   r   s      r   object_fr   8   s1      -b< *,#	-r   c                   b    e Zd Zd Zd Zd Zd Zed        Zd Z	d Z
d Zd	 Zed
        Zd Zy)TestGILReleasec                 L    t        j                  |t         j                        S )Ndtype)nparangeint64)self	n_memberss     r   make_test_arrayzTestGILRelease.make_test_arrayE   s    yy"((33r   c                    g } || j                  d      t        j                  dt        j                               | j                  d      }t	        |      D ]z  }t        j                  |j
                  t        j                        }t        j                  j                  |       t        j                  |||f      }|j                  |       | |D ]  }|j                           |D ]  }|j                           |S )Nr	   r   2   )targetargs)r&   r!   r"   intprangesizerandomshuffle	threadingThreadappendstartjoin)r$   func	n_threadsthreadsarrir   ts           r   run_in_threadszTestGILRelease.run_in_threadsH   s     	T!!!$bii&AB""2&y! 	A ii8GIIg&  C>BANN1	  	AGGI	 	AFFH	
r   c                 x    | j                  |d      }t        |      }| j                  t        |      d|       y )N   )r6   r	   )r;   setassertEquallen)r$   r5   r8   distincts       r   check_gil_heldzTestGILRelease.check_gil_held\   s6    !!$!!4s8X84r   c                     dD ]=  }| j                  ||      }t        |      }	 | j                  t        |      d|        y  # t        $ r}|}Y d }~Rd }~ww xY w)N)r=          r	   )r;   r>   assertGreaterr@   AssertionError)r$   r5   r6   r8   rA   efailures          r   check_gil_releasedz!TestGILRelease.check_gil_releaseda   sl    $ 
	I %%dI6C3xH""3x=!X> 
	 	 " s   A	AAAc                 \     t        t        d      t              }| j                  |       y)zu
        Test the GIL is held by default, by checking serialized runs
        produce deterministic results.
        TnopythonNr   f_sigr   rB   r$   cfuncs     r   test_gil_heldzTestGILRelease.test_gil_heldo   s%     *ED)!,E"r   c                 ^     t        t        dd      t              }| j                  |       y)zj
        Test releasing the GIL, by checking parallel runs produce
        unpredictable results.
        TrM   nogilNr   rO   r   rJ   rP   s     r   test_gil_releasedz TestGILRelease.test_gil_releasedx   s'    
 6ED5a8&r   c                 ^     t        t        dd      t              }| j                  |       y)zz
        Test the GIL can by released by a lifted loop even though the
        surrounding code uses object mode.
        TforceobjrU   N)r   rO   r   rJ   rP   s     r   $test_gil_released_inside_lifted_loopz3TestGILRelease.test_gil_released_inside_lifted_loop   s'    
 6ED5h?&r   c                      t        t        d      t              t        t        dd      fd       }| j                  |       y)zh
        Releasing the GIL in the caller is sufficient to have it
        released in a callee.
        TrL   rT   c                      | |       y N r   r9   
compiled_fs     r   callerz:TestGILRelease.test_gil_released_by_caller.<locals>.caller       q!r   NrV   r$   rb   ra   s     @r   test_gil_released_by_callerz*TestGILRelease.test_gil_released_by_caller   sA    
 /S.q1
	UT	.	 
/	'r   c                      t        t        dd      t              t        t        dd      fd       }| j                  |       y)zR
        Same, but with both caller and callee asking to release the GIL.
        TrT   c                      | |       y r^   r_   r`   s     r   rb   zETestGILRelease.test_gil_released_by_caller_and_callee.<locals>.caller   rc   r   NrV   rd   s     @r   &test_gil_released_by_caller_and_calleez5TestGILRelease.test_gil_released_by_caller_and_callee   sC     ;ST:1=
	UT	.	 
/	'r   c                      t        t        dd      t              t        t        d      fd       }| j                  |       y)zP
        When only the callee asks to release the GIL, it gets ignored.
        TrT   rL   c                      | |       y r^   r_   r`   s     r   rb   z9TestGILRelease.test_gil_ignored_by_callee.<locals>.caller   rc   r   NrN   rd   s     @r   test_gil_ignored_by_calleez)TestGILRelease.test_gil_ignored_by_callee   sA    
 ;ST:1=
	UT	"	 
#	F#r   c                 B   t        j                  d      5 }t        j                  dt        j                          t        t        dd      t              }ddd       | j                  t        d D              |       | j                  d       y# 1 sw Y   >xY w)za
        When the function is compiled in object mode, a warning is
        printed out.
        T)recordalwaysrY   Nc              3      K   | ]9  }|j                   t        j                  u xr d t        |j                        v  ; yw)z:Code running in object mode won't allow parallel executionN)categoryr   NumbaWarningstrmessage).0ws     r   	<genexpr>z2TestGILRelease.test_object_mode.<locals>.<genexpr>   sH      , ! JJ&*=*== o \`cdedmdm`n no ,s   ?A   )warningscatch_warningssimplefilterr   rq   r   rO   r   
assertTrueanyr;   )r$   wlistrQ   s      r   test_object_modezTestGILRelease.test_object_mode   s    
 $$D1 	DU!!(F,?,?@9CD9(CE	D 	 ,%*, ,-2	4 	E1%	D 	Ds   ABBN)__name__
__module____qualname__r&   r;   rB   rJ   r   rR   rW   r[   re   rh   rk   r~   r_   r   r   r   r   C   sW    4(5
 # #''	(( $ $&r   r   __main__))ctypesctypes.utilossysr0   rx   numpyr!   unittestnumbar   
numba.corer   numba.tests.supportr   r   r   	pythonapir   c_longrestypeargtypesnamewindllkernel32Sleepr   c_uintr   CDLLutilfind_libraryusleepc_intr   rO   r   r   r   r   mainr_   r   r   <module>r      s     	 
       D D #,,FF $*MM  !%'  " 77d?MM""((Emm_ENEMLFKK0056==Emm_ENLLEML- 	"--q&X q&h zHMMO r   