o
    8ήc                     @   s&  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
mZ d dlmZ d dlmZ d dlmZmZ e jjZe je_g e_ejdkr\e jjjZe jge_de_dZne e j !dj"Ze jge_e j#e_d	Zd
d Z$dZ%dd Z&dd Z'G dd deZ(e)dkre*  dS dS )    N)compile_isolatedFlags)jit)errors)TestCasetagnt   ci  c                 C   s$   |D ]}t dt  t | |< qd S )N
   )sleepsleep_factorPyThread_get_thread_identaindicesidx r   ;/tmp/pip-target-vg8gfxp4/lib/python/numba/tests/test_gil.pyf#   s   r   zvoid(int64[:], intp[:])c                 C   s*   t   |D ]}tdt  t | |< qdS )z/
    Same as f(), but inside a lifted loop
    r   N)objectr   r   r   r   r   r   r   lifted_f/   s
   r   c                 C   s*   |D ]}t dt  t  t | |< qdS )z)
    Same as f(), but in object mode
    r   N)r   r   r   r   r   r   r   r   object_f9   s
   r   c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )TestGILReleasec                 C   s   t j|t jdS )Ndtype)nparangeint64)self	n_membersr   r   r   make_test_arrayF   s   zTestGILRelease.make_test_arrayc                 C   s   g }||  dtjdtjd |  d}t|D ]}tj|jtjd}tj| tj	|||fd}|
| q|D ]}|  q;|D ]}|  qD|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   tr   r   r   run_in_threadsI   s   


zTestGILRelease.run_in_threadsc                 C   s,   | j |dd}t|}| t|d| d S )N   )r0   r	   )r5   setassertEquallen)r   r/   r2   distinctr   r   r   check_gil_held]   s   zTestGILRelease.check_gil_heldc                 C   sd   dD ]-}|  ||}t|}z| t|d| W  d S  ty/ } z|}W Y d }~qd }~ww |)N)r6          r	   )r5   r7   assertGreaterr9   AssertionError)r   r/   r0   r2   r:   efailurer   r   r   check_gil_releasedb   s   z!TestGILRelease.check_gil_releasedc                 C      t tddt}| | dS )zu
        Test the GIL is held by default, by checking serialized runs
        produce deterministic results.
        TnopythonNr   f_sigr   r;   r   cfuncr   r   r   test_gil_heldp      zTestGILRelease.test_gil_heldc                 C   s    t tdddt}| | dS )zj
        Test releasing the GIL, by checking parallel runs produce
        unpredictable results.
        TrE   nogilNr   rG   r   rB   rH   r   r   r   test_gil_releasedx   s   z TestGILRelease.test_gil_releasedc                 C   rC   )zz
        Test the GIL can by released by a lifted loop even though the
        surrounding code uses object mode.
        TrM   N)r   rG   r   rB   rH   r   r   r   $test_gil_released_inside_lifted_loop   rK   z3TestGILRelease.test_gil_released_inside_lifted_loopc                    s8   t tddt t tddd fdd}| | dS )zh
        Releasing the GIL in the caller is sufficient to have it
        released in a callee.
        TrD   rL   c                        | | d S Nr   r   r3   
compiled_fr   r   caller      z:TestGILRelease.test_gil_released_by_caller.<locals>.callerNrN   r   rW   r   rU   r   test_gil_released_by_caller   s   z*TestGILRelease.test_gil_released_by_callerc                    s:   t tdddt t tddd fdd}| | dS )zR
        Same, but with both caller and callee asking to release the GIL.
        TrL   c                    rR   rS   r   rT   rU   r   r   rW      rX   zETestGILRelease.test_gil_released_by_caller_and_callee.<locals>.callerNrN   rY   r   rU   r   &test_gil_released_by_caller_and_callee   s   z5TestGILRelease.test_gil_released_by_caller_and_calleec                    s8   t tdddt t tdd fdd}| | dS )zP
        When only the callee asks to release the GIL, it gets ignored.
        TrL   rD   c                    rR   rS   r   rT   rU   r   r   rW      rX   z9TestGILRelease.test_gil_ignored_by_callee.<locals>.callerNrF   rY   r   rU   r   test_gil_ignored_by_callee   s   
z)TestGILRelease.test_gil_ignored_by_calleec                 C   st   t jdd}t dtj ttddt}W d   n1 s w   Y  | t	dd |D | | 
|d dS )	za
        When the function is compiled in object mode, a warning is
        printed out.
        T)recordalwaysrP   Nc                 s   s*    | ]}|j tju od t|jv V  qdS )z:Code running in object mode won't allow parallel executionN)categoryr   NumbaWarningstrmessage).0wr   r   r   	<genexpr>   s    
z2TestGILRelease.test_object_mode.<locals>.<genexpr>   )warningscatch_warningssimplefilterr   r`   r   rG   r   
assertTrueanyr5   )r   wlistrI   r   r   r   test_object_mode   s   zTestGILRelease.test_object_modeN)__name__
__module____qualname__r!   r5   r;   rB   rJ   rO   rQ   rZ   r[   r\   rm   r   r   r   r   r   D   s    

r   __main__)+ctypesctypes.utilossysr*   rg   numpyr   unittestnumba.core.compilerr   r   numbar   
numba.corer   numba.tests.supportr   r   	pythonapir   c_longrestypeargtypesnamewindllkernel32Sleepr   c_uintr   CDLLutilfind_libraryusleepc_intr   rG   r   r   r   rn   mainr   r   r   r   <module>   s@    





r