Ñò
u©×Nc        
   @   sø   d  d k  l Z l Z l Z d  d k l Z l Z d  d k Z d  d k l Z d  d k	 Z
 d  d k Z d  d k l Z d „  Z d d d „  ƒ  YZ e d	 ƒ a d
 „  Z d „  Z d e f d „  ƒ  YZ h  Z e ƒ  Z d „  Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   Lockt   Threadt   BoundedSemaphore(   t   Queuet   EmptyN(   t	   get_ident(   t   getglobaluic         C   sF   x t  | ƒ D] } |  i ƒ  q Wx t  | ƒ D] } |  i ƒ  q. Wd S(   s`   Wait until the semaphore gets back to its original state -- all acquired
    resources released.N(   t   ranget   acquiret   release(   t	   semaphoret   originalstatet   i(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   semaphorereset   s      t
   threadlistc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   t  ƒ  |  _ g  |  _ d  S(   N(   R    t   lockt   list(   t   self(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   __init__(   s    c         C   s6   |  i  i ƒ  z |  i i | ƒ Wd  |  i  i ƒ  Xd  S(   N(   R   R   R   t   appendR	   (   R   t   thread(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   add,   s    c         C   s6   |  i  i ƒ  z |  i i | ƒ Wd  |  i  i ƒ  Xd  S(   N(   R   R   R   t   removeR	   (   R   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR   3   s    c         C   sH   |  i  i ƒ  z& t |  i ƒ p d  S|  i i ƒ  SWd  |  i  i ƒ  Xd  S(   N(   R   R   t   lenR   t   Nonet   popR	   (   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR   :   s    c         C   s,   x% |  i  ƒ  } | p d  S| i ƒ  q d  S(   N(   R   t   join(   R   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   resetC   s
    (   t   __name__t
   __module__R   R   R   R   R   (    (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR   '   s
   					id   c         C   s@   x9 y  t  i t d ƒ } |  | ƒ Wq t j
 o q Xq d S(   s  An infinite "monitoring" loop watching for finished ExitNotifyThread's.

    :param callback: the function to call when a thread terminated. That 
                     function is called with a single argument -- the 
                     ExitNotifyThread that has terminated. The monitor will 
                     not continue to monitor for other threads until
                     'callback' returns, so if it intends to perform long
                     calculations, it should start a new thread itself -- but
                     NOT an ExitNotifyThread, or else an infinite loop 
                     may result.
                     Furthermore, the monitor will hold the lock all the 
                     while the other thread is waiting.
    :type callback:  a callable function
    i<   N(   t   exitthreadst   gett   TrueR   (   t   callbackt   thrd(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   exitnotifymonitorloopQ   s    c         C   s™   t  ƒ  } |  i ƒ  d j o> t |  i ƒ  t ƒ o
 t ‚ n | i |  ƒ t i d ƒ n< |  i ƒ  d j o | i	 ƒ  t i d ƒ n | i
 |  ƒ d S(   s   Called when a thread exits.t	   EXCEPTIONid   t   SYNC_WITH_TIMER_TERMINATEN(   R   t   getExitCauset
   isinstancet   getExitExceptiont
   SystemExitt   threadExceptiont   syst   exitt   getExitMessaget	   terminatet   threadExited(   R   t   ui(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   threadexitedl   s    	

t   ExitNotifyThreadc           B   st   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z e d
 „  ƒ Z RS(   s‹   This class is designed to alert a "monitor" to the fact that a thread has
    exited and to provide for the ability for it to find out why.c         C   so  t  ƒ  |  _ yÄ t i p t i |  ƒ n¦ y d d  k } Wn t j
 o d d  k } n X| i	 ƒ  } y | i
 d t ƒ  t ƒ  ƒ } Wn t j
 o n X| i t i i t i d |  i |  i ƒ  f ƒ ƒ WnO |  i d ƒ t o4 |  i t i ƒ  d ƒ t i ƒ  } |  i | ƒ q/n X|  i d ƒ t |  d ƒ p |  i d  ƒ n t o t i |  t ƒ n d  S(   Niÿÿÿÿs   Thread.run(self)s
   %s_%s.profR$   i   t   NORMALt   exitmessage(    R   t   threadidR2   t
   profiledirR   t   runt   cProfilet   ImportErrort   profilet   Profilet   runctxt   globalst   localsR)   t
   dump_statst   ost   pathR   t   getNamet   setExitCauseR+   t   setExitExceptiont   exc_infot	   tracebackt
   format_exct   setExitStackTracet   hasattrt   setExitMessageR   R   t   putR    (   R   R:   t   proft   tb(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR7   „   s4    
!c         C   s   | |  _  d  S(   N(   t	   exitcause(   R   t   cause(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyRC   ¤   s    c         C   s   |  i  S(   s   Returns the cause of the exit, one of:
        'EXCEPTION' -- the thread aborted because of an exception
        'NORMAL' -- normal termination.(   RN   (   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR&   ¦   s    c         C   s   | |  _  d  S(   N(   t   exitexception(   R   t   exc(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyRD   «   s    c         C   s   |  i  S(   sd   If getExitCause() is 'EXCEPTION', holds the value from
        sys.exc_info()[1] for this exception.(   RP   (   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR(   ­   s    c         C   s   | |  _  d  S(   N(   t   exitstacktrace(   R   t   st(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyRH   ±   s    c         C   s   |  i  S(   sk   If getExitCause() is 'EXCEPTION', returns a string representing
        the stack trace for this exception.(   RR   (   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   getExitStackTrace³   s    c         C   s   | |  _  d S(   s   Sets the exit message to be fetched by a subsequent call to
        getExitMessage.  This message may be any object or type except
        None.N(   R4   (   R   t   msg(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyRJ   ·   s    c         C   s   |  i  S(   s   For any exit cause, returns the message previously set by
        a call to setExitMessage(), or None if there was no such message
        set.(   R4   (   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR-   ¼   s    c         C   s   | |  _  d S(   s6   If set, will output profile information to 'directory'N(   R6   (   t   clst	   directory(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   set_profiledirÂ   s    N(   R   R   t   __doc__R   R6   R7   RC   R&   RD   R(   RH   RT   RJ   R-   t   classmethodRX   (    (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR2   ~   s   	 								c         C   s<   t  i ƒ  t i |  ƒ p t | ƒ t |  <n t  i ƒ  d S(   sy   Initialize the instance-limited thread implementation to permit
    up to intancemax threads with the given instancename.N(   t   instancelimitedlockR   t   instancelimitedsemst   has_keyR   R	   (   t   instancenamet   instancemax(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   initInstanceLimitÏ   s    
t   InstanceLimitedThreadc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         O   s&   | |  _  t t |  ƒ i | | Ž  d  S(   N(   R^   t   superRa   R   (   R   R^   t   argst   kwargs(    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR   Ø   s    	c         C   s"   t  |  i i ƒ  t i |  ƒ d  S(   N(   R\   R^   R   R2   t   start(   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyRe   Ü   s    c         C   sC   z t  i |  ƒ Wd  t o# t |  i o t |  i i ƒ  n Xd  S(   N(   R2   R7   R\   R^   R	   (   R   (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyR7   à   s    (   R   R   R   Re   R7   (    (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyRa   ×   s   		(    (   t	   threadingR    R   R   R   R   RF   R   R   t   os.pathR@   R+   t   offlineimap.uiR   R   R   R   R#   R1   R2   R\   R[   R`   Ra   (    (    (    sz   /projectLibrary/707976 EPBuoyPrototypeAndTesting/SpotWatchCircleAlarm/spaetz-offlineimap-5f3ac2c/offlineimap/threadutil.pyt   <module>   s    		(		N		