
    pi#                         d dl Z d dlZd dlZd dlmZmZ d dlmZm	Z	 d dl
Z
 G d de
j                        Z G d de
j                        Zedk(  r e
j                          yy)	    N)captured_stdoutskip_if_freethreading)	vectorizeguvectorizec                   &    e Zd ZdZd Zed        Zy)TestParUfuncIssuesFc                     t        dd      d        }d}|dkD  rbt        j                  |       t        j                  d      x}}t        j
                  j                  ||z    |||             |dz  }|dkD  ray	y	)
z
        Related to #89.
        This does not test #89 but tests the fix for it.
        We want to make sure the worker threads can be used multiple times
        and with different time gap between each execution.
        zfloat64(float64, float64)parallel)targetc                     | |z   S N )abs     q/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/numba/tests/npyufunc/test_parallel_ufunc_issues.pyfnvz4TestParUfuncIssues.test_thread_response.<locals>.fnv   s    q5L       gh㈵>i    N)r   timesleepnparangetestingassert_equal)selfr   
sleep_timer   r   s        r   test_thread_responsez'TestParUfuncIssues.test_thread_response   s~     
.z	B	 
C	 
7"JJz"IIe$$AJJ##AE3q!95!OJ 7"r   c                    t        j                  dt         j                        }dfd} ||      t        dgdd      fd       }d	D ]  }t	        j
                  |t        j                  
      }|j                         }t               5 } ||      }ddd       j                         }	|j                          t        t        d |	j                                     }
t        |      D cg c]  }t        |dz         }}|t        |      D cg c]
  }|dz      c}z  }t        |      }| j!                  |
|       t        j"                  j%                  d|z           y# 1 sw Y   xY wc c}w c c}w )z<
        Testing issue #1998 due to GIL reacquiring
        N
abcdefghijc                 "    t        |           y r   printx
characterss    r   barz;TestParUfuncIssues.test_gil_reacquire_deadlock.<locals>.bar,       *Q- r   zint32(int32)r
   Tr   nopythonc                 @    t        | dz          | dz         | dz  S )N
   r   r"   )r%   cbars    r   fooz;TestParUfuncIssues.test_gil_reacquire_deadlock.<locals>.foo2   s"    !b&MRLq5Lr   r   r,   d   i  dtypec                 "    | j                         S r   stripr%   s    r   <lambda>z@TestParUfuncIssues.test_gil_reacquire_deadlock.<locals>.<lambda>E       aggi r   r,   r   )ctypes	CFUNCTYPEc_int32r   r   r   int32copyr   getvalueclosesortedmap
splitlinesrangestrassertEqualr   r   r   protor'   r.   nelemr   acopybufgotstdout
got_outputr%   expected_outputr-   r&   s                @@r   test_gil_reacquire_deadlockz.TestParUfuncIssues.test_gil_reacquire_deadlock#   sQ      v~~6!
	! Sz 
N#J	F	 
G	 ( 	4E		%rxx0AFFHE " c!f\\^FIIK$79J9J9L MNJ49%LAqs1r6{AOAE%LIq
1r6 2IIO$_5OZ9JJ##CU3#	4
  BIs   	E14E=F1E:	N)__name__
__module____qualname___numba_parallel_test_r   r   rO   r   r   r   r   r      s!    !( (4 (4r   r   c                        e Zd ZdZed        Zy)TestParGUfuncIssuesFc                    t        j                  dt         j                        }dfd} ||      t        dgddd      fd	       }d
D ]  }t	        j
                  |t        j                        }|j                         }t               5 } ||      }ddd       j                         }	|j                          t        t        d |	j                                     }
t        |      D cg c]  }t        |dz         }}|t        |      D cg c]
  }|dz      c}z  }t        |      }| j!                  |
|       t        j"                  j%                  d|z           y# 1 sw Y   xY wc c}w c c}w )zR
        Testing similar issue to #1998 due to GIL reacquiring for Gufunc
        Nr    c                 "    t        |           y r   r"   r$   s    r   r'   z<TestParGUfuncIssues.test_gil_reacquire_deadlock.<locals>.bar]   r(   r   z(int32, int32[:])z()->()r
   Tr)   c                 H    t        | dz          | dz         | dz  |d<   y )Nr,   r   r   r"   )r%   outr-   s     r   r.   z<TestParGUfuncIssues.test_gil_reacquire_deadlock.<locals>.fooc   s(     !b&MRLUCFr   r/   r1   c                 "    | j                         S r   r4   r6   s    r   r7   zATestParGUfuncIssues.test_gil_reacquire_deadlock.<locals>.<lambda>w   r8   r   r,   r   )r9   r:   r;   r   r   r   r<   r=   r   r>   r?   r@   rA   rB   rC   rD   rE   r   r   rF   s                @@r   rO   z/TestParGUfuncIssues.test_gil_reacquire_deadlockT   sV      v~~6!
	! Sz 
)*H&
7	
7	 ( 	4E		%rxx0AFFHE " c!f\\^FIIK$79J9J9L MNJ49%LAqs1r6{AOAE%LIq
1r6 2IIO$_5OZ9JJ##CU3#	4
  BIs   	E25E>F2E;	N)rP   rQ   rR   rS   r   rO   r   r   r   rU   rU   P   s    !)4 )4r   rU   __main__)r   r9   numpyr   numba.tests.supportr   r   numbar   r   unittestTestCaser   rU   rP   mainr   r   r   <module>rb      sY       F ( A4** A4J.4(++ .4b zHMMO r   