
    `i                        d Z ddlmZ ddlZddlZddlZddlmZmZ ej	        dk    rddl
Z
nddlZ G d d          ZdS )z3Simple cross-platform advisory file lock utilities.    )annotationsN)AnyLiteralwin32c                  D    e Zd ZdZddZddZddZddZdddZddZ	dS )FileLocka  Provide a cross-platform file locking mechanism using Python's stdlib.

    This class implements an advisory lock, which must be respected by all
    cooperating processes. Please note that this lock does not prevent the same process
    from acquiring the lock multiple times; it is the caller's responsibility to
    manage this.

    Examples
    --------
    .. code-block:: python

        from tvm_ffi.utils import FileLock

        with FileLock("/tmp/my.lock"):
            # Critical section guarded by the lock.
            # Other processes attempting to acquire the same lock will block
            # (or fail, if using ``acquire()``) until this context exits.
            do_work()

    lock_file_pathstrreturnNonec                "    || _         d| _        dS )z6Initialize a file lock using the given lock file path.N)r	   _file_descriptor)selfr	   s     j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/utils/lockfile.py__init__zFileLock.__init__7   s    ,,0    c                .    |                                   | S )zlAcquire the lock upon entering the context.

        This method blocks until the lock is acquired.
        )blocking_acquirer   s    r   	__enter__zFileLock.__enter__<   s    
 	r   exc_typer   exc_valexc_tbLiteral[False]c                .    |                                   dS )zIContext manager protocol: release the lock upon exiting the 'with' block.F)release)r   r   r   r   s       r   __exit__zFileLock.__exit__D   s    ur   boolc                   	 | j         dS t          j        dk    rit          j        | j        t          j        t          j        z  t          j        z            | _         t          j
        | j         t          j        d           ngt          j        | j        t          j        t          j        z            | _         t          j        | j         t          j        t          j        z             dS # t"          t$          f$ r+ | j          t          j        | j                    d| _         Y dS t(          $ r>}| j          t          j        | j                    d| _         t+          d|           d}~ww xY w)a$  Acquire an exclusive, non-blocking lock on the file.

        Returns
        -------
        ret: bool
            True if the lock was acquired, False otherwise.

        Raises
        ------
        RuntimeError
            If an unexpected error occurs during lock acquisition.

        NFr      TzAn unexpected error occurred: )r   sysplatformosopenr	   O_RDWRO_CREATO_BINARYmsvcrtlockingLK_NBLCKO_WRONLYfcntlflockLOCK_EXLOCK_NBOSErrorBlockingIOErrorclose	ExceptionRuntimeError)r   es     r   acquirezFileLock.acquireI   sR   	E$0u|w&&(*'RZ)?"+)M) )% t4foqIIII(*0CR[SUS]E](^(^%D15=5=3PQQQ4) 	 	 	$0.///(,%55 	E 	E 	E$0.///(,%CCCDDD		Es#   C- C C- -8E/(	E/19E**E/N皙?timeoutfloat | Nonepoll_intervalfloatc                   | j         t          d          t          j                    }	 |                                 rdS |5t          j                    |z
  |k    rt	          d| j         d| d          t          j        |           b)a  Wait until an exclusive lock can be acquired, with an optional timeout.

        Parameters
        ----------
        timeout: float, optional
            The maximum time to wait for the lock in seconds.  A value of None means wait indefinitely.
        poll_interval: float
            The time to wait between lock attempts in seconds.

        Returns
        -------
        ret: bool
            True if the lock was acquired.

        Raises
        ------
        TimeoutError
            If the lock is not acquired within the timeout period.
        RuntimeError
            If the lock is already held by this instance.

        Nz&Lock is already held by this instance.TzFailed to acquire lock on 'z' after z	 seconds.)r   r4   timer6   TimeoutErrorr	   sleep)r   r8   r:   
start_times       r   r   zFileLock.blocking_acquiren   s    .  ,GHHHY[[

	&||~~ t "	j(@G'K'K"a$2Eaawaaa   J}%%%
	&r   c                   | j         |t          j        dk    r&t          j        | j         t          j        d           n$t          j        | j         t          j                   t          j
        | j                    d| _         dS dS )z1Releases the lock and closes the file descriptor.Nr   r    )r   r!   r"   r(   r)   LK_UNLCKr,   r-   LOCK_UNr#   r2   r   s    r   r   zFileLock.release   st     ,|w&&t4foqIIIID15=AAAHT*+++$(D!!! -,r   )r	   r
   r   r   )r   r   )r   r   r   r   r   r   r   r   )r   r   )Nr7   )r8   r9   r:   r;   r   r   )r   r   )
__name__
__module____qualname____doc__r   r   r   r6   r   r    r   r   r   r   !   s         *1 1 1 1
      
#E #E #E #EJ$& $& $& $& $&L) ) ) ) ) )r   r   )rG   
__future__r   r#   r!   r=   typingr   r   r"   r(   r,   r   rH   r   r   <module>rK      s   " : 9 " " " " " " 				 



          <7MMMMLLL{) {) {) {) {) {) {) {) {) {)r   