B
     \L                 @   s,  d dl m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mZ d dlmZmZ ddlmZmZ ejjZeje_g e_ejdkre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r(e-  dS )    )print_functionN)compile_isolatedFlags)errorsjit   )TestCasetagntci  c             C   s(   x"|D ]}t dt  t | |< qW d S )N
   )sleepsleep_factorPyThread_get_thread_ident)aindicesidx r   3lib/python3.7/site-packages/numba/tests/test_gil.pyf$   s    
r   zvoid(int64[:], intp[:])c             C   s.   t   x"|D ]}tdt  t | |< qW dS )z/
    Same as f(), but inside a lifted loop
    r   N)objectr   r   r   )r   r   r   r   r   r   lifted_f0   s    
r   c             C   s.   x(|D ] }t dt  t  t | |< qW dS )z)
    Same as f(), but in object mode
    r   N)r   r   r   r   )r   r   r   r   r   r   object_f:   s    
r   c               @   sl   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zed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arangeZint64)selfZ	n_membersr   r   r   make_test_arrayG   s    zTestGILRelease.make_test_arrayc             C   s   g }||  dtjdtjd |  d}xJt|D ]>}tj|jtjd}tj| tj	|||fd}|
| q4W x|D ]}|  q|W x|D ]}|  qW |S )Nr   )r   2   )targetargs)r   r   r   ZintprangesizeZrandomZshuffle	threadingZThreadappendstartjoin)r   func	n_threadsZthreadsarrir   tr   r   r   run_in_threadsJ   s    


zTestGILRelease.run_in_threadsc             C   s,   | j |dd}t|}| t|d| d S )N   )r)   r   )r-   setZassertEquallen)r   r(   r*   distinctr   r   r   check_gil_held^   s    zTestGILRelease.check_gil_heldc             C   sl   xbdD ]Z}|  ||}t|}y| t|d| W n& tk
rZ } z|}W d d }~X Y qX d S qW |d S )N)r.          r   )r-   r/   ZassertGreaterr0   AssertionError)r   r(   r)   r*   r1   eZfailurer   r   r   check_gil_releasedc   s    
z!TestGILRelease.check_gil_releasedc             C   s   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   r2   )r   cfuncr   r   r   test_gil_heldq   s    zTestGILRelease.test_gil_heldZ	importantc             C   s    t tdddt}| | dS )zj
        Test releasing the GIL, by checking parallel runs produce
        unpredictable results.
        T)r8   nogilN)r   r9   r   r7   )r   r:   r   r   r   test_gil_releasedy   s    z TestGILRelease.test_gil_releasedc             C   s   t tddt}| | dS )zz
        Test the GIL can by released by a lifted loop even though the
        surrounding code uses object mode.
        T)r<   N)r   r9   r   r7   )r   r:   r   r   r   $test_gil_released_inside_lifted_loop   s    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.
        T)r8   )r8   r<   c                s    | | d S )Nr   )r   r+   )
compiled_fr   r   caller   s    z:TestGILRelease.test_gil_released_by_caller.<locals>.callerN)r   r9   r   r7   )r   r@   r   )r?   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.
        T)r8   r<   c                s    | | d S )Nr   )r   r+   )r?   r   r   r@      s    zETestGILRelease.test_gil_released_by_caller_and_callee.<locals>.callerN)r   r9   r   r7   )r   r@   r   )r?   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.
        T)r8   r<   )r8   c                s    | | d S )Nr   )r   r+   )r?   r   r   r@      s    z9TestGILRelease.test_gil_ignored_by_callee.<locals>.callerN)r   r9   r   r2   )r   r@   r   )r?   r   test_gil_ignored_by_callee   s    z)TestGILRelease.test_gil_ignored_by_calleec          	   C   s`   t jdd$}t dtj ttddt}W dQ R X | t	dd |D | | 
|d dS )	za
        When the function is compiled in object mode, a warning is
        printed out.
        T)recordalways)r<   Nc             s   s(   | ] }|j tjkod t|jk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   rG   r   r9   r   Z
assertTrueanyr-   )r   Zwlistr:   r   r   r   test_object_mode   s    zTestGILRelease.test_object_modeN)__name__
__module____qualname__r   r-   r2   r7   r;   r	   r=   r>   rA   rB   rC   rR   r   r   r   r   r   E   s   	

r   __main__).Z
__future__r   ZctypesZctypes.utilossysr$   rN   Znumpyr   Znumba.unittest_supportZunittest_supportZunittestZnumba.compilerr   r   Znumbar   r   Zsupportr   r	   Z	pythonapir   Zc_longZrestypeZargtypesnameZwindllZkernel32ZSleepr   Zc_uintr   ZCDLLutilZfind_libraryZusleepZc_intr   r9   r   r   r   rS   mainr   r   r   r   <module>   s<   





s
