o
    ,wi,                     @   s  d Z ddlZg dejdd dkrg dng  ZdgZddlZdZeg d	7 Zed
 e	edr7ed ej
Z
ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ e	edred e	edred d.ddZd/ddZd/ddZ d0d d!Z!G d"d# d#Z"d$d% Z#G d&d' d'eZ$e$ Z%Z&d(d) Z'e	ed*rej(Z)d.d+d*Z(ne*d* eee+ edd,Zee Z,e,*d- dS )1aK  
Implementation of the standard :mod:`thread` module that spawns greenlets.

.. note::

    This module is a helper for :mod:`gevent.monkey` and is not
    intended to be used directly. For spawning greenlets in your
    applications, prefer higher level constructs like
    :class:`gevent.Greenlet` class or :func:`gevent.spawn`.
    N)allocate_lock	get_identexitLockType
stack_sizestart_new_thread_local   )      )start_joinable_threadlock_ThreadHandle_make_thread_handleerror_thread)TIMEOUT_MAXallocateexit_threadinterrupt_main	start_newget_native_iddaemon_threads_allowed)PYPY)copy_globals)
getcurrentGreenletExit)sleep)get_hub_if_exists)Greenlet)BoundedSemaphore)local)LoopExitRLockset_namec                 C   s   | d u rt  } t| S N)r   id)gr r)   J/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/gevent/thread.pyr   O   s   r   r)   c                 C   s:   |d urt j| g|R i |}|S t j| g|R  }|S r&   )r    spawn)functionargskwargsgreenletr)   r)   r*   _start_new_greenletU   s
   r0   c                 C   s   t t| ||S r&   )r   r0   )r,   r-   r.   r)   r)   r*   r   \   s   r   Tc                 C   sF   t | }|du rt }nt|tstd|jrtd|| |S )a  
    *For internal use only*: start a new thread.

    Like start_new_thread(), this starts a new thread calling the given function.
    Unlike start_new_thread(), this returns a handle object with methods to join
    or detach the given thread.
    This function is not for third-party code, please use the
    `threading` module instead. During finalization the runtime will not wait for
    the thread to exit if daemon is True. If handle is provided it must be a
    newly created thread._ThreadHandle instance.
    NzMust be a gevent thread handlezthread already started)r0   r   
isinstanceAssertionError_had_greenletRuntimeError_set_greenlet)r,   handledaemonr/   r)   r)   r*   r   _   s   

r   c                   @   s\   e Zd Zdd ZdZdZdd Zdd Zdd
dZe	dd Z
dd ZdddZdd ZdS )r   c                 O   s   dS )zDoes nothing; ignores argsNr)   )self_args_kwargsr)   r)   r*   __init__   s    z_ThreadHandle.__init__NFc                 C   s,   ddl m} |d usJ ||| _d| _d S )Nr   )refT)weakrefr<   _greenlet_refr3   )r8   gletr<   r)   r)   r*   r5      s   

z_ThreadHandle._set_greenletc                 C   s   | j d ur	|   S d S r&   )r>   r8   r)   r)   r*   _get_greenlet   s
   
z_ThreadHandle._get_greenletc                 C   s   |dkrd n|}| j std|  }|d urP|t u r tdt|dr*||S ddlm} d }|r9| | }|  sP|d urH| |krHd S td |  r=d S )NrB   thread not startedzCannot join current threadjoinr   )timeMbP?)	r3   r4   rA   r   hasattrrD   rE   is_doner   )r8   timeoutr?   rE   endr)   r)   r*   rD      s&   



z_ThreadHandle.joinc                 C   s   |   }|d urt|S d S r&   )rA   r   r8   r?   r)   r)   r*   ident   s   z_ThreadHandle.identc                 C   s   |   }|d u r
dS |jS NT)rA   deadrK   r)   r)   r*   rH      s   z_ThreadHandle.is_doneTc                 C   s(   | j stdd| _|rtd dS dS )a
  
        Mark the thread as complete.

        This releases our reference (if any) to our greenlet.

        By default, this will bounce back to the hub so that waiters
        in ``join`` can get notified. Set *enter_hub* to false not to
        do this.
        rC   NrF   )r3   r4   r>   r   )r8   	enter_hubr)   r)   r*   	_set_done   s   
z_ThreadHandle._set_donec                 C   s    d| j j| j jt| |  f S )Nz<%s.%s at 0x%x greenlet=%r>)	__class__
__module____name__r'   rA   r@   r)   r)   r*   __repr__   s   z_ThreadHandle.__repr__)rB   )T)rS   rR   __qualname__r;   r>   r3   r5   rA   rD   propertyrL   rH   rP   rT   r)   r)   r)   r*   r      s    
 

r   c                  G   s   t  }|t  |S )z
    Called on 3.13 after forking in the child.
    Takes ``(module, ident)``, returns a handle object
    with that ident.
    )r   r5   r   )r9   r6   r)   r)   r*   r      s   r   c                       sJ   e Zd ZdZejZereZej	Z
 fddZedd Zd
dd	Z  ZS )r   z
    The basic lock type.

    .. versionchanged:: 24.10.1
       Subclassing this object is no longer allowed. This matches the
       Python 3 API.
    c                    s   t    dS )z
        .. versionchanged:: 24.10.1
           No longer accepts arguments to pass to the super class. If you
           want a semaphore with a different count, use a semaphore class directly.
           This matches the Lock API of Python 3
        N)superr;   r@   rQ   r)   r*   r;   
  s   zLockType.__init__c                 C      t r&   )	TypeError)clsr)   r)   r*   __init_subclass__  s   zLockType.__init_subclass__TrB   c                 C   s   |dkrd }|s|d urt d|d ur%|dk rt d|| jkr%tdz	t| ||}W n ty<   |r8 d}Y nw |sJ|sJt t urJt  |S )NrB   z/can't specify a timeout for a non-blocking callr   z'timeout value must be strictly positiveztimeout value is too largeF)	
ValueError_TIMEOUT_MAXOverflowErrorr!   acquirer#   r   r   r   )r8   blockingrI   acquiredr)   r)   r*   r`     s&   	
	
zLockType.acquire)TrB   )rS   rR   rU   __doc__
__thread__r   _OVER_RELEASE_ERRORr   r4   r   r^   r;   classmethodr\   r`   __classcell__r)   r)   rX   r*   r      s    		
r   c                   C   rY   r&   r   r)   r)   r)   r*   r   J  s   r   r   c                 C   s$   | d u rt  S | t  krt | S d S r&   )_original_stack_size)sizer)   r)   r*   r   Q  s
   
)
only_namesignore_missing_namesr   r&   )r)   NrM   )-rc   sysversion_info__implements____imports__r   rd   
__target__appendrG   r   gevent._compatr   gevent._utilr   
gevent.hubr   r   r   gevent._hub_localr   gevent.greenletr    gevent.lockr!   gevent.localr"   r   gevent.exceptionsr#   r   r0   r   r   r   r   r   r   r   r   r   rh   removeglobals__all__r)   r)   r)   r*   <module>   s`    











'gO

